-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
45 lines (33 loc) · 1.12 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
import yaml
class Config(dict):
def __init__(self, dicts):
super().__init__(dicts)
def __getitem__(self, key):
if key not in self:
raise Exception("key {} not in available_setting".format(key))
return super().__getitem__(key)
def __setitem__(self, key, value):
if key not in self:
raise Exception("key {} not in available_setting".format(key))
return super().__setitem__(key, value)
def get(self, key):
try:
return self[key]
except KeyError as e:
return e
except Exception as e:
raise e
def transform_keys(data, parent_key=''):
new_data = {}
for k, v in data.items():
new_key = f'{parent_key}_{k}'.upper() if parent_key else k.upper()
if isinstance(v, dict):
new_data.update(transform_keys(v, new_key))
else:
new_data[new_key] = v
return new_data
def get_config(key=''):
with open('config/config_{}.yml'.format(key), 'r') as f:
yaml_data = yaml.safe_load(f)
config = Config(transform_keys(yaml_data))
return config