Passed
Pull Request — master (#320)
by Osma
03:27
created

NotInitializedException.format_message()   A

Complexity

Conditions 3

Size

Total Lines 10
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 10
rs 9.9
c 0
b 0
f 0
cc 3
nop 1
1
"""Custom exceptions used by Annif"""
2
3
4
from click import ClickException
5
6
7
class AnnifException(ClickException):
8
    """Base Annif exception. We define this as a subclass of ClickException so
9
    that the CLI can automatically handle exceptions. This exception cannot be
10
    instantiated directly - subclasses should be used instead."""
11
12
    def __init__(self, message, project_id=None, backend_id=None):
13
        super().__init__(message)
14
        self.project_id = project_id
15
        self.backend_id = backend_id
16
17
        if self.prefix is None:
18
            raise TypeError("Cannot instantiate exception without a prefix.")
19
20
    # subclasses should set this to a descriptive prefix
21
    prefix = None
22
23
    def format_message(self):
24
        if self.project_id is not None:
25
            return "{} project '{}': {}".format(self.prefix,
26
                                                self.project_id,
27
                                                self.message)
28
        if self.backend_id is not None:
29
            return "{} backend '{}': {}".format(self.prefix,
30
                                                self.backend_id,
31
                                                self.message)
32
        return "{}: {}".format(self.prefix, self.message)
33
34
35
class NotInitializedException(AnnifException):
36
    """Exception raised for attempting to use a project or backend that
37
    cannot be initialized, most likely since it is not yet functional
38
    because of lack of vocabulary or training."""
39
40
    prefix = "Couldn't initialize"
41
42
43
class ConfigurationException(AnnifException):
44
    """Exception raised when a project or backend is misconfigured."""
45
46
    prefix = "Misconfigured"
47
48
49
class NotSupportedException(AnnifException):
50
    """Exception raised when an operation is not supported by a project or
51
    backend."""
52
53
    prefix = "Not supported"
54