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: |
|
|
|
|
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: |
|
|
|
|
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): |
|
|
|
|
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
|
|
|
return projects_file |
70
|
|
|
|
71
|
|
|
if os.path.exists('projects.cfg'): |
72
|
|
|
return 'projects.cfg' |
73
|
|
|
|
74
|
|
|
if os.path.exists('projects.toml'): |
75
|
|
|
return 'projects.toml' |
76
|
|
|
|
77
|
|
|
logger.warning( |
78
|
|
|
'Could not find project configuration file ' + |
79
|
|
|
'"projects.cfg" or "projects.toml". ' + |
80
|
|
|
'You can set the path to the project configuration ' + |
81
|
|
|
'file using the ANNIF_PROJECTS environment ' + |
82
|
|
|
'variable or the command-line option "--projects".') |
83
|
|
|
return None |
84
|
|
|
|
85
|
|
|
|
86
|
|
View Code Duplication |
def parse_config(projects_file): |
|
|
|
|
87
|
|
|
filename = find_config(projects_file) |
88
|
|
|
|
89
|
|
|
if not filename: # not found |
90
|
|
|
return None |
91
|
|
|
|
92
|
|
|
if filename.endswith('.toml'): # TOML format |
93
|
|
|
return AnnifConfigTOML(filename) |
94
|
|
|
else: # classic CFG/INI style format |
95
|
|
|
return AnnifConfigCFG(filename) |
96
|
|
|
|