Replace unicode with six.text_type

The Unicode type is 'unicode' in Python 2 and 'str' on Python 3.
This patch replaces unicode with six.text_type to make Murano
more compatible with Python 3. Also it replaces the tuple
'(str, unicode)' with six.string_types for instance comparision.

Blueprint murano-python-3-support

Change-Id: I4db27afdb556ec30dd36d7d4e170055274edfa8b
This commit is contained in:
Ravi Shekhar Jethani 2015-12-28 05:07:08 -08:00
parent 5da4c5f22a
commit 782808db60
14 changed files with 39 additions and 19 deletions

View File

@ -14,6 +14,7 @@
Cinder's faultwrapper
"""
import six
import sys
import traceback
@ -93,7 +94,7 @@ class FaultWrapper(wsgi.Middleware):
ex_type = ex.__class__.__name__
full_message = unicode(ex)
full_message = six.text_type(ex)
if full_message.find('\n') > -1:
message, msg_trace = full_message.split('\n', 1)
else:

View File

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
from oslo_db import exception as db_exc
from oslo_log import log as logging
from sqlalchemy import desc
@ -68,7 +70,7 @@ class Controller(object):
LOG.exception(msg)
raise exc.HTTPBadRequest(explanation=msg)
name = unicode(body['name'])
name = six.text_type(body['name'])
if len(name) > 255:
msg = _('Environment name should be 255 characters maximum')
LOG.exception(msg)

View File

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
from oslo_db import exception as db_exc
from oslo_log import log as logging
from webob import exc
@ -272,7 +274,7 @@ class Controller(object):
LOG.exception(msg)
raise exc.HTTPBadRequest(explanation=msg)
name = unicode(body['name'])
name = six.text_type(body['name'])
if len(name) > 255:
msg = _('Environment template name should be 255 characters '
'maximum')

View File

@ -18,6 +18,7 @@
import datetime
import errno
import re
import six
import socket
import sys
import time
@ -515,7 +516,7 @@ class JSONDictSerializer(DictSerializer):
if isinstance(obj, datetime.datetime):
_dtime = obj - datetime.timedelta(microseconds=obj.microsecond)
return _dtime.isoformat()
return unicode(obj)
return six.text_type(obj)
if result:
data.body = jsonutils.dumps(result)
return jsonutils.dumps(data, default=sanitizer)
@ -882,7 +883,7 @@ class JSONPatchDeserializer(TextDeserializer):
if not allowed_methods:
msg = _("Attribute '{0}' is invalid").format(change_path)
raise webob.exc.HTTPForbidden(explanation=unicode(msg))
raise webob.exc.HTTPForbidden(explanation=six.text_type(msg))
if change_op not in allowed_methods:
msg = _("Method '{method}' is not allowed for a path with name "
@ -891,7 +892,7 @@ class JSONPatchDeserializer(TextDeserializer):
name=change_path,
ops=', '.join(allowed_methods))
raise webob.exc.HTTPForbidden(explanation=unicode(msg))
raise webob.exc.HTTPForbidden(explanation=six.text_type(msg))
property_to_update = {change_path: change['value']}

View File

@ -12,13 +12,15 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
from alembic import op
import sqlalchemy as sa
def transform_table(name, renames, defaults, *columns, **kw):
def escape(val):
if isinstance(val, (str, unicode)):
if isinstance(val, six.string_types):
return "'{0}'".format(val)
elif val is None:
return 'NULL'

View File

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
from keystoneclient import exceptions as ks_exceptions
from oslo_config import cfg
from oslo_log import log as logging
@ -254,7 +256,7 @@ class EnvironmentServices(object):
elif isinstance(data, list):
return [EnvironmentServices._objectify(v, replacements)
for v in data]
elif isinstance(data, (str, unicode)):
elif isinstance(data, six.string_types):
for key, value in replacements.iteritems():
data = data.replace('%' + key + '%', value)
return data

View File

@ -15,6 +15,7 @@
import collections
import contextlib
import itertools
import six
import weakref
import eventlet
@ -144,7 +145,7 @@ class MuranoDslExecutor(object):
def _log_method(self, context, args, kwargs):
method = helpers.get_current_method(context)
param_gen = itertools.chain(
(unicode(arg) for arg in args),
(six.text_type(arg) for arg in args),
(u'{0} => {1}'.format(name, value)
for name, value in kwargs.iteritems()))
params_str = u', '.join(param_gen)

View File

@ -14,6 +14,7 @@
import inspect
import os.path
import six
from yaql import specs
@ -66,7 +67,7 @@ def compose_stack_frame(context):
method = helpers.get_current_method(context)
return {
'instruction': None if instruction is None
else unicode(instruction),
else six.text_type(instruction),
'location': None if instruction is None
else instruction.source_file_position,

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
import sys
import uuid
@ -57,7 +58,7 @@ class TypeScheme(object):
if value is None:
return None
try:
return unicode(value)
return six.text_type(value)
except Exception:
raise exceptions.ContractViolationException(
'Value {0} violates string() contract'.format(
@ -318,4 +319,4 @@ class TypeScheme(object):
def format_scalar(value):
if isinstance(value, basestring):
return "'{0}'".format(value)
return unicode(value)
return six.text_type(value)

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
import sys
import weakref
@ -74,7 +75,7 @@ class PropertySpec(Spec):
return super(PropertySpec, self).validate(*args, **kwargs)
except exceptions.ContractViolationException as e:
msg = u'[{0}.{1}{2}] {3}'.format(
self.class_name, self.property_name, e.path, unicode(e))
self.class_name, self.property_name, e.path, six.text_type(e))
raise exceptions.ContractViolationException, msg, sys.exc_info()[2]
@ -91,5 +92,5 @@ class ArgumentSpec(Spec):
except exceptions.ContractViolationException as e:
msg = u'[{0}::{1}({2}{3})] {4}'.format(
self.class_name, self.method_name, self.arg_name,
e.path, unicode(e))
e.path, six.text_type(e))
raise exceptions.ContractViolationException, msg, sys.exc_info()[2]

View File

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
from murano.dsl import constants
from murano.dsl import dsl_exception
from murano.dsl import expressions
@ -53,7 +55,7 @@ class ThrowMacro(expressions.DslExpression):
def __unicode__(self):
if self._message:
return u'Throw {0}: {1}'.format(self._names, self._message)
return u'Throw ' + unicode(self._names)
return u'Throw ' + six.text_type(self._names)
class CatchBlock(expressions.DslExpression):

View File

@ -13,6 +13,7 @@
# under the License.
import re
import six
from yaql.language import exceptions as yaql_exceptions
from yaql.language import expressions
@ -26,7 +27,7 @@ class YaqlExpression(dsl_types.YaqlExpression):
def __init__(self, expression, version):
self._version = version
if isinstance(expression, basestring):
self._expression = unicode(expression)
self._expression = six.text_type(expression)
self._parsed_expression = yaql_integration.parse(
self._expression, version)
self._file_position = None
@ -35,7 +36,7 @@ class YaqlExpression(dsl_types.YaqlExpression):
self._parsed_expression = expression._parsed_expression
self._file_position = expression._file_position
elif isinstance(expression, expressions.Statement):
self._expression = unicode(expression)
self._expression = six.text_type(expression)
self._parsed_expression = expression
self._file_position = None
else:

View File

@ -17,6 +17,7 @@ import base64
import collections
import random
import re
import six
import string
import time
@ -81,7 +82,7 @@ def _convert_macro_parameter(macro, mappings):
def replace(match):
replaced[0] = True
return unicode(mappings.get(match.group(1)))
return six.text_type(mappings.get(match.group(1)))
result = re.sub('{(\\w+?)}', replace, macro)
if replaced[0]:

View File

@ -13,12 +13,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import six
import murano.common.exceptions as e
class PackageException(e.Error):
def __str__(self):
return unicode(self.message).encode('UTF-8')
return six.text_type(self.message).encode('UTF-8')
class PackageClassLoadError(PackageException):