change the dict output format to make consistency under py27 and py34

For the command output in ceilometer cli, if it is a dict and it has
items which is list type, originally it won't handle the items in the
list.

As to under py27, the string type in the list is unicode, the string
always look like u"http://alarm", while under py34 the string is
"http://alarm", this cause output inconsistency. This patch will handle
this inconsistency.

Change-Id: I5e7cf4052f68e5a434bcf86242b11365b34750a0
This commit is contained in:
xialinjuan
2016-01-13 17:31:10 +08:00
parent 25517d0226
commit d6560fb498
2 changed files with 49 additions and 18 deletions

View File

@@ -89,7 +89,7 @@ def print_dict(d, dict_property="Property", wrap=0):
pt.align = 'l'
for k, v in sorted(six.iteritems(d)):
# convert dict to str to check length
if isinstance(v, dict):
if isinstance(v, (list, dict)):
v = jsonutils.dumps(v)
# if value has a newline, add in multiple rows
# e.g. fault with stacktrace

View File

@@ -50,6 +50,14 @@ class UtilsTest(test_utils.BaseTestCase):
'type': 'threshold',
'threshold': '1.0',
'statistic': 'avg',
'alarm_actions': [u'http://something/alarm1',
u'http://something/alarm2'],
'ok_actions': [{"get_attr1":
[u"web_server_scaleup_policy1",
u"alarm_url1"]},
{"get_attr2":
[u"web_server_scaleup_policy2",
u"alarm_url2"]}],
'time_constraints': '[{name: c1,'
'\\n description: test,'
'\\n start: 0 18 * * *,'
@@ -57,23 +65,46 @@ class UtilsTest(test_utils.BaseTestCase):
'\\n timezone: US}]'},
wrap=72)
expected = u'''\
+------------------+----------------------------------+
| Property | Value |
+------------------+----------------------------------+
| alarm_id | 262567fd-d79a-4bbb-a9d0-59d879b6 |
| description | \u6d4b\u8bd5 |
| name | \u6d4b\u8bd5 |
| 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 |
+------------------+----------------------------------+
+------------------+-------------------------------------------------------\
--------+
| Property | Value \
|
+------------------+-------------------------------------------------------\
--------+
| alarm_actions | ["http://something/alarm1", "http://something/alarm2"]\
|
| alarm_id | 262567fd-d79a-4bbb-a9d0-59d879b6 \
|
| description | \u6d4b\u8bd5 \
|
| name | \u6d4b\u8bd5 \
|
| ok_actions | [{"get_attr1": ["web_server_scaleup_policy1", "alarm_u\
rl1"]}, |
| | {"get_attr2": ["web_server_scaleup_policy2", "alarm_ur\
l2"]}] |
| 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 \
|
+------------------+-------------------------------------------------------\
--------+
'''
# py2 prints str type, py3 prints unicode type
if six.PY2: