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

annif.config.AnnifConfigTOML.project_ids()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 3
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 3
loc 3
rs 10
c 0
b 0
f 0
1
"""Configuration file handling"""
2
3
4
import os.path
5
import configparser
6
import tomli
7
import annif
8
import annif.util
9
from annif.exception import ConfigurationException
10
11
12
logger = annif.logger
13
14
15 View Code Duplication
class AnnifConfigCFG:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
16
    """Class for reading configuration in CFG/INI format"""
17
18
    def __init__(self, filename):
19
        self._config = configparser.ConfigParser()
20
        self._config.optionxform = annif.util.identity
21
        with open(filename, encoding='utf-8-sig') as projf:
22
            try:
23
                logger.debug(
24
                    f"Reading configuration file {filename} in CFG format")
25
                self._config.read_file(projf)
26
            except (configparser.DuplicateOptionError,
27
                    configparser.DuplicateSectionError) as err:
28
                raise ConfigurationException(err)
29
30
    @property
31
    def project_ids(self):
32
        return self._config.sections()
33
34
    def __getitem__(self, key):
35
        return self._config[key]
36
37
38 View Code Duplication
class AnnifConfigTOML:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
39
    """Class for reading configuration in TOML format"""
40
41
    def __init__(self, filename):
42
        with open(filename, "rb") as projf:
43
            try:
44
                logger.debug(
45
                    f"Reading configuration file {filename} in TOML format")
46
                self._config = tomli.load(projf)
47
            except tomli.TOMLDecodeError as err:
48
                raise ConfigurationException(err)
49
50
    @property
51
    def project_ids(self):
52
        return self._config.keys()
53
54
    def __getitem__(self, key):
55
        return self._config[key]
56
57
58 View Code Duplication
def find_config(projects_file):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
59
    if projects_file:
60
        if os.path.exists(projects_file):
61
            return projects_file
62
        else:
63
            logger.warning(
64
                f'Project configuration file "{projects_file}" is ' +
65
                'missing. Please provide one. ' +
66
                'You can set the path to the project configuration ' +
67
                'file using the ANNIF_PROJECTS environment ' +
68
                'variable or the command-line option "--projects".')
69
            return None
70
71
    for filename in ('projects.cfg', 'projects.toml'):
72
        if os.path.exists(filename):
73
            return filename
74
75
    logger.warning(
76
        'Could not find project configuration file ' +
77
        '"projects.cfg" or "projects.toml". ' +
78
        'You can set the path to the project configuration ' +
79
        'file using the ANNIF_PROJECTS environment ' +
80
        'variable or the command-line option "--projects".')
81
    return None
82
83
84 View Code Duplication
def parse_config(projects_file):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
85
    filename = find_config(projects_file)
86
87
    if not filename:  # not found
88
        return None
89
90
    if filename.endswith('.toml'):  # TOML format
91
        return AnnifConfigTOML(filename)
92
    else:  # classic CFG/INI style format
93
        return AnnifConfigCFG(filename)
94