Esempio n. 1
0
 def _convert_to_python(doc, struct, path="", root_path=""):
     for key in struct:
         if type(key) is type:
             new_key = "$%s" % key.__name__
         else:
             new_key = key
         new_path = ".".join([path, new_key]).strip(".")
         if isinstance(struct[key], dict):
             if doc:  # we don't need to process an empty doc
                 if key in doc:  # we don't care about missing fields
                     _convert_to_python(doc[key], struct[key], new_path, root_path)
         elif type(struct[key]) is list:
             if struct[key]:
                 if struct[key][0] is datetime.datetime:
                     l_objs = []
                     for obj in doc[key]:
                         obj = fromtimestamp(obj)
                         l_objs.append(obj)
                     doc[key] = l_objs
                 elif isinstance(struct[key][0], R):
                     l_objs = []
                     for obj in doc[key]:
                         db = obj.get("_database") or obj.get("$db")
                         col = obj.get("_collection") or obj.get("$ref")
                         if "_id" in obj:
                             id_ref = "_id"
                             if "$oid" in obj["_id"]:
                                 obj["_id"] = ObjectId(obj["_id"]["$oid"])
                         elif "$id" in obj:
                             id_ref = "$id"
                         obj_class = struct[key][0]._doc
                         _id = obj[id_ref]
                         obj = getattr(self.connection[db][col], obj_class.__name__).one({"_id": _id})
                         # obj = struct[key][0]._doc(obj, collection=self.connection[db][col]).get_dbref()
                         l_objs.append(obj)
                     doc[key] = l_objs
                 elif isinstance(struct[key][0], dict):
                     if doc[key]:
                         for obj in doc[key]:
                             _convert_to_python(obj, struct[key][0], new_path, root_path)
         elif struct[key] is datetime.datetime and doc[key] is not None:
             doc[key] = fromtimestamp(doc[key])
         elif (isinstance(struct[key], R) or isinstance(struct[key], DocumentProperties)) and doc[
             key
         ] is not None:
             db = doc[key].get("_database") or doc[key].get("$db")
             col = doc[key].get("_collection") or doc[key].get("$ref")
             if "_id" in doc[key]:
                 id_ref = "_id"
             elif "$id" in doc[key]:
                 id_ref = "$id"
             if "$oid" in doc[key][id_ref]:
                 doc[key][id_ref] = ObjectId(doc[key][id_ref]["$oid"])
             if isinstance(struct[key], R):
                 obj_class = struct[key]._doc
             else:
                 obj_class = struct[key]
             # _id = obj_class(doc[key], collection=self.connection[db][col])[id_ref]
             _id = doc[key][id_ref]
             doc[key] = getattr(self.connection[db][col], obj_class.__name__).one({"_id": _id})
Esempio n. 2
0
 def _convert_to_python(doc, struct, path="", root_path=""):
     for key in struct:
         if type(key) is type:
             new_key = '$%s' % key.__name__
         else:
             new_key = key
         new_path = ".".join([path, new_key]).strip('.')
         if isinstance(struct[key], dict):
             if doc:  # we don't need to process an empty doc
                 if key in doc:  # we don't care about missing fields
                     _convert_to_python(doc[key], struct[key], new_path, root_path)
         elif type(struct[key]) is list:
             if struct[key]:
                 if struct[key][0] is datetime.datetime:
                     l_objs = []
                     for obj in doc[key]:
                         obj = fromtimestamp(obj)
                         l_objs.append(obj)
                     doc[key] = l_objs
                 elif isinstance(struct[key][0], R):
                     l_objs = []
                     for obj in doc[key]:
                         db = obj.get('_database') or obj.get('$db')
                         col = obj.get('_collection') or obj.get('$ref')
                         if '_id' in obj:
                             id_ref = '_id'
                             if '$oid' in obj['_id']:
                                 obj['_id'] = ObjectId(obj['_id']['$oid'])
                         elif '$id' in obj:
                             id_ref = '$id'
                         obj_class = struct[key][0]._doc
                         _id = obj[id_ref]
                         obj = getattr(self.connection[db][col], obj_class.__name__).one({'_id': _id})
                         #obj = struct[key][0]._doc(obj, collection=self.connection[db][col]).get_dbref()
                         l_objs.append(obj)
                     doc[key] = l_objs
                 elif isinstance(struct[key][0], dict):
                     if doc[key]:
                         for obj in doc[key]:
                             _convert_to_python(obj, struct[key][0], new_path, root_path)
         elif struct[key] is datetime.datetime and doc[key] is not None:
             doc[key] = fromtimestamp(doc[key])
         elif (isinstance(struct[key], R) or isinstance(struct[key],
                                                        DocumentProperties)) and doc[key] is not None:
             db = doc[key].get('_database') or doc[key].get('$db')
             col = doc[key].get('_collection') or doc[key].get('$ref')
             if '_id' in doc[key]:
                 id_ref = '_id'
             elif '$id' in doc[key]:
                 id_ref = '$id'
             if '$oid' in doc[key][id_ref]:
                 doc[key][id_ref] = ObjectId(doc[key][id_ref]['$oid'])
             if isinstance(struct[key], R):
                 obj_class = struct[key]._doc
             else:
                 obj_class = struct[key]
             #_id = obj_class(doc[key], collection=self.connection[db][col])[id_ref]
             _id = doc[key][id_ref]
             doc[key] = getattr(self.connection[db][col], obj_class.__name__).one({'_id': _id})
