18
18
of keys is important. A SON object can be used just like a normal Python
19
19
dictionary."""
20
20
21
- import datetime
22
- import re
23
- import binascii
24
- import base64
25
- import types
26
-
27
21
28
22
class SON (dict ):
29
23
"""SON data.
@@ -190,7 +184,6 @@ def __cmp__(self, other):
190
184
def __len__ (self ):
191
185
return len (self .keys ())
192
186
193
- # Thanks to Jeff Jenkins for the idea and original implementation
194
187
def to_dict (self ):
195
188
"""Convert a SON document to a normal Python dictionary instance.
196
189
@@ -199,129 +192,13 @@ def to_dict(self):
199
192
"""
200
193
201
194
def transform_value (value ):
202
- if isinstance (value , types . ListType ):
195
+ if isinstance (value , list ):
203
196
return [transform_value (v ) for v in value ]
204
197
if isinstance (value , SON ):
205
198
value = dict (value )
206
- if isinstance (value , types . DictType ):
199
+ if isinstance (value , dict ):
207
200
for k , v in value .iteritems ():
208
201
value [k ] = transform_value (v )
209
202
return value
210
203
211
204
return transform_value (dict (self ))
212
-
213
- def from_xml (cls , xml ):
214
- """Create an instance of SON from an xml document.
215
-
216
- This is really only used for testing, and is probably unnecessary.
217
- """
218
- try :
219
- import xml .etree .ElementTree as ET
220
- except ImportError :
221
- import elementtree .ElementTree as ET
222
-
223
- from code import Code
224
- from binary import Binary
225
- from objectid import ObjectId
226
- from dbref import DBRef
227
- from errors import UnsupportedTag
228
-
229
- def pad (list , index ):
230
- while index >= len (list ):
231
- list .append (None )
232
-
233
- def make_array (array ):
234
- doc = make_doc (array )
235
- array = []
236
- for (key , value ) in doc .items ():
237
- index = int (key )
238
- pad (array , index )
239
- array [index ] = value
240
- return array
241
-
242
- def make_string (string ):
243
- return string .text is not None and unicode (string .text ) or u""
244
-
245
- def make_code (code ):
246
- return code .text is not None and Code (code .text ) or Code ("" )
247
-
248
- def make_binary (binary ):
249
- if binary .text is not None :
250
- return Binary (base64 .decodestring (binary .text ))
251
- return Binary ("" )
252
-
253
- def make_boolean (bool ):
254
- return bool .text == "true"
255
-
256
- def make_date (date ):
257
- return datetime .datetime .utcfromtimestamp (float (date .text ) /
258
- 1000.0 )
259
-
260
- def make_ref (dbref ):
261
- return DBRef (make_elem (dbref [0 ]), make_elem (dbref [1 ]))
262
-
263
- def make_oid (oid ):
264
- return ObjectId (binascii .unhexlify (oid .text ))
265
-
266
- def make_int (data ):
267
- return int (data .text )
268
-
269
- def make_null (null ):
270
- return None
271
-
272
- def make_number (number ):
273
- return float (number .text )
274
-
275
- def make_regex (regex ):
276
- return re .compile (make_elem (regex [0 ]), make_elem (regex [1 ]))
277
-
278
- def make_options (data ):
279
- options = 0
280
- if not data .text :
281
- return options
282
- if "i" in data .text :
283
- options |= re .IGNORECASE
284
- if "l" in data .text :
285
- options |= re .LOCALE
286
- if "m" in data .text :
287
- options |= re .MULTILINE
288
- if "s" in data .text :
289
- options |= re .DOTALL
290
- if "u" in data .text :
291
- options |= re .UNICODE
292
- if "x" in data .text :
293
- options |= re .VERBOSE
294
- return options
295
-
296
- def make_elem (elem ):
297
- try :
298
- return {"array" : make_array ,
299
- "doc" : make_doc ,
300
- "string" : make_string ,
301
- "binary" : make_binary ,
302
- "boolean" : make_boolean ,
303
- "code" : make_code ,
304
- "date" : make_date ,
305
- "ref" : make_ref ,
306
- "ns" : make_string ,
307
- "oid" : make_oid ,
308
- "int" : make_int ,
309
- "null" : make_null ,
310
- "number" : make_number ,
311
- "pattern" : make_string ,
312
- "options" : make_options ,
313
- }[elem .tag ](elem )
314
- except KeyError :
315
- raise UnsupportedTag ("cannot parse tag: %s" % elem .tag )
316
-
317
- def make_doc (doc ):
318
- son = SON ()
319
- for elem in doc :
320
- son [elem .attrib ["name" ]] = make_elem (elem )
321
- return son
322
-
323
- tree = ET .XML (xml )
324
- doc = tree [1 ]
325
-
326
- return make_doc (doc )
327
- from_xml = classmethod (from_xml )
0 commit comments