From 8c4e145b926e9d29a14dbc0e3886239b312475f4 Mon Sep 17 00:00:00 2001 From: Chuck Short Date: Mon, 24 Jun 2013 10:03:19 -0500 Subject: [PATCH] python3: Fix unicode compatibility python2/python3 Python3 enforces the distinction between byte strings and text strings more rigorously than python2. So use six.text_type/six.u() where appropriate Change-Id: I890e19cb857e10f0292aabdaebaa8e7a7bd8db23 Signed-off-by: Chuck Short --- doc/source/conf.py | 12 +- novaclient/shell.py | 7 +- novaclient/tests/v1_1/fakes.py | 156 +++++++++--------- .../tests/v1_1/test_availability_zone.py | 27 +-- novaclient/tests/v1_1/test_servers.py | 3 +- novaclient/utils.py | 5 +- requirements.txt | 1 + 7 files changed, 115 insertions(+), 96 deletions(-) diff --git a/doc/source/conf.py b/doc/source/conf.py index 8aab078bd..4583d15e5 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -39,8 +39,8 @@ source_suffix = '.rst' master_doc = 'index' # General information about the project. -project = u'python-novaclient' -copyright = u'OpenStack Contributors' +project = 'python-novaclient' +copyright = 'OpenStack Contributors' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the @@ -93,8 +93,8 @@ pygments_style = 'sphinx' # List of tuples 'sourcefile', 'target', u'title', u'Authors name', 'manual' man_pages = [ - ('man/nova', 'nova', u'OpenStack Nova command line client', - [u'OpenStack Contributors'], 1), + ('man/nova', 'nova', 'OpenStack Nova command line client', + ['OpenStack Contributors'], 1), ] # -- Options for HTML output -------------------------------------------------- @@ -183,8 +183,8 @@ htmlhelp_basename = 'python-novaclientdoc' # (source start file, target name, title, author, documentclass [howto/manual]) # . latex_documents = [ - ('index', 'python-novaclient.tex', u'python-novaclient Documentation', - u'Rackspace - based on work by Jacob Kaplan-Moss', 'manual'), + ('index', 'python-novaclient.tex', 'python-novaclient Documentation', + 'Rackspace - based on work by Jacob Kaplan-Moss', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of diff --git a/novaclient/shell.py b/novaclient/shell.py index 289df3750..b10d26acf 100644 --- a/novaclient/shell.py +++ b/novaclient/shell.py @@ -26,10 +26,12 @@ import imp import itertools import logging import os -import pkg_resources import pkgutil import sys +import pkg_resources +import six + HAS_KEYRING = False all_errors = ValueError try: @@ -766,7 +768,8 @@ def main(): except Exception as e: logger.debug(e, exc_info=1) - print("ERROR: %s" % strutils.safe_encode(unicode(e)), file=sys.stderr) + print("ERROR: %s" % strutils.safe_encode(six.text_type(e)), + file=sys.stderr) sys.exit(1) diff --git a/novaclient/tests/v1_1/fakes.py b/novaclient/tests/v1_1/fakes.py index cab34e6c3..625f4e512 100644 --- a/novaclient/tests/v1_1/fakes.py +++ b/novaclient/tests/v1_1/fakes.py @@ -17,6 +17,8 @@ from datetime import datetime import urlparse +import six + from novaclient import client as base_client from novaclient.v1_1 import client from novaclient.tests import fakes @@ -1208,87 +1210,93 @@ class FakeHTTPClient(base_client.HTTPClient): # def get_os_simple_tenant_usage(self, **kw): return (200, {}, - {u'tenant_usages': [{ - u'total_memory_mb_usage': 25451.762807466665, - u'total_vcpus_usage': 49.71047423333333, - u'total_hours': 49.71047423333333, - u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', - u'stop': u'2012-01-22 19:48:41.750722', - u'server_usages': [{ - u'hours': 49.71047423333333, - u'uptime': 27035, - u'local_gb': 0, - u'ended_at': None, - u'name': u'f15image1', - u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', - u'vcpus': 1, - u'memory_mb': 512, - u'state': u'active', - u'flavor': u'm1.tiny', - u'started_at': u'2012-01-20 18:06:06.479998'}], - u'start': u'2011-12-25 19:48:41.750687', - u'total_local_gb_usage': 0.0}]}) + {six.u('tenant_usages'): [{ + six.u('total_memory_mb_usage'): 25451.762807466665, + six.u('total_vcpus_usage'): 49.71047423333333, + six.u('total_hours'): 49.71047423333333, + six.u('tenant_id'): + six.u('7b0a1d73f8fb41718f3343c207597869'), + six.u('stop'): six.u('2012-01-22 19:48:41.750722'), + six.u('server_usages'): [{ + six.u('hours'): 49.71047423333333, + six.u('uptime'): 27035, + six.u('local_gb'): 0, + six.u('ended_at'): None, + six.u('name'): six.u('f15image1'), + six.u('tenant_id'): + six.u('7b0a1d73f8fb41718f3343c207597869'), + six.u('vcpus'): 1, + six.u('memory_mb'): 512, + six.u('state'): six.u('active'), + six.u('flavor'): six.u('m1.tiny'), + six.u('started_at'): + six.u('2012-01-20 18:06:06.479998')}], + six.u('start'): six.u('2011-12-25 19:48:41.750687'), + six.u('total_local_gb_usage'): 0.0}]}) def get_os_simple_tenant_usage_tenantfoo(self, **kw): return (200, {}, - {u'tenant_usage': { - u'total_memory_mb_usage': 25451.762807466665, - u'total_vcpus_usage': 49.71047423333333, - u'total_hours': 49.71047423333333, - u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', - u'stop': u'2012-01-22 19:48:41.750722', - u'server_usages': [{ - u'hours': 49.71047423333333, - u'uptime': 27035, u'local_gb': 0, - u'ended_at': None, - u'name': u'f15image1', - u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', - u'vcpus': 1, u'memory_mb': 512, - u'state': u'active', - u'flavor': u'm1.tiny', - u'started_at': u'2012-01-20 18:06:06.479998'}], - u'start': u'2011-12-25 19:48:41.750687', - u'total_local_gb_usage': 0.0}}) + {six.u('tenant_usage'): { + six.u('total_memory_mb_usage'): 25451.762807466665, + six.u('total_vcpus_usage'): 49.71047423333333, + six.u('total_hours'): 49.71047423333333, + six.u('tenant_id'): + six.u('7b0a1d73f8fb41718f3343c207597869'), + six.u('stop'): six.u('2012-01-22 19:48:41.750722'), + six.u('server_usages'): [{ + six.u('hours'): 49.71047423333333, + six.u('uptime'): 27035, six.u('local_gb'): 0, + six.u('ended_at'): None, + six.u('name'): six.u('f15image1'), + six.u('tenant_id'): + six.u('7b0a1d73f8fb41718f3343c207597869'), + six.u('vcpus'): 1, six.u('memory_mb'): 512, + six.u('state'): six.u('active'), + six.u('flavor'): six.u('m1.tiny'), + six.u('started_at'): + six.u('2012-01-20 18:06:06.479998')}], + six.u('start'): six.u('2011-12-25 19:48:41.750687'), + six.u('total_local_gb_usage'): 0.0}}) def get_os_simple_tenant_usage_test(self, **kw): - return (200, {}, {u'tenant_usage': { - u'total_memory_mb_usage': 25451.762807466665, - u'total_vcpus_usage': 49.71047423333333, - u'total_hours': 49.71047423333333, - u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', - u'stop': u'2012-01-22 19:48:41.750722', - u'server_usages': [{ - u'hours': 49.71047423333333, - u'uptime': 27035, u'local_gb': 0, - u'ended_at': None, - u'name': u'f15image1', - u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', - u'vcpus': 1, u'memory_mb': 512, - u'state': u'active', - u'flavor': u'm1.tiny', - u'started_at': u'2012-01-20 18:06:06.479998'}], - u'start': u'2011-12-25 19:48:41.750687', - u'total_local_gb_usage': 0.0}}) + return (200, {}, {six.u('tenant_usage'): { + six.u('total_memory_mb_usage'): 25451.762807466665, + six.u('total_vcpus_usage'): 49.71047423333333, + six.u('total_hours'): 49.71047423333333, + six.u('tenant_id'): six.u('7b0a1d73f8fb41718f3343c207597869'), + six.u('stop'): six.u('2012-01-22 19:48:41.750722'), + six.u('server_usages'): [{ + six.u('hours'): 49.71047423333333, + six.u('uptime'): 27035, six.u('local_gb'): 0, + six.u('ended_at'): None, + six.u('name'): six.u('f15image1'), + six.u('tenant_id'): six.u('7b0a1d73f8fb41718f3343c207597869'), + six.u('vcpus'): 1, six.u('memory_mb'): 512, + six.u('state'): six.u('active'), + six.u('flavor'): six.u('m1.tiny'), + six.u('started_at'): six.u('2012-01-20 18:06:06.479998')}], + six.u('start'): six.u('2011-12-25 19:48:41.750687'), + six.u('total_local_gb_usage'): 0.0}}) def get_os_simple_tenant_usage_tenant_id(self, **kw): - return (200, {}, {u'tenant_usage': { - u'total_memory_mb_usage': 25451.762807466665, - u'total_vcpus_usage': 49.71047423333333, - u'total_hours': 49.71047423333333, - u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', - u'stop': u'2012-01-22 19:48:41.750722', - u'server_usages': [{ - u'hours': 49.71047423333333, - u'uptime': 27035, u'local_gb': 0, - u'ended_at': None, - u'name': u'f15image1', - u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', - u'vcpus': 1, u'memory_mb': 512, - u'state': u'active', - u'flavor': u'm1.tiny', - u'started_at': u'2012-01-20 18:06:06.479998'}], - u'start': u'2011-12-25 19:48:41.750687', - u'total_local_gb_usage': 0.0}}) + return (200, {}, {six.u('tenant_usage'): { + six.u('total_memory_mb_usage'): 25451.762807466665, + six.u('total_vcpus_usage'): 49.71047423333333, + six.u('total_hours'): 49.71047423333333, + six.u('tenant_id'): six.u('7b0a1d73f8fb41718f3343c207597869'), + six.u('stop'): six.u('2012-01-22 19:48:41.750722'), + six.u('server_usages'): [{ + six.u('hours'): 49.71047423333333, + six.u('uptime'): 27035, six.u('local_gb'): 0, + six.u('ended_at'): None, + six.u('name'): six.u('f15image1'), + six.u('tenant_id'): six.u('7b0a1d73f8fb41718f3343c207597869'), + six.u('vcpus'): 1, six.u('memory_mb'): 512, + six.u('state'): six.u('active'), + six.u('flavor'): six.u('m1.tiny'), + six.u('started_at'): six.u('2012-01-20 18:06:06.479998')}], + six.u('start'): six.u('2011-12-25 19:48:41.750687'), + six.u('total_local_gb_usage'): 0.0}}) # # Certificates # diff --git a/novaclient/tests/v1_1/test_availability_zone.py b/novaclient/tests/v1_1/test_availability_zone.py index 896b78ad7..93cb6bebf 100644 --- a/novaclient/tests/v1_1/test_availability_zone.py +++ b/novaclient/tests/v1_1/test_availability_zone.py @@ -14,6 +14,8 @@ # License for the specific language governing permissions and limitations # under the License. +import six + from novaclient.v1_1 import availability_zones from novaclient.v1_1 import shell from novaclient.tests.v1_1 import fakes @@ -39,8 +41,8 @@ class AvailabilityZoneTest(utils.TestCase): self.assertEqual(2, len(zones)) - l0 = [u'zone-1', u'available'] - l1 = [u'zone-2', u'not available'] + l0 = [six.u('zone-1'), six.u('available')] + l1 = [six.u('zone-2'), six.u('not available')] z0 = shell._treeizeAvailabilityZone(zones[0]) z1 = shell._treeizeAvailabilityZone(zones[1]) @@ -60,15 +62,18 @@ class AvailabilityZoneTest(utils.TestCase): self.assertEqual(3, len(zones)) - l0 = [u'zone-1', u'available'] - l1 = [u'|- fake_host-1', u''] - l2 = [u'| |- nova-compute', u'enabled :-) 2012-12-26 14:45:25'] - l3 = [u'internal', u'available'] - l4 = [u'|- fake_host-1', u''] - l5 = [u'| |- nova-sched', u'enabled :-) 2012-12-26 14:45:25'] - l6 = [u'|- fake_host-2', u''] - l7 = [u'| |- nova-network', u'enabled XXX 2012-12-26 14:45:24'] - l8 = [u'zone-2', u'not available'] + l0 = [six.u('zone-1'), six.u('available')] + l1 = [six.u('|- fake_host-1'), six.u('')] + l2 = [six.u('| |- nova-compute'), + six.u('enabled :-) 2012-12-26 14:45:25')] + l3 = [six.u('internal'), six.u('available')] + l4 = [six.u('|- fake_host-1'), six.u('')] + l5 = [six.u('| |- nova-sched'), + six.u('enabled :-) 2012-12-26 14:45:25')] + l6 = [six.u('|- fake_host-2'), six.u('')] + l7 = [six.u('| |- nova-network'), + six.u('enabled XXX 2012-12-26 14:45:24')] + l8 = [six.u('zone-2'), six.u('not available')] z0 = shell._treeizeAvailabilityZone(zones[0]) z1 = shell._treeizeAvailabilityZone(zones[1]) diff --git a/novaclient/tests/v1_1/test_servers.py b/novaclient/tests/v1_1/test_servers.py index 5874a006c..04db70529 100644 --- a/novaclient/tests/v1_1/test_servers.py +++ b/novaclient/tests/v1_1/test_servers.py @@ -3,6 +3,7 @@ import StringIO import mock +import six from novaclient import exceptions from novaclient.v1_1 import servers @@ -108,7 +109,7 @@ class ServersTest(utils.TestCase): image=1, flavor=1, meta={'foo': 'bar'}, - userdata=u'こんにちは', + userdata=six.u('こんにちは'), key_name="fakekey", files={ '/etc/passwd': 'some data', # a file diff --git a/novaclient/utils.py b/novaclient/utils.py index d1e08e961..729dcf2ab 100644 --- a/novaclient/utils.py +++ b/novaclient/utils.py @@ -6,6 +6,7 @@ import textwrap import uuid import prettytable +import six from novaclient import exceptions from novaclient.openstack.common import strutils @@ -339,9 +340,9 @@ def slugify(value): """ import unicodedata if not isinstance(value, unicode): - value = unicode(value) + value = six.text_type(value) value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') - value = unicode(_slugify_strip_re.sub('', value).strip().lower()) + value = six.text_type(_slugify_strip_re.sub('', value).strip().lower()) return _slugify_hyphenate_re.sub('-', value) diff --git a/requirements.txt b/requirements.txt index f7deffed1..3390adde9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ iso8601>=0.1.4 prettytable>=0.6,<0.8 requests>=0.8 simplejson +six