diff --git a/ceilometerclient/common/utils.py b/ceilometerclient/common/utils.py index 288601f..bc9507b 100644 --- a/ceilometerclient/common/utils.py +++ b/ceilometerclient/common/utils.py @@ -86,23 +86,25 @@ def format_nested_list_of_dict(l, column_names): def print_dict(d, dict_property="Property", wrap=0): pt = prettytable.PrettyTable([dict_property, 'Value'], print_empty=False) pt.align = 'l' - for k, v in six.iteritems(d): + for k, v in sorted(six.iteritems(d)): # convert dict to str to check length if isinstance(v, dict): v = str(v) - if wrap > 0: - v = textwrap.fill(str(v), wrap) # if value has a newline, add in multiple rows # e.g. fault with stacktrace if v and isinstance(v, six.string_types) and r'\n' in v: lines = v.strip().split(r'\n') col1 = k for line in lines: + if wrap > 0: + line = textwrap.fill(str(line), wrap) pt.add_row([col1, line]) col1 = '' else: + if wrap > 0: + v = textwrap.fill(str(v), wrap) pt.add_row([k, v]) - encoded = encodeutils.safe_encode(pt.get_string(sortby=dict_property)) + encoded = encodeutils.safe_encode(pt.get_string()) # FIXME(gordc): https://bugs.launchpad.net/oslo-incubator/+bug/1370710 if six.PY3: encoded = encoded.decode() diff --git a/ceilometerclient/tests/test_utils.py b/ceilometerclient/tests/test_utils.py index ccace89..3d2bac0 100644 --- a/ceilometerclient/tests/test_utils.py +++ b/ceilometerclient/tests/test_utils.py @@ -39,6 +39,38 @@ class UtilsTest(test_utils.BaseTestCase): | K | k | | Key | Value | +----------+-------+ +''', stdout.getvalue()) + + with mock.patch('sys.stdout', new=six.StringIO()) as stdout: + utils.print_dict({'alarm_id': '262567fd-d79a-4bbb-a9d0-59d879b6', + 'description': 'test alarm', + 'state': 'insufficient data', + 'repeat_actions': 'False', + 'type': 'threshold', + 'threshold': '1.0', + 'statistic': 'avg', + 'time_constraints': '[{name: c1,' + '\\n description: test,' + '\\n start: 0 18 * * *,' + '\\n duration: 1,' + '\\n timezone: US}]'}) + self.assertEqual('''\ ++------------------+----------------------------------+ +| Property | Value | ++------------------+----------------------------------+ +| alarm_id | 262567fd-d79a-4bbb-a9d0-59d879b6 | +| description | test alarm | +| repeat_actions | False | +| state | insufficient data | +| statistic | avg | +| threshold | 1.0 | +| time_constraints | [{name: c1, | +| | description: test, | +| | start: 0 18 * * *, | +| | duration: 1, | +| | timezone: US}] | +| type | threshold | ++------------------+----------------------------------+ ''', stdout.getvalue()) def test_print_list(self):