Ejemplo n.º 1
0
def test_update_max_4(monty_update, mongo_update):
    docs = [
        {"a": {"b": 5}}
    ]
    spec = {"$max": {"a": MaxKey()}}

    monty_c = monty_update(docs, spec)
    mongo_c = mongo_update(docs, spec)

    assert next(mongo_c) == next(monty_c)
    monty_c.rewind()
    assert next(monty_c) == {"a": MaxKey()}
Ejemplo n.º 2
0
def object_hook(dct):
    if "$oid" in dct:
        return ObjectId(str(dct["$oid"]))
    if "$ref" in dct:
        return DBRef(dct["$ref"], dct["$id"], dct.get("$db", None))
    if "$date" in dct:
        secs = float(dct["$date"]) / 1000.0
        return EPOCH_AWARE + datetime.timedelta(seconds=secs)
    if "$regex" in dct:
        flags = 0
        if "i" in dct["$options"]:
            flags |= re.IGNORECASE
        if "m" in dct["$options"]:
            flags |= re.MULTILINE
        return re.compile(dct["$regex"], flags)
    if "$minKey" in dct:
        return MinKey()
    if "$maxKey" in dct:
        return MaxKey()
    if "$binary" in dct:
        if isinstance(dct["$type"], int):
            dct["$type"] = "%02x" % dct["$type"]
        subtype = int(dct["$type"], 16)
        if subtype >= 0xffffff80:  # Handle mongoexport values
            subtype = int(dct["$type"][6:], 16)
        return Binary(base64.b64decode(dct["$binary"].encode()), subtype)
    if "$code" in dct:
        return Code(dct["$code"], dct.get("$scope"))
    if bson.has_uuid() and "$uuid" in dct:
        return bson.uuid.UUID(dct["$uuid"])
    return dct
Ejemplo n.º 3
0
    def test_maxkey_pickling(self):
        maxk = MaxKey()
        pickled_with_3 = (b'\x80\x04\x95\x1e\x00\x00\x00\x00\x00\x00\x00\x8c' 
                          b'\x0cbson.max_key\x94\x8c\x06MaxKey\x94\x93\x94)' 
                          b'\x81\x94.')

        self.round_trip_pickle(maxk, pickled_with_3)
Ejemplo n.º 4
0
def object_hook(dct, json_options=DEFAULT_JSON_OPTIONS):
    if "$oid" in dct:
        return ObjectId(str(dct["$oid"]))
    if "$ref" in dct:
        return DBRef(dct["$ref"], dct["$id"], dct.get("$db", None))
    if "$date" in dct:
        return _get_date(dct, json_options)
    if "$regex" in dct:
        flags = 0
        # PyMongo always adds $options but some other tools may not.
        for opt in dct.get("$options", ""):
            flags |= _RE_OPT_TABLE.get(opt, 0)
        return Regex(dct["$regex"], flags)
    if "$minKey" in dct:
        return MinKey()
    if "$maxKey" in dct:
        return MaxKey()
    if "$binary" in dct:
        return _get_binary(dct, json_options)
    if "$code" in dct:
        return Code(dct["$code"], dct.get("$scope"))
    if "$uuid" in dct:
        return uuid.UUID(dct["$uuid"])
    if "$undefined" in dct:
        return None
    if "$numberLong" in dct:
        return Int64(dct["$numberLong"])
    if "$timestamp" in dct:
        tsp = dct["$timestamp"]
        return Timestamp(tsp["t"], tsp["i"])
    if "$numberDecimal" in dct:
        return Decimal128(dct["$numberDecimal"])
    return dct
Ejemplo n.º 5
0
def test_qop_gt_13(monty_find, mongo_find):
    oid_0 = ObjectId(b"000000000000")
    max_k = MaxKey()
    min_k = MinKey()
    docs = [
        {
            "a": oid_0
        },
        {
            "a": max_k
        },
        {
            "a": min_k
        },
        {
            "a": 55
        },
    ]
    spec = {"a": {"$gt": max_k}}

    monty_c = monty_find(docs, spec)
    mongo_c = mongo_find(docs, spec)

    assert FieldWalker(docs[1]).go("a").get().value == [max_k]
    assert mongo_c.count() == 3
    assert monty_c.count() == mongo_c.count()
    for i in range(3):
        assert next(mongo_c) == next(monty_c)
