-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
63 lines (46 loc) · 1.62 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import tomllib
from collections.abc import MutableMapping
from constants import CONFIG_FILE
class AttrDict(MutableMapping):
"""
A dictionary that allows for attribute-style access.
"""
def __init__(self, *args, **kwargs):
self.__dict__["_data"] = dict(*args, **kwargs)
def __getattr__(self, item):
try:
return self._data[item]
except KeyError as e:
raise AttributeError(f"'AttrDict' object has no attribute '{item}'") from e
def __setattr__(self, key, value):
# self._data[key] = value
raise NotImplementedError("Not supported")
def __delattr__(self, item):
# del self._data[item]
raise NotImplementedError("Not supported")
def __getitem__(self, key):
return self._data[key]
def __setitem__(self, key, value):
# self._data[key] = value
raise NotImplementedError("Not supported")
def __delitem__(self, key):
# del self._data[key]
raise NotImplementedError("Not supported")
def __iter__(self):
return iter(self._data)
def __len__(self):
return len(self._data)
def __repr__(self):
return f"{type(self).__name__}({self._data})"
def to_attrdict(obj):
if isinstance(obj, dict):
return AttrDict({k: to_attrdict(v) for k, v in obj.items()})
elif isinstance(obj, list):
return [to_attrdict(v) for v in obj]
else:
return obj
class Config(AttrDict):
def __init__(self, config_path=CONFIG_FILE):
with open(config_path, "rb") as f:
data = tomllib.load(f)
super().__init__(to_attrdict(data))