forked from MeshAddicts/meshinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
58 lines (50 loc) · 1.52 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
import datetime
import json
import uuid
class Config:
@classmethod
def load(cls):
config = cls.load_from_file('config.json')
random_uuid = str(uuid.uuid4())
config['broker']['client_id'] = config['broker']['client_id_prefix'] + '-' + random_uuid
config['server']['start_time'] = datetime.datetime.now(datetime.timezone.utc).astimezone()
try:
version_info = cls.load_from_file('version-info.json')
if version_info is not None:
config['server']['version_info'] = version_info
except FileNotFoundError:
pass
return config
@classmethod
def load_from_file(cls, path):
with open(path, 'r') as f:
return json.load(f)
@classmethod
def cleanse(cls, config):
config_clean = config.copy()
blacklist = {
"config": {
"broker": {
"password": {},
"username": {},
},
"integrations": {
"discord": {
"token": {},
},
"geocoding": {
"geocode.maps.co": {
"api_key": {},
}
}
}
}
}
def recursive_filter_dict(d, blacklist):
for key, value in list(d.items()):
if key in blacklist:
del d[key]
elif isinstance(value, dict):
recursive_filter_dict(value, blacklist[key] if key in blacklist else {})
recursive_filter_dict(config_clean, blacklist)
return config_clean