1
|
|
|
from synergine.core.exception.NotFoundError import NotFoundError |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class ConfigurationManager(): |
5
|
|
|
""" |
6
|
|
|
Management of dict based configuration data |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
def __init__(self, config: dict={}): |
10
|
|
|
self._configs = config |
11
|
|
|
|
12
|
|
|
def get(self, config_name: "the.config.name", default=None): |
13
|
|
|
inceptions = config_name.split('.') |
14
|
|
|
config = self._configs |
15
|
|
|
for inception in inceptions: |
16
|
|
|
if inception in config: |
17
|
|
|
config = config[inception] |
18
|
|
|
elif default is not None: |
19
|
|
|
return default |
20
|
|
|
else: |
21
|
|
|
raise NotFoundError('Config "'+config_name+'"" not found') |
22
|
|
|
return config |
23
|
|
|
|
24
|
|
|
def update_config(self, config_name: "the.config.name", config_value): |
25
|
|
|
inceptions = config_name.split('.') |
26
|
|
|
inception_count = 0 |
27
|
|
|
parent_config = self._configs |
28
|
|
|
config = self._configs |
29
|
|
|
for inception in inceptions: |
30
|
|
|
inception_count += 1 |
31
|
|
|
if inception in config: |
32
|
|
|
parent_config = config |
33
|
|
|
config = config[inception] |
34
|
|
|
else: |
35
|
|
|
raise Exception('Config "'+config_name+'"" not found') |
36
|
|
|
parent_config[inception] = config_value |
37
|
|
|
|
38
|
|
|
def set_config(self, config_name: "the.config.name", config_value): |
39
|
|
|
inceptions = config_name.split('.') |
40
|
|
|
config = self._configs |
41
|
|
|
for inception in inceptions: |
42
|
|
|
if inception in config: |
43
|
|
|
config = config[inception] |
44
|
|
|
elif inceptions.index(inception)+1 == len(inceptions): |
45
|
|
|
config[inception] = config_value |
46
|
|
|
else: |
47
|
|
|
config[inception] = {inceptions.__getitem__(inceptions.index(inception)+1): {}} |
48
|
|
|
config = config[inception] |
49
|
|
|
|
50
|
|
|
def load(self, config_to_load): |
51
|
|
|
self._configs = self._merge(self._configs, config_to_load) |
52
|
|
|
|
53
|
|
|
def _merge(self, a, b, path=None): |
|
|
|
|
54
|
|
|
"merges b into a" |
55
|
|
|
if path is None: |
56
|
|
|
path = [] |
57
|
|
|
for key in b: |
58
|
|
|
if key in a: |
59
|
|
|
if isinstance(a[key], dict) and isinstance(b[key], dict): |
60
|
|
|
self._merge(a[key], b[key], path + [str(key)]) |
61
|
|
|
elif a[key] == b[key]: |
62
|
|
|
pass |
63
|
|
|
else: |
64
|
|
|
a[key] = b[key] |
65
|
|
|
else: |
66
|
|
|
a[key] = b[key] |
67
|
|
|
return a |
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.