Revision 137161c9 lib/cli.py
b/lib/cli.py | ||
---|---|---|
37 | 37 |
Option, OptionValueError, SUPPRESS_HELP) |
38 | 38 |
|
39 | 39 |
__all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain", "SubmitOpCode", |
40 |
"cli_option", |
|
40 |
"cli_option", "OutputTable",
|
|
41 | 41 |
"ARGS_NONE", "ARGS_FIXED", "ARGS_ATLEAST", "ARGS_ANY", "ARGS_ONE", |
42 | 42 |
"USEUNITS_OPT", "FIELDS_OPT"] |
43 | 43 |
|
... | ... | |
49 | 49 |
action="store_true", dest="no_headers", |
50 | 50 |
help="Don't display column headers") |
51 | 51 |
|
52 |
SEP_OPT = make_option("--separator", default=" ",
|
|
52 |
SEP_OPT = make_option("--separator", default=None,
|
|
53 | 53 |
action="store", dest="separator", |
54 | 54 |
help="Separator between output fields" |
55 | 55 |
" (defaults to one space)") |
... | ... | |
274 | 274 |
utils.LockCleanup() |
275 | 275 |
|
276 | 276 |
return result |
277 |
|
|
278 |
|
|
279 |
def OutputTable(headers, fields, separator, data, |
|
280 |
numfields=None, unitfields=None): |
|
281 |
"""Prints a table with headers and different fields. |
|
282 |
|
|
283 |
Args: |
|
284 |
headers: Dict of header titles or None if no headers should be shown |
|
285 |
fields: List of fields to show |
|
286 |
separator: String used to separate fields or None for spaces |
|
287 |
data: Data to be printed |
|
288 |
numfields: List of fields to be aligned to right |
|
289 |
unitfields: List of fields to be formatted as units |
|
290 |
|
|
291 |
""" |
|
292 |
if numfields is None: |
|
293 |
numfields = [] |
|
294 |
if unitfields is None: |
|
295 |
unitfields = [] |
|
296 |
|
|
297 |
format_fields = [] |
|
298 |
for field in fields: |
|
299 |
if separator is not None: |
|
300 |
format_fields.append("%s") |
|
301 |
elif field in numfields: |
|
302 |
format_fields.append("%*s") |
|
303 |
else: |
|
304 |
format_fields.append("%-*s") |
|
305 |
|
|
306 |
if separator is None: |
|
307 |
mlens = [0 for name in fields] |
|
308 |
format = ' '.join(format_fields) |
|
309 |
else: |
|
310 |
format = separator.replace("%", "%%").join(format_fields) |
|
311 |
|
|
312 |
for row in data: |
|
313 |
for idx, val in enumerate(row): |
|
314 |
if fields[idx] in unitfields: |
|
315 |
try: |
|
316 |
val = int(val) |
|
317 |
except ValueError: |
|
318 |
pass |
|
319 |
else: |
|
320 |
val = row[idx] = utils.FormatUnit(val) |
|
321 |
if separator is None: |
|
322 |
mlens[idx] = max(mlens[idx], len(val)) |
|
323 |
|
|
324 |
if headers: |
|
325 |
args = [] |
|
326 |
for idx, name in enumerate(fields): |
|
327 |
hdr = headers[name] |
|
328 |
if separator is None: |
|
329 |
mlens[idx] = max(mlens[idx], len(hdr)) |
|
330 |
args.append(mlens[idx]) |
|
331 |
args.append(hdr) |
|
332 |
logger.ToStdout(format % tuple(args)) |
|
333 |
|
|
334 |
for line in data: |
|
335 |
args = [] |
|
336 |
for idx in xrange(len(fields)): |
|
337 |
if separator is None: |
|
338 |
args.append(mlens[idx]) |
|
339 |
args.append(line[idx]) |
|
340 |
logger.ToStdout(format % tuple(args)) |
Also available in: Unified diff