##// END OF EJS Templates
tests: added extra tests for perm origin dicts and serialization problems
super-admin -
r4953:8a6b63aa default
parent child Browse files
Show More
@@ -0,0 +1,72 b''
1 import collections
2
3
4 class PermOriginDict(dict):
5 """
6 A special dict used for tracking permissions along with their origins.
7
8 `__setitem__` has been overridden to expect a tuple(perm, origin)
9 `__getitem__` will return only the perm
10 `.perm_origin_stack` will return the stack of (perm, origin) set per key
11
12 >>> perms = PermOriginDict()
13 >>> perms['resource'] = 'read', 'default', 1
14 >>> perms['resource']
15 'read'
16 >>> perms['resource'] = 'write', 'admin', 2
17 >>> perms['resource']
18 'write'
19 >>> perms.perm_origin_stack
20 {'resource': [('read', 'default', 1), ('write', 'admin', 2)]}
21 """
22
23 def __init__(self, *args, **kw):
24 dict.__init__(self, *args, **kw)
25 super(PermOriginDict, self).__setattr__('perm_origin_stack', collections.OrderedDict())
26
27 def __setitem__(self, key, perm_origin_obj_id):
28 print(f"setting: {perm_origin_obj_id}")
29 if isinstance(perm_origin_obj_id, tuple):
30 (perm, origin, obj_id) = perm_origin_obj_id
31 self.perm_origin_stack.setdefault(key, []).append((perm, origin, obj_id))
32 else:
33 perm = perm_origin_obj_id
34 dict.__setitem__(self, key, perm)
35
36 # def __getstate__(self):
37 # dict_state = self.__dict__.copy()
38 # perm_origin_stack = self.perm_origin_stack.__dict__.copy()
39 # print(dict_state, perm_origin_stack)
40 # return dict_state, perm_origin_stack
41 #
42 # def __setstate__(self, state):
43 # print('setstate called')
44 # print(f'setstate os {state}')
45 # self.perm_origin_stack = collections.OrderedDict()
46
47
48 dd = PermOriginDict()
49
50 dd['key1'] = 'perm1', 'from_where1', 121
51 dd['key2'] = 'perm2', 'from_where2', 122
52 dd['key3'] = 'perm3', 'from_where3', 123
53 dd['key3'] = 'perm4', 'from_where4', 125
54
55
56 print(list(dd.keys()))
57 print(f"key:{dd['key1']}")
58 print(dd.perm_origin_stack)
59
60 print ('-- before pickle --')
61
62 import pickle
63
64 data = pickle.dumps(dd, protocol=pickle.HIGHEST_PROTOCOL)
65 print(data)
66
67 new_dd = pickle.loads(data)
68
69 print ('-- after pickle --')
70 print(list(dd.keys()))
71 print(f"key:{dd['key1']}")
72 print(dd.perm_origin_stack)
General Comments 0
You need to be logged in to leave comments. Login now