-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
45 lines (40 loc) · 1.32 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
import json
from pathlib import Path
import yaml
def get_config_local(filename: Path) -> dict:
with open(filename) as file:
try:
return yaml.safe_load(file)
except yaml.YAMLError as e:
print(e)
return {'error': str(e)}
def get_first_config() -> dict:
files: list[Path] = [
Path('/config/config.yaml'),
Path('config.yaml'),
Path('config.sh10rt.example.yaml')
]
for file in files:
if file.exists():
loaded_config: dict = get_config_local(file)
break
else:
raise FileNotFoundError
options_files: list[Path] = [
Path('/data/options.json'),
Path('/data/options.yaml'),
]
for options_file in options_files:
if options_file.exists():
if options_file.suffix == '.json':
with open(options_file) as file:
options: dict = json.load(file)
else:
options: dict = get_config_local(options_file)
for key, option in options.items():
if isinstance(option, str) and option:
loaded_config[key] = option
elif isinstance(option, int) or isinstance(option, bool):
loaded_config[key] = option
break
return loaded_config