-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconfig_manager.py
63 lines (52 loc) · 1.85 KB
/
config_manager.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 json
from os import makedirs
from os.path import exists, join
class ConfigManager:
def __init__(self):
self.config_dir_name = "config"
self.config_file_name = join(self.config_dir_name, "config.json")
self.defaults = {
"lrToken": None
}
self.config = self.defaults
def initialize(self):
makedirs(self.config_dir_name, exist_ok=True)
if not exists(self.config_file_name):
js = json.dumps(self.config)
with open(self.config_file_name, "w") as io_writer:
io_writer.write(js)
def read_config(self) -> bool:
with open(self.config_file_name, "r") as io_reader:
js = io_reader.read()
try:
jo = json.loads(js)
except Exception as ex:
print(f"Unable to parse json config file: {ex}")
return False
for key in self.config.keys():
if key not in jo:
print(f"Unable to locate key '{key}'in config file")
continue
self.config[key] = jo[key]
def get(self, key: str):
if key in self.config:
return self.config[key]
else:
print(f"Key '{key}' is not a valid config key")
def simple_get(self, key: str):
self.read_config()
return self.get(key)
def write_config(self):
js = json.dumps(self.config)
with open(self.config_file_name, "w") as io_writer:
io_writer.write(js)
def set(self, key: str, value):
if key in self.config:
self.config[key] = value
else:
print(f"Key '{key}' is not a valid config key")
def simple_set(self, key: str, value):
self.set(key, value)
self.write_config()
cM = ConfigManager()
cM.initialize()