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

annif.config   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 86
Duplicated Lines 77.91 %

Importance

Changes 0
Metric Value
eloc 63
dl 67
loc 86
rs 10
c 0
b 0
f 0
wmc 16

1 Function

Rating   Name   Duplication   Size   Complexity  
B parse_config() 28 28 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A AnnifConfigTOML.__getitem__() 2 2 1
A AnnifConfigTOML.__init__() 8 8 3
A AnnifConfigTOML.project_ids() 3 3 1
A AnnifConfigCFG.project_ids() 3 3 1
A AnnifConfigCFG.__getitem__() 2 2 1
A AnnifConfigCFG.__init__() 11 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 parse_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 not os.path.exists(projects_file):
61
            logger.warning(
62
                f'Project configuration file "{projects_file}" is ' +
63
                'missing. Please provide one. ' +
64
                'You can set the path to the project configuration ' +
65
                'file using the ANNIF_PROJECTS environment ' +
66
                'variable or the command-line option "--projects".')
67
            return None
68
    else:
69
        if os.path.exists('projects.cfg'):
70
            projects_file = 'projects.cfg'
71
        elif os.path.exists('projects.toml'):
72
            projects_file = 'projects.toml'
73
        else:
74
            logger.warning(
75
                'Could not find project configuration file ' +
76
                '"projects.cfg" or "projects.toml". ' +
77
                'You can set the path to the project configuration ' +
78
                'file using the ANNIF_PROJECTS environment ' +
79
                'variable or the command-line option "--projects".')
80
            return None
81
82
    if projects_file.endswith('.toml'):  # TOML format
83
        return AnnifConfigTOML(projects_file)
84
    else:  # classic CFG/INI style format
85
        return AnnifConfigCFG(projects_file)
86