-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.py
57 lines (47 loc) · 1.44 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
import json
__author__ = 'mocsar'
import os
class Settings(object):
_data = None
@staticmethod
def get_all_keys():
res = None
try:
res = ['db-uri', 'mongo-db-collection', ]
except:
pass
return res
@staticmethod
def get(key, default=None):
if Settings._data is None: Settings._read_data()
return Settings._data.get(key, default)
@staticmethod
def set(key, value):
if Settings._data is None:
try:
Settings._read_data()
except:
pass
if Settings._data is None:
Settings._data = {}
Settings._data[key] = value
Settings._write_data()
@staticmethod
def _create_sfile_name():
home_dir = os.path.expanduser("~")
sfile = os.path.join(home_dir, '.config', 'csocso', 'ini.json')
return sfile
@staticmethod
def _read_data():
sfile = Settings._create_sfile_name()
with open(sfile) as json_file:
Settings._data = json.load(json_file)
if Settings._data is None:
Settings._data = {}
@staticmethod
def _write_data():
sfile = Settings._create_sfile_name()
if not os.path.exists(os.path.dirname(sfile)):
os.makedirs(os.path.dirname(sfile))
with open(sfile, 'w') as json_file:
json.dump(Settings._data, json_file, sort_keys=True, indent=4)