| Total Complexity | 10 |
| Total Lines | 54 |
| Duplicated Lines | 72.22 % |
| Changes | 0 | ||
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: |
|
|
|
|||
| 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: |
|
| 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 |