Esempio n. 1
0
 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))
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)