-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfig.py
executable file
·59 lines (46 loc) · 1.35 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python3 -u
"""
Configuration for various MantisBT utility scripts
"""
from collections.abc import Mapping
from pathlib import Path
import yaml
# Constants
ORG_MANTIS = 'mantisbt'
ORG_PLUGINS = 'mantisbt-plugins'
class Config(dict):
"""
The Config class extends dict wrapper allowing its keys to be accessed as
attributes for convenience.
"""
def __getattr__(self, name):
if name in self:
return self[name]
raise AttributeError(name)
def __update(base, upd):
"""
Recursively update 'base' dict with corresponding values in 'upd'
"""
if (isinstance(base, Mapping) and
isinstance(upd, Mapping)):
for key, value in upd.items():
base[key] = __update(base.get(key, {}), value)
elif upd is not None:
return upd
return base
def __read_config():
"""
Read YML config files and return the data
"""
path = Path(__file__).parent.resolve()
# Read default configuration values
with open(Path.joinpath(path, "config_defaults.yml"), 'r') as ymlfile:
cfg = yaml.safe_load(ymlfile)
# Read local settings
try:
with open(Path.joinpath(path, "config.yml"), 'r') as ymlfile:
cfg = __update(cfg, yaml.safe_load(ymlfile))
except FileNotFoundError:
pass
return Config(cfg)
cfg = __read_config()