-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig_manager.py
69 lines (57 loc) · 2.02 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
64
65
66
67
68
69
import json
import logging
from os.path import exists
class ConfigManager:
def __init__(self):
self.config_file_name = "config.json"
self.defaults = {
"sp_dc": None,
"accessToken": None,
"accessTokenExpire": -1
}
self.config = self.defaults
def initialize(self):
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:
logging.error(f"Unable to parse json config file: {ex}")
return False
for key in self.config.keys():
if key not in jo:
logging.error(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:
logging.error(f"Key '{key}' is not a valid config key")
def simple_get(self, key: str):
self.read_config()
return self.get(key)
def get_default(self, key: str):
if key in self.defaults:
return self.defaults[key]
else:
logging.error(f"Key '{key}' is not a valid default config 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:
logging.error(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()