Skip to content

Commit ba1410e

Browse files
authored
Better handles unicode in slcli for raw output (softlayer#740)
* Better handles unicode in slcli for raw output Closes softlayer#739. * Added a test case that will return 'invalid' instead of failing outright Also fixes a couple new style issues * Make the unicode test just for python 2
1 parent 10600bf commit ba1410e

7 files changed

Lines changed: 34 additions & 11 deletions

File tree

SoftLayer/CLI/environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def list_commands(self, *path):
6969
path_str = ':'.join(path)
7070

7171
commands = []
72-
for command in self.commands.keys():
72+
for command in self.commands:
7373

7474
# Filter based on prefix and the segment length
7575
if all([command.startswith(path_str),

SoftLayer/CLI/formatting.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,14 @@ def __str__(self):
323323
# If the original value is None, represent this as 'NULL'
324324
if self.original is None:
325325
return 'NULL'
326-
return str(self.original)
327326

328-
__repr__ = __str__
327+
try:
328+
return str(self.original)
329+
except UnicodeError:
330+
return 'invalid'
331+
332+
def __repr__(self):
333+
return 'FormattedItem(%r, %r)' % (self.original, self.formatted)
329334

330335
# Implement sorting methods.
331336
# NOTE(kmcdonald): functools.total_ordering could be used once support for

SoftLayer/CLI/virt/create_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def add_block_rows(disks, name):
9090

9191
simple[bid].append(str(block['diskImage']['capacity']))
9292

93-
for label in sorted(simple.keys()):
93+
for label in sorted(simple):
9494
table.add_row(['%s disk(%s)' % (name, label),
9595
formatting.listing(simple[label],
9696
separator=',')])

SoftLayer/fixtures/SoftLayer_Account.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1+
# -*- coding: UTF-8 -*-
2+
13
getPrivateBlockDeviceTemplateGroups = [{
24
'accountId': 1234,
35
'blockDevices': [],
46
'createDate': '2013-12-05T21:53:03-06:00',
57
'globalIdentifier': 'E6DBD73B-1651-4B28-BCBA-A11DF7C9D79E',
68
'id': 200,
79
'name': 'test_image',
8-
'parentId': ''
10+
'parentId': '',
11+
'publicFlag': False,
912
}, {
1013
'accountId': 1234,
1114
'blockDevices': [],
1215
'createDate': '2013-12-05T21:53:03-06:00',
1316
'globalIdentifier': 'F9329795-4220-4B0A-B970-C86B950667FA',
1417
'id': 201,
15-
'name': 'private_image2',
16-
'parentId': ''
18+
# 'name': 'private_image2',
19+
'name': u'a¬ሴ€耀',
20+
'parentId': '',
21+
'publicFlag': False,
1722
}]
1823

1924
getVirtualGuests = [{

SoftLayer/fixtures/SoftLayer_Virtual_Guest_Block_Device_Template_Group.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
'globalIdentifier': '0B5DEAF4-643D-46CA-A695-CECBE8832C9D',
66
'id': 100,
77
'name': 'test_image',
8-
'parentId': ''
8+
'parentId': '',
9+
'publicFlag': True,
910
}, {
1011
'accountId': 1234,
1112
'blockDevices': [],
1213
'createDate': '2013-12-05T21:53:03-06:00',
1314
'globalIdentifier': 'EB38414C-2AB3-47F3-BBBD-56A5F689620B',
1415
'id': 101,
1516
'name': 'test_image2',
16-
'parentId': ''
17+
'parentId': '',
18+
'publicFlag': True,
1719
}]
1820

1921
getObject = IMAGES[0]

SoftLayer/testing/xmlrpc.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ def do_POST(self):
5959
methodresponse=True)
6060

6161
self.send_response(200)
62-
self.send_header("Content-type", "application/xml")
62+
self.send_header("Content-type", "application/xml; charset=UTF-8")
6363
self.end_headers()
64-
self.wfile.write(response_body.encode('utf-8'))
64+
try:
65+
self.wfile.write(response_body.encode('utf-8'))
66+
except UnicodeDecodeError:
67+
self.wfile.write(response_body)
6568

6669
except SoftLayer.SoftLayerAPIError as ex:
6770
self.send_response(200)

tests/CLI/helper_tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import click
1313
import mock
14+
import six
1415

1516
from SoftLayer.CLI import core
1617
from SoftLayer.CLI import exceptions
@@ -95,6 +96,13 @@ def test_init(self):
9596
self.assertEqual('test', item.formatted)
9697
self.assertEqual('test', str(item))
9798

99+
def test_unicode(self):
100+
if six.PY2:
101+
item = formatting.FormattedItem(u'\u32423', u'\u32423')
102+
self.assertEqual(u'\u32423', item.original)
103+
self.assertEqual(u'\u32423', item.formatted)
104+
self.assertEqual('invalid', str(item))
105+
98106
def test_mb_to_gb(self):
99107
item = formatting.mb_to_gb(1024)
100108
self.assertEqual(1024, item.original)

0 commit comments

Comments
 (0)