-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconf.py
49 lines (34 loc) · 967 Bytes
/
conf.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
import gc
import json
class Settings:
"""
Simple settings wrapper that will load settings from
a configuration file.
The file is lazy loaded the first time a setting is
accessed.
"""
defaults = {
"WIFI_SSID": "",
"WIFI_PASSWORD": "",
"TWITCH_IRC_TOKEN": "",
"TWITCH_NICK": "",
"TWITCH_PREFIX": "",
"TWITCH_CHANNEL": "",
}
_settings = None
def __init__(self, config=None):
self.config_file = config or "config.json"
def __getattr__(self, attr_name):
if self._settings is None:
self._load_config()
return self._settings[attr_name]
def _load_config(self):
with open(self.config_file) as fp:
data = json.load(fp)
self._settings = {
key: data.get(key, default)
for key, default in self.defaults.items()
}
gc.collect()
settings = Settings()
# TODO: Tests =/