Esempio n. 3
0
 def _convert_to_python(doc, struct, path="", root_path=""):
     for key in struct:
         if type(key) is type:
             new_key = '$%s' % key.__name__
         else:
             new_key = key
         new_path = ".".join([path, new_key]).strip('.')
         if isinstance(struct[key], dict):
             if doc:  # we don't need to process an empty doc
                 if key in doc:  # we don't care about missing fields
                     _convert_to_python(doc[key], struct[key], new_path, root_path)
         elif type(struct[key]) is list:
             if struct[key]:
                 if struct[key][0] is datetime.datetime:
                     l_objs = []
                     for obj in doc[key]:
                         obj = fromtimestamp(obj)
                         l_objs.append(obj)
                     doc[key] = l_objs
                 elif isinstance(struct[key][0], R):
                     l_objs = []
                     for obj in doc[key]:
                         db = obj.get('_database') or obj.get('$db')
                         col = obj.get('_collection') or obj.get('$ref')
                         if '_id' in obj:
                             id_ref = '_id'
                             if '$oid' in obj['_id']:
                                 obj['_id'] = ObjectId(obj['_id']['$oid'])
                         elif '$id' in obj:
                             id_ref = '$id'
                         obj_class = struct[key][0]._doc
                         _id = obj[id_ref]
                         obj = getattr(self.connection[db][col], obj_class.__name__).one({'_id': _id})
                         #obj = struct[key][0]._doc(obj, collection=self.connection[db][col]).get_dbref()
                         l_objs.append(obj)
                     doc[key] = l_objs
                 elif isinstance(struct[key][0], dict):
                     if doc[key]:
                         for obj in doc[key]:
                             _convert_to_python(obj, struct[key][0], new_path, root_path)
         elif struct[key] is datetime.datetime and doc[key] is not None:
             #2.6 and newer
             if isinstance(doc[key], collections.Mapping) and '$date' in doc[key]:
                 if isinstance(doc[key]['$date'], basestring):
                     #import pdb;pdb.set_trace();
                     aware = datetime.datetime.strptime(
                         doc[key]["$date"][:23], "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=utc)
                     offset = doc[key]["$date"][23:]
                     if not offset:
                         # No offset, assume UTC.
                         doc[key] = aware
                     elif len(offset) == 5:
                         # Offset from mongoexport is in format (+|-)HHMM
                         secs = (int(offset[1:3]) * 3600 + int(offset[3:]) * 60)
                         if offset[0] == "-":
                             secs *= -1
                         doc[key] = aware - datetime.timedelta(seconds=secs)
                     else:
                         # Some other tool created this, or mongoexport changed again?
                         raise ValueError("invalid format for offset")
                 elif(isinstance(doc[key]['$date'], collections.Mapping) and '$numberLong' in doc[key]['$date']):
                     doc[key] = fromtimestamp(float(doc[key]["$date"]["$numberLong"]))
                 else:
                     doc[key] = fromtimestamp(float(doc[key]["$date"]))
             # mongoexport before 2.6
             else:
                 doc[key] = fromtimestamp(doc[key])
         elif (isinstance(struct[key], R) or isinstance(struct[key],
                                                        DocumentProperties)) and doc[key] is not None:
             db = doc[key].get('_database') or doc[key].get('$db')
             col = doc[key].get('_collection') or doc[key].get('$ref')
             if '_id' in doc[key]:
                 id_ref = '_id'
             elif '$id' in doc[key]:
                 id_ref = '$id'
             if '$oid' in doc[key][id_ref]:
                 doc[key][id_ref] = ObjectId(doc[key][id_ref]['$oid'])
             if isinstance(struct[key], R):
                 obj_class = struct[key]._doc
             else:
                 obj_class = struct[key]
             #_id = obj_class(doc[key], collection=self.connection[db][col])[id_ref]
             _id = doc[key][id_ref]
             doc[key] = getattr(self.connection[db][col], obj_class.__name__).one({'_id': _id})