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

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, ...@@ -37,7 +37,7 @@ from optparse import (OptionParser, make_option, TitledHelpFormatter,
Option, OptionValueError, SUPPRESS_HELP) Option, OptionValueError, SUPPRESS_HELP)
__all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain", "SubmitOpCode", __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", "ARGS_NONE", "ARGS_FIXED", "ARGS_ATLEAST", "ARGS_ANY", "ARGS_ONE",
"USEUNITS_OPT", "FIELDS_OPT", "FORCE_OPT"] "USEUNITS_OPT", "FIELDS_OPT", "FORCE_OPT"]
...@@ -280,8 +280,8 @@ def GenericMain(commands): ...@@ -280,8 +280,8 @@ def GenericMain(commands):
return result return result
def OutputTable(headers, fields, separator, data, def GenerateTable(headers, fields, separator, data,
numfields=None, unitfields=None): numfields=None, unitfields=None):
"""Prints a table with headers and different fields. """Prints a table with headers and different fields.
Args: Args:
...@@ -325,6 +325,7 @@ def OutputTable(headers, fields, separator, data, ...@@ -325,6 +325,7 @@ def OutputTable(headers, fields, separator, data,
if separator is None: if separator is None:
mlens[idx] = max(mlens[idx], len(val)) mlens[idx] = max(mlens[idx], len(val))
result = []
if headers: if headers:
args = [] args = []
for idx, name in enumerate(fields): for idx, name in enumerate(fields):
...@@ -333,7 +334,7 @@ def OutputTable(headers, fields, separator, data, ...@@ -333,7 +334,7 @@ def OutputTable(headers, fields, separator, data,
mlens[idx] = max(mlens[idx], len(hdr)) mlens[idx] = max(mlens[idx], len(hdr))
args.append(mlens[idx]) args.append(mlens[idx])
args.append(hdr) args.append(hdr)
logger.ToStdout(format % tuple(args)) result.append(format % tuple(args))
for line in data: for line in data:
args = [] args = []
...@@ -341,4 +342,6 @@ def OutputTable(headers, fields, separator, data, ...@@ -341,4 +342,6 @@ def OutputTable(headers, fields, separator, data,
if separator is None: if separator is None:
args.append(mlens[idx]) args.append(mlens[idx])
args.append(line[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): ...@@ -61,9 +61,12 @@ def ListInstances(opts, args):
numfields = ["admin_ram", "oper_ram"] numfields = ["admin_ram", "oper_ram"]
OutputTable(separator=opts.separator, headers=headers, data = GenerateTable(separator=opts.separator, headers=headers,
fields=selected_fields, unitfields=unitfields, fields=selected_fields, unitfields=unitfields,
numfields=numfields, data=output) numfields=numfields, data=output)
for line in data:
logger.ToStdout(line)
return 0 return 0
......
...@@ -63,9 +63,11 @@ def ListNodes(opts, args): ...@@ -63,9 +63,11 @@ def ListNodes(opts, args):
numfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree", "pinst", "sinst"] numfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree", "pinst", "sinst"]
OutputTable(separator=opts.separator, headers=headers, data = GenerateTable(separator=opts.separator, headers=headers,
fields=selected_fields, unitfields=unitfields, fields=selected_fields, unitfields=unitfields,
numfields=numfields, data=output) numfields=numfields, data=output)
for line in data:
logger.ToStdout(line)
return 0 return 0
...@@ -130,9 +132,12 @@ def ListVolumes(opts, args): ...@@ -130,9 +132,12 @@ def ListVolumes(opts, args):
numfields = ["size"] numfields = ["size"]
OutputTable(separator=opts.separator, headers=headers, data = GenerateTable(separator=opts.separator, headers=headers,
fields=selected_fields, unitfields=unitfields, fields=selected_fields, unitfields=unitfields,
numfields=numfields, data=output) numfields=numfields, data=output)
for line in data:
logger.ToStdout(line)
return 0 return 0
......
...@@ -58,8 +58,11 @@ def ListOS(opts, args): ...@@ -58,8 +58,11 @@ def ListOS(opts, args):
else: else:
headers = None headers = None
OutputTable(separator=None, headers=headers, fields=["name"], data = GenerateTable(separator=None, headers=headers, fields=["name"],
data=[[os] for os in os_set]) data=[[os] for os in os_set])
for line in data:
logger.ToStdout(line)
return 0 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