-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.py
55 lines (41 loc) · 1.22 KB
/
loader.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
import yaml
"""
This file handles the configuration file loading and parsing
"""
def merge_dicts(main: dict, second: dict):
"""
Merges two dicts into main
Args:
main (dict): The target dict
second (dict): The dict to be added to main
"""
for key, value in second.items():
if key in main and type(value) is dict:
merge_dicts(main[key], value)
elif type(main) is dict and key not in main:
main[key] = value
def load(filename: str) -> dict:
"""
Loads a configuration file and parses
Args:
filename (str): Path to file
Returns:
dict: Parsed yaml
"""
include_configs = dict()
with open(filename, 'r') as f:
for line in f:
if '#include' in line:
include_filename = line.split(' ')[1].strip()
merge_dicts(include_configs, load(include_filename))
else:
break
with open(filename, 'r') as stream:
try:
main_configs = yaml.load(stream)
except yaml.YAMLError as exc:
print(exc)
raise
# Merge main file with include file
merge_dicts(main_configs, include_configs)
return main_configs