-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsettings.py
61 lines (50 loc) · 1.81 KB
/
settings.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
import os
import json
import logging
import os.path as path
SETTINGS_LOG = logging.getLogger("md_spine_tools.settings")
class MDSTSettings(object):
def __init__(self, filepath):
if path.exists(filepath):
# read settings file
self.load_settings_file(filepath)
else:
# new settings file
try:
os.makedirs(path.dirname(filepath))
with open(filepath, 'w') as _:
pass
except OSError:
SETTINGS_LOG.error('Failed creating new settings file', exc_info=True)
# default settings
self.config_path = filepath
self.config_dir = path.dirname(filepath)
def __setattr__(self, name, value):
super(MDSTSettings, self).__setattr__(name, value)
self.save_settings_file()
def __getattr__(self, attr):
try:
return super(MDSTSettings, self).__getattr__(attr)
except AttributeError:
return None
def __delattr__(self, name):
super(MDSTSettings, self).__delattr__(name)
self.save_settings_file()
def load_settings_file(self, filepath):
# load settings file
settings_dict = {}
with open(filepath) as f:
try:
settings_dict = json.load(f)
except Exception:
SETTINGS_LOG.error('Failed loading settings file', exc_info=True)
self.config_path = filepath
for k, v in settings_dict.items():
setattr(self, k, v)
def save_settings_file(self):
# save settings file
with open(self.config_path, 'w') as f:
try:
json.dump(self.__dict__, f, indent=4)
except Exception:
SETTINGS_LOG.error('Failed saving settings file', exc_info=True)