Revision f105d79d snf-webproject/synnefo/webproject/management/util.py
b/snf-webproject/synnefo/webproject/management/util.py | ||
---|---|---|
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 | 34 |
import json |
35 |
import csv |
|
36 |
import functools |
|
35 | 37 |
from datetime import datetime |
36 | 38 |
from django.utils.timesince import timesince, timeuntil |
37 | 39 |
|
... | ... | |
134 | 136 |
else: |
135 | 137 |
return str(obj) |
136 | 138 |
|
137 |
headers = map(stringnify, headers) |
|
139 |
if headers: |
|
140 |
headers = map(stringnify, headers) |
|
138 | 141 |
table = [map(stringnify, row) for row in table] |
139 | 142 |
|
140 | 143 |
if output_format == "json": |
144 |
assert(headers is not None), "json output format requires headers" |
|
141 | 145 |
table = [dict(zip(headers, row)) for row in table] |
142 | 146 |
out.write(json.dumps(table, indent=4)) |
143 | 147 |
out.write("\n") |
144 | 148 |
elif output_format == "csv": |
149 |
cw = csv.writer(out) |
|
145 | 150 |
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 |
table.insert(0, headers) |
|
152 |
table = map(functools.partial(map, uenc), table) |
|
153 |
cw.writerows(table) |
|
151 | 154 |
elif output_format == "pretty": |
152 | 155 |
# Find out the max width of each column |
153 |
widths = [max(map(len, col)) for col in zip(*([headers] + table))] |
|
156 |
columns = [headers] + table if headers else table |
|
157 |
widths = [max(map(len, col)) for col in zip(*(columns))] |
|
154 | 158 |
|
155 | 159 |
t_length = sum(widths) + len(sep) * (len(widths) - 1) |
156 | 160 |
if headers: |
Also available in: Unified diff