-
Notifications
You must be signed in to change notification settings - Fork 18
/
config.py
49 lines (41 loc) · 1.34 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
import yaml
def load_config(path, default_path=None):
"""
Loads config file.
Args:
path (str): path to config file.
default_path (str, optional): whether to use default path. Defaults to None.
Returns:
cfg (dict): config dict.
"""
# load configuration from file itself
with open(path, 'r') as f:
cfg_special = yaml.full_load(f)
# check if we should inherit from a config
inherit_from = cfg_special.get('inherit_from')
# if yes, load this config first as default
# if no, use the default_path
if inherit_from is not None:
cfg = load_config(inherit_from, default_path)
elif default_path is not None:
with open(default_path, 'r') as f:
cfg = yaml.full_load(f)
else:
cfg = dict()
# include main configuration
update_recursive(cfg, cfg_special)
return cfg
def update_recursive(dict1, dict2):
"""
Update two config dictionaries recursively.
Args:
dict1 (dict): first dictionary to be updated.
dict2 (dict): second dictionary which entries should be used.
"""
for k, v in dict2.items():
if k not in dict1:
dict1[k] = dict()
if isinstance(v, dict):
update_recursive(dict1[k], v)
else:
dict1[k] = v