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

annif.config.AnnifConfigCFG.__init__()   A

Complexity

Conditions 3

Size

Total Lines 11
Code Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nop 2
dl 11
loc 11
rs 9.85
c 0
b 0
f 0
1
"""Configuration file handling"""
2
3
4
import configparser
5
import tomli
6
import annif
7
from annif.exception import ConfigurationException
8
9
10
logger = annif.logger
11
12
13 View Code Duplication
class AnnifConfigCFG:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
14
    """Class for reading configuration in CFG/INI format"""
15
16
    def __init__(self, filename):
17
        self._config = configparser.ConfigParser()
18
        self._config.optionxform = annif.util.identity
19
        with open(filename, encoding='utf-8-sig') as projf:
20
            try:
21
                logger.debug(
22
                    f"Reading configuration file {filename} in CFG format")
23
                self._config.read_file(projf)
24
            except (configparser.DuplicateOptionError,
25
                    configparser.DuplicateSectionError) as err:
26
                raise ConfigurationException(err)
27
28
    @property
29
    def project_ids(self):
30
        return self._config.sections()
31
32
    def __getitem__(self, key):
33
        return self._config[key]
34
35
36 View Code Duplication
class AnnifConfigTOML:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
37
    """Class for reading configuration in TOML format"""
38
39
    def __init__(self, filename):
40
        with open(filename, "rb") as projf:
41
            try:
42
                logger.debug(
43
                    f"Reading configuration file {filename} in TOML format")
44
                self._config = tomli.load(projf)
45
            except tomli.TOMLDecodeError as err:
46
                raise ConfigurationException(err)
47
48
    @property
49
    def project_ids(self):
50
        return self._config.keys()
51
52
    def __getitem__(self, key):
53
        return self._config[key]
54