Merge "Removes unicode 'u' response from "glance image-tag-update""
This commit is contained in:
commit
1ef5e5d169
|
@ -177,6 +177,12 @@ def pretty_choice_list(l):
|
|||
|
||||
|
||||
def print_list(objs, fields, formatters=None, field_settings=None):
|
||||
'''Prints a list of objects.
|
||||
|
||||
@param objs: Objects to print
|
||||
@param fields: Fields on each object to be printed
|
||||
@param formatters: Custom field formatters
|
||||
'''
|
||||
formatters = formatters or {}
|
||||
field_settings = field_settings or {}
|
||||
pt = prettytable.PrettyTable([f for f in fields], caching=False)
|
||||
|
@ -196,11 +202,36 @@ def print_list(objs, fields, formatters=None, field_settings=None):
|
|||
field_name = field.lower().replace(' ', '_')
|
||||
data = getattr(o, field_name, None) or ''
|
||||
row.append(data)
|
||||
count = 0
|
||||
# Converts unicode values in list to string
|
||||
for part in row:
|
||||
count = count + 1
|
||||
if isinstance(part, list):
|
||||
part = unicode_key_value_to_string(part)
|
||||
row[count - 1] = part
|
||||
pt.add_row(row)
|
||||
|
||||
print(encodeutils.safe_decode(pt.get_string()))
|
||||
|
||||
|
||||
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 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 print_dict(d, max_column_width=80):
|
||||
pt = prettytable.PrettyTable(['Property', 'Value'], caching=False)
|
||||
pt.align = 'l'
|
||||
|
|
|
@ -115,6 +115,42 @@ class TestUtils(testtools.TestCase):
|
|||
''',
|
||||
output_dict.getvalue())
|
||||
|
||||
def test_print_list_with_list_no_unicode(self):
|
||||
class Struct(object):
|
||||
def __init__(self, **entries):
|
||||
self.__dict__.update(entries)
|
||||
|
||||
# test for removing 'u' from lists in print_list output
|
||||
columns = ['ID', 'Tags']
|
||||
images = [Struct(**{'id': 'b8e1c57e-907a-4239-aed8-0df8e54b8d2d',
|
||||
'tags': [u'Name1', u'Tag_123', u'veeeery long']})]
|
||||
saved_stdout = sys.stdout
|
||||
try:
|
||||
sys.stdout = output_list = six.StringIO()
|
||||
utils.print_list(images, columns)
|
||||
|
||||
finally:
|
||||
sys.stdout = saved_stdout
|
||||
|
||||
self.assertEqual('''\
|
||||
+--------------------------------------+--------------------------------------+
|
||||
| ID | Tags |
|
||||
+--------------------------------------+--------------------------------------+
|
||||
| b8e1c57e-907a-4239-aed8-0df8e54b8d2d | ['Name1', 'Tag_123', 'veeeery long'] |
|
||||
+--------------------------------------+--------------------------------------+
|
||||
''',
|
||||
output_list.getvalue())
|
||||
|
||||
def test_unicode_key_value_to_string(self):
|
||||
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))
|
||||
|
||||
def test_schema_args_with_list_types(self):
|
||||
# NOTE(flaper87): Regression for bug
|
||||
# https://bugs.launchpad.net/python-glanceclient/+bug/1401032
|
||||
|
|
Loading…
Reference in New Issue