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

annif.config   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 72.22 %

Importance

Changes 0
Metric Value
eloc 37
dl 39
loc 54
rs 10
c 0
b 0
f 0
wmc 10

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 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