Пример #1
0
def print_dict(d, dict_property="Property", wrap=0, args = None, sort_f = None):
    pt = prettytable.PrettyTable([dict_property, 'Value'], caching=False)
    pt.align = 'l'
    _set_table_common_opt(pt, args)

    keys = None
    if sort_f is None:
        keys = d.keys()
    else:
        keys = sorted(d.keys(), cmp = sort_f)

    for k in keys:
        v = d[k]
        # convert dict to str to check length
        if isinstance(v, dict):
            v = unicode(v)
        if wrap > 0:
            v = textwrap.fill(unicode(v), wrap)
        # if value has a newline, add in multiple rows
        # e.g. fault with stacktrace
        if v and isinstance(v, basestring) and r'\n' in v:
            lines = v.strip().split(r'\n')
            col1 = k
            for line in lines:
                pt.add_row([col1, line])
                col1 = ''
        else:
            pt.add_row([k, v])
    print strutils.safe_encode(pt.get_string())
Пример #2
0
def print_list(objs, fields, formatters={}, sortby_index=None, args = None):

    if sortby_index is None:
        sortby = None
    else:
        sortby = fields[sortby_index]

    wrap = _get_column_maxlen(args)

    pt = prettytable.PrettyTable([f for f in fields], caching=False)
    pt.align = 'l' 
    _set_table_common_opt(pt, args)

    data = None
    for o in objs:
        row = []
        for field in fields:
            if field in formatters:
                data = formatters[field](o)
                row.append(data)
            else:
                field_name = field.lower().replace(' ', '_')
                data = o.get(field_name, '')
                if wrap > 0:
                    data = textwrap.fill(unicode(data), wrap)
                row.append(data)

        pt.add_row(row)

    s = None
    if sortby is not None:
        s = pt.get_string(sortby=sortby)
    else:
        s = pt.get_string()

    print strutils.safe_encode(s)