Passed
Pull Request — master (#560)
by Osma
05:47
created

annif.registry.AnnifRegistry._create_projects()   A

Complexity

Conditions 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nop 3
dl 16
loc 16
rs 9.85
c 0
b 0
f 0
1
"""Registry that keeps track of Annif projects"""
2
3
import collections
4
from flask import current_app
5
import annif
6
from annif.config import parse_config
7
from annif.project import Access, AnnifProject
8
9
logger = annif.logger
10
11
12 View Code Duplication
class AnnifRegistry:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
13
    """Class that keeps track of the Annif projects"""
14
15
    # Note: The individual projects are stored in a shared static variable,
16
    # keyed by the "registry ID" which is unique to the registry instance.
17
    # This is done to make it possible to serialize AnnifRegistry instances
18
    # without including the potentially huge project objects (which contain
19
    # backends with large models, vocabularies with lots of concepts etc).
20
    # Serialized AnnifRegistry instances can then be passed between
21
    # processes when using the multiprocessing module.
22
    _projects = {}
23
24
    def __init__(self, projects_file, datadir, init_projects):
25
        self._rid = id(self)
26
        self._projects[self._rid] = \
27
            self._create_projects(projects_file, datadir)
28
        if init_projects:
29
            for project in self._projects[self._rid].values():
30
                project.initialize()
31
32
    def _create_projects(self, projects_file, datadir):
33
        # parse the configuration
34
        config = parse_config(projects_file)
35
36
        # handle the case where the config file doesn't exist
37
        if config is None:
38
            return {}
39
40
        # create AnnifProject objects from the configuration file
41
        projects = collections.OrderedDict()
42
        for project_id in config.project_ids:
43
            projects[project_id] = AnnifProject(project_id,
44
                                                config[project_id],
45
                                                datadir,
46
                                                self)
47
        return projects
48
49
    def get_projects(self, min_access=Access.private):
50
        """Return the available projects as a dict of project_id ->
51
        AnnifProject. The min_access parameter may be used to set the minimum
52
        access level required for the returned projects."""
53
54
        return {project_id: project
55
                for project_id, project in self._projects[self._rid].items()
56
                if project.access >= min_access}
57
58
    def get_project(self, project_id, min_access=Access.private):
59
        """return the definition of a single Project by project_id"""
60
61
        projects = self.get_projects(min_access)
62
        try:
63
            return projects[project_id]
64
        except KeyError:
65
            raise ValueError("No such project {}".format(project_id))
66
67
68
def initialize_projects(app):
69
    projects_file = app.config['PROJECTS_FILE']
70
    datadir = app.config['DATADIR']
71
    init_projects = app.config['INITIALIZE_PROJECTS']
72
    app.annif_registry = AnnifRegistry(projects_file, datadir, init_projects)
73
74
75
def get_projects(min_access=Access.private):
76
    """Return the available projects as a dict of project_id ->
77
    AnnifProject. The min_access parameter may be used to set the minimum
78
    access level required for the returned projects."""
79
    if not hasattr(current_app, 'annif_registry'):
80
        initialize_projects(current_app)
81
82
    return current_app.annif_registry.get_projects(min_access)
83
84
85
def get_project(project_id, min_access=Access.private):
86
    """return the definition of a single Project by project_id"""
87
88
    projects = get_projects(min_access)
89
    try:
90
        return projects[project_id]
91
    except KeyError:
92
        raise ValueError("No such project {}".format(project_id))
93