-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_config.py
47 lines (36 loc) · 1.6 KB
/
load_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
import os
import json
from types import SimpleNamespace
from meshtastic import BROADCAST_NUM
class ConfigLoader:
_config = None
@staticmethod
def load_config_file(filename):
if ConfigLoader._config is not None:
return ConfigLoader._config # Return already loaded config
script_dir = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(script_dir, filename)
if not os.path.exists(config_path):
raise FileNotFoundError(f"Configuration file not found: {config_path}")
with open(config_path, 'r') as config_file:
conf = json.load(config_file)
# Expand default key
conf["channel"]["key"] = "1PG7OiApB1nwvP+rz05pAQ==" if conf["channel"]["key"] == "AQ==" else conf["channel"]["key"]
# Create keys not in config
conf["node"]["number"] = int(conf["node"]["id"].replace("!", ""), 16)
conf["destination_id"] = BROADCAST_NUM
# Convert to nested SimpleNamespace
def dict_to_namespace(data):
if isinstance(data, dict):
return SimpleNamespace(**{k: dict_to_namespace(v) for k, v in data.items()})
return data
ConfigLoader._config = dict_to_namespace(conf)
return ConfigLoader._config
@staticmethod
def get_config():
if ConfigLoader._config is None:
raise ValueError("Config has not been loaded yet.")
return ConfigLoader._config
if __name__ == "__main__":
config = ConfigLoader.load_config_file('config.json')
print(json.dumps(config, default=lambda o: o.__dict__, indent=4))