summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhiQiang Fan <zhiqiang.fan@huawei.com>2015-01-19 14:52:08 +0800
committergordon chung <gord@live.ca>2015-04-16 12:58:59 +0000
commit09bcf5c89a437524aa9d02a6b5c2dada605d9d0d (patch)
tree165f5bd9e081c39abd7357763dae289ee6b3000a
parent1b346fb64796cd9aba39ee1e9453223738bd1b30 (diff)
downloadpython-ceilometerclient-09bcf5c89a437524aa9d02a6b5c2dada605d9d0d.tar.gz
Support unicode for alarm
We're using ceilometer.common.utils.print_dict() for displaying alarm, but assume only contain ascii characters, which will cause UnicodeEncodeError when alarm's name or description has unicode characters. The oslo-incubator.cliutils.print_dict() is more friendly to unicode, but it has no sort option, and has different line wrap strategy. Since the upstream improvement can be low progress, this patch adds unicode support based on current code. Change-Id: Ia58d5813c6f80cb8b44bf7636e1efc0cdd6e6d5b Closes-Bug: #1412300 (cherry picked from commit f1f452405a80884c7a9cd78cad4aa4d73e290745)
-rw-r--r--ceilometerclient/common/utils.py4
-rw-r--r--ceilometerclient/tests/test_utils.py17
2 files changed, 14 insertions, 7 deletions
diff --git a/ceilometerclient/common/utils.py b/ceilometerclient/common/utils.py
index 4e77632..b6408cf 100644
--- a/ceilometerclient/common/utils.py
+++ b/ceilometerclient/common/utils.py
@@ -98,12 +98,12 @@ def print_dict(d, dict_property="Property", wrap=0):
col1 = k
for line in lines:
if wrap > 0:
- line = textwrap.fill(str(line), wrap)
+ line = textwrap.fill(six.text_type(line), wrap)
pt.add_row([col1, line])
col1 = ''
else:
if wrap > 0:
- v = textwrap.fill(str(v), wrap)
+ v = textwrap.fill(six.text_type(v), wrap)
pt.add_row([k, v])
encoded = encodeutils.safe_encode(pt.get_string())
# FIXME(gordc): https://bugs.launchpad.net/oslo-incubator/+bug/1370710
diff --git a/ceilometerclient/tests/test_utils.py b/ceilometerclient/tests/test_utils.py
index 6ef1fd1..23235d3 100644
--- a/ceilometerclient/tests/test_utils.py
+++ b/ceilometerclient/tests/test_utils.py
@@ -43,7 +43,8 @@ class UtilsTest(test_utils.BaseTestCase):
with mock.patch('sys.stdout', new=six.StringIO()) as stdout:
utils.print_dict({'alarm_id': '262567fd-d79a-4bbb-a9d0-59d879b6',
- 'description': 'test alarm',
+ 'name': u'\u6d4b\u8bd5',
+ 'description': u'\u6d4b\u8bd5',
'state': 'insufficient data',
'repeat_actions': 'False',
'type': 'threshold',
@@ -53,13 +54,15 @@ class UtilsTest(test_utils.BaseTestCase):
'\\n description: test,'
'\\n start: 0 18 * * *,'
'\\n duration: 1,'
- '\\n timezone: US}]'})
- self.assertEqual('''\
+ '\\n timezone: US}]'},
+ wrap=72)
+ expected = u'''\
+------------------+----------------------------------+
| Property | Value |
+------------------+----------------------------------+
| alarm_id | 262567fd-d79a-4bbb-a9d0-59d879b6 |
-| description | test alarm |
+| description | \u6d4b\u8bd5 |
+| name | \u6d4b\u8bd5 |
| repeat_actions | False |
| state | insufficient data |
| statistic | avg |
@@ -71,7 +74,11 @@ class UtilsTest(test_utils.BaseTestCase):
| | timezone: US}] |
| type | threshold |
+------------------+----------------------------------+
-''', stdout.getvalue())
+'''
+ # py2 prints str type, py3 prints unicode type
+ if six.PY2:
+ expected = expected.encode('utf-8')
+ self.assertEqual(expected, stdout.getvalue())
def test_print_list(self):
class Foo(object):