-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.py
31 lines (24 loc) · 1.02 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
import yaml
class DefaultDict(dict):
def __init__(self, arg_dict, defaults_dict):
super().__init__(arg_dict)
self.defaults_dict = defaults_dict
for key, value in self.items():
if type(value) is dict:
self[key] = DefaultDict(value, self.defaults_dict[key])
def get(self, key):
return super().get(key) if super().get(key) is not None else self.defaults_dict.get(key)
def __getitem__(self, key):
return (
super().__getitem__(key)
if key in self and super().__getitem__(key) is not None
else self.defaults_dict[key]
)
class Config(DefaultDict):
def __init__(self, cfg_file, defaults_cfg_file="defaults_config.yaml"):
with open(cfg_file) as yaml_cfg_file:
with open(defaults_cfg_file) as yaml_defaults_cfg_file:
super().__init__(
yaml.load(yaml_cfg_file, yaml.Loader),
yaml.load(yaml_defaults_cfg_file, yaml.Loader),
)