-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
115 lines (86 loc) · 3.19 KB
/
config.py
File metadata and controls
115 lines (86 loc) · 3.19 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import json
import logging
from pathlib import Path
class ConfigManager:
# JSON content
_cfg = dict()
# Config file path
_cfg_file = None
def __init__(self, config_file):
""" This class takes a JSON config file and makes it available
so that if you provide a key, you will get the value back.
Values can also bet set or removed from the config. Setting
and removing values will be written to the initial config file.
"""
if config_file:
self._cfg_file = config_file
self._read_cfg()
else:
logging.error("ERROR: No config file provided")
def _read_cfg(self):
""" Read the JSON content of a given configuration file """
try:
if Path(self._cfg_file).is_file():
with open(self._cfg_file) as config_file:
self._cfg = json.load(config_file)
else:
self._cfg = {}
except Exception as e:
err = f"Can't read '{self._cfg_file}'"
logging.error(f"{repr(e)} - {err}")
def _write_cfg(self):
""" Write the JSON dictionary into the given configuration file """
try:
Path(self._cfg_file).parent.mkdir(parents=True, exist_ok=True)
with open(self._cfg_file, "w") as config_file:
json.dump(self._cfg, config_file, indent=4)
except Exception as e:
err = f"Can't write '{self._cfg_file}'"
logging.error(f"{repr(e)} - {err}")
def get(self, *keys):
""" Return the value of the given key(s) from a configuration file """
if not self._cfg:
self._read_cfg()
if not keys:
return self._cfg
value = self._cfg
try:
for key in keys:
value = value[key]
except Exception as e:
err = f"Can't get '{keys}' from '{self._cfg_file}'"
logging.debug(f"{repr(e)} - {err}")
return None
return value
def set(self, value, *keys):
""" Set a new value for the given key(s) in the configuration file.
Will also execute the callback method if there is one """
if not self._cfg:
self._read_cfg()
if not keys:
return
tmp_cfg = self._cfg
try:
for key in keys[:-1]:
tmp_cfg = tmp_cfg.setdefault(key, {})
tmp_cfg[keys[-1]] = value
self._write_cfg()
except Exception as e:
err = f"Can't set '{keys}' in '{self._cfg_file}'"
logging.debug(f"{repr(e)} - {err}")
def remove(self, *keys):
""" Remove given key(s) from the configuration file.
Will also execute the callback method if there is one """
if not self._cfg:
self._read_cfg()
if not keys:
return
tmp_cfg = self._cfg
try:
for key in keys[:-1]:
tmp_cfg = tmp_cfg.setdefault(key, {})
del tmp_cfg[keys[-1]]
self._write_cfg()
except KeyError as e:
err = f"Can't remove key '{keys}' from '{self._cfg_file}'"
logging.debug(f"{repr(e)} - {err}")