-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.py
56 lines (39 loc) · 1.27 KB
/
menu.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
#!/usr/bin/python3
from collections import OrderedDict
from yaml import SafeLoader as YamlSafeLoader
KEY_MAPPING = {
"applications": "app",
"database": "db",
"virtualization": "vt"
}
DEFAULT_MAPPING_TAG = 'tag:yaml.org,2002:map'
def dict_constructor(loader, node):
return OrderedDict(loader.construct_pairs(node))
YamlSafeLoader.add_constructor(DEFAULT_MAPPING_TAG, dict_constructor)
def load_yaml(stream):
loader = YamlSafeLoader(stream)
try:
return loader.get_single_data()
finally:
loader.dispose()
def load_config(file_name):
with open(file_name, 'r') as stream:
return load_yaml(stream)
from collections.abc import MutableMapping
def flatten_dict(d: MutableMapping, parent_key: str = '', sep: str ='.') -> MutableMapping:
items = []
for k, v in d.items():
k = k.lower()
k = KEY_MAPPING.get(k, k)
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, MutableMapping):
items.extend(flatten_dict(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
def __main__():
menu = load_config('ctx.yml')
for k, v in flatten_dict(menu).items():
print("-",k)
if __name__ == "__main__":
__main__()