Skip to content

Commit

Permalink
Merge pull request #300 from NatLibFi/issue282-handling-syntax-errors…
Browse files Browse the repository at this point in the history
…-in-projects-config

Issue282 handling syntax errors in projects config
  • Loading branch information
osma authored Aug 23, 2019
2 parents d2dff10 + 47d4f2a commit b660eed
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
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 @@ -63,10 +63,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:
logger.warning(err.format_message())

def _initialize_subjects(self):
try:
Expand All @@ -88,10 +91,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 @@ -121,13 +124,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 @@ -215,7 +227,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')}
}

def remove_model_data(self):
Expand All @@ -242,7 +254,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

0 comments on commit b660eed

Please sign in to comment.