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
This commit is contained in:
Cao ShuFeng 2016-07-27 10:13:36 -04:00 committed by Michael Dovgal
parent 702988b8b9
commit 64c8f74a7c
4 changed files with 27 additions and 10 deletions

View File

@ -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):

View File

@ -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",

View File

@ -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):

View File

@ -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):