Python bson.json_util.object_hook() Examples

The following are 11 code examples of bson.json_util.object_hook(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module bson.json_util , or try the search function .
Example #1
Source File: export_tools.py    From kobo-predict with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def query_mongo(username, id_string, query=None, hide_deleted=True):
    # print("incoming query", query)
    qry = query
    qry["_deleted_at"] = {'$exists': False}
    # query = None
    # query = json.loads(query, object_hook=json_util.object_hook)\
        # if query else {}
    # query = dict_for_mongo(query)
    # query[USERFORM_ID] = u'{0}_{1}'.format(username, id_string)
    # if hide_deleted:
    #     query = {"$and": [query, {"_deleted_at": None}]}
    # query = {"$and": [query, qry]}
    # print(query)
    print("cpount", xform_instances.find(qry).count())
    print("qry", qry)
    return xform_instances.find(qry) 
Example #2
Source File: admin_plugin.py    From rpaas with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def list_healings(args):
    parser = _base_args("list-healings")
    parser.add_argument("-n", "--quantity", default=20, required=False, type=int)
    parsed_args = parser.parse_args(args)
    result = proxy_request(parsed_args.service, "/admin/healings?quantity=" + str(parsed_args.quantity),
                           method="GET")
    body = result.read().rstrip("\n")
    if result.getcode() != 200:
        sys.stderr.write("ERROR: " + body + "\n")
        sys.exit(1)
    try:
        healings_list = []
        healings_list = json.loads(body, object_hook=json_util.object_hook)
    except Exception as e:
        sys.stderr.write("ERROR: invalid json response - {}\n".format(e.message))
        sys.exit(1)
    healings_table = DisplayTable(['Instance', 'Machine', 'Start Time', 'Duration', 'Status'])
    _render_healings_list(healings_table, healings_list) 
Example #3
Source File: parsed_instance.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def mongo_aggregate(cls, query, pipeline, hide_deleted=True):
        """Perform mongo aggregate queries
        query - is a dict which is to be passed to $match, a pipeline operator
        pipeline - list of dicts or dict of mongodb pipeline operators,
        http://docs.mongodb.org/manual/reference/operator/aggregation-pipeline
        """
        if isinstance(query, basestring):
            query = json.loads(
                query, object_hook=json_util.object_hook) if query else {}
        if not (isinstance(pipeline, dict) or isinstance(pipeline, list)):
            raise Exception(_(u"Invalid pipeline! %s" % pipeline))
        if not isinstance(query, dict):
            raise Exception(_(u"Invalid query! %s" % query))
        query = dict_for_mongo(query)
        if hide_deleted:
            # display only active elements
            deleted_at_query = {
                "$or": [{"_deleted_at": {"$exists": False}},
                        {"_deleted_at": None}]}
            # join existing query with deleted_at_query on an $and
            query = {"$and": [query, deleted_at_query]}
        k = [{'$match': query}]
        if isinstance(pipeline, list):
            k.extend(pipeline)
        else:
            k.append(pipeline)
        results = xform_instances.aggregate(k)
        return results['result'] 
Example #4
Source File: parsed_instance.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def query_mongo_minimal(
            cls, query, fields, sort, start=0, limit=DEFAULT_LIMIT,
            count=False, hide_deleted=True):
        fields_to_select = {cls.USERFORM_ID: 0}
        # TODO: give more detailed error messages to 3rd parties
        # using the API when json.loads fails
        query = json.loads(
            query, object_hook=json_util.object_hook) if query else {}
        query = dict_for_mongo(query)
        if hide_deleted:
            # display only active elements
            # join existing query with deleted_at_query on an $and
            query = {"$and": [query, {"_deleted_at": None}]}
        # fields must be a string array i.e. '["name", "age"]'
        fields = json.loads(
            fields, object_hook=json_util.object_hook) if fields else []
        # TODO: current mongo (2.0.4 of this writing)
        # cant mix including and excluding fields in a single query
        if type(fields) == list and len(fields) > 0:
            fields_to_select = dict(
                [(_encode_for_mongo(field), 1) for field in fields])
        sort = json.loads(
            sort, object_hook=json_util.object_hook) if sort else {}
        cursor = xform_instances.find(query, fields_to_select)
        if count:
            return [{"count": cursor.count()}]

        if start < 0 or limit < 0:
            raise ValueError(_("Invalid start/limit params"))

        cursor.skip(start).limit(limit)
        if type(sort) == dict and len(sort) == 1:
            sort_key = sort.keys()[0]
            # TODO: encode sort key if it has dots
            sort_dir = int(sort[sort_key])  # -1 for desc, 1 for asc
            cursor.sort(_encode_for_mongo(sort_key), sort_dir)
        # set batch size
        cursor.batch_size = cls.DEFAULT_BATCHSIZE
        return cursor 
Example #5
Source File: files.py    From strax with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run_metadata(self, run_id, projection=None):
        path = self._run_meta_path(run_id)
        if not osp.exists(path):
            raise strax.RunMetadataNotAvailable(
                f"No file at {path}, cannot find run metadata for {run_id}")
        with open(path, mode='r') as f:
            md = json.loads(f.read(),
                            object_hook=json_util.object_hook)
        md = strax.flatten_run_metadata(md)
        if projection is not None:
            md = {k: v
                  for k, v in md.items()
                  if k in projection}
        return md 
Example #6
Source File: compat.py    From jsondb with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def decode(value):
    from bson import json_util

    return json_decode(value, encoding='utf-8', object_hook=json_util.object_hook) 
Example #7
Source File: json.py    From yeti with Apache License 2.0 5 votes vote down vote up
def decode(self, s):

        def object_hook(obj):
            return bson_hook(obj)

        return simplejson.loads(s, object_hook=self.object_hook) 
Example #8
Source File: cli.py    From tracboat with GNU General Public License v3.0 5 votes vote down vote up
def _loads(content, fmt=None):
    if fmt == 'toml':
        return toml.loads(content)
    elif fmt == 'json':
        return json.loads(content, object_hook=json_util.object_hook)
    elif fmt == 'python':
        return ast.literal_eval(content)
    elif fmt == 'pickle':
        return pickle.loads(content)
    else:
        return content 
Example #9
Source File: mongo.py    From Archery with Apache License 2.0 5 votes vote down vote up
def parse_oids(oids):
    if not isinstance(oids, list):
        raise Exception("$oids takes an array as input.")

    return [bson_object_hook({"$oid": oid}) for oid in oids] 
Example #10
Source File: mongo.py    From Archery with Apache License 2.0 5 votes vote down vote up
def datetime_parser(dct):
    for k, v in dct.items():
        if isinstance(v, str):
            m = date_regex.findall(v)
            if len(m) > 0:
                dct[k] = parse(m[0], yearfirst=True)

    if "$humanTime" in dct:
        return parse_human_time(dct["$humanTime"])

    if "$oids" in dct:
        return parse_oids(dct["$oids"])

    return bson_object_hook(dct) 
Example #11
Source File: parsed_instance.py    From kobo-predict with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def query_mongo(cls, username, id_string, query, fields, sort, start=0,
                    limit=DEFAULT_LIMIT, count=False, hide_deleted=True):
        fields_to_select = {cls.USERFORM_ID: 0}
        # TODO: give more detailed error messages to 3rd parties
        # using the API when json.loads fails
        if isinstance(query, basestring):
            query = json.loads(query, object_hook=json_util.object_hook)
        query = query if query else {}
        query = dict_for_mongo(query)
        query[cls.USERFORM_ID] = u'%s_%s' % (username, id_string)

        # check if query contains and _id and if its a valid ObjectID
        if '_uuid' in query and ObjectId.is_valid(query['_uuid']):
            query['_uuid'] = ObjectId(query['_uuid'])

        if hide_deleted:
            # display only active elements
            # join existing query with deleted_at_query on an $and
            query = {"$and": [query, {"_deleted_at": None}]}

        # fields must be a string array i.e. '["name", "age"]'
        if isinstance(fields, basestring):
            fields = json.loads(fields, object_hook=json_util.object_hook)
        fields = fields if fields else []

        # TODO: current mongo (2.0.4 of this writing)
        # cant mix including and excluding fields in a single query
        if type(fields) == list and len(fields) > 0:
            fields_to_select = dict(
                [(_encode_for_mongo(field), 1) for field in fields])
        if isinstance(sort, basestring):
            sort = json.loads(sort, object_hook=json_util.object_hook)
        sort = sort if sort else {}

        cursor = xform_instances.find(query, fields_to_select)
        if count:
            return [{"count": cursor.count()}]

        if start < 0 or limit < 0:
            raise ValueError(_("Invalid start/limit params"))

        cursor.skip(start).limit(limit)
        if type(sort) == dict and len(sort) == 1:
            sort_key = sort.keys()[0]
            # TODO: encode sort key if it has dots
            sort_dir = int(sort[sort_key])  # -1 for desc, 1 for asc
            cursor.sort(_encode_for_mongo(sort_key), sort_dir)
        # set batch size
        cursor.batch_size = cls.DEFAULT_BATCHSIZE
        return cursor