-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstorable.py
42 lines (35 loc) · 1.17 KB
/
storable.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
import minijson
class Error(Exception):
pass
class KeyError(Error):
pass
class Storable(dict):
'''
Easy interface for storing dicts in a KVStore.
Object acts like a dict, but adds a save method that takes
a kvstore and returns the stored id.
'''
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self.oid = None
def load(self, datastore, oid):
dat = datastore.get(oid)
if dat:
try:
dat = minijson.decode(dat)
if isinstance(dat, dict):
self.clear()
self.update(dat)
self.oid = oid
else:
raise Error('StorableDict must be loaded from dict, key: %s' % oid)
except ValueError:
raise Error('StorableDict data at %s is invalid' % oid)
else:
raise Error('StorableDict got invalid key: %s' % oid)
def save(self, datastore):
self.oid = datastore.store(minijson.encode(self))
return self.oid
def __setitem__(self, *args):
self.oid = None
dict.__setitem__(self, *args)