-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatastore1.py
41 lines (35 loc) · 1.11 KB
/
datastore1.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
import os, json
class DataStore:
dpath = '/datastore'
data = dict()
def __init__(self, store_freq=5, buckets=['default']):
if not self.isdir(self.dpath):
os.mkdir(self.dpath)
for b in buckets:
bpath = '/' + self.dpath + '/' + b
if self.isfile(bpath):
v = json.load(bpath)
else:
v = dict()
self.data[b] = v
self.alive_f = True
def alive(self):
return self.alive_f
def isdir(self, path):
try:
return os.stat(self.dpath)[0] & 0o40000
except OSError:
return False
def isfile(self, path):
try:
return os.stat(self.dpath)[0] & 0o100000
except OSError:
return False
def persist(self, bucket='default', all=False): # XXX needs thread safety
bpath = '/' + self.dpath + '/' + bucket
bpath_t = bpath + '.tmp'
fp = open(bpath_t, 'w')
json.dump(self.data[bucket], fp)
fp.flush()
fp.close()
os.rename(bpath_t, bpath) # XXX May not be necessary with littlefs