コード例 #1
0
def query_from_json(jdata):
    method = jdata.pop('method', None)
    if method not in ['get', 'get_many', 'get_all',]:
        return None, 'unknown method'
    callmethod = globals().get(method)
    if not callmethod:
        return None, 'failed to call target method'
    index_name = jdata.pop('index', None)
    if index_name not in db().indexes_names:
        return None, 'unknown index'
    if 'with_doc' not in jdata:
        jdata['with_doc'] = True
    if 'with_storage' not in jdata:
        jdata['with_storage'] = True
    try:
        result = callmethod(index_name, **jdata)
    except:
        return None, lg.format_exception()
    def _clean_doc(doc):
        doc.pop('_id')
        doc.pop('_rev')
        return doc
    if jdata['with_doc'] and index_name != 'id':
        if method in ['get_many', 'get_all',]:
            return (_clean_doc(r['doc']) for r in result), ''
        else:
            return (_clean_doc(r['doc']) for r in [result,]), ''
    return result, ''
コード例 #2
0
ファイル: coins_db.py プロジェクト: vesellov/bitdust.devel
def query_json(jdata):
    """
    Input keys:

    method: 'get', 'get_many' or 'get_all'     index: 'id', 'idurl',
    etc.     key: key to read single record from db (optional)
    start: low key limit to search records in range     end: high key
    limit to search records in range Returns tuple:     generator object
    or None, error message
    """
    if not db().opened:
        return None, 'database is closed'
    method = jdata.pop('method', None)
    if method not in ['get', 'get_many', 'get_all', ]:
        return None, 'unknown method'
    callmethod = globals().get(method)
    if not callmethod:
        return None, 'failed to call target method'
    index_name = jdata.pop('index', None)
    if index_name not in db().indexes_names:
        return None, 'unknown index'
    if 'with_doc' not in jdata:
        jdata['with_doc'] = True
    if 'with_storage' not in jdata:
        jdata['with_storage'] = True
    try:
        result = callmethod(index_name, **jdata)
    except:
        return None, lg.format_exception()
    if jdata['with_doc'] and index_name != 'id':
        return (_clean_doc(r['doc']) for r in result), ''
    return result, ''
コード例 #3
0
def query_from_json(jdata):
    method = jdata.pop('method', None)
    if method not in [
            'get',
            'get_many',
            'get_all',
    ]:
        return None, 'unknown method'
    callmethod = globals().get(method)
    if not callmethod:
        return None, 'failed to call target method'
    index_name = jdata.pop('index', None)
    if index_name not in db().indexes_names:
        return None, 'unknown index'
    if 'with_doc' not in jdata:
        jdata['with_doc'] = True
    if 'with_storage' not in jdata:
        jdata['with_storage'] = True
    try:
        result = callmethod(index_name, **jdata)
    except:
        return None, lg.format_exception()

    def _clean_doc(doc):
        doc.pop('_id')
        doc.pop('_rev')
        return doc

    if jdata['with_doc'] and index_name != 'id':
        if method in [
                'get_many',
                'get_all',
        ]:
            return (_clean_doc(r['doc']) for r in result), ''
        else:
            return (_clean_doc(r['doc']) for r in [
                result,
            ]), ''
    return result, ''