diff options
author | Matt Riedemann <mriedem@us.ibm.com> | 2015-10-07 12:31:18 -0700 |
---|---|---|
committer | Matt Riedemann <mriedem@us.ibm.com> | 2015-10-07 12:46:29 -0700 |
commit | 075379920e481949a6ff2c4a8b3a9dbeb080ce56 (patch) | |
tree | 3ad0d8351a9eba7516abed9e171f7ab3a5ef3683 | |
parent | e5fdc5420061faed8c0b709d1746e212cee500e4 (diff) | |
download | oslo-incubator-075379920e481949a6ff2c4a8b3a9dbeb080ce56.tar.gz |
Allow specifying a table header for the value column in print_dict
If you can provide a customer header label for the property (first)
column in the print_dict output you should also be able to provide a
customer header label for the value (second) column.
Partial-Bug: #1503841
Change-Id: Iaa96b80249389fdf12ffbaf84073debebca4b5a4
-rw-r--r-- | openstack/common/cliutils.py | 5 | ||||
-rw-r--r-- | tests/unit/test_cliutils.py | 18 |
2 files changed, 21 insertions, 2 deletions
diff --git a/openstack/common/cliutils.py b/openstack/common/cliutils.py index 96b4fedc..de57f02c 100644 --- a/openstack/common/cliutils.py +++ b/openstack/common/cliutils.py @@ -186,14 +186,15 @@ def print_list(objs, fields, formatters=None, sortby_index=0, print(encodeutils.safe_encode(pt.get_string(**kwargs))) -def print_dict(dct, dict_property="Property", wrap=0): +def print_dict(dct, dict_property="Property", wrap=0, dict_value='Value'): """Print a `dict` as a table of two columns. :param dct: `dict` to print :param dict_property: name of the first column :param wrap: wrapping for the second column + :param dict_value: header label for the value (second) column """ - pt = prettytable.PrettyTable([dict_property, 'Value']) + pt = prettytable.PrettyTable([dict_property, dict_value]) pt.align = 'l' for k, v in six.iteritems(dct): # convert dict to str to check length diff --git a/tests/unit/test_cliutils.py b/tests/unit/test_cliutils.py index 14acb95b..5a1b8a9e 100644 --- a/tests/unit/test_cliutils.py +++ b/tests/unit/test_cliutils.py @@ -591,6 +591,24 @@ class PrintResultStringTestCase(test_base.BaseTestCase): ''' self.assertEqual(expected, out) + def test_print_dict_string_custom_headers(self): + orig = sys.stdout + sys.stdout = six.StringIO() + cliutils.print_dict({"K": "k", "Key": "Value"}, dict_property='Foo', + dict_value='Bar') + out = sys.stdout.getvalue() + sys.stdout.close() + sys.stdout = orig + expected = '''\ ++-----+-------+ +| Foo | Bar | ++-----+-------+ +| K | k | +| Key | Value | ++-----+-------+ +''' + self.assertEqual(expected, out) + class DecoratorsTestCase(test_base.BaseTestCase): |