-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathdrivers.py
More file actions
211 lines (152 loc) · 5.02 KB
/
drivers.py
File metadata and controls
211 lines (152 loc) · 5.02 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
botogram.shared.drivers
Builtin generic drivers for the shared state
Copyright (C) 2015-2016 Pietro Albini <pietro@pietroalbini.io>
Released under the MIT license
"""
import builtins
import threading
import uuid
import functools
# This is used as the default argument for some methods of DictProxy
_None = object()
class SharedObject:
def __init__(self, id, type):
self.id = id
self.type = type
self._prepare()
def _prepare(self):
raise NotImplementedError
def export(self):
raise NotImplementedError
def restore(self):
raise NotImplementedError
class SharedDict(SharedObject):
def _prepare(self):
self.value = {}
def restore(self, value):
self.value = value
def export(self):
return self.value.copy()
class SharedLock(SharedObject):
def _prepare(self):
self.lock = threading.Lock()
self.acquired = False
def restore(self, value):
# Restore lock to a clean state
self._prepare()
def export(self):
return self.acquired
class LocalDriver:
"""Local driver for the shared memory"""
def __init__(self):
self._objects = {}
self._types_mapping = {
"dict": SharedDict,
"lock": SharedLock,
}
def __reduce__(self):
return rebuild_local_driver, (self.data_export(),)
def _ensure_object(type):
"""Ensure the object exists and it's of that type"""
def decorator(f):
@functools.wraps(f)
def wrapper(self, object_id, *args, **kwargs):
if object_id not in self._objects:
raise ValueError("Object doesn't exist: %s" % object_id)
obj = self._objects[object_id]
if obj.type != type:
raise TypeError("Operation not supported on the %s type" %
obj.type)
return f(self, obj, *args, **kwargs)
return wrapper
return decorator
##############################
# objects implementation #
##############################
def object_list(self):
return list(self._objects.keys())
def object_exists(self, id):
return id in self._objects
def object_type(self, id):
return self._objects[id].type
def object_create(self, type, id):
if id in self._objects:
raise NameError("An object with id %s already exists!" % id)
try:
cls = self._types_mapping[type]
except KeyError:
raise TypeError("Unsupported type: %s" % type)
self._objects[id] = cls(id, type)
def object_delete(self, id):
if id in self._objects:
del self._objects[id]
############################
# dicts implementation #
############################
@_ensure_object("dict")
def dict_length(self, obj):
return len(obj.value)
@_ensure_object("dict")
def dict_item_get(self, obj, key):
return obj.value[key]
@_ensure_object("dict")
def dict_item_set(self, obj, key, value):
obj.value[key] = value
@_ensure_object("dict")
def dict_item_delete(self, obj, key):
del obj.value[key]
@_ensure_object("dict")
def dict_contains(self, obj, key):
return key in obj.value
@_ensure_object("dict")
def dict_keys(self, obj):
return tuple(obj.value.keys())
@_ensure_object("dict")
def dict_values(self, obj):
return tuple(obj.value.values())
@_ensure_object("dict")
def dict_items(self, obj):
return tuple(obj.value.items())
@_ensure_object("dict")
def dict_clear(self, obj):
obj.value.clear()
@_ensure_object("dict")
def dict_pop(self, obj, key=_None):
# If no keys are provided pop a random item
if key is _None:
return obj.value.popitem()
else:
return obj.value.pop(key)
############################
# Locks implementation #
############################
@_ensure_object("lock")
def lock_acquire(self, obj):
obj.lock.acquire()
obj.acquired = True
@_ensure_object("lock")
def lock_release(self, obj):
obj.acquired = False
obj.lock.release()
@_ensure_object("lock")
def lock_status(self, obj):
return obj.acquired
###############################
# Importing and exporting #
###############################
def data_import(self, data):
"""Import data from another driver"""
for id, content in data.items():
self.object_create(content["type"], id)
self._objects[id].restore(content["value"])
def data_export(self):
"""Export the data contained in this driver"""
result = {}
for obj in self._objects.values():
result[obj.id] = {"type": obj.type, "value": obj.export()}
return result
def rebuild_local_driver(data):
obj = LocalDriver()
obj.data_import(data)
return obj