Child: [a9f890] (diff)

Download this file

base.py    385 lines (329 with data), 12.3 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import decimal
import hashlib
from datetime import datetime
from collections import defaultdict
import pymongo
class Object(dict):
'Dict providing object-like attr access'
def __init__(self, *l, **kw):
dict.__init__(self, *l, **kw)
def __getattr__(self, name):
try:
return self[name]
except KeyError:
raise AttributeError, name
def __setattr__(self, name, value):
self.__setitem__(name, value)
@classmethod
def from_bson(cls, bson):
if isinstance(bson, dict):
return Object(
(k, Object.from_bson(v))
for k,v in bson.iteritems())
elif isinstance(bson, list):
return [
Object.from_bson(v)
for v in bson ]
else:
return bson
def make_safe(self):
safe_self = _safe_bson(self)
self.update(safe_self)
class ManagerDescriptor(object):
'''Python descriptor to provide a way to add the .m. attribute to mapped
classes (which is a Manager - see below) such that the object at the
attribute "knows" which instance it's attached to.'''
def __init__(self, mgr_cls):
self.mgr_cls = mgr_cls
def __get__(self, instance, cls):
return self.mgr_cls(instance, cls)
class Manager(object):
'''Simple class that proxies a bunch of commands to the Session object for
the managed class/instance.'''
def __init__(self, instance, cls):
self.session = cls.__mongometa__.session
self.instance = instance
self.cls = cls
def __call__(self, session):
'''In order to use an alternate session, just use Class.mgr(other_session)'''
return Manager(session, self.instance, self.cls)
def get(self, **kwargs):
"""
Returns one matching record, or None
e.g.
get(source='sf.net',shortname='foo')
"""
return self.session.get(self.cls, **kwargs)
def find(self, *args, **kwargs):
"""
spec=None, fields=None, ...
http://api.mongodb.org/python/0.15.1/pymongo.collection.Collection-class.html#find
e.g.
find({"source": "sf.net"})
find({"source": "sf.net"},['shortname']) # only return shortname fields
"""
return self.session.find(self.cls, *args, **kwargs)
def remove(self, *args, **kwargs):
"""
remove(spec_or_object_id)
"""
return self.session.remove(self.cls, *args, **kwargs)
def find_by(self, **kwargs):
"""
same as find(spec=kwargs)
e.g.
find_by(source='sf.net', foo='bar')
"""
return self.session.find_by(self.cls, **kwargs)
def count(self):
return self.session.count(self.cls)
def ensure_index(self, fields):
return self.session.ensure_index(self.cls, fields)
def ensure_indexes(self):
return self.session.ensure_indexes(self.cls)
def group(self, *args, **kwargs):
return self.session.group(self.cls, *args, **kwargs)
def update_partial(self, spec, fields, upsert=False):
return self.session.update_partial(self.cls, spec, fields, upsert)
def save(self, *args):
"""
Acts on object instance
e.g.
cp = model.CustomPage(...)
cp['foo'] = 3
cp.m.save()
with parameters, only sets specified fields
cp.m.save('foo')
"""
return self.session.save(self.instance, *args)
def insert(self):
"""
Acts on object instance
e.g.
model.CustomPage(...).m.insert()
"""
return self.session.insert(self.instance)
def update(self, spec, upsert=False):
"""
Acts on object instance
e.g.
model.CustomPage(...).m.update({'foo':'bar'})
"""
return self.session.update(self.instance, spec, upsert)
def delete(self):
"""
Acts on object instance
e.g.
model.CustomPage(...).m.delete()
"""
return self.session.delete(self.instance)
def set(self, fields_values):
"""
Acts on object instance
e.g.
model.CustomPage(...).m.set({'foo':'bar'})
"""
return self.session.set(self.instance, fields_values)
def increase_field(self, **kwargs):
"""
Acts on object instance
Sets a field to value, only if value is greater than the current value
Does not change it locally
e.g.
model.GlobalSettings.instance().increase_field(key=value)
"""
return self.session.increase_field(self.instance, **kwargs)
def migrate(self):
'''Load each object in the collection and immediately save it.
'''
for m in self.find({}):
m.m.save()
class DocumentMeta(type):
'''Metaclass for Documents providing several services:
- the __mongometa__ attribute of the class is modified so that it subclasses
the __mongometa__ attributes of the Document's base classes (i.e. "class
Child.__mongometa__(Parent.__mongometa__)
- The "special" __mongometa__ attribute "schema" will extend, not override
parent __mongometa__'s "schema" attributes
- The class is added to a polymorphic registry to allow for polymorphic
loading from the DB if it specifies which field is its polymorphic
discriminator ("polymorphic_on")
'''
def __init__(cls, name, bases, dct):
from . import schema
# Build mongometa (make it inherit from base classes' mongometas
mm_bases = []
for base in bases:
mm = getattr(base, '__mongometa__', None)
if mm is None: continue
mm_bases.append(mm)
mm_dict = {}
if hasattr(cls, '__mongometa__'):
mm_dict.update(cls.__mongometa__.__dict__)
mm = cls.__mongometa__ = type('__mongometa__', tuple(mm_bases), mm_dict)
if not hasattr(mm, 'polymorphic_on'):
mm.polymorphic_on = None
mm.polymorphic_registry = None
# Make sure mongometa's schema incorporates base schemas
my_schema = schema.Object()
for base in mm_bases:
if hasattr(base, 'schema'):
if base.schema:
my_schema.extend(schema.SchemaItem.make(base.schema))
if mm.schema:
my_schema.extend(schema.SchemaItem.make(mm.schema))
if not my_schema.fields:
mm.schema = None
else:
polymorphic_identity = mm_dict.get('polymorphic_identity', cls.__name__)
mm.schema = my_schema
mm.schema.managed_class = cls
mm.schema.set_polymorphic(
mm.polymorphic_on, mm.polymorphic_registry, polymorphic_identity)
class Document(Object):
'''Base class for all mapped MongoDB objects (the Document class can be
thought of as the "collection", where a Document instance is a "document".
'''
__metaclass__=DocumentMeta
_registry = defaultdict(list)
m = ManagerDescriptor(Manager)
class __mongometa__:
'''Supply various information on how the class is mapped without
polluting the class's namespace. In particular,
name - collection name
session - Session object managing the object (link to a DataStore)
indexes - list of field name tuples specifying which indexes should exist
for the document
schema - (optional) schema object
polymorphic_on - (optional) field name that specifies the concrete class
of each document in a polymorphic collection
polymorphic_identity - (optional) value that should be in the
polymorphic_on field to specify that the concrete
class is the current one (if unspecified, the
class's __name__ attribute is used)
'''
name=None
session=None
schema=None
indexes=[]
def __init__(self, data):
data = Object.from_bson(data)
dict.update(self, data)
@classmethod
def make(cls, data, allow_extra=False, strip_extra=True):
'Kind of a virtual constructor'
if cls.__mongometa__.schema:
return cls.__mongometa__.schema.validate(
data, allow_extra=allow_extra, strip_extra=strip_extra)
else:
return cls(data)
class VersionedDocument(Document):
'''Special Document allowing snapshot and rollback.'''
class __mongometa__:
'''Extension of the Document class providing
snapshot - class to be used as snapshots of the current class
snapshot_untracked - field names to be skipped when snapshotting
'''
name=None
session=None
indexes=[]
snapshot=None
snapshot_untracked=set()
schema=dict(
_last_snapshot_id=str)
def snapshot(self, author):
'''Create a snapshot object of the current object with the specified
author field.
'''
mm = self.__mongometa__
# Get last snapshot
snapshot = mm.snapshot(dict(
(k,v) for k,v in self.iteritems()
if k not in mm.snapshot_untracked))
snapshot._original_id = self._id
snapshot._author = author
del snapshot['_id']
snapshot._id = hashlib.sha1(str(snapshot)).hexdigest()
if snapshot._id == self.get('_last_snapshot_id', None):
return
snapshot.m.save()
self._last_snapshot_id = snapshot._id
def rollback(self):
'''Revert one snapshot operation, restoring the values from the most
recent snapshot and destroying the most recent snapshot in the process.
'''
mm = self.__mongometa__
snapshot = mm.snapshot.m.find(
dict(_id=self._last_snapshot_id)).one()
fields = dict(snapshot)
del fields['_author']
self.update(fields)
if '_last_snapshot_id' not in snapshot:
del self['_last_snapshot_id']
snapshot.m.delete()
class Cursor(object):
'''Python class proxying a MongoDB cursor, constructing and validating
objects that it tracks
'''
def __init__(self, cls, cursor):
self.cls = cls
self.cursor = cursor
def __iter__(self):
return self
def __len__(self):
return self.count()
def next(self):
bson = self.cursor.next()
if bson:
return self.cls.make(bson, allow_extra=False, strip_extra=True)
else:
return None
def count(self):
return self.cursor.count()
def limit(self, limit):
self.cursor = self.cursor.limit(limit)
return self
def skip(self, skip):
self.cursor = self.cursor.skip(skip)
return self
def hint(self, index_or_name):
self.cursor = self.cursor.hint(index_or_name)
return self
def sort(self, *args, **kwargs):
self.cursor = self.cursor.sort(*args, **kwargs)
return self
def one(self):
try:
result = self.next()
except StopIteration:
raise ValueError, 'Less than one result from .one()'
try:
self.next()
except StopIteration:
return result
raise ValueError, 'More than one result from .one()'
def first(self):
try:
return self.next()
except StopIteration:
return None
def all(self):
return list(self)
def _safe_bson(obj):
'''Verify that the obj is safe for bsonification (in particular, no tuples or
Decimal objects
'''
if isinstance(obj, list):
return map(_safe_bson, obj)
elif isinstance(obj, dict):
return Object((k, _safe_bson(v)) for k,v in obj.iteritems())
elif isinstance(obj, (basestring, int, long, float, datetime)):
return obj
elif isinstance(obj, decimal.Decimal):
return float(obj)
elif obj is None:
return obj
elif isinstance(obj, pymongo.objectid.ObjectId):
return obj
else:
assert False, '%s is not safe for bsonification' % type(obj)