-
Notifications
You must be signed in to change notification settings - Fork 376
/
Copy pathStore.py
326 lines (264 loc) · 11.6 KB
/
Store.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python3
# Contest Management System - http://cms-dev.github.io/
# Copyright © 2011-2015 Luca Wehrstedt <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import logging
import os
import re
from gevent.lock import RLock
from cmsranking.Entity import Entity, InvalidKey, InvalidData
logger = logging.getLogger(__name__)
# Global shared lock for all Store instances.
LOCK = RLock()
class Store:
"""A store for entities.
Provide methods to perform the CRUD operations (create, retrieve,
update, delete) on a set of entities, accessed by their unique key.
It's very similar to a dict, except that keys are strings, values
are of a single type (defined at init-time) and it's possible to
get notified when something changes by providing appropriate
callbacks.
"""
def __init__(self, entity, path, all_stores, depends=None):
"""Initialize an empty EntityStore.
The entity definition given as argument will define what kind
of entities will be stored. It cannot be changed.
entity (type): the class definition of the entities that will
be stored
"""
if not issubclass(entity, Entity):
raise ValueError("The 'entity' parameter "
"isn't a subclass of Entity")
self._entity = entity
self._path = path
self._all_stores = all_stores
self._depends = depends if depends is not None else []
self._store = dict()
self._create_callbacks = list()
self._update_callbacks = list()
self._delete_callbacks = list()
def load_from_disk(self):
"""Load the initial data for this store from the disk.
"""
try:
os.mkdir(self._path)
except OSError:
# it's ok: it means the directory already exists
pass
try:
for name in os.listdir(self._path):
# TODO check that the key is '[A-Za-z0-9_]+'
if name[-5:] == '.json' and name[:-5] != '':
with open(os.path.join(self._path, name), 'rb') as rec:
item = self._entity()
item.set(json.load(rec))
item.key = name[:-5]
self._store[name[:-5]] = item
except OSError:
# the path isn't a directory or is inaccessible
logger.error("Path is not a directory or is not accessible "
"(or other I/O error occurred)", exc_info=True)
except ValueError:
logger.error("Invalid JSON", exc_info=False,
extra={'location': os.path.join(self._path, name)})
except InvalidData as exc:
logger.error(str(exc), exc_info=False,
extra={'location': os.path.join(self._path, name)})
def add_create_callback(self, callback):
"""Add a callback to be called when entities are created.
Callbacks can be any kind of callable objects. They must accept
a single argument: the key of the entity.
"""
self._create_callbacks.append(callback)
def add_update_callback(self, callback):
"""Add a callback to be called when entities are updated.
Callbacks can be any kind of callable objects. They must accept
a single argument: the key of the entity.
"""
self._update_callbacks.append(callback)
def add_delete_callback(self, callback):
"""Add a callback to be called when entities are deleted.
Callbacks can be any kind of callable objects. They must accept
a single argument: the key of the entity.
"""
self._delete_callbacks.append(callback)
def create(self, key, data):
"""Create a new entity.
Create a new entity with the given key and the given data.
key (unicode): the key with which the entity will be later
accessed
data (dict): the properties of the entity
raise (InvalidKey): if key isn't a unicode or if an entity
with the same key is already present in the store.
raise (InvalidData): if data cannot be parsed, if it's missing
some properties or if properties are of the wrong type.
"""
if not isinstance(key, str) or key in self._store:
raise InvalidKey("Key already in store.")
# create entity
with LOCK:
item = self._entity()
item.set(data)
if not item.consistent(self._all_stores):
raise InvalidData("Inconsistent data")
item.key = key
self._store[key] = item
# notify callbacks
for callback in self._create_callbacks:
callback(key, item)
# reflect changes on the persistent storage
try:
path = os.path.join(self._path, key + '.json')
with open(path, 'wt', encoding="utf-8") as rec:
json.dump(self._store[key].get(), rec)
except OSError:
logger.error("I/O error occured while creating entity",
exc_info=True)
def update(self, key, data):
"""Update an entity.
Update an existing entity with the given key and the given
data.
key (unicode): the key of the entity that has to be updated
data (dict): the new properties of the entity
raise (InvalidKey): if key isn't a unicode or if no entity
with that key is present in the store.
raise (InvalidData): if data cannot be parsed, if it's missing
some properties or if properties are of the wrong type.
"""
if not isinstance(key, str) or key not in self._store:
raise InvalidKey("Key not in store.")
# update entity
with LOCK:
item = self._entity()
item.set(data)
if not item.consistent(self._all_stores):
raise InvalidData("Inconsistent data")
item.key = key
old_item = self._store[key]
self._store[key] = item
# notify callbacks
for callback in self._update_callbacks:
callback(key, old_item, item)
# reflect changes on the persistent storage
try:
path = os.path.join(self._path, key + '.json')
with open(path, 'wt', encoding="utf-8") as rec:
json.dump(self._store[key].get(), rec)
except OSError:
logger.error("I/O error occured while updating entity",
exc_info=True)
def merge_list(self, data_dict):
"""Merge a list of entities.
Take a dictionary of entites and, for each of them:
- if it's not present in the store, create it
- if it's present, update it
data_dict (dict): the dictionary of entities
raise (InvalidData) if data cannot be parsed, if an entity is
missing some properties or if properties are of the wrong
type.
"""
with LOCK:
if not isinstance(data_dict, dict):
raise InvalidData("Not a dictionary")
item_dict = dict()
for key, value in data_dict.items():
try:
# FIXME We should allow keys to be arbitrary unicode
# strings, so this just needs to be a non-empty check.
if not re.match('[A-Za-z0-9_]+', key):
raise InvalidData("Invalid key")
item = self._entity()
item.set(value)
if not item.consistent(self._all_stores):
raise InvalidData("Inconsistent data")
item.key = key
item_dict[key] = item
except InvalidData as exc:
raise InvalidData("[entity %s] %s" % (key, exc))
for key, value in item_dict.items():
is_new = key not in self._store
old_value = self._store.get(key)
# insert entity
self._store[key] = value
# notify callbacks
if is_new:
for callback in self._create_callbacks:
callback(key, value)
else:
for callback in self._update_callbacks:
callback(key, old_value, value)
# reflect changes on the persistent storage
try:
path = os.path.join(self._path, key + '.json')
with open(path, 'wt', encoding="utf-8") as rec:
json.dump(value.get(), rec)
except OSError:
logger.error(
"I/O error occured while merging entity lists",
exc_info=True)
def delete(self, key):
"""Delete an entity.
Delete an existing entity from the store.
key (unicode): the key of the entity that has to be deleted
raise (InvalidKey): if key isn't a unicode or if no entity
with that key is present in the store.
"""
if not isinstance(key, str) or key not in self._store:
raise InvalidKey("Key not in store.")
with LOCK:
# delete entity
old_value = self._store[key]
del self._store[key]
# enforce consistency
for depend in self._depends:
for o_key, o_value in list(depend._store.items()):
if not o_value.consistent(self._all_stores):
depend.delete(o_key)
# notify callbacks
for callback in self._delete_callbacks:
callback(key, old_value)
# reflect changes on the persistent storage
try:
os.remove(os.path.join(self._path, key + '.json'))
except OSError:
logger.error("Unable to delete entity", exc_info=True)
def delete_list(self):
"""Delete all entities.
Delete all existing entities from the store.
"""
with LOCK:
# delete all entities
for key in list(self._store.keys()):
self.delete(key)
def retrieve(self, key):
"""Retrieve an entity.
Retrieve an existing entity from the store.
key (unicode): the key of the entity that has to be retrieved
raise (InvalidKey): if key isn't a unicode or if no entity
with that key is present in the store.
"""
if not isinstance(key, str) or key not in self._store:
raise InvalidKey("Key not in store.")
# retrieve entity
return self._store[key].get()
def retrieve_list(self):
"""Retrieve a list of all entities."""
result = dict()
for key, value in self._store.items():
result[key] = value.get()
return result
def __contains__(self, key):
return key in self._store