Pretty print 'extra_specs' and 'group_specs'

Now we have 'extra_specs', 'group_specs' pretty
printed.

+-------------+---------------+
| other_key   | value         |
| extra_specs | key1 : value1 |
|             | key2 : value2 |
+-------------+---------------+

Change-Id: I3500f148f29bad89aacc89ad12a993edf4f7d460
This commit is contained in:
TommyLike 2017-05-12 15:35:20 +08:00
parent cd12a8945d
commit 16af9f72b6
2 changed files with 33 additions and 1 deletions

View File

@ -294,6 +294,12 @@ class PrintListTestCase(test_utils.TestCase):
class PrintDictTestCase(test_utils.TestCase):
def test__pretty_format_dict(self):
content = {'key1': 'value1', 'key2': 'value2'}
expected = "key1 : value1\nkey2 : value2"
result = utils._pretty_format_dict(content)
self.assertEqual(expected, result)
def test_print_dict_with_return(self):
d = {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'test\rcarriage\n\rreturn'}
with CaptureStdout() as cso:
@ -308,4 +314,20 @@ class PrintDictTestCase(test_utils.TestCase):
| d | test carriage |
| | return |
+----------+---------------+
""", cso.read())
def test_print_dict_with_dict_inside(self):
content = {'a': 'A', 'b': 'B', 'f_key':
{'key1': 'value1', 'key2': 'value2'}}
with CaptureStdout() as cso:
utils.print_dict(content, formatters='f_key')
self.assertEqual("""\
+----------+---------------+
| Property | Value |
+----------+---------------+
| a | A |
| b | B |
| f_key | key1 : value1 |
| | key2 : value2 |
+----------+---------------+
""", cso.read())

View File

@ -193,6 +193,15 @@ def build_query_param(params, sort=False):
return query_string
def _pretty_format_dict(data_dict):
formatted_data = []
for k in sorted(data_dict):
formatted_data.append("%s : %s" % (k, data_dict[k]))
return "\n".join(formatted_data)
def print_dict(d, property="Property", formatters=None):
pt = prettytable.PrettyTable([property, 'Value'], caching=False)
pt.align = 'l'
@ -203,7 +212,8 @@ def print_dict(d, property="Property", formatters=None):
if r[0] in formatters:
r[1] = unicode_key_value_to_string(r[1])
if isinstance(r[1], dict):
r[1] = _pretty_format_dict(r[1])
if isinstance(r[1], six.string_types) and "\r" in r[1]:
r[1] = r[1].replace("\r", " ")
pt.add_row(r)