-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfiguration.py
More file actions
56 lines (48 loc) · 1.9 KB
/
Configuration.py
File metadata and controls
56 lines (48 loc) · 1.9 KB
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
import os
import types
import yaml
class Configuration:
_FILE = None
__CONFIG_PATH = '.'
__CONFIG_FILES = None
@staticmethod
def __get_yaml_files():
yaml_files = []
for file in os.listdir(Configuration.__CONFIG_PATH):
if file.endswith('.yaml'):
yaml_files.append(file)
return yaml_files
def __init__(self, file=None) -> None:
'''loads the config and returs if already loaded'''
if Configuration._FILE is None:
Configuration.__CONFIG_FILES = Configuration.__get_yaml_files()
if file is None:
if len(Configuration.__CONFIG_FILES) > 1:
raise Exception(
'Please select a configuration file to load')
file = Configuration.__CONFIG_FILES[0]
Configuration._FILE = os.path.join(Configuration.__CONFIG_PATH,
file)
Configuration.__instanciate__()
@staticmethod
def __instanciate_nested(k, v):
setattr(Configuration, k, types.SimpleNamespace())
for kk, vv in v.items():
if type(vv) == dict:
Configuration.__instanciate_nested(kk, vv)
else:
setattr(getattr(Configuration, k), kk, vv)
@staticmethod
def __instanciate__():
# Configuration.CONFIG = types.SimpleNamespace()
# here = os.path.abspath(os.path.dirname(__file__))
with open(Configuration._FILE) as f:
data = yaml.safe_load(f)
for k, v in data.items():
if type(v) == dict:
Configuration.__instanciate_nested(k, v)
# for kk, vv in v.items():
# setattr(getattr(Configuration, k), kk, vv)
# setattr(Configuration.CONFIG, k, v)
else:
setattr(Configuration, k, v)