Revision b0e7f310 snf-webproject/synnefo/webproject/management/util.py

b/snf-webproject/synnefo/webproject/management/util.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
import json
34 35
from datetime import datetime
35 36
from django.utils.timesince import timesince, timeuntil
36 37

  
......
113 114
    return (filter_dict, exclude_dict)
114 115

  
115 116

  
116
def pprint_table(out, table, headers=None, separator=None):
117
def pprint_table(out, table, headers=None, output_format='pretty',
118
                 separator=None):
117 119
    """Print a pretty, aligned string representation of table.
118 120

  
119 121
    Works by finding out the max width of each column and padding to data
......
121 123
    """
122 124

  
123 125
    assert(isinstance(table, (list, tuple))), "Invalid table type"
124
    sep = separator if separator else "  "
125

  
126 126
    if headers:
127 127
        assert(isinstance(headers, (list, tuple))), "Invalid headers type"
128
        table.insert(0, headers)
129 128

  
130
    def strignify(obj):
129
    sep = separator if separator else "  "
130

  
131
    def stringnify(obj):
131 132
        if isinstance(obj, (unicode, str)):
132 133
            return udec(obj)
133 134
        else:
134 135
            return str(obj)
135 136

  
136
    table = [map(strignify, row) for row in table]
137

  
138
    # Find out the max width of each column
139
    widths = [max(map(len, col)) for col in zip(*table)]
140

  
141
    t_length = sum(widths) + len(sep) * (len(widths) - 1)
142
    if headers:
143
        # pretty print the headers
144
        line = sep.join(uenc(v.rjust(w)) for v, w in zip(headers, widths))
145
        out.write(line + "\n")
146
        out.write("-" * t_length + "\n")
147
        # remove headers
148
        table = table[1:]
149

  
150
    # print the rest table
151
    for row in table:
152
        line = sep.join(uenc(v.rjust(w)) for v, w in zip(row, widths))
153
        out.write(line + "\n")
137
    headers = map(stringnify, headers)
138
    table = [map(stringnify, row) for row in table]
139

  
140
    if output_format == "json":
141
        table = [dict(zip(headers, row)) for row in table]
142
        out.write(json.dumps(table, indent=4))
143
        out.write("\n")
144
    elif output_format == "csv":
145
        if headers:
146
            line = ",".join("\"%s\"" % uenc(v) for v in headers)
147
            out.write(line + "\n")
148
            for row in table:
149
                line = ",".join("\"%s\"" % uenc(v) for v in row)
150
                out.write(line + "\n")
151
    elif output_format == "pretty":
152
        # Find out the max width of each column
153
        widths = [max(map(len, col)) for col in zip(*([headers] + table))]
154

  
155
        t_length = sum(widths) + len(sep) * (len(widths) - 1)
156
        if headers:
157
            # pretty print the headers
158
            line = sep.join(uenc(v.rjust(w)) for v, w in zip(headers, widths))
159
            out.write(line + "\n")
160
            out.write("-" * t_length + "\n")
161

  
162
        # print the rest table
163
        for row in table:
164
            line = sep.join(uenc(v.rjust(w)) for v, w in zip(row, widths))
165
            out.write(line + "\n")
166
    else:
167
        raise ValueError("Unknown output format '%s'" % output_format)

Also available in: Unified diff