-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmodels.py
More file actions
309 lines (240 loc) · 9.71 KB
/
models.py
File metadata and controls
309 lines (240 loc) · 9.71 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
import simpledb
import datetime
__all__ = ['FieldError', 'Field', 'NumberField', 'BooleanField', 'DateTimeField', 'Manager', 'Model']
class FieldError(Exception): pass
class Field(object):
name = False
def __init__(self, default=None, required=False):
self.default = default
self.required = required
def install(self, name, cls):
default = self.default
# If the default argument is a callable, call it.
if callable(default):
default = default()
setattr(cls, name, default)
def decode(self, value):
"""Decodes an object from the datastore into a python object."""
return value
def encode(self, value):
"""Encodes a python object into a value suitable for the backend datastore."""
return value
class ItemName(Field):
"""The item's name. Must be a UTF8 string."""
name = True
class NumberField(Field):
def __init__(self, padding=0, offset=0, precision=0, **kwargs):
self.padding = padding
self.offset = offset
self.precision = precision
super(NumberField, self).__init__(**kwargs)
def encode(self, value):
"""
Converts a python number into a padded string that is suitable for storage
in Amazon SimpleDB and can be sorted lexicographically.
Numbers are shifted by an offset so that negative numbers sort correctly. Once
shifted, they are converted to zero padded strings.
"""
padding = self.padding
if self.precision > 0 and self.padding > 0:
# Padding shouldn't include decimal digits or the decimal point.
padding += self.precision + 1
return ('%%0%d.%df' % (padding, self.precision)) % (value + self.offset)
def decode(self, value):
"""
Decoding converts a string into a numerical type then shifts it by the
offset.
"""
return float(value) - self.offset
class BooleanField(Field):
def encode(self, value):
"""
Converts a python boolean into a string '1'/'0' for storage in SimpleDB.
"""
return ('0','1')[value]
def decode(self, value):
"""
Converts an encoded string '1'/'0' into a python boolean object.
"""
return {'0': False, '1': True}[value]
class DateTimeField(Field):
def __init__(self, format='%Y-%m-%dT%H:%M:%S', **kwargs):
self.format = format
super(DateTimeField, self).__init__(**kwargs)
def encode(self, value):
"""
Converts a python datetime object to a string format controlled by the
`format` attribute. The default format is ISO 8601, which supports
lexicographical order comparisons.
"""
return value.strftime(self.format)
def decode(self, value):
"""
Decodes a string representation of a date and time into a python
datetime object.
"""
return datetime.datetime.strptime(value, self.format)
class FieldEncoder(simpledb.AttributeEncoder):
def __init__(self, fields):
self.fields = fields
def encode(self, domain, attribute, value):
try:
field = self.fields[attribute]
except KeyError:
return value
else:
return field.encode(value)
def decode(self, domain, attribute, value):
try:
field = self.fields[attribute]
except KeyError:
return value
else:
return field.decode(value)
class Query(simpledb.Query):
def values(self, *fields):
# If you ask for specific values return a simpledb.Item instead of the Model
q = self._clone(klass=simpledb.Query)
q.fields = fields
return q
def _get_results(self):
if self._result_cache is None:
self._result_cache = [self.domain.model.from_item(item) for item in
self.domain.select(self.to_expression())]
return self._result_cache
class Manager(object):
# Tracks each time a Manager instance is created. Used to retain order.
creation_counter = 0
def __init__(self):
self._set_creation_counter()
self.model = None
def install(self, name, model):
self.model = model
setattr(model, name, ManagerDescriptor(self))
if not getattr(model, '_default_manager', None) or self.creation_counter < model._default_manager.creation_counter:
model._default_manager = self
def _set_creation_counter(self):
"""
Sets the creation counter value for this instance and increments the
class-level copy.
"""
self.creation_counter = Manager.creation_counter
Manager.creation_counter += 1
def filter(self, *args, **kwargs):
return self._get_query().filter(*args, **kwargs)
def all(self):
return self._get_query()
def count(self):
return self._get_query().count()
def values(self, *args):
return self._get_query().values(*args)
def item_names(self):
return self._get_query().item_names()
def get(self, name, consistent_read=False):
return self.model.from_item(self.model.Meta.domain.get(name,
consistent_read))
def _get_query(self):
return Query(self.model.Meta.domain)
class ManagerDescriptor(object):
# This class ensures managers aren't accessible via model instances.
# For example, Poll.objects works, but poll_obj.objects raises AttributeError.
def __init__(self, manager):
self.manager = manager
def __get__(self, instance, type=None):
if instance != None:
raise AttributeError("Manager isn't accessible via %s instances" % type.__name__)
return self.manager
class ModelMetaclass(type):
"""
Metaclass for `simpledb.models.Model` instances. Installs
`simpledb.models.Field` instances declared as attributes of the
new class.
"""
def __new__(cls, name, bases, attrs):
parents = [b for b in bases if isinstance(b, ModelMetaclass)]
if not parents:
# If this isn't a subclass of Model, don't do anything special.
return super(ModelMetaclass, cls).__new__(cls, name, bases, attrs)
fields = {}
for base in bases:
if isinstance(base, ModelMetaclass) and hasattr(base, 'fields'):
fields.update(base.fields)
new_fields = {}
managers = {}
# Move all the class's attributes that are Fields to the fields set.
for attrname, field in attrs.items():
if isinstance(field, Field):
new_fields[attrname] = field
if field.name:
# Add _name_field attr so we know what the key is
if '_name_field' in attrs:
raise FieldError("Multiple key fields defined for model '%s'" % name)
attrs['_name_field'] = attrname
elif attrname in fields:
# Throw out any parent fields that the subclass defined as
# something other than a field
del fields[attrname]
# Track managers
if isinstance(field, Manager):
managers[attrname] = field
fields.update(new_fields)
attrs['fields'] = fields
new_cls = super(ModelMetaclass, cls).__new__(cls, name, bases, attrs)
for field, value in new_fields.items():
new_cls.add_to_class(field, value)
if not managers:
managers['objects'] = Manager()
for field, value in managers.items():
new_cls.add_to_class(field, value)
if hasattr(new_cls, 'Meta'):
# If the new class's Meta.domain attribute is a string turn it into
# a simpledb.Domain instance.
if isinstance(new_cls.Meta.domain, basestring):
new_cls.Meta.domain = simpledb.Domain(new_cls.Meta.domain, new_cls.Meta.connection)
# Install a reference to the new model class on the Meta.domain so
# Query can use it.
# TODO: Should we be using weakref here? Not sure it matters since it's
# a class (global) that's long lived anyways.
new_cls.Meta.domain.model = new_cls
# Set the connection object's AttributeEncoder
new_cls.Meta.connection.encoder = FieldEncoder(fields)
return new_cls
def add_to_class(cls, name, value):
if hasattr(value, 'install'):
value.install(name, cls)
else:
setattr(cls, name, value)
class Model(object):
__metaclass__ = ModelMetaclass
def __init__(self, **kwargs):
for name, value in kwargs.items():
setattr(self, name, value)
self._item = None
def _get_name(self):
return getattr(self, self._name_field)
def save(self):
if self._item is None:
self._item = simpledb.Item(self.Meta.connection, self.Meta.domain, self._get_name())
for name, field in self.fields.items():
if field.name:
continue
value = getattr(self, name)
if value is None:
if field.required:
raise FieldError("Missing required field '%s'" % name)
else:
del self._item[name]
continue
self._item[name] = getattr(self, name)
self._item.save()
def delete(self):
del self.Meta.domain[self._get_name()]
@classmethod
def from_item(cls, item):
obj = cls()
obj._item = item
for name, field in obj.fields.items():
if name in obj._item:
setattr(obj, name, obj._item[name])
setattr(obj, obj._name_field, obj._item.name)
return obj