Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue282 handling syntax errors in projects config #300

Merged
merged 6 commits into from
Aug 23, 2019
4 changes: 4 additions & 0 deletions annif/default_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ class TestingInitializeConfig(TestingConfig):

class TestingNoProjectsConfig(TestingConfig):
PROJECTS_FILE = 'tests/notfound.cfg'


class TestingInvalidProjectsConfig(TestingConfig):
PROJECTS_FILE = 'tests/projects_invalid.cfg'
38 changes: 27 additions & 11 deletions annif/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ def _init_access(self):
project_id=self.project_id)

def _initialize_analyzer(self):
analyzer = self.analyzer
logger.debug("Project '%s': initialized analyzer: %s",
self.project_id,
str(analyzer))
try:
analyzer = self.analyzer
logger.debug("Project '%s': initialized analyzer: %s",
self.project_id,
str(analyzer))
except AnnifException as err:
osma marked this conversation as resolved.
Show resolved Hide resolved
logger.warning(err.format_message())

def _initialize_subjects(self):
try:
Expand All @@ -87,10 +90,10 @@ def _initialize_vectorizer(self):

def _initialize_backend(self):
logger.debug("Project '%s': initializing backend", self.project_id)
if not self.backend:
logger.debug("Cannot initialize backend: does not exist")
return
try:
if not self.backend:
logger.debug("Cannot initialize backend: does not exist")
return
self.backend.initialize()
except AnnifException as err:
logger.warning(err.format_message())
Expand Down Expand Up @@ -120,13 +123,22 @@ def _suggest_with_backend(self, text, backend_params):

@property
def analyzer(self):
if self._analyzer is None and self.analyzer_spec:
self._analyzer = annif.analyzer.get_analyzer(self.analyzer_spec)
if self._analyzer is None:
if self.analyzer_spec:
self._analyzer = annif.analyzer.get_analyzer(
self.analyzer_spec)
else:
raise ConfigurationException(
"analyzer setting is missing (and needed by the backend)",
project_id=self.project_id)
return self._analyzer

@property
def backend(self):
if self._backend is None:
if 'backend' not in self.config:
raise ConfigurationException(
"backend setting is missing", project_id=self.project_id)
backend_id = self.config['backend']
try:
backend_class = annif.backend.get_backend(backend_id)
Expand Down Expand Up @@ -214,7 +226,7 @@ def dump(self):
return {'project_id': self.project_id,
'name': self.name,
'language': self.language,
'backend': {'backend_id': self.config['backend']}
'backend': {'backend_id': self.config.get('backend')}
}


Expand All @@ -230,7 +242,11 @@ def _create_projects(projects_file, datadir, init_projects):
config = configparser.ConfigParser()
config.optionxform = lambda option: option
with open(projects_file, encoding='utf-8') as projf:
config.read_file(projf)
try:
config.read_file(projf)
except (configparser.DuplicateOptionError,
configparser.DuplicateSectionError) as err:
raise ConfigurationException(err)

# create AnnifProject objects from the configuration file
projects = collections.OrderedDict()
Expand Down
6 changes: 6 additions & 0 deletions tests/projects.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ language=en
backend=tfidf
analyzer=snowball(english)

[nobackend]
name=Dummy with no backend
language=en
vocab=dummy
analyzer=snowball(english)

[pav]
name=PAV Ensemble Finnish
language=fi
Expand Down
23 changes: 23 additions & 0 deletions tests/projects_invalid.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Project configuration for Annif unit tests

[duplicatedproject]
name=Dummy with no backend
language=en
backend=dummy
vocab=dummy
analyzer=snowball(english)

[duplicatedproject]
name=Dummy with no backend
language=en
backend=dummy
vocab=dummy
analyzer=snowball(english)

[duplicatedvocab]
name=Dummy with no backend
language=en
backend=dummy
vocab=dummy
vocab=dummy
analyzer=snowball(english)
22 changes: 22 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,35 @@ def test_get_project_nonexistent(app):
annif.project.get_project('nonexistent')


def test_get_project_noanalyzer(app):
with app.app_context():
project = annif.project.get_project('noanalyzer')
with pytest.raises(ConfigurationException):
analyzer = project.analyzer


def test_get_project_novocab(app):
with app.app_context():
project = annif.project.get_project('novocab')
with pytest.raises(ConfigurationException):
vocab = project.vocab


def test_get_project_nobackend(app):
with app.app_context():
project = annif.project.get_project('nobackend')
with pytest.raises(ConfigurationException):
backend = project.backend


def test_get_project_invalid_config_file(app):
app = annif.create_app(
config_name='annif.default_config.TestingInvalidProjectsConfig')
with app.app_context():
with pytest.raises(ConfigurationException):
project = annif.project.get_project('duplicatedvocab')


def test_project_load_vocabulary_tfidf(app, vocabulary, testdatadir):
with app.app_context():
project = annif.project.get_project('tfidf-fi')
Expand Down