Ejemplo n.º 6
0
def object_hook(dct):
    if "$oid" in dct:
        return ObjectId(str(dct["$oid"]))
    if "$ref" in dct:
        return DBRef(dct["$ref"], dct["$id"], dct.get("$db", None))
    if "$date" in dct:
        secs = float(dct["$date"]) / 1000.0
        return EPOCH_AWARE + datetime.timedelta(seconds=secs)
    if "$regex" in dct:
        flags = 0
        if "i" in dct["$options"]:
            flags |= re.IGNORECASE
        if "m" in dct["$options"]:
            flags |= re.MULTILINE
        return re.compile(dct["$regex"], flags)
    if "$minKey" in dct:
        return MinKey()
    if "$maxKey" in dct:
        return MaxKey()
    if "$binary" in dct:
        return Binary(base64.b64decode(dct["$binary"].encode()), dct["$type"])
    if "$code" in dct:
        return Code(dct["$code"], dct.get("$scope"))
    if bson.has_uuid() and "$uuid" in dct:
        return bson.uuid.UUID(dct["$uuid"])
    return dct
def test_qop_lt_13(monty_find, mongo_find):
    oid_0 = ObjectId(b"000000000000")
    max_k = MaxKey()
    min_k = MinKey()
    docs = [
        {
            "a": oid_0
        },
        {
            "a": max_k
        },
        {
            "a": min_k
        },
        {
            "a": 55
        },
    ]
    spec = {"a": {"$lt": min_k}}

    monty_c = monty_find(docs, spec)
    mongo_c = mongo_find(docs, spec)

    assert mongo_c.count() == 3
    assert monty_c.count() == mongo_c.count()
    for i in range(3):
        assert next(mongo_c) == next(monty_c)
Ejemplo n.º 8
0
def _parse_canonical_maxkey(doc):
    """Decode a JSON MaxKey to bson.max_key.MaxKey."""
    if type(doc['$maxKey']) is not int or doc['$maxKey'] != 1:
        raise TypeError('$maxKey value must be 1: %s', (doc, ))
    if len(doc) != 1:
        raise TypeError('Bad $minKey, extra field(s): %s' % (doc, ))
    return MaxKey()
Ejemplo n.º 9
0
def object_hook(dct: dict):
    """Helper function for converting json to mongo bson
    Args:
        dct: json data

    Returns:
        bson json format
    """
    if "$oid" in dct:
        return ObjectId(str(dct["$oid"]))
    if "$ref" in dct:
        return DBRef(dct["$ref"], dct["$id"], dct.get("$db", None))
    if "$date" in dct:
        return datetime.datetime.fromtimestamp(float(dct["$date"]) / 1000.0,
                                               utc)
    if "$regex" in dct:
        flags = 0
        if "i" in dct["$options"]:
            flags |= re.IGNORECASE
        if "m" in dct["$options"]:
            flags |= re.MULTILINE
        return re.compile(dct["$regex"], flags)
    if "$minKey" in dct:
        return MinKey()
    if "$maxKey" in dct:
        return MaxKey()
    if _use_uuid and "$uuid" in dct:
        return uuid.UUID(dct["$uuid"])
    return dct
Ejemplo n.º 10
0
def object_hook(dct, compile_re=True):
    if "$oid" in dct:
        return ObjectId(str(dct["$oid"]))
    if "$ref" in dct:
        return DBRef(dct["$ref"], dct["$id"], dct.get("$db", None))
    if "$date" in dct:
        secs = float(dct["$date"]) / 1000.0
        return EPOCH_AWARE + datetime.timedelta(seconds=secs)
    if "$regex" in dct:
        flags = 0
        # PyMongo always adds $options but some other tools may not.
        for opt in dct.get("$options", ""):
            flags |= _RE_OPT_TABLE.get(opt, 0)

        if compile_re:
            return re.compile(dct["$regex"], flags)
        else:
            return Regex(dct["$regex"], flags)
    if "$minKey" in dct:
        return MinKey()
    if "$maxKey" in dct:
        return MaxKey()
    if "$binary" in dct:
        if isinstance(dct["$type"], int):
            dct["$type"] = "%02x" % dct["$type"]
        subtype = int(dct["$type"], 16)
        if subtype >= 0xffffff80:  # Handle mongoexport values
            subtype = int(dct["$type"][6:], 16)
        return Binary(base64.b64decode(dct["$binary"].encode()), subtype)
    if "$code" in dct:
        return Code(dct["$code"], dct.get("$scope"))
    if bson.has_uuid() and "$uuid" in dct:
        return bson.uuid.UUID(dct["$uuid"])
    return dct
