-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.py
74 lines (57 loc) · 2.23 KB
/
config.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
#Copyright (c) 2015 3D Control Systems LTD
#3DPrinterOS client is free software: you can redistribute it and/or modify
#it under the terms of the GNU Affero General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#3DPrinterOS client is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU Affero General Public License for more details.
#You should have received a copy of the GNU Affero General Public License
#along with 3DPrinterOS client. If not, see <http://www.gnu.org/licenses/>.
# Author: Vladimir Avdeev <another.vic@yandex.ru>
import json
import threading
def get_settings():
return Config.instance().settings
def get_profiles():
return Config.instance().profiles
def get_app():
return Config.instance().app
class Singleton(object):
lock = threading.Lock()
_instance = None
@classmethod
def instance(cls):
with cls.lock:
if not cls._instance:
print "Creating new instance of " + cls.__name__
cls._instance = cls()
return cls._instance
class Config(Singleton):
SETTINGS_FILE_NAME = 'settings.json'
def __init__(self):
self.settings = self.load_settings()
self.profiles = None
self.app = None
def load_settings(self):
with open(self.SETTINGS_FILE_NAME) as settings_file:
try:
config = json.loads(settings_file.read())
except Exception as e:
print "Error reading %s: %s" % (self.SETTINGS_FILE_NAME, str(e))
else:
return config
def save_settings(self, settings):
with open(self.SETTINGS_FILE_NAME, 'w') as settings_file:
try:
jdata = json.dumps(settings)
except Exception as e:
print "Error writing %s: %s" % (self.SETTINGS_FILE_NAME, str(e))
return False
settings_file.write(jdata)
return True
def set_profiles(self, profiles):
self.profiles = profiles
def set_app_pointer(self, app):
self.app = app