Esempio n. 1
0
def _jsonify_collection_info(coll, dbname=None):
    """Private function to turn information about one collection into base 
       JSON """

    if dbname is None:
        dbname = coll.name
    results = _get_db_records(coll)

    json_db_data = {}
    json_db_data['dbinfo'] = {}
    json_db_data['dbinfo']['name'] = dbname
    json_db_data['records'] = {}
    json_db_data['fields'] = {}

    lst = set()
    for doc in results:
        lst = lst | set(doc['_id'])
    lst = list(lst)
    lst.sort()

    for doc in lst:
        try:
            rls = dbtools.get_sample_record(coll, str(doc))
            try:
                typedesc = id_object.get_description(rls[str(doc)])
            except:
                typedesc = 'Type cannot be identified (' \
                           + str(type(rls[str(doc)])) + ')'
            try:
                strval =  str(rls[str(doc)]).decode('unicode_escape').\
                          encode('ascii','ignore')
            except:
                strval = 'Record cannot be stringified'
        except:
            typedesc = 'Record cannot be found containing key'
            strval = 'N/A'

        lstr = len(strval)
        strval = strval.replace('\n', ' ').replace('\r', '')
        strval = '`' + strval[:100].strip() + '`'
        if lstr > 100:
            strval = strval + ' ...'
        json_db_data['fields'][str(doc)] = {}
        json_db_data['fields'][str(doc)]['type'] = typedesc
        json_db_data['fields'][str(doc)]['example'] = strval

    for recordid, doc in enumerate(results):
        json_db_data['records'][recordid] = {}
        json_db_data['records'][recordid]['count'] = int(doc['value'])
        json_db_data['records'][recordid]['schema'] = doc['_id']

    indices = coll.index_information()
    json_db_data['indices'] = {}
    for recordid, index in enumerate(indices):
        json_db_data['indices'][recordid] = {}
        json_db_data['indices'][recordid]['name'] = index
        json_db_data['indices'][recordid]['keys'] = indices[index]['key']

    return json_db_data
Esempio n. 2
0
def _jsonify_record(name, record, parse_jsonb=False, inferred=False):
    vals = {}
    if isinstance(record, dict) and parse_jsonb:
        for el in record:
            merge_dicts(
                vals,
                _jsonify_record(name + "." + str(el),
                                record[el],
                                parse_jsonb=parse_jsonb,
                                inferred=True))

    if isinstance(record, list) and parse_jsonb:
        if len(record) > 0 and isinstance(record[0], dict):
            merge_dicts(
                vals,
                _jsonify_record(name,
                                record[0],
                                parse_jsonb=parse_jsonb,
                                inferred=True))

    try:
        typedesc = id_object.get_description(record)
    except:
        typedesc = 'Type cannot be identified (' \
            + str(type(record)) + ')'
    try:
        strval =  str(record).decode('unicode_escape').\
            encode('ascii','ignore')
    except:
        strval = 'Record cannot be stringified'

    lstr = len(strval)
    strval = strval.replace('\n', ' ').replace('\r', '')
    strval = '`' + strval[:100].strip() + '`'
    if lstr > 100:
        strval = strval + ' ...'
    vals[str(name)] = {}
    vals[str(name)]['type'] = typedesc
    vals[str(name)]['example'] = strval
    vals[str(name)]['inferred'] = inferred
    return vals
Esempio n. 3
0
def _jsonify_table_info(table, dbname = None):

    """Private function to turn information about one table into base 
       JSON """
    # Needs to be rewritten for Postgres
    raise NotImplementedError

    if dbname is None:
        dbname = table.search_table
    results = _get_db_records(table)

    json_db_data = {}
    json_db_data['dbinfo'] ={}
    json_db_data['dbinfo']['name'] = dbname
    json_db_data['records'] = {}
    json_db_data['fields'] = {}

    lst=set()
    for doc in results:
        lst = lst | set(doc['_id'])
    lst=list(lst)
    lst.sort()

    for doc in lst:
        try:
            rls = dbtools.get_sample_record(table, str(doc))
            try:
                typedesc = id_object.get_description(rls[str(doc)])
            except:
                typedesc = 'Type cannot be identified (' \
                           + str(type(rls[str(doc)])) + ')'
            try:
                strval =  str(rls[str(doc)]).decode('unicode_escape').\
                          encode('ascii','ignore')
            except:
                strval = 'Record cannot be stringified'
        except:
            typedesc = 'Record cannot be found containing key'
            strval = 'N/A'

        lstr = len(strval)
        strval = strval.replace('\n',' ').replace('\r','')
        strval = '`' + strval[:100].strip() + '`'
        if lstr > 100:
            strval = strval + ' ...'
        json_db_data['fields'][str(doc)] = {}
        json_db_data['fields'][str(doc)]['type'] = typedesc
        json_db_data['fields'][str(doc)]['example'] = strval


    for recordid, doc in enumerate(results):
        json_db_data['records'][recordid] = {}
        json_db_data['records'][recordid]['count'] = int(doc['value'])
        json_db_data['records'][recordid]['schema'] = doc['_id']

    indices = table.index_information()
    json_db_data['indices'] = {}
    for recordid, index in enumerate(indices):
        json_db_data['indices'][recordid] = {}
        json_db_data['indices'][recordid]['name'] = index
        json_db_data['indices'][recordid]['keys'] = indices[index]['key']

    return json_db_data