OLD | NEW |
| (Empty) |
1 # Copyright (c) 2006,2007,2008 Mitch Garnaat http://garnaat.org/ | |
2 # | |
3 # Permission is hereby granted, free of charge, to any person obtaining a | |
4 # copy of this software and associated documentation files (the | |
5 # "Software"), to deal in the Software without restriction, including | |
6 # without limitation the rights to use, copy, modify, merge, publish, dis- | |
7 # tribute, sublicense, and/or sell copies of the Software, and to permit | |
8 # persons to whom the Software is furnished to do so, subject to the fol- | |
9 # lowing conditions: | |
10 # | |
11 # The above copyright notice and this permission notice shall be included | |
12 # in all copies or substantial portions of the Software. | |
13 # | |
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- | |
16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | |
17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
20 # IN THE SOFTWARE. | |
21 | |
22 from datetime import datetime | |
23 from boto.s3.key import Key | |
24 from boto.s3.bucket import Bucket | |
25 from boto.sdb.persist import revive_object_from_id | |
26 from boto.exception import SDBPersistenceError | |
27 from boto.utils import Password | |
28 | |
29 ISO8601 = '%Y-%m-%dT%H:%M:%SZ' | |
30 | |
31 class ValueChecker: | |
32 | |
33 def check(self, value): | |
34 """ | |
35 Checks a value to see if it is of the right type. | |
36 | |
37 Should raise a TypeError exception if an in appropriate value is passed
in. | |
38 """ | |
39 raise TypeError | |
40 | |
41 def from_string(self, str_value, obj): | |
42 """ | |
43 Takes a string as input and returns the type-specific value represented
by that string. | |
44 | |
45 Should raise a ValueError if the value cannot be converted to the approp
riate type. | |
46 """ | |
47 raise ValueError | |
48 | |
49 def to_string(self, value): | |
50 """ | |
51 Convert a value to it's string representation. | |
52 | |
53 Should raise a ValueError if the value cannot be converted to a string r
epresentation. | |
54 """ | |
55 raise ValueError | |
56 | |
57 class StringChecker(ValueChecker): | |
58 | |
59 def __init__(self, **params): | |
60 if params.has_key('maxlength'): | |
61 self.maxlength = params['maxlength'] | |
62 else: | |
63 self.maxlength = 1024 | |
64 if params.has_key('default'): | |
65 self.check(params['default']) | |
66 self.default = params['default'] | |
67 else: | |
68 self.default = '' | |
69 | |
70 def check(self, value): | |
71 if isinstance(value, str) or isinstance(value, unicode): | |
72 if len(value) > self.maxlength: | |
73 raise ValueError, 'Length of value greater than maxlength' | |
74 else: | |
75 raise TypeError, 'Expecting String, got %s' % type(value) | |
76 | |
77 def from_string(self, str_value, obj): | |
78 return str_value | |
79 | |
80 def to_string(self, value): | |
81 self.check(value) | |
82 return value | |
83 | |
84 class PasswordChecker(StringChecker): | |
85 def check(self, value): | |
86 if isinstance(value, str) or isinstance(value, unicode) or isinstance(va
lue, Password): | |
87 if len(value) > self.maxlength: | |
88 raise ValueError, 'Length of value greater than maxlength' | |
89 else: | |
90 raise TypeError, 'Expecting String, got %s' % type(value) | |
91 | |
92 class IntegerChecker(ValueChecker): | |
93 | |
94 __sizes__ = { 'small' : (65535, 32767, -32768, 5), | |
95 'medium' : (4294967295, 2147483647, -2147483648, 10), | |
96 'large' : (18446744073709551615, 9223372036854775807, -9223372
036854775808, 20)} | |
97 | |
98 def __init__(self, **params): | |
99 self.size = params.get('size', 'medium') | |
100 if self.size not in self.__sizes__.keys(): | |
101 raise ValueError, 'size must be one of %s' % self.__sizes__.keys() | |
102 self.signed = params.get('signed', True) | |
103 self.default = params.get('default', 0) | |
104 self.format_string = '%%0%dd' % self.__sizes__[self.size][-1] | |
105 | |
106 def check(self, value): | |
107 if not isinstance(value, int) and not isinstance(value, long): | |
108 raise TypeError, 'Expecting int or long, got %s' % type(value) | |
109 if self.signed: | |
110 min = self.__sizes__[self.size][2] | |
111 max = self.__sizes__[self.size][1] | |
112 else: | |
113 min = 0 | |
114 max = self.__sizes__[self.size][0] | |
115 if value > max: | |
116 raise ValueError, 'Maximum value is %d' % max | |
117 if value < min: | |
118 raise ValueError, 'Minimum value is %d' % min | |
119 | |
120 def from_string(self, str_value, obj): | |
121 val = int(str_value) | |
122 if self.signed: | |
123 val = val + self.__sizes__[self.size][2] | |
124 return val | |
125 | |
126 def to_string(self, value): | |
127 self.check(value) | |
128 if self.signed: | |
129 value += -self.__sizes__[self.size][2] | |
130 return self.format_string % value | |
131 | |
132 class BooleanChecker(ValueChecker): | |
133 | |
134 def __init__(self, **params): | |
135 if params.has_key('default'): | |
136 self.default = params['default'] | |
137 else: | |
138 self.default = False | |
139 | |
140 def check(self, value): | |
141 if not isinstance(value, bool): | |
142 raise TypeError, 'Expecting bool, got %s' % type(value) | |
143 | |
144 def from_string(self, str_value, obj): | |
145 if str_value.lower() == 'true': | |
146 return True | |
147 else: | |
148 return False | |
149 | |
150 def to_string(self, value): | |
151 self.check(value) | |
152 if value == True: | |
153 return 'true' | |
154 else: | |
155 return 'false' | |
156 | |
157 class DateTimeChecker(ValueChecker): | |
158 | |
159 def __init__(self, **params): | |
160 if params.has_key('maxlength'): | |
161 self.maxlength = params['maxlength'] | |
162 else: | |
163 self.maxlength = 1024 | |
164 if params.has_key('default'): | |
165 self.default = params['default'] | |
166 else: | |
167 self.default = datetime.now() | |
168 | |
169 def check(self, value): | |
170 if not isinstance(value, datetime): | |
171 raise TypeError, 'Expecting datetime, got %s' % type(value) | |
172 | |
173 def from_string(self, str_value, obj): | |
174 try: | |
175 return datetime.strptime(str_value, ISO8601) | |
176 except: | |
177 raise ValueError, 'Unable to convert %s to DateTime' % str_value | |
178 | |
179 def to_string(self, value): | |
180 self.check(value) | |
181 return value.strftime(ISO8601) | |
182 | |
183 class ObjectChecker(ValueChecker): | |
184 | |
185 def __init__(self, **params): | |
186 self.default = None | |
187 self.ref_class = params.get('ref_class', None) | |
188 if self.ref_class == None: | |
189 raise SDBPersistenceError('ref_class parameter is required') | |
190 | |
191 def check(self, value): | |
192 if value == None: | |
193 return | |
194 if isinstance(value, str) or isinstance(value, unicode): | |
195 # ugly little hack - sometimes I want to just stick a UUID string | |
196 # in here rather than instantiate an object. | |
197 # This does a bit of hand waving to "type check" the string | |
198 t = value.split('-') | |
199 if len(t) != 5: | |
200 raise ValueError | |
201 else: | |
202 try: | |
203 obj_lineage = value.get_lineage() | |
204 cls_lineage = self.ref_class.get_lineage() | |
205 if obj_lineage.startswith(cls_lineage): | |
206 return | |
207 raise TypeError, '%s not instance of %s' % (obj_lineage, cls_lin
eage) | |
208 except: | |
209 raise ValueError, '%s is not an SDBObject' % value | |
210 | |
211 def from_string(self, str_value, obj): | |
212 if not str_value: | |
213 return None | |
214 try: | |
215 return revive_object_from_id(str_value, obj._manager) | |
216 except: | |
217 raise ValueError, 'Unable to convert %s to Object' % str_value | |
218 | |
219 def to_string(self, value): | |
220 self.check(value) | |
221 if isinstance(value, str) or isinstance(value, unicode): | |
222 return value | |
223 if value == None: | |
224 return '' | |
225 else: | |
226 return value.id | |
227 | |
228 class S3KeyChecker(ValueChecker): | |
229 | |
230 def __init__(self, **params): | |
231 self.default = None | |
232 | |
233 def check(self, value): | |
234 if value == None: | |
235 return | |
236 if isinstance(value, str) or isinstance(value, unicode): | |
237 try: | |
238 bucket_name, key_name = value.split('/', 1) | |
239 except: | |
240 raise ValueError | |
241 elif not isinstance(value, Key): | |
242 raise TypeError, 'Expecting Key, got %s' % type(value) | |
243 | |
244 def from_string(self, str_value, obj): | |
245 if not str_value: | |
246 return None | |
247 if str_value == 'None': | |
248 return None | |
249 try: | |
250 bucket_name, key_name = str_value.split('/', 1) | |
251 if obj: | |
252 s3 = obj._manager.get_s3_connection() | |
253 bucket = s3.get_bucket(bucket_name) | |
254 key = bucket.get_key(key_name) | |
255 if not key: | |
256 key = bucket.new_key(key_name) | |
257 return key | |
258 except: | |
259 raise ValueError, 'Unable to convert %s to S3Key' % str_value | |
260 | |
261 def to_string(self, value): | |
262 self.check(value) | |
263 if isinstance(value, str) or isinstance(value, unicode): | |
264 return value | |
265 if value == None: | |
266 return '' | |
267 else: | |
268 return '%s/%s' % (value.bucket.name, value.name) | |
269 | |
270 class S3BucketChecker(ValueChecker): | |
271 | |
272 def __init__(self, **params): | |
273 self.default = None | |
274 | |
275 def check(self, value): | |
276 if value == None: | |
277 return | |
278 if isinstance(value, str) or isinstance(value, unicode): | |
279 return | |
280 elif not isinstance(value, Bucket): | |
281 raise TypeError, 'Expecting Bucket, got %s' % type(value) | |
282 | |
283 def from_string(self, str_value, obj): | |
284 if not str_value: | |
285 return None | |
286 if str_value == 'None': | |
287 return None | |
288 try: | |
289 if obj: | |
290 s3 = obj._manager.get_s3_connection() | |
291 bucket = s3.get_bucket(str_value) | |
292 return bucket | |
293 except: | |
294 raise ValueError, 'Unable to convert %s to S3Bucket' % str_value | |
295 | |
296 def to_string(self, value): | |
297 self.check(value) | |
298 if value == None: | |
299 return '' | |
300 else: | |
301 return '%s' % value.name | |
302 | |
OLD | NEW |