forked from eiwa-dev/uodm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uodm.py
292 lines (228 loc) · 9 KB
/
uodm.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
"""Micro-ODM for MongoDB.
Features:
- All attribute writes are immediately commited.
- An ODM object encapsulates the connection and allows only one object
per document to exist at a given time.
- Does NOT use the "_id" field. Documents are indexed by a "_name_"
field with a uuid. It is an error to have two documents with the
same name.
- Untyped schema.
- Object can hold referenced to other objects.
"""
from __future__ import division, print_function, unicode_literals
import weakref
import abc
import uuid
__author__ = [ "Juan Carrano <[email protected]>",
"Diego Vazquez <[email protected]>"
]
__copyright__ = "Copyright 2016 EIWA S.A. All rights reserved."
__license__ = """Unauthorized copying of this file, via any medium is
strictly prohibited. Proprietary and confidential"""
class DocumentError(Exception):
pass
class Attr:
"""Describe a Document attribute."""
def __init__(self, flags='', *args):
"""Flags is a string which can be empty or have any of the
following characters:
'm': mutable (can be modified)
'r': reference (is a reference to another MappedDocument)
If 'r' is specified, the class of the referenced MappedDocument
must be specified as an additional argument.
If 'r' is not specified, an additional argument is interpreted
as a default value.
"""
self.mutable = 'm' in flags
self.reference = 'r' in flags
if self.reference:
self.ref_class = args[0]
self.has_default = not self.reference and len(args)
if self.has_default:
self.default = args[0]
def raw_value(self, value):
"""If the attribute is a reference, return v's name"""
return value if not self.reference else (
value if isinstance(value, uuid.UUID) else (
value._name_ if value is not None else None))
class Document(abc.ABC):
"""A Document object maps to a document in a collection.
All documents have a _name_ attribute consisting of a UUID.
Attributes can be references to other Documents. The database
document will store the UUID of the referred document. When the
attribute is acceces, the UUID is looked up in the ODM context and
the corresponding Document object is returned.
References can be None.
"""
@property
@abc.abstractmethod
def DB_COLLECTION(self):
"""The name of the collection where object of this type will be
stored."""
pass
@property
@abc.abstractmethod
def ATTRIBUTES(self):
"""Mapping from attribute name to an Attr object.
Example:
ATTRIBUTES={
'first_name': Attr()
'nickname': Attr('s')
'logged_in': Attr('s', False)
'city': Attr('sr', CityClass)
'parent': Attr('r', Parent)
"""
pass
def __init__(self, odm, _name_ = None, **kwargs):
"""Initialize the Document with the given values. If _name_
is not given, it is assigned a new UUID.
odm must be a ODM object.
This constructor DOES NOT write the document to the database.
Note that you probably don't want to use this method directly,
but rather call ODM.new or Document.new_like .
"""
self.contents = {}
_kwargs = dict(kwargs)
for k, attr_ in self.ATTRIBUTES.items():
if k in _kwargs:
value = _kwargs.pop(k)
elif attr_.has_default:
value = attr_.default
else:
raise ValueError("Argument ´%s´ not given and no default available"%k)
raw_value = attr_.raw_value(value)
self.contents[k] = raw_value
_kwargs.pop('_id', None)
if _kwargs:
raise ValueError("Too many keyword arguments")
self._name_ = _name_ or self.generate_uuid()
self._odm = odm
@classmethod
def generate_uuid(cls):
return uuid.uuid1()
def write(self):
"""Insert the document in the database. This is a low-level
method"""
_doc = {"_name_": self._name_}
_doc.update(self.contents)
self._odm.db_conn[self.DB_COLLECTION].insert(_doc)
def __getattr__(self, name):
if name in self.ATTRIBUTES:
_attr = self.ATTRIBUTES[name]
raw_value = self.contents[name]
if _attr.reference and raw_value is not None:
return self._odm.find_one(_attr.ref_class, raw_value)
else:
return raw_value
else:
raise AttributeError(name)
def __setattr__(self, name, value):
if name in self.ATTRIBUTES:
_attr = self.ATTRIBUTES[name]
if not _attr.mutable:
raise AttributeError("Attribute %s is read-only"%name)
raw_value = _attr.raw_value(value)
# FIXME: ensure that if the db update fails, this object is
# not modified.
self._odm.db_conn[self.DB_COLLECTION].update(
{"_name_":self._name_},
{'$set':{name: raw_value}})
self.contents[name] = raw_value
else:
super().__setattr__(name, value)
def __repr__(self):
return "{}({})".format(self.__class__.__name__,
",".join("{!r}={!r}".format(*it) for it in self.contents.items()))
def set_multiple(self, d):
"""Modify multiple values in one operation.
This works because document-level operations in Mongo are atomic.
"""
raw_update = {}
for k, v in d.items():
try:
_attr = self.ATTRIBUTES[k]
except KeyError:
raise AttributeError("Attribute %s not defined"%k)
if not _attr.mutable:
raise AttributeError("Attribute %s is read-only"%k)
raw_value = _attr.raw_value(v)
raw_update[k] = raw_value
self._odm.db_conn[self.DB_COLLECTION].update(
{"_name_":self._name_},
{'$set':raw_update})
def find_one(self, _name_):
"""Find one document of the same type in the same ODM context as
this object.
"""
return self._odm.find_one(type(self), _name_)
def find_all(self, criteria):
"""Find all document of the same type which match "criteria"
in the same ODM context as this object.
"""
return self._odm.find_all(type(self), criteria)
def new_like(self, **kwargs):
"""Create a new object of the same type of this object in the
same ODM context"""
return self._odm.new(type(self), **kwargs)
class ODM:
"""ODM context.
This class encapsulates a database connection. The key is that it
provides a cache to ensure that only one object with a given _name_
exists at any given time. This is the reason for using UUIDs instead
of Mongo's _id field, which is guaranteed to be unique only within
one collection.
"""
def __init__(self, db_conn):
"""db_conn must be a mongo database. Example:
client = pymongo.MongoClient(connection_uri)[db_name]
"""
self.db_conn = db_conn
self._cache = weakref.WeakValueDictionary()
def find_one(self, cls, _name_):
"""Find a document by name. If more or less than one document is
found, raise a DocumentError.
cls: A class derived from Document
_name_: a UUID.
"""
try:
obj = self._cache[_name_]
return obj
except KeyError:
pass
cursor = self.db_conn[cls.DB_COLLECTION].find({'_name_':_name_})
if cursor.count() == 0:
raise DocumentError("No such document.")
elif cursor.count() != 1:
raise DocumentError("More than one document with the same _name_. Possible DB corruption.")
d = cursor[0]
return self._new(cls, **d)
def find_all(self, cls, criteria):
"""Return an iterable yielding all matching documents.
cls: A class derived from Document
criteria: search criteria like the one used in mongo's find()
method.
"""
cursor = self.db_conn[cls.DB_COLLECTION].find(criteria)
for d in cursor:
try:
obj = self._cache[d['_name_']]
yield obj
continue
except KeyError:
pass
yield self._new(cls, **d)
def _new(self, cls, **kwargs):
"""Create a new object and register it in the cache.
"""
obj = cls(self, **kwargs)
self._cache[obj._name_] = obj
return obj
def new(self, cls, **kwargs):
"""Create a new object, register it in the cache and commit it
to the database.
cls: A class derived from Document
kwargs: Object initializers.
"""
obj = self._new(cls, **kwargs)
obj.write()
return obj