-
Notifications
You must be signed in to change notification settings - Fork 1
/
maroon.py
477 lines (378 loc) · 14.9 KB
/
maroon.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
'''
maroon models - simplified object-relational mapper for Python and MongoDB
by Jeremy Kelley <[email protected]> and Jeff McGee <[email protected]>
'''
import calendar
from datetime import datetime as _dt
from collections import defaultdict
from copy import copy
import re
import pprint
SLUG_REGEX = re.compile('[\w@\.]+$')
class BogusQuery(Exception): pass
class Q(dict):
def __init__(self, d=None):
dict.__init__(self,d)
def __and__(self, v):
for key in set(self)&set(v):
if key != '$or':
raise BogusQuery( "field %s can't match %s and %s"%(
key, str(self[key]), str(v[key])
))
q = Q(self) #we do not want to modify self or v
q.update(v)
if self.has_key('$or') and v.has_key('$or'):
#combine the things in $or using the distributive property
#(a|b)&(c|d) -> (a&c | a&d | b&c | b&d)
q['$or'] = [
self_term & v_term
for self_term in self['$or']
for v_term in v['$or']
]
return q
def __or__(self, v):
fixed_self = self._to_distributed_list()
fixed_v = v._to_distributed_list()
return Q({'$or':fixed_self+fixed_v})
def _to_distributed_list(self):
#returns a list of Q objects that is equivalent to self if the terms
#of the list are ORed together
if not self.has_key('$or'):
return [self]
if len(self) ==1:
return self['$or']
outer = copy(self)
del outer['$or']
#mongo does not let you nest or statements - use boolean algebra to
#return a "sum of products"
return [ (outer & inner) for inner in self['$or']]
def to_mongo_dict(self):
"take the query and turn it into a mongo-style dictionary"
d = defaultdict(dict)
for key,val in self.iteritems():
#crawl the tree
if isinstance(val, list):
mongo_value = [
item.to_mongo_dict() if isinstance(item,Q) else item
for item in val
]
else:
mongo_value = val
#expand the tuples
if isinstance(key, tuple):
if key[0] in self:
raise BogusQuery( "field %s can't be %s and match %s"%(
key[0], str(self[key[0]]), str(val)
))
#convert self[('size','$gte')] to d['size']['$gte']
d[key[0]][key[1]] = mongo_value
else:
d[key] = mongo_value
return d
class Property(object):
def __init__(self, name, default=None, null=True):
self.name = name or None
if default is not None:
self.default = lambda: default
self.null = null
def default(self):
return None
def validated(self, val):
"""Subclasses raise TypeError or ValueError if they are sent invalid
data. If this method is overridden, it may return a
functionally-equivalent copy of val."""
if val is None and not self.null:
raise ValueError("You can't assign None to an non-null property.")
return val
def to_d(self, val, **kwargs):
"Changes val into something that can go to json.dumps"
return val
def __repr__(self):
default = self.default()
if default is None:
return "%s(%r)"%(self.__class__.__name__, self.name)
else:
return "%s(%r,%r)"%(self.__class__.__name__,self.name,default)
def __eq__(self, v): return Q({self.name: v})
def __ge__(self, v): return Q({(self.name, '$gte'):v})
def __gt__(self, v): return Q({(self.name, '$gt' ):v})
def __le__(self, v): return Q({(self.name, '$lte'):v})
def __lt__(self, v): return Q({(self.name, '$lt' ):v})
def __ne__(self, v): return Q({(self.name, '$ne' ):v})
def is_in(self, terms): return Q({(self.name, '$in' ):terms})
def is_not_in(self, terms): return Q({(self.name, '$nin' ):terms})
def exists(self,exists=True): return Q({(self.name, '$exists' ):exists})
def range(self, start=None, end=None):
"create a query to find objects where start<=val<end"
if end is not None:
end = self.validated(end)
if start is None:
return Q({}) if end is None else (self<end)
else:
start = self.validated(start)
return self>=start if end is None else (self>=start) & (self<end)
class EnumProperty(Property):
def __init__(self, name, constants, **kwargs):
Property.__init__(self, name, **kwargs)
self.constants = constants
def validated(self, val):
val = Property.validated(self, val)
if val not in self.constants:
raise TypeError("value not in %r"%self.constants)
return val
class TypedProperty(Property):
def __init__(self, name, kind, **kwargs):
Property.__init__(self, name, **kwargs)
self.kind = kind
def validated(self, val):
return self.kind(Property.validated(self, val))
class BoolProperty(TypedProperty):
def __init__(self, name, **kwargs):
TypedProperty.__init__(self, name, bool, **kwargs)
class IntProperty(TypedProperty):
def __init__(self, name, **kwargs):
TypedProperty.__init__(self, name, int, **kwargs)
class FloatProperty(TypedProperty):
def __init__(self, name, **kwargs):
TypedProperty.__init__(self, name, float, **kwargs)
class DictProperty(TypedProperty):
def __init__(self, name, **kwargs):
TypedProperty.__init__(self, name, dict, **kwargs)
class DateTimeProperty(Property):
def __init__(self, name, format=None, **kwargs):
"Creates a DateTimeProperty. format is a strptime string for decoding"
Property.__init__(self, name, **kwargs)
self._format = format
def validated(self, val):
"Accepts datetime, string, or list of 6 ints. Returns a datetime."
if isinstance(val,_dt):
return val
if self._format and isinstance(val,basestring):
return _dt.strptime(val,self._format)
try:
return _dt.utcfromtimestamp(float(val))
except TypeError:
pass # it was not a number
if len(val) > 2:
return _dt(*val[:6])
raise TypeError("value %r isn't a datetime"%val)
def to_d(self, val, **kwargs):
format = kwargs.get('dateformat',None)
if format=="datetime":
return val
elif format=="epoch":
return calendar.timegm(val.timetuple())
elif format in (None,"list"):
return val.timetuple()[0:6]
else:
return val.strftime(format)
class TextProperty(Property):
"""TextProperty needs to work correctly with Unicode and String objects.
That is the reason this is not a subclass of TypedProperty."""
def validated(self, val):
val = Property.validated(self, val)
if not isinstance(val, basestring):
raise TypeError("value not text")
return val
def __floordiv__(self, pattern):
return Q({self.name: re.compile(pattern)})
class IdProperty(Property):
pass
class RegexTextProperty(TextProperty):
def __init__(self, name, pattern, **kwargs):
TextProperty.__init__(self, name, **kwargs)
self._pattern = re.compile(pattern)
def validated(self, value):
"""
Verifies that the string matches the pattern. Note that it uses
python's match() and not search(). If the first character of value
does not match, the pattern does not match.
"""
value = super(TextProperty, self).validated(value)
if value is None and not self.null:
raise ValueError("this property can't be empty")
if value and not self._pattern.match(value):
raise ValueError(
'"%s" does not match "%s"'%(value,self._pattern.pattern)
)
return value
class SlugProperty(RegexTextProperty):
def __init__(self, name, **kwargs):
RegexTextProperty.__init__(self, name, SLUG_REGEX, **kwargs)
class CreatedAtProperty(DateTimeProperty):
def default(self):
return _dt.utcnow()
class ListPropertyInstance(list):
def __init__(self, property):
self.property = property
def __setslice__(self,i,j,seq):
self.__setitem__(slice(i,j),seq)
def __setitem__(self, key, value):
if isinstance(key, slice):
for obj in value:
self.property.validated_item(obj)
else:
self.property.validated_item(value)
list.__setitem__(self, key, value)
class ListProperty(Property):
def __init__(self, name, kind=None, **kwargs):
Property.__init__(self, name, **kwargs)
self._kind = kind
def validated(self, val):
val = Property.validated(self, val)
ret = ListPropertyInstance(self)
ret.extend((self.validated_item(v) for v in val))
return ret
def validated_item(self, val):
if self._kind and not isinstance(val, self._kind):
raise TypeError("%s in list is not a %s"%(val,self._kind.__name__))
return val
def has_all(self, terms): return Q({(self.name, '$all' ):terms})
class SlugListProperty(ListProperty):
def __init__(self, name, **kwargs):
ListProperty.__init__(self, name, basestring, **kwargs)
def validated_item(self, val):
if not SLUG_REGEX.match(val):
raise ValueError(
'"%s" does not match "%s"'%(val,SLUG_REGEX)
)
return val
class ModelMetaclass(type):
def __init__(cls, name, bases, d):
type.__init__(cls,name, bases, d)
cls.update_long_names()
class ModelPart(object):
__metaclass__=ModelMetaclass
ignored = ()
long_names = {}
def __init__(self, from_dict=None, **kwargs):
if from_dict:
self.update(from_dict)
self.update(kwargs)
#set defaults
for name in self.long_names.values():
if name not in self.__dict__:
prop = getattr(type(self),name)
self.__dict__[name] = prop.default()
def __setattr__(self, n, v):
field = getattr(type(self),n,None)
if field and isinstance(field, Property):
if v is not None:
v = field.validated(v)
self.__dict__[n] = v
def __repr__(self):
d = pprint.pformat(self.to_d())
return "%s(%s)"%(self.__class__.__name__,d)
@classmethod
def update_long_names(cls):
cls.long_names = {}
for name in dir(cls):
prop = getattr(cls,name)
if isinstance(prop, Property):
cls.long_names[prop.name] = name
def to_d(self, **kwargs):
'Build a dictionary from all the properties attached to self.'
d = dict()
model = type(self)
for name,val in self.__dict__.iteritems():
if val is None or name in self.ignored: continue
prop = getattr(model,name,None)
if isinstance(prop, Property):
key = name if kwargs.get('long_names') else prop.name
d[key]=prop.to_d(val, **kwargs)
else:
try:
d[name]=val.to_d()
except AttributeError:
d[name]=val
return d
def update(self,d):
"""convert key names in d to long names, and then use d to update
self.__dict__"""
for k,v in d.iteritems():
setattr(self,self.long_names.get(k,k),v)
class ModelProperty(TypedProperty):
def __init__(self, name, part, default=None, **kwargs):
TypedProperty.__init__(self, name, part, **kwargs)
if default is not None:
self.default = lambda: self.kind(from_dict=default)
def to_d(self, val, **kwargs):
return val.to_d(**kwargs)
def validated(self, val):
val = Property.validated(self, val)
if not isinstance(val, self.kind):
return self.kind(val)
return val
class ModelListProperty(ListProperty):
def __init__(self, name, kind=ModelPart, **kwargs):
ListProperty.__init__(self, name, kind, **kwargs)
def to_d(self, val, **kwargs):
return [x.to_d(**kwargs) for x in val]
def validated_item(self, val):
if not isinstance(val, self._kind):
return self._kind(val)
return val
class Model(ModelPart):
_id = IdProperty("_id")
_rev = IdProperty('_rev')
def save(self):
"Save the model to the database, may overwrite existing objects"
return self.database.save(self)
def delete(self):
"remove the object from the database"
return self.database.delete_id(self.__class__.__name__,self._id)
def merge(self):
"Use this object to update an object that is already in the database."
return self.database.merge(self)
@classmethod
def bulk_save(cls, models):
return cls.database.bulk_save_models(models, cls)
@classmethod
def in_db(cls,_id):
return cls.database.in_coll(cls, _id)
@classmethod
def __contains__(cls,_id):
return cls.in_db(_id)
@classmethod
def get_id(cls, _id, **kwargs):
return cls.database.get_id(cls,_id, **kwargs)
@classmethod
def get_all(cls,**kwargs):
return cls.database.get_all(cls,**kwargs)
@classmethod
def coll(cls):
"Get the collection - only works for mongo-esque databases"
return cls.database[cls.__name__]
@classmethod
def find(cls, q=None, **kwargs):
"execute the query - only works with mongodb"
if q is False or q is True:
#make sure we didn't call one of python's comparison operators
raise BogusQuery("The first term in a comparison must be a Property.")
return cls.database.find(cls, q, **kwargs)
@classmethod
def paged_view(cls,view_name,**kwargs):
"look at a view through an iterator - only works with couchdb"
return cls.database.paged_view(view_name,cls=cls,**kwargs)
class ModelCache(dict):
def __init__(self, Class, limit=10000, **kwargs):
dict.__init__(self)
self.Class = Class
self.limit = limit
self.kwargs = kwargs
def __missing__(self, key):
if len(self)>=self.limit:
#random cache replacement policy
for x in xrange(len(self)/2):
self.popitem()
obj = self.Class.get_id(key, **self.kwargs)
self[key] = obj
return obj
def ensure_ids(self, ids):
"""make sure that all of the objects with an id in ids are in the cache.
This exists to reduce the number of database calls made."""
#FIXME: make a couch version
ids = [id for id in ids if id not in self]
if ids:
for obj in self.Class.find(Model._id.is_in(ids)):
self[obj._id] = obj