Completed
Branch master (db5e7a)
by Osma
09:07 queued 05:09
created

AnnifBackend   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 50
rs 10
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 7 1
A _get_datadir() 0 6 2
A load_subjects() 0 3 1
A debug() 0 3 1
A warning() 0 3 1
A analyze() 0 7 2
A _analyze() 0 5 1
A info() 0 3 1
1
"""Common functionality for backends."""
2
3
import abc
4
import os
5
import os.path
6
import annif
0 ignored issues
show
Unused Code introduced by
The import annif seems to be unused.
Loading history...
7
from annif import logger
8
9
10
class AnnifBackend(metaclass=abc.ABCMeta):
1 ignored issue
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
11
    """Base class for Annif backends that perform analysis. The
12
    non-implemented methods should be overridden in subclasses."""
13
14
    name = None
15
16
    def __init__(self, backend_id, params, datadir):
17
        """Initialize backend with specific parameters. The
18
        parameters are a dict. Keys and values depend on the specific
19
        backend type."""
20
        self.backend_id = backend_id
21
        self.params = params
22
        self._datadir = os.path.join(datadir, 'backends', self.backend_id)
23
24
    def _get_datadir(self):
25
        """return the path of the directory where this backend can store its
26
        data files"""
27
        if not os.path.exists(self._datadir):
28
            os.makedirs(self._datadir)
29
        return self._datadir
30
31
    def load_subjects(self, subjects, analyzer):
32
        """load the given subjects analyzed using the given analyzer"""
33
        pass  # default is to do nothing, subclasses may override
34
35
    @abc.abstractmethod
36
    def _analyze(self, text, params):
37
        """This method should implemented by backends. It implements
38
        the analyze functionality, with pre-processed parameters."""
39
        pass
40
41
    def analyze(self, text, params=None):
42
        """Analyze some input text and return a list of subjects represented
43
        as a list of AnalysisHit objects."""
44
        beparams = dict(self.params)
45
        if params is not None:
46
            beparams.update(params)
47
        return self._analyze(text, params=beparams)
48
49
    def debug(self, message):
50
        """Log a debug message from this backend"""
51
        logger.debug("Backend {}: {}".format(self.backend_id, message))
0 ignored issues
show
introduced by
Use formatting in logging functions and pass the parameters as arguments
Loading history...
52
53
    def info(self, message):
54
        """Log an info message from this backend"""
55
        logger.info("Backend {}: {}".format(self.backend_id, message))
0 ignored issues
show
introduced by
Use formatting in logging functions and pass the parameters as arguments
Loading history...
56
57
    def warning(self, message):
58
        """Log a warning message from this backend"""
59
        logger.warning("Backend {}: {}".format(self.backend_id, message))
0 ignored issues
show
introduced by
Use formatting in logging functions and pass the parameters as arguments
Loading history...
60