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 <chuck.short@canonical.com>
This commit is contained in:
Chuck Short 2013-06-24 10:03:19 -05:00
parent 05ca996e67
commit 8c4e145b92
7 changed files with 115 additions and 96 deletions

@ -39,8 +39,8 @@ source_suffix = '.rst'
master_doc = 'index' master_doc = 'index'
# General information about the project. # General information about the project.
project = u'python-novaclient' project = 'python-novaclient'
copyright = u'OpenStack Contributors' copyright = 'OpenStack Contributors'
# The version info for the project you're documenting, acts as replacement for # The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the # |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' # List of tuples 'sourcefile', 'target', u'title', u'Authors name', 'manual'
man_pages = [ man_pages = [
('man/nova', 'nova', u'OpenStack Nova command line client', ('man/nova', 'nova', 'OpenStack Nova command line client',
[u'OpenStack Contributors'], 1), ['OpenStack Contributors'], 1),
] ]
# -- Options for HTML output -------------------------------------------------- # -- Options for HTML output --------------------------------------------------
@ -183,8 +183,8 @@ htmlhelp_basename = 'python-novaclientdoc'
# (source start file, target name, title, author, documentclass [howto/manual]) # (source start file, target name, title, author, documentclass [howto/manual])
# . # .
latex_documents = [ latex_documents = [
('index', 'python-novaclient.tex', u'python-novaclient Documentation', ('index', 'python-novaclient.tex', 'python-novaclient Documentation',
u'Rackspace - based on work by Jacob Kaplan-Moss', 'manual'), '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 # The name of an image file (relative to this directory) to place at the top of

@ -26,10 +26,12 @@ import imp
import itertools import itertools
import logging import logging
import os import os
import pkg_resources
import pkgutil import pkgutil
import sys import sys
import pkg_resources
import six
HAS_KEYRING = False HAS_KEYRING = False
all_errors = ValueError all_errors = ValueError
try: try:
@ -766,7 +768,8 @@ def main():
except Exception as e: except Exception as e:
logger.debug(e, exc_info=1) 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) sys.exit(1)

@ -17,6 +17,8 @@
from datetime import datetime from datetime import datetime
import urlparse import urlparse
import six
from novaclient import client as base_client from novaclient import client as base_client
from novaclient.v1_1 import client from novaclient.v1_1 import client
from novaclient.tests import fakes from novaclient.tests import fakes
@ -1208,87 +1210,93 @@ class FakeHTTPClient(base_client.HTTPClient):
# #
def get_os_simple_tenant_usage(self, **kw): def get_os_simple_tenant_usage(self, **kw):
return (200, {}, return (200, {},
{u'tenant_usages': [{ {six.u('tenant_usages'): [{
u'total_memory_mb_usage': 25451.762807466665, six.u('total_memory_mb_usage'): 25451.762807466665,
u'total_vcpus_usage': 49.71047423333333, six.u('total_vcpus_usage'): 49.71047423333333,
u'total_hours': 49.71047423333333, six.u('total_hours'): 49.71047423333333,
u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', six.u('tenant_id'):
u'stop': u'2012-01-22 19:48:41.750722', six.u('7b0a1d73f8fb41718f3343c207597869'),
u'server_usages': [{ six.u('stop'): six.u('2012-01-22 19:48:41.750722'),
u'hours': 49.71047423333333, six.u('server_usages'): [{
u'uptime': 27035, six.u('hours'): 49.71047423333333,
u'local_gb': 0, six.u('uptime'): 27035,
u'ended_at': None, six.u('local_gb'): 0,
u'name': u'f15image1', six.u('ended_at'): None,
u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', six.u('name'): six.u('f15image1'),
u'vcpus': 1, six.u('tenant_id'):
u'memory_mb': 512, six.u('7b0a1d73f8fb41718f3343c207597869'),
u'state': u'active', six.u('vcpus'): 1,
u'flavor': u'm1.tiny', six.u('memory_mb'): 512,
u'started_at': u'2012-01-20 18:06:06.479998'}], six.u('state'): six.u('active'),
u'start': u'2011-12-25 19:48:41.750687', six.u('flavor'): six.u('m1.tiny'),
u'total_local_gb_usage': 0.0}]}) 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): def get_os_simple_tenant_usage_tenantfoo(self, **kw):
return (200, {}, return (200, {},
{u'tenant_usage': { {six.u('tenant_usage'): {
u'total_memory_mb_usage': 25451.762807466665, six.u('total_memory_mb_usage'): 25451.762807466665,
u'total_vcpus_usage': 49.71047423333333, six.u('total_vcpus_usage'): 49.71047423333333,
u'total_hours': 49.71047423333333, six.u('total_hours'): 49.71047423333333,
u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', six.u('tenant_id'):
u'stop': u'2012-01-22 19:48:41.750722', six.u('7b0a1d73f8fb41718f3343c207597869'),
u'server_usages': [{ six.u('stop'): six.u('2012-01-22 19:48:41.750722'),
u'hours': 49.71047423333333, six.u('server_usages'): [{
u'uptime': 27035, u'local_gb': 0, six.u('hours'): 49.71047423333333,
u'ended_at': None, six.u('uptime'): 27035, six.u('local_gb'): 0,
u'name': u'f15image1', six.u('ended_at'): None,
u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', six.u('name'): six.u('f15image1'),
u'vcpus': 1, u'memory_mb': 512, six.u('tenant_id'):
u'state': u'active', six.u('7b0a1d73f8fb41718f3343c207597869'),
u'flavor': u'm1.tiny', six.u('vcpus'): 1, six.u('memory_mb'): 512,
u'started_at': u'2012-01-20 18:06:06.479998'}], six.u('state'): six.u('active'),
u'start': u'2011-12-25 19:48:41.750687', six.u('flavor'): six.u('m1.tiny'),
u'total_local_gb_usage': 0.0}}) 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): def get_os_simple_tenant_usage_test(self, **kw):
return (200, {}, {u'tenant_usage': { return (200, {}, {six.u('tenant_usage'): {
u'total_memory_mb_usage': 25451.762807466665, six.u('total_memory_mb_usage'): 25451.762807466665,
u'total_vcpus_usage': 49.71047423333333, six.u('total_vcpus_usage'): 49.71047423333333,
u'total_hours': 49.71047423333333, six.u('total_hours'): 49.71047423333333,
u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', six.u('tenant_id'): six.u('7b0a1d73f8fb41718f3343c207597869'),
u'stop': u'2012-01-22 19:48:41.750722', six.u('stop'): six.u('2012-01-22 19:48:41.750722'),
u'server_usages': [{ six.u('server_usages'): [{
u'hours': 49.71047423333333, six.u('hours'): 49.71047423333333,
u'uptime': 27035, u'local_gb': 0, six.u('uptime'): 27035, six.u('local_gb'): 0,
u'ended_at': None, six.u('ended_at'): None,
u'name': u'f15image1', six.u('name'): six.u('f15image1'),
u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', six.u('tenant_id'): six.u('7b0a1d73f8fb41718f3343c207597869'),
u'vcpus': 1, u'memory_mb': 512, six.u('vcpus'): 1, six.u('memory_mb'): 512,
u'state': u'active', six.u('state'): six.u('active'),
u'flavor': u'm1.tiny', six.u('flavor'): six.u('m1.tiny'),
u'started_at': u'2012-01-20 18:06:06.479998'}], six.u('started_at'): six.u('2012-01-20 18:06:06.479998')}],
u'start': u'2011-12-25 19:48:41.750687', six.u('start'): six.u('2011-12-25 19:48:41.750687'),
u'total_local_gb_usage': 0.0}}) six.u('total_local_gb_usage'): 0.0}})
def get_os_simple_tenant_usage_tenant_id(self, **kw): def get_os_simple_tenant_usage_tenant_id(self, **kw):
return (200, {}, {u'tenant_usage': { return (200, {}, {six.u('tenant_usage'): {
u'total_memory_mb_usage': 25451.762807466665, six.u('total_memory_mb_usage'): 25451.762807466665,
u'total_vcpus_usage': 49.71047423333333, six.u('total_vcpus_usage'): 49.71047423333333,
u'total_hours': 49.71047423333333, six.u('total_hours'): 49.71047423333333,
u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', six.u('tenant_id'): six.u('7b0a1d73f8fb41718f3343c207597869'),
u'stop': u'2012-01-22 19:48:41.750722', six.u('stop'): six.u('2012-01-22 19:48:41.750722'),
u'server_usages': [{ six.u('server_usages'): [{
u'hours': 49.71047423333333, six.u('hours'): 49.71047423333333,
u'uptime': 27035, u'local_gb': 0, six.u('uptime'): 27035, six.u('local_gb'): 0,
u'ended_at': None, six.u('ended_at'): None,
u'name': u'f15image1', six.u('name'): six.u('f15image1'),
u'tenant_id': u'7b0a1d73f8fb41718f3343c207597869', six.u('tenant_id'): six.u('7b0a1d73f8fb41718f3343c207597869'),
u'vcpus': 1, u'memory_mb': 512, six.u('vcpus'): 1, six.u('memory_mb'): 512,
u'state': u'active', six.u('state'): six.u('active'),
u'flavor': u'm1.tiny', six.u('flavor'): six.u('m1.tiny'),
u'started_at': u'2012-01-20 18:06:06.479998'}], six.u('started_at'): six.u('2012-01-20 18:06:06.479998')}],
u'start': u'2011-12-25 19:48:41.750687', six.u('start'): six.u('2011-12-25 19:48:41.750687'),
u'total_local_gb_usage': 0.0}}) six.u('total_local_gb_usage'): 0.0}})
# #
# Certificates # Certificates
# #

@ -14,6 +14,8 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import six
from novaclient.v1_1 import availability_zones from novaclient.v1_1 import availability_zones
from novaclient.v1_1 import shell from novaclient.v1_1 import shell
from novaclient.tests.v1_1 import fakes from novaclient.tests.v1_1 import fakes
@ -39,8 +41,8 @@ class AvailabilityZoneTest(utils.TestCase):
self.assertEqual(2, len(zones)) self.assertEqual(2, len(zones))
l0 = [u'zone-1', u'available'] l0 = [six.u('zone-1'), six.u('available')]
l1 = [u'zone-2', u'not available'] l1 = [six.u('zone-2'), six.u('not available')]
z0 = shell._treeizeAvailabilityZone(zones[0]) z0 = shell._treeizeAvailabilityZone(zones[0])
z1 = shell._treeizeAvailabilityZone(zones[1]) z1 = shell._treeizeAvailabilityZone(zones[1])
@ -60,15 +62,18 @@ class AvailabilityZoneTest(utils.TestCase):
self.assertEqual(3, len(zones)) self.assertEqual(3, len(zones))
l0 = [u'zone-1', u'available'] l0 = [six.u('zone-1'), six.u('available')]
l1 = [u'|- fake_host-1', u''] l1 = [six.u('|- fake_host-1'), six.u('')]
l2 = [u'| |- nova-compute', u'enabled :-) 2012-12-26 14:45:25'] l2 = [six.u('| |- nova-compute'),
l3 = [u'internal', u'available'] six.u('enabled :-) 2012-12-26 14:45:25')]
l4 = [u'|- fake_host-1', u''] l3 = [six.u('internal'), six.u('available')]
l5 = [u'| |- nova-sched', u'enabled :-) 2012-12-26 14:45:25'] l4 = [six.u('|- fake_host-1'), six.u('')]
l6 = [u'|- fake_host-2', u''] l5 = [six.u('| |- nova-sched'),
l7 = [u'| |- nova-network', u'enabled XXX 2012-12-26 14:45:24'] six.u('enabled :-) 2012-12-26 14:45:25')]
l8 = [u'zone-2', u'not available'] 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]) z0 = shell._treeizeAvailabilityZone(zones[0])
z1 = shell._treeizeAvailabilityZone(zones[1]) z1 = shell._treeizeAvailabilityZone(zones[1])

@ -3,6 +3,7 @@
import StringIO import StringIO
import mock import mock
import six
from novaclient import exceptions from novaclient import exceptions
from novaclient.v1_1 import servers from novaclient.v1_1 import servers
@ -108,7 +109,7 @@ class ServersTest(utils.TestCase):
image=1, image=1,
flavor=1, flavor=1,
meta={'foo': 'bar'}, meta={'foo': 'bar'},
userdata=u'こんにちは', userdata=six.u('こんにちは'),
key_name="fakekey", key_name="fakekey",
files={ files={
'/etc/passwd': 'some data', # a file '/etc/passwd': 'some data', # a file

@ -6,6 +6,7 @@ import textwrap
import uuid import uuid
import prettytable import prettytable
import six
from novaclient import exceptions from novaclient import exceptions
from novaclient.openstack.common import strutils from novaclient.openstack.common import strutils
@ -339,9 +340,9 @@ def slugify(value):
""" """
import unicodedata import unicodedata
if not isinstance(value, unicode): if not isinstance(value, unicode):
value = unicode(value) value = six.text_type(value)
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') 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) return _slugify_hyphenate_re.sub('-', value)

@ -5,3 +5,4 @@ iso8601>=0.1.4
prettytable>=0.6,<0.8 prettytable>=0.6,<0.8
requests>=0.8 requests>=0.8
simplejson simplejson
six