- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstorage.py
More file actions
Latest commit
56 lines (47 loc) · 1.52 KB
/
Copy pathstorage.py
File metadata and controls
56 lines (47 loc) · 1.52 KB
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
importos
importos.path
importjson
importtime
importcopy
classStorage:
''' Storage simulation for both server and client. '''
def__init__(self, filename):
self.filename=filename
ifos.path.exists(filename):
withopen(filename, 'r') asf:
self.objects=json.loads(f.read())
else:
self.objects= { }
defnext_key(self):
keys=self.objects.keys()
max_key= (keysandmax(keys)) or0
returnmax_key+1
defcreate(self, obj):
''' key, ts might be originated from server. '''
key=self.next_key()
obj['key'] =key
self.objects[key] =obj
self.store_locally()
returnkey
defread(self, **kwargs):
''' not responsible for futher operations on the object. '''
if'key'inkwargs:
returncopy.deepcopy(self.objects[kwargs['key']])
forobjinself.objects.values():
matches=True
fork, vinkwargs.items():
ifknotinobjorobj[k]!=v:
matches=False
break
ifmatches:
returncopy.deepcopy(obj)
raiseKeyError
defupdate(self, key, obj):
obj['key'] =key
self.objects[key] =obj
self.store_locally()
defstore_locally(self):
withopen(self.filename, 'w') asf:
f.write(json.dumps(self.objects, indent=4))
defread_all(self):
returncopy.deepcopy(self.objects.values())