Ejemplo n.º 11
0
def object_hook(dct):
    print('oh', dct)
    if '_id' in dct:
        dct['_id'] = ObjectId(str(dct["_id"]))
        return dct
    if "$oid" in dct:
        return ObjectId(str(dct["$oid"]))
    if "$ref" in dct:
        return DBRef(dct["$ref"], dct["$id"], dct.get("$db", None))
    if "$date" in dct:
        return datetime.datetime.fromtimestamp(
            float(dct["$date"]) / 1000.0, utc)
    if "$regex" in dct:
        flags = 0
        if "i" in dct["$options"]:
            flags |= re.IGNORECASE
        if "m" in dct["$options"]:
            flags |= re.MULTILINE
        return re.compile(dct["$regex"], flags)
    if "$minKey" in dct:
        return MinKey()
    if "$maxKey" in dct:
        return MaxKey()
    if _use_uuid and "$uuid" in dct:
        return uuid.UUID(dct["$uuid"])
    return dct
Ejemplo n.º 12
0
def _parse_canonical_maxkey(doc: Any) -> MaxKey:
    """Decode a JSON MaxKey to bson.max_key.MaxKey."""
    if type(doc["$maxKey"]) is not int or doc["$maxKey"] != 1:
        raise TypeError("$maxKey value must be 1: %s", (doc, ))
    if len(doc) != 1:
        raise TypeError("Bad $minKey, extra field(s): %s" % (doc, ))
    return MaxKey()
Ejemplo n.º 13
0
def test_qop_type_21(monty_find, mongo_find):
    docs = [{"a": MaxKey()}]
    spec = {"a": {"$type": 127}}  # maxKey

    monty_c = monty_find(docs, spec)
    mongo_c = mongo_find(docs, spec)

    assert mongo_c.count() == 1
    assert monty_c.count() == mongo_c.count()
Ejemplo n.º 14
0
def test_sort_8(monty_sort, mongo_sort):
    docs = [{"a": MinKey()}, {"a": MaxKey()}]
    sort = [("a", -1)]

    monty_c = monty_sort(docs, sort)
    mongo_c = mongo_sort(docs, sort)

    for i in range(len(docs)):
        assert next(mongo_c)["_id"] == next(monty_c)["_id"]
Ejemplo n.º 15
0
def object_hook(dct, compile_re=True):
    if "$oid" in dct:
        return ObjectId(str(dct["$oid"]))
    if "$numberLong" in dct:
        return int(dct["$numberLong"])
    if "$decimal" in dct:
        v = str(dct["$decimal"])
        if "$precision" in dct:
            precision = dct["$precision"][0]
            scale = dct["$precision"][1]
            d = Decimal(v, precision, scale)
        else:
            d = Decimal(v)
        return d
    if "$ref" in dct:
        return DBRef(dct["$ref"], dct["$id"], dct.get("$db", None))
    if "$date" in dct:
        try:
            secs = float(dct["$date"]) / 1000.0
            return EPOCH_AWARE + datetime.timedelta(seconds=secs)
        except ValueError:
            return datetime.datetime.strptime(dct["$date"], "%Y-%m-%d")
    if "$timestamp" in dct:
        try:
            ms = long_type(dct["$timestamp"])
            return Timestamp(ms / 1000, ms % 1000 * 1000)
        except ValueError:
            dt = datetime.datetime.strptime(dct["$timestamp"],
                                            "%Y-%m-%d-%H.%M.%S.%f")
            secs = long_type(time.mktime(dt.timetuple()))
            return Timestamp(secs, dt.microsecond)
    if "$regex" in dct:
        flags = 0
        # PyMongo always adds $options but some other tools may not.
        for opt in dct.get("$options", ""):
            flags |= _RE_OPT_TABLE.get(opt, 0)

        if compile_re:
            return re.compile(dct["$regex"], flags)
        else:
            return Regex(dct["$regex"], flags)
    if "$minKey" in dct:
        return MinKey()
    if "$maxKey" in dct:
        return MaxKey()
    if "$binary" in dct:
        if isinstance(dct["$type"], int):
            dct["$type"] = "%d" % dct["$type"]
        subtype = int(dct["$type"])
        return Binary(base64.b64decode(dct["$binary"].encode()), subtype)
    if "$code" in dct:
        return Code(dct["$code"], dct.get("$scope"))
    if bson.has_uuid() and "$uuid" in dct:
        return bson.uuid.UUID(dct["$uuid"])
    return dct
Ejemplo n.º 16
0
    def check_encode_then_decode(self,
                                 doc_class=dict,
                                 decoder=decode,
                                 encoder=encode):

        # Work around http://bugs.jython.org/issue1728
        if sys.platform.startswith('java'):
            doc_class = SON

        def helper(doc):
            self.assertEqual(doc, (decoder(encoder(doc_class(doc)))))
            self.assertEqual(doc, decoder(encoder(doc)))

        helper({})
        helper({"test": u"hello"})
        self.assertTrue(
            isinstance(
                decoder(encoder({"hello": "world"}))["hello"], text_type))
        helper({"mike": -10120})
        helper({"long": Int64(10)})
        helper({"really big long": 2147483648})
        helper({u"hello": 0.0013109})
        helper({"something": True})
        helper({"false": False})
        helper({"an array": [1, True, 3.8, u"world"]})
        helper({"an object": doc_class({"test": u"something"})})
        helper({"a binary": Binary(b"test", 100)})
        helper({"a binary": Binary(b"test", 128)})
        helper({"a binary": Binary(b"test", 254)})
        helper({"another binary": Binary(b"test", 2)})
        helper(SON([(u'test dst', datetime.datetime(1993, 4, 4, 2))]))
        helper(
            SON([(u'test negative dst', datetime.datetime(1, 1, 1, 1, 1, 1))]))
        helper({"big float": float(10000000000)})
        helper({"ref": DBRef("coll", 5)})
        helper({"ref": DBRef("coll", 5, foo="bar", bar=4)})
        helper({"ref": DBRef("coll", 5, "foo")})
        helper({"ref": DBRef("coll", 5, "foo", foo="bar")})
        helper({"ref": Timestamp(1, 2)})
        helper({"foo": MinKey()})
        helper({"foo": MaxKey()})
        helper({"$field": Code("function(){ return true; }")})
        helper({
            "$field":
            Code("return function(){ return x; }", scope={'x': False})
        })

        def encode_then_decode(doc):
            return doc_class(doc) == decoder(
                encode(doc), CodecOptions(document_class=doc_class))

        qcheck.check_unittest(self, encode_then_decode,
                              qcheck.gen_mongo_dict(3))
def test_qop_lt_12(monty_find, mongo_find):
    min_k = MinKey()
    max_k = MaxKey()
    docs = [{"a": min_k}, {"a": max_k}]
    spec = {"a": {"$lt": max_k}}

    monty_c = monty_find(docs, spec)
    mongo_c = mongo_find(docs, spec)

    assert mongo_c.count() == 1
    assert monty_c.count() == mongo_c.count()
    assert next(mongo_c) == next(monty_c)
Ejemplo n.º 18
0
def test_qop_gt_12(monty_find, mongo_find):
    min_k = MinKey()
    max_k = MaxKey()
    docs = [{"a": min_k}, {"a": max_k}]
    spec = {"a": {"$gt": min_k}}

    monty_c = monty_find(docs, spec)
    mongo_c = mongo_find(docs, spec)

    assert FieldWalker(docs[1]).go("a").get().value == [max_k]
    assert mongo_c.count() == 1
    assert monty_c.count() == mongo_c.count()
    assert next(mongo_c) == next(monty_c)
Ejemplo n.º 19
0
    def test_encode_then_decode(self):
        def helper(dict):
            self.assertEqual(dict, (BSON.encode(dict)).decode())

        helper({})
        helper({"test": u"hello"})
        self.assertTrue(
            isinstance(
                BSON.encode({
                    "hello": "world"
                }).decode()["hello"], unicode))
        helper({"mike": -10120})
        helper({"long": long(10)})
        helper({"really big long": 2147483648})
        helper({u"hello": 0.0013109})
        helper({"something": True})
        helper({"false": False})
        helper({"an array": [1, True, 3.8, u"world"]})
        helper({"an object": {"test": u"something"}})
        helper({"a binary": Binary(b("test"), 100)})
        helper({"a binary": Binary(b("test"), 128)})
        helper({"a binary": Binary(b("test"), 254)})
        helper({"another binary": Binary(b("test"), 2)})
        helper(SON([(u'test dst', datetime.datetime(1993, 4, 4, 2))]))
        helper(
            SON([(u'test negative dst', datetime.datetime(1, 1, 1, 1, 1, 1))]))
        helper({"big float": float(10000000000)})
        helper({"ref": DBRef("coll", 5)})
        helper({"ref": DBRef("coll", 5, foo="bar", bar=4)})
        helper({"ref": DBRef("coll", 5, "foo")})
        helper({"ref": DBRef("coll", 5, "foo", foo="bar")})
        helper({"ref": Timestamp(1, 2)})
        helper({"foo": MinKey()})
        helper({"foo": MaxKey()})
        helper({"$field": Code("function(){ return true; }")})
        helper({
            "$field":
            Code("return function(){ return x; }", scope={'x': False})
        })

        doc_class = dict
        # Work around http://bugs.jython.org/issue1728
        if (sys.platform.startswith('java')
                and sys.version_info[:3] == (2, 5, 2)):
            doc_class = SON

        def encode_then_decode(doc):
            return doc == (BSON.encode(doc)).decode(as_class=doc_class)

        qcheck.check_unittest(self, encode_then_decode,
                              qcheck.gen_mongo_dict(3))
Ejemplo n.º 20
0
    def get_sub_table_bound(self):
        if self.config_rd.get(
                "subtbl_bd"
        ) is not None and '' != self.config_rd.get("subtbl_bd"):
            sub_tbl_bound = eval(self.config_rd.get("subtbl_bd"))
            # get LowBound
            for sub_bound in sub_tbl_bound.get("LowBound"):
                if 'MinKey()' == sub_tbl_bound.get("LowBound").get(sub_bound):
                    sub_tbl_bound["LowBound"][sub_bound] = MinKey()
            # get UpBound
            for sub_bound in sub_tbl_bound.get("UpBound"):
                if 'MaxKey()' == sub_tbl_bound.get("UpBound").get(sub_bound):
                    sub_tbl_bound["UpBound"][sub_bound] = MaxKey()

            return sub_tbl_bound
Ejemplo n.º 21
0
def test_qop_gte_12(monty_find, mongo_find):
    min_k = MinKey()
    max_k = MaxKey()
    docs = [
        {"a": min_k},
        {"a": max_k}
    ]
    spec = {"a": {"$gte": min_k}}

    monty_c = monty_find(docs, spec)
    mongo_c = mongo_find(docs, spec)

    assert mongo_c.count() == 2
    assert monty_c.count() == mongo_c.count()
    for i in range(2):
        assert next(mongo_c) == next(monty_c)
Ejemplo n.º 22
0
def test_sort_19(monty_sort, mongo_sort):
    docs = [
        {"a": ["x", True]},
        {"a": None},
        {"a": []},
        {"a": [5, []]},
        {"a": {"s": 7}},
        {"a": {"s": [9]}},
        {"a": {"s": 10}},
        {"a": 6},
        {"a": 4},
        {"a": [5, None]},
        {"a": [5, [1]]},
        {"a": [Decimal128("4.5"), Binary(b"0")]},
        {"a": [{"s": 5}, False]},
        {"a": [{"s": 9}]},
        {"a": [True, "y"]},
        {"a": Binary(b"a")},
        {"a": b"bytes"},
        {"a": ["abc"]},
        {"a": "banana"},
        {"a": "appple"},
        {"a": [Regex("^a", "ix")]},
        {"a": Regex("^b")},
        {"a": Code("x", {"m": 0})},
        {"a": Code("y")},
        {"a": Code("y", {})},
        {"a": Code("y", {"m": 0})},
        {"a": MinKey()},
        {"a": MaxKey()},
        {"a": Timestamp(0, 1)},
        {"a": Timestamp(1, 1)},
        {"a": ObjectId(b"000000000000")},
        {"a": ObjectId(b"000000000001")},
        {"a": datetime(1900, 1, 1)},
        {"a": datetime(1900, 1, 2)},
    ]
    sort = [("a", 1)]

    monty_c = monty_sort(docs, sort)
    mongo_c = mongo_sort(docs, sort)

    for i in range(len(docs)):
        assert next(mongo_c)["_id"] == next(monty_c)["_id"]
Ejemplo n.º 23
0
 def splitChunkMiddle(self, chunk):
     if 'size' in chunk:
         for key, value in chunk['min'].iteritems():
             if value == MinKey():
                 print("We skipped {} due to having a minKey on {}".format(
                     chunk['_id'], key))
                 return False
         for key, value in chunk['max'].iteritems():
             if value == MaxKey():
                 print("We skipped {} due to having a maxKey on {}".format(
                     chunk['_id'], key))
                 return False
         key_name = chunk['min'].iteritems().next()[0]
         min_value = Decimal(chunk['min'].iteritems().next()[1])
         max_value = Decimal(chunk['max'].iteritems().next()[1])
         desired_chunks = Decimal(
             math.ceil(chunk['size'] / self.max_chunk_size()))
         step_size = ((min_value - max_value) / desired_chunks).quantize(
             Decimal(0e-50), rounding=decimal.ROUND_DOWN)
         if (step_size == 0 or step_size == -0):
             print("Step was %s" % step_size)
             return False
         if (Decimal(min_value).is_signed()
                 or Decimal(max_value).is_signed()):
             split_points = range(max_value, min_value, step_size)
         else:
             split_points = range(min_value, max_value, step_size)
         errors = 0
         for split_point in split_points:
             try:
                 self.conn.admin.command(
                     "split",
                     chunk['ns'],
                     middle={key_name: long(split_point)})
             except Exception as e:
                 print(e)
                 print("Failed to  run split due to {}".format(e))
                 pass
                 errors += 1
         return True if errors != len(split_points) else False
     else:
         return False
Ejemplo n.º 24
0
    def test_encode_then_decode(self):
        def helper(dict):
            self.assertEqual(dict, (BSON.encode(dict)).decode())

        helper({})
        helper({"test": u"hello"})
        self.assert_(
            isinstance(
                BSON.encode({
                    "hello": "world"
                }).decode()["hello"], unicode))
        helper({"mike": -10120})
        helper({"long": long(10)})
        helper({"really big long": 2147483648})
        helper({u"hello": 0.0013109})
        helper({"something": True})
        helper({"false": False})
        helper({"an array": [1, True, 3.8, u"world"]})
        helper({"an object": {"test": u"something"}})
        helper({"a binary": Binary("test", 100)})
        helper({"a binary": Binary("test", 128)})
        helper({"a binary": Binary("test", 254)})
        helper({"another binary": Binary("test")})
        helper(SON([(u'test dst', datetime.datetime(1993, 4, 4, 2))]))
        helper(
            SON([(u'test negative dst', datetime.datetime(1, 1, 1, 1, 1, 1))]))
        helper({"big float": float(10000000000)})
        helper({"ref": DBRef("coll", 5)})
        helper({"ref": DBRef("coll", 5, foo="bar", bar=4)})
        helper({"ref": DBRef("coll", 5, "foo")})
        helper({"ref": DBRef("coll", 5, "foo", foo="bar")})
        helper({"ref": Timestamp(1, 2)})
        helper({"foo": MinKey()})
        helper({"foo": MaxKey()})

        def encode_then_decode(dict):
            return dict == (BSON.encode(dict)).decode()

        qcheck.check_unittest(self, encode_then_decode,
                              qcheck.gen_mongo_dict(3))
Ejemplo n.º 25
0
def object_hook(dct):
    if "$oid" in dct:
        return ObjectId(str(dct["$oid"]))
    if "$ref" in dct:
        return DBRef(dct["$ref"], dct["$id"], dct.get("$db", None))
    if "$date" in dct:
        secs = float(dct["$date"]) / 1000.0
        return EPOCH_AWARE + datetime.timedelta(seconds=secs)
    if "$regex" in dct:
        flags = 0
        if "i" in dct["$options"]:
            flags |= re.IGNORECASE
        if "m" in dct["$options"]:
            flags |= re.MULTILINE
        return re.compile(dct["$regex"], flags)
    if "$minKey" in dct:
        return MinKey()
    if "$maxKey" in dct:
        return MaxKey()
    if _use_uuid and "$uuid" in dct:
        return uuid.UUID(dct["$uuid"])
    return dct
Ejemplo n.º 26
0
def object_hook(dct, compile_re=True):
    if "$oid" in dct:
        return ObjectId(str(dct["$oid"]))
    if "$ref" in dct:
        return DBRef(dct["$ref"], dct["$id"], dct.get("$db", None))
    if "$date" in dct:
        dtm = dct["$date"]
        # mongoexport 2.6 and newer
        if isinstance(dtm, str):
            # datetime.datetime.strptime is new in python 2.5
            naive = datetime.datetime(
                *(time.strptime(dtm[:19], "%Y-%m-%dT%H:%M:%S")[0:6]))
            # The %f format is new in python 2.6
            micros = int(dtm[20:23]) * 1000
            aware = naive.replace(microsecond=micros, tzinfo=utc)
            offset = dtm[23:]
            if not offset or offset == 'Z':
                # UTC
                return aware
            else:
                if len(offset) == 5:
                    # Offset from mongoexport is in format (+|-)HHMM
                    secs = (int(offset[1:3]) * 3600 + int(offset[3:]) * 60)
                elif ':' in offset and len(offset) == 6:
                    # RFC-3339 format (+|-)HH:MM
                    hours, minutes = offset[1:].split(':')
                    secs = (int(hours) * 3600 + int(minutes) * 60)
                else:
                    # Not RFC-3339 compliant or mongoexport output.
                    raise ValueError("invalid format for offset")
                if offset[0] == "-":
                    secs *= -1
                return aware - datetime.timedelta(seconds=secs)
        # mongoexport 2.6 and newer, time before the epoch (SERVER-15275)
        elif isinstance(dtm, dict):
            secs = float(dtm["$numberLong"]) / 1000.0
        # mongoexport before 2.6
        else:
            secs = float(dtm) / 1000.0
        return EPOCH_AWARE + datetime.timedelta(seconds=secs)
    if "$regex" in dct:
        flags = 0
        # PyMongo always adds $options but some other tools may not.
        for opt in dct.get("$options", ""):
            flags |= _RE_OPT_TABLE.get(opt, 0)

        if compile_re:
            return re.compile(dct["$regex"], flags)
        else:
            return Regex(dct["$regex"], flags)
    if "$minKey" in dct:
        return MinKey()
    if "$maxKey" in dct:
        return MaxKey()
    if "$binary" in dct:
        if isinstance(dct["$type"], int):
            dct["$type"] = "%02x" % dct["$type"]
        subtype = int(dct["$type"], 16)
        if subtype >= 0xffffff80:  # Handle mongoexport values
            subtype = int(dct["$type"][6:], 16)
        return Binary(base64.b64decode(dct["$binary"].encode()), subtype)
    if "$code" in dct:
        return Code(dct["$code"], dct.get("$scope"))
    if bson.has_uuid() and "$uuid" in dct:
        return bson.uuid.UUID(dct["$uuid"])
    if "$undefined" in dct:
        return None
    if "$numberLong" in dct:
        # 2to3 will change this to int. PyMongo 3.0 supports
        # a new type, Int64, to avoid round trip issues.
        return int(dct["$numberLong"])
    if "$timestamp" in dct:
        tsp = dct["$timestamp"]
        return Timestamp(tsp["t"], tsp["i"])
    return dct
Ejemplo n.º 27
0
    ord(BSONUND): lambda u, v, w, x, y, z: (None, w),  # Deprecated undefined
    ord(BSONOID): _get_oid,
    ord(BSONBOO): _get_boolean,
    ord(BSONDAT): _get_date,
    ord(BSONNUL): lambda u, v, w, x, y, z: (None, w),
    ord(BSONRGX): _get_regex,
    ord(BSONREF): _get_ref,  # Deprecated DBPointer
    ord(BSONCOD): _get_code,
    ord(BSONSYM): _get_string,  # Deprecated symbol
    ord(BSONCWS): _get_code_w_scope,
    ord(BSONINT): _get_int,
    ord(BSONTIM): _get_timestamp,
    ord(BSONLON): _get_int64,
    ord(BSONDEC): _get_decimal128,
    ord(BSONMIN): lambda u, v, w, x, y, z: (MinKey(), w),
    ord(BSONMAX): lambda u, v, w, x, y, z: (MaxKey(), w)
}

if _USE_C:

    def _element_to_dict(data, view, position, obj_end, opts):
        return _cbson._element_to_dict(data, position, obj_end, opts)
else:

    def _element_to_dict(data, view, position, obj_end, opts):
        """Decode a single key, value pair."""
        element_type = data[position]
        position += 1
        element_name, position = _get_c_string(data, view, position, opts)
        try:
            value, position = _ELEMENT_GETTER[element_type](data, view,
Ejemplo n.º 28
0
    BSONUND: lambda v, w, x, y, z: (None, w),  # Deprecated undefined
    BSONOID: _get_oid,
    BSONBOO: _get_boolean,
    BSONDAT: _get_date,
    BSONNUL: lambda v, w, x, y, z: (None, w),
    BSONRGX: _get_regex,
    BSONREF: _get_ref,  # Deprecated DBPointer
    BSONCOD: _get_code,
    BSONSYM: _get_string,  # Deprecated symbol
    BSONCWS: _get_code_w_scope,
    BSONINT: _get_int,
    BSONTIM: _get_timestamp,
    BSONLON: _get_int64,
    BSONDEC: _get_decimal128,
    BSONMIN: lambda v, w, x, y, z: (MinKey(), w),
    BSONMAX: lambda v, w, x, y, z: (MaxKey(), w)
}


def _element_to_dict(data, position, obj_end, opts):
    """Decode a single key, value pair."""
    element_type = data[position:position + 1]
    position += 1
    element_name, position = _get_c_string(data, position, opts)
    try:
        value, position = _ELEMENT_GETTER[element_type](data, position,
                                                        obj_end, opts,
                                                        element_name)
    except KeyError:
        _raise_unknown_type(element_type, element_name)
    return element_name, value, position
Ejemplo n.º 29
0
 def test_maxkey(self):
     self.round_trip({"m": MaxKey()})
Ejemplo n.º 30
0
    BSONBIN: _get_binary,
    BSONUND: lambda w, x, y, z: (None, x),  # Deprecated undefined
    BSONOID: _get_oid,
    BSONBOO: _get_boolean,
    BSONDAT: _get_date,
    BSONNUL: lambda w, x, y, z: (None, x),
    BSONRGX: _get_regex,
    BSONREF: _get_ref,  # Deprecated DBPointer
    BSONCOD: _get_code,
    BSONSYM: _get_string,  # Deprecated symbol
    BSONCWS: _get_code_w_scope,
    BSONINT: _get_int,
    BSONTIM: _get_timestamp,
    BSONLON: _get_int64,
    BSONMIN: lambda w, x, y, z: (MinKey(), x),
    BSONMAX: lambda w, x, y, z: (MaxKey(), x)}


def _element_to_dict(data, position, obj_end, opts):
    """Decode a single key, value pair."""
    element_type = data[position:position + 1]
    position += 1
    element_name, position = _get_c_string(data, position)
    value, position = _ELEMENT_GETTER[element_type](data,
                                                    position, obj_end, opts)
    return element_name, value, position


def _elements_to_dict(data, position, obj_end, opts, subdocument=None):
    """Decode a BSON document."""
    if type(opts.document_class) == tuple: