forked from StarryPy/StarryPy3k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration_manager.py
83 lines (72 loc) · 2.7 KB
/
configuration_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import json
import logging
import sys
from pathlib import Path
from utilities import recursive_dictionary_update, DotDict
logger = logging.getLogger("starrypy.configuration_manager")
class ConfigurationManager:
def __init__(self):
self._raw_config = None
self._raw_default_config = None
self._config = {}
self._dot_dict = None
self._path = None
def __repr__(self):
return "<ConfigurationManager: {}>".format(json.dumps(self.config))
@property
def config(self):
if self._dot_dict is None:
self._dot_dict = DotDict(self._config)
return self._dot_dict
def load_config(self, path, default=False):
if not isinstance(path, Path):
path = Path(path)
if default:
self.load_defaults(path)
try:
with path.open() as f:
self._raw_config = f.read()
except FileNotFoundError:
path.touch()
with path.open("w") as f:
f.write("{}")
self._raw_config = "{}"
self._path = path
try:
recursive_dictionary_update(self._config,
json.loads(self._raw_config))
except ValueError as e:
logger.error("Error while loading config.json file:\n\t"
"{}".format(e))
sys.exit(1)
if "plugins" not in self._config:
self._config['plugins'] = DotDict({})
def load_defaults(self, path):
path = Path(str(path) + ".default")
with path.open() as f:
self._raw_default_config = f.read()
recursive_dictionary_update(self._config,
json.loads(self._raw_default_config))
def save_config(self, path=None):
if path is None:
path = self._path
temp_path = Path(str(path) + "_")
with temp_path.open("w") as f:
json.dump(self.config, f, sort_keys=True, indent=4,
separators=(',', ': '), ensure_ascii=False)
path.unlink()
temp_path.rename(path)
logger.debug("Config file saved.")
def get_plugin_config(self, plugin_name):
if plugin_name not in self.config.plugins:
storage = DotDict({})
self.config.plugins[plugin_name] = storage
else:
storage = self.config.plugins[plugin_name]
return storage
def update_config(self, plugin_name, new_value):
if plugin_name not in self.config.plugins:
raise ValueError("Plugin name provided is not valid.")
if isinstance(new_value, dict):
self.config.plugins[plugin_name] = new_value
self.save_config()