-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
67 lines (50 loc) · 1.54 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
62
63
64
65
66
67
import json
import os
import time
import utils
import threading
SETTINGS_PATH = os.path.join(utils.HOME_DIR, 'settings.json')
class Settings(dict):
def __init__(self, path, *args, **kwds):
self.path = path
self.lock = threading.Lock()
if os.path.exists(self.path):
with open(path, 'r') as f:
try:
self.update(json.load(f))
except:
logging.error('could not load settings.')
dict.__init__(self, *args, **kwds)
def __setitem__(self, key, value):
dict.__setitem__(self, key, value)
self.save()
def __getitem__(self, key):
try:
return dict.__getitem__(self, key) or 0
except KeyError:
return 0
def clear(self):
with self.lock:
dict.clear(self)
self.save()
def increment(self, key, value=1):
with self.lock:
self[key] += value
def save(self):
tmp = '%s_%s' % (self.path, time.time())
with open(tmp, 'w') as f:
json.dump(self, f, separators=(',', ':'))
os.replace(tmp, self.path)
settings = Settings(SETTINGS_PATH)
if False:
print('Settings:')
for k,v in sorted(settings.items()): print(' ', k, repr(v))
if __name__ == '__main__':
# settings.clear()
import random
import logging
logging.set_level(logging.DEBUG)
logging.debug(settings)
settings['abc'] = random.randint(0, 100)
settings['xyz'] = random.randint(0, 100)
logging.debug(settings)