Commit 16be8703 authored by Iustin Pop's avatar Iustin Pop
Browse files

Change cli.OutputTable to cli.GenerateTable

Since the table generation might be useful elsewhere, let's change it to
return the data instead of directly printing it.

Its callers have also been updated.

Reviewed-by: imsnah
parent 1005b0c1
......@@ -37,7 +37,7 @@ from optparse import (OptionParser, make_option, TitledHelpFormatter,
Option, OptionValueError, SUPPRESS_HELP)
__all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain", "SubmitOpCode",
"cli_option", "OutputTable",
"cli_option", "GenerateTable",
"ARGS_NONE", "ARGS_FIXED", "ARGS_ATLEAST", "ARGS_ANY", "ARGS_ONE",
"USEUNITS_OPT", "FIELDS_OPT", "FORCE_OPT"]
......@@ -280,8 +280,8 @@ def GenericMain(commands):
return result
def OutputTable(headers, fields, separator, data,
numfields=None, unitfields=None):
def GenerateTable(headers, fields, separator, data,
numfields=None, unitfields=None):
"""Prints a table with headers and different fields.
Args:
......@@ -325,6 +325,7 @@ def OutputTable(headers, fields, separator, data,
if separator is None:
mlens[idx] = max(mlens[idx], len(val))
result = []
if headers:
args = []
for idx, name in enumerate(fields):
......@@ -333,7 +334,7 @@ def OutputTable(headers, fields, separator, data,
mlens[idx] = max(mlens[idx], len(hdr))
args.append(mlens[idx])
args.append(hdr)
logger.ToStdout(format % tuple(args))
result.append(format % tuple(args))
for line in data:
args = []
......@@ -341,4 +342,6 @@ def OutputTable(headers, fields, separator, data,
if separator is None:
args.append(mlens[idx])
args.append(line[idx])
logger.ToStdout(format % tuple(args))
result.append(format % tuple(args))
return result
......@@ -61,9 +61,12 @@ def ListInstances(opts, args):
numfields = ["admin_ram", "oper_ram"]
OutputTable(separator=opts.separator, headers=headers,
fields=selected_fields, unitfields=unitfields,
numfields=numfields, data=output)
data = GenerateTable(separator=opts.separator, headers=headers,
fields=selected_fields, unitfields=unitfields,
numfields=numfields, data=output)
for line in data:
logger.ToStdout(line)
return 0
......
......@@ -63,9 +63,11 @@ def ListNodes(opts, args):
numfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree", "pinst", "sinst"]
OutputTable(separator=opts.separator, headers=headers,
fields=selected_fields, unitfields=unitfields,
numfields=numfields, data=output)
data = GenerateTable(separator=opts.separator, headers=headers,
fields=selected_fields, unitfields=unitfields,
numfields=numfields, data=output)
for line in data:
logger.ToStdout(line)
return 0
......@@ -130,9 +132,12 @@ def ListVolumes(opts, args):
numfields = ["size"]
OutputTable(separator=opts.separator, headers=headers,
fields=selected_fields, unitfields=unitfields,
numfields=numfields, data=output)
data = GenerateTable(separator=opts.separator, headers=headers,
fields=selected_fields, unitfields=unitfields,
numfields=numfields, data=output)
for line in data:
logger.ToStdout(line)
return 0
......
......@@ -58,8 +58,11 @@ def ListOS(opts, args):
else:
headers = None
OutputTable(separator=None, headers=headers, fields=["name"],
data=[[os] for os in os_set])
data = GenerateTable(separator=None, headers=headers, fields=["name"],
data=[[os] for os in os_set])
for line in data:
logger.ToStdout(line)
return 0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment