From 64c8f74a7c238694d473884682a763f6a8a5b02b Mon Sep 17 00:00:00 2001 From: Cao ShuFeng Date: Wed, 27 Jul 2016 10:13:36 -0400 Subject: [PATCH] Remove extra 'u' from cli output The unicode_key_value_to_string() function is designed to remove extra 'u' in cinderclient cli output. However this patch[1] bring the extra 'u' back. Let's remove the extra 'u' again. Closes-bug: #1615921 Closes-bug: #1606904 [1] https://review.openstack.org/#/c/342734/ Change-Id: I26f0ad7149f57e935953c2398ba90b7b3585e201 --- cinderclient/tests/unit/test_utils.py | 11 +++++++++-- cinderclient/tests/unit/v2/fakes.py | 2 +- cinderclient/utils.py | 21 +++++++++++++++------ cinderclient/v3/shell.py | 3 ++- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/cinderclient/tests/unit/test_utils.py b/cinderclient/tests/unit/test_utils.py index 0c6df4b..08b4d50 100644 --- a/cinderclient/tests/unit/test_utils.py +++ b/cinderclient/tests/unit/test_utils.py @@ -16,6 +16,7 @@ import sys import mock from six import moves +import six from cinderclient import api_versions from cinderclient.apiclient import base as common_base @@ -281,8 +282,14 @@ class PrintListTestCase(test_utils.TestCase): """, cso.read()) def test_unicode_key_value_to_string(self): - expected = {u'key': u'\u043f\u043f\u043f\u043f\u043f'} - self.assertEqual(expected, utils.unicode_key_value_to_string(expected)) + src = {u'key': u'\u70fd\u7231\u5a77'} + expected = {'key': '\xe7\x83\xbd\xe7\x88\xb1\xe5\xa9\xb7'} + if six.PY2: + self.assertEqual(expected, utils.unicode_key_value_to_string(src)) + else: + # u'xxxx' in PY3 is str, we will not get extra 'u' from cli + # output in PY3 + self.assertEqual(src, utils.unicode_key_value_to_string(src)) class PrintDictTestCase(test_utils.TestCase): diff --git a/cinderclient/tests/unit/v2/fakes.py b/cinderclient/tests/unit/v2/fakes.py index d2bcae4..4574172 100644 --- a/cinderclient/tests/unit/v2/fakes.py +++ b/cinderclient/tests/unit/v2/fakes.py @@ -28,7 +28,7 @@ REQUEST_ID = 'req-test-request-id' def _stub_volume(*args, **kwargs): volume = { "migration_status": None, - "attachments": [{'server_id': 1234}], + "attachments": [{u'server_id': u'1234'}], "links": [ { "href": "http://localhost/v2/fake/volumes/1234", diff --git a/cinderclient/utils.py b/cinderclient/utils.py index 78adbd9..c5341c0 100644 --- a/cinderclient/utils.py +++ b/cinderclient/utils.py @@ -179,13 +179,22 @@ def print_list(objs, fields, exclude_unavailable=False, formatters=None, _print(pt, order_by) -def unicode_key_value_to_string(dictionary): +def _encode(src): + """remove extra 'u' in PY2.""" + if six.PY2 and isinstance(src, unicode): + return src.encode('utf-8') + return src + + +def unicode_key_value_to_string(src): """Recursively converts dictionary keys to strings.""" - if not isinstance(dictionary, dict): - return dictionary - return dict((six.text_type(k), - six.text_type(unicode_key_value_to_string(v))) - for k, v in dictionary.items()) + if isinstance(src, dict): + return dict((_encode(k), + _encode(unicode_key_value_to_string(v))) + for k, v in src.items()) + if isinstance(src, list): + return [unicode_key_value_to_string(l) for l in src] + return _encode(src) def build_query_param(params, sort=False): diff --git a/cinderclient/v3/shell.py b/cinderclient/v3/shell.py index 33b3426..b35fecc 100644 --- a/cinderclient/v3/shell.py +++ b/cinderclient/v3/shell.py @@ -332,7 +332,8 @@ def do_show(cs, args): info.pop('links', None) utils.print_dict(info, - formatters=['metadata', 'volume_image_metadata']) + formatters=['metadata', 'volume_image_metadata', + 'attachments']) class CheckSizeArgForCreate(argparse.Action):