Esempio n. 1
0
def print_query_result_db(res, distict_generator = False, count = False, raw = False, interactive = True):
    '''
    Print the results from the result db (mongodb).

    Parameters
    ----------
    count : bool, optional (default is False)
        Only print count, not results
    distict_generator : bool, optional (default is False)
        Res is generator<object> created from the distinct(...) method of mongodb.
        If generaor<dict>, convert each dict to json.
        Otherwise just print.
    raw : bool, optional (default is False)
        Print raw data from gridfs
        Otherwise print json.
    res : gridfs.grid_file.GridOutCursor or generator<object> or pymongo.cursor.Cursor
        First if non_document and non_document_raw.
        Second if disctinct values wanted.
        Thirst otherwise.
        The results to print
    interactive: bool, optional (default is True)
        Iterate interactive through the result cursor
    '''
    from pymongo.errors import PyMongoError

    try:
        # print count
        if count:
            cnt = 0
            # res is list
            if distict_generator:
                cnt = len(res)
            # res is cursor
            else:
                cnt = res.count()
            clilog.info(cnt)
        else:
            if distict_generator:
                for r in sorted(res):
                    if isinstance(r, dict):
                        r = dict2json(res)
                    clilog.info(r)
            else:
                for i, res in enumerate(res, 1):
                    # interactive result view
                    if i != 1 and interactive and raw_input('Press any key to view next result or abort with "no" !)').lower() == 'no':
                        break
                    sys.stderr.write('/* {} */\n'.format(i))
                    # print raw data
                    if raw:
                        # gridfs.grid_file.GridOut
                        for gridout_obj in res:
                            clilog.info(gridout_obj)
                    # print json
                    else:
                        clilog.info(dict2json(res))

    except PyMongoError as e:
        log.exception(e)
Esempio n. 2
0
def format_query_result_db(res_cursor, distict_generator = False, count = False, raw = False, html = False):
    '''
    Format the results from the result db (mongodb).

    Parameters
    ----------
    res_cursor : gridfs.grid_file.GridOutCursor or generator<object> or pymongo.cursor.Cursor
        First if non_document and non_document_raw.
        Second if distinct values wanted.
        Thirst otherwise.
    distict_generator : bool, optional (default is False)
        Res is generator<object> created from the distinct(...) method of mongodb.
        If generaor<dict>, convert each dict to json.
        Otherwise just print.
    count : bool, optional (default is False)
        Only print count, not results
    raw : bool, optional (default is False)
        Print raw data from gridfs
        Otherwise print json.
        If `raw` will not be converted to html!
    html : bool, optional (default is False)
        Format as html.

    Returns
    -------
    str
    '''
    from pymongo.errors import PyMongoError
    from androlyze.ui.util import HtmlUtil

    # if html enabled convert to table view if `json2html` is present
    # otherwise use pygmentize
    json_convert = lambda json : json
    if html:
        try:
            from json2html import json2html
            json_convert = lambda j : json2html.convert(json = j)
        except ImportError:
            from pygments import highlight
            from pygments.formatters import HtmlFormatter
            from pygments.lexers import get_lexer_by_name
            
            json_convert = lambda json: highlight(json, get_lexer_by_name('json'), HtmlFormatter())

    # collect results as list<str>
    resl = []

    def anl(text):
        ''' Append a newline '''
        # dont format raw data as html
        return '%s\n' % text if not html or raw else HtmlUtil.newline(HtmlUtil.prefy(text))

    try:
        # return count
        if count:
            cnt = 0
            
            if is_pymongo_cursor(res_cursor):
                cnt = res_cursor.count()
            elif distict_generator:
                cnt = len(list(res_cursor))
            
            return '%d' % cnt
        
        else:
            if distict_generator:
                for r in sorted(res_cursor):
                    if isinstance(r, dict):
                        r = dict2json(res_cursor)
                        resl.append(r)
                    elif isinstance(r, (str, unicode)):
                        resl.append(r)
            else:
                for i, res in enumerate(res_cursor, 1):
                    delimiter = '/* %d */' % i
                    text = HtmlUtil.newline(delimiter) if html else delimiter
                    if html: text = HtmlUtil.redify(text)
                    resl.append(text)
                    # return raw data
                    if raw:
                        # gridfs.grid_file.GridOut
                        for gridout_obj in res:
                            resl.append(gridout_obj)
                    # return json
                    else:
                        j = dict2json(res)
                        # convert json (if enabled)
                        j = json_convert(j)
                        resl.append(j)
        # return result by joining single strings
        return ''.join([anl(res_str) for res_str in resl])
    except PyMongoError as e:
        log.exception(e)
Esempio n. 3
0
            if res_collection:
                log.debug("dropping collection %s", RESULT_DOCUMENTS_COLLECTION_NAME)
                self.db.drop_collection(RESULT_DOCUMENTS_COLLECTION_NAME)
                self._open_res_coll()
                log.debug("recreating collection %s", RESULT_DOCUMENTS_COLLECTION_NAME)
        except PyMongoError as e:
            log.critical(e)

def factory_from_config(settings):
    ''' Get a factory_from_config object from the distributed config.
    
    Parameters
    ----------
    settings : Settings
     '''
    from androlyze.celery.celerysettings import settings
    return ResultDatabaseStorage(*settings.get_mongodb_settings())

if __name__ == '__main__':
    from androlyze.model.script.ScriptUtil import dict2json
    print "foo"
    res_db = ResultDatabaseStorage()
    _if = ["script meta", "apk meta", "apkinfo.libraries"]
    _if = None
    ef = ["apkinfo"]
    wheres = [("apk meta.package name", "a2dp.Vol")]
    version = "0.1"
    for res in res_db.get_results(include_fields = _if, exclude_fields = ef, wheres = wheres, n = 2, version=version):
        print dict2json(res)

    print res_db
Esempio n. 4
0
def print_query_result_db(res,
                          distict_generator=False,
                          count=False,
                          raw=False,
                          interactive=True):
    '''
    Print the results from the result db (mongodb).

    Parameters
    ----------
    count : bool, optional (default is False)
        Only print count, not results
    distict_generator : bool, optional (default is False)
        Res is generator<object> created from the distinct(...) method of mongodb.
        If generaor<dict>, convert each dict to json.
        Otherwise just print.
    raw : bool, optional (default is False)
        Print raw data from gridfs
        Otherwise print json.
    res : gridfs.grid_file.GridOutCursor or generator<object> or pymongo.cursor.Cursor
        First if non_document and non_document_raw.
        Second if disctinct values wanted.
        Thirst otherwise.
        The results to print
    interactive: bool, optional (default is True)
        Iterate interactive through the result cursor
    '''
    from pymongo.errors import PyMongoError

    try:
        # print count
        if count:
            cnt = 0
            # res is list
            if distict_generator:
                cnt = len(res)
            # res is cursor
            else:
                cnt = res.count()
            clilog.info(cnt)
        else:
            if distict_generator:
                for r in sorted(res):
                    if isinstance(r, dict):
                        r = dict2json(res)
                    clilog.info(r)
            else:
                for i, res in enumerate(res, 1):
                    # interactive result view
                    if i != 1 and interactive and raw_input(
                            'Press any key to view next result or abort with "no" !)'
                    ).lower() == 'no':
                        break
                    sys.stderr.write('/* {} */\n'.format(i))
                    # print raw data
                    if raw:
                        # gridfs.grid_file.GridOut
                        for gridout_obj in res:
                            clilog.info(gridout_obj)
                    # print json
                    else:
                        clilog.info(dict2json(res))

    except PyMongoError as e:
        log.exception(e)