Python3: Replace basestring by six.string_types

The builtin basestring abstract type was removed in python3,
use str instead, we need to use six.string_types to replace
the basestring for py3 compatibility.

This patch was generated by the following tool using 'basestring'
option.

https://github.com/haypo/sixer
Command:
python sixer.py -w basestring murano/

This also adds a check to murano/hacking/checks.py that should
catch this error in the future.

Blueprint murano-python-3-support

Change-Id: I26c236ce6dd2fffd6a4ca50e55ad62deb01bd9dd
This commit is contained in:
Ravi Shekhar Jethani 2016-01-18 23:25:53 -08:00
parent b8d3ce1b73
commit f963039bd7
20 changed files with 47 additions and 30 deletions

View File

@ -10,3 +10,4 @@ Murano Specific Commandments
- [M323] Python 3: do not use dict.iteritems.
- [M324] Python 3: do not use dict.iterkeys.
- [M325] Python 3: do not use dict.itervalues.
- [M326] Python 3: do not use basestring.

View File

@ -57,7 +57,7 @@ class TokenSanitizer(object):
return [self.sanitize(item) for item in obj]
elif isinstance(obj, tuple):
k, v = obj
if self._contains_token(k) and isinstance(v, basestring):
if self._contains_token(k) and isinstance(v, six.string_types):
return k, self.message
return k, self.sanitize(v)
else:

View File

@ -27,7 +27,7 @@ LOG = logging.getLogger(__name__)
class TraverseHelper(object):
value_type = (basestring, int, float, bool)
value_type = (six.string_types, int, float, bool)
@staticmethod
def get(path, source):
@ -165,8 +165,8 @@ def is_different(obj1, obj2):
if o1 is o2:
return
elif (isinstance(o1, basestring) and
isinstance(o2, basestring)) and o1 == o2:
elif (isinstance(o1, six.string_types) and
isinstance(o2, six.string_types)) and o1 == o2:
return
elif type(o1) != type(o2):
raise Difference()

View File

@ -1040,6 +1040,6 @@ class FormDataDeserializer(TextDeserializer):
def default(self, request):
form_data_parts = request.POST
for key, value in six.iteritems(form_data_parts):
if isinstance(value, basestring):
if isinstance(value, six.string_types):
form_data_parts[key] = self._from_json(value)
return {'body': form_data_parts}

View File

@ -52,7 +52,7 @@ class MuranoType(yaqltypes.PythonType):
if value is None or isinstance(value, yaql_expressions.Expression):
return True
murano_class = self.murano_class
if isinstance(murano_class, basestring):
if isinstance(murano_class, six.string_types):
murano_class_name = murano_class
else:
murano_class_name = murano_class.name
@ -95,7 +95,7 @@ class MuranoTypeName(yaqltypes.LazyParameterType, yaqltypes.PythonType):
def __init__(self, nullable=False, context=None):
self._context = context
super(MuranoTypeName, self).__init__(
(dsl_types.MuranoClassReference, basestring), nullable)
(dsl_types.MuranoClassReference, six.string_types), nullable)
def convert(self, value, sender, context, function_spec, engine,
*args, **kwargs):
@ -104,7 +104,7 @@ class MuranoTypeName(yaqltypes.LazyParameterType, yaqltypes.PythonType):
value = value(utils.NO_VALUE, context, engine)
value = super(MuranoTypeName, self).convert(
value, sender, context, function_spec, engine)
if isinstance(value, basestring):
if isinstance(value, six.string_types):
if function_spec.meta.get(constants.META_MURANO_METHOD):
context = helpers.get_caller_context(context)
murano_type = helpers.get_type(context)

View File

@ -225,7 +225,7 @@ class MuranoDslExecutor(object):
for res in self._list_potential_object_ids(val):
yield res
elif isinstance(data, collections.Iterable) and not isinstance(
data, basestring):
data, six.string_types):
for val in data:
for res in self._list_potential_object_ids(val):
yield res

View File

@ -83,8 +83,10 @@ def merge_dicts(dict1, dict2, max_levels=0):
if key in dict2:
value2 = dict2[key]
if type(value2) != type(value1):
if ((isinstance(value1, basestring) or value1 is None) and
(isinstance(value2, basestring) or value2 is None)):
if ((isinstance(value1,
six.string_types) or value1 is None) and
(isinstance(value2,
six.string_types) or value2 is None)):
continue
raise TypeError()
if max_levels != 1 and isinstance(value2, dict):
@ -285,7 +287,7 @@ def cast(obj, murano_class, pov_or_version_spec=None):
obj = obj.object
if isinstance(pov_or_version_spec, dsl_types.MuranoClass):
pov_or_version_spec = pov_or_version_spec.package
elif isinstance(pov_or_version_spec, basestring):
elif isinstance(pov_or_version_spec, six.string_types):
pov_or_version_spec = parse_version_spec(pov_or_version_spec)
if isinstance(murano_class, dsl_types.MuranoClass):
if pov_or_version_spec is None:

View File

@ -141,7 +141,7 @@ class WhileDoMacro(expressions.DslExpression):
class ForMacro(expressions.DslExpression):
def __init__(self, For, In, Do):
if not isinstance(For, basestring):
if not isinstance(For, six.string_types):
raise exceptions.DslSyntaxError(
'For value must be of string type')
self._code = CodeBlock(Do)

View File

@ -86,7 +86,8 @@ def _pass12_serialize(value, parent, serialized_objects,
designer_attributes_getter):
if isinstance(value, dsl.MuranoObjectInterface):
value = value.object
if isinstance(value, (basestring, int, float, bool)) or value is None:
if isinstance(value, (six.string_types,
int, float, bool)) or value is None:
return value, False
if isinstance(value, dsl_types.MuranoObject):
if value.owner is not parent or value.object_id in serialized_objects:

View File

@ -175,7 +175,7 @@ class TypeScheme(object):
obj = object_store.load(
value, owner, root_context, defaults=default)
elif isinstance(value, basestring):
elif isinstance(value, six.string_types):
obj = object_store.get(value)
if obj is None:
if not object_store.initializing:
@ -317,6 +317,6 @@ class TypeScheme(object):
def format_scalar(value):
if isinstance(value, basestring):
if isinstance(value, six.string_types):
return "'{0}'".format(value)
return six.text_type(value)

View File

@ -26,7 +26,7 @@ from murano.dsl import yaql_integration
class YaqlExpression(dsl_types.YaqlExpression):
def __init__(self, expression, version):
self._version = version
if isinstance(expression, basestring):
if isinstance(expression, six.string_types):
self._expression = six.text_type(expression)
self._parsed_expression = yaql_integration.parse(
self._expression, version)
@ -66,7 +66,7 @@ class YaqlExpression(dsl_types.YaqlExpression):
@staticmethod
def is_expression(expression, version):
if not isinstance(expression, basestring):
if not isinstance(expression, six.string_types):
return False
if re.match('^[\s\w\d.:]*$', expression):
return False

View File

@ -16,6 +16,7 @@
from oslo_config import cfg
import oslo_messaging as messaging
import six
from murano.common import uuidutils
from murano.dsl import dsl
@ -34,7 +35,7 @@ class StatusReporter(object):
StatusReporter.transport,
publisher_id=uuidutils.generate_uuid(),
topic='murano')
if isinstance(environment, basestring):
if isinstance(environment, six.string_types):
self._environment_id = environment
else:
self._environment_id = environment.id

View File

@ -59,7 +59,7 @@ def pselect(collection, composer):
@specs.parameter('mappings', collections.Mapping)
@specs.extension_method
def bind(obj, mappings):
if isinstance(obj, basestring) and obj.startswith('$'):
if isinstance(obj, six.string_types) and obj.startswith('$'):
value = _convert_macro_parameter(obj[1:], mappings)
if value is not None:
return value
@ -70,7 +70,7 @@ def bind(obj, mappings):
for key, value in six.iteritems(obj):
result[bind(key, mappings)] = bind(value, mappings)
return result
elif isinstance(obj, basestring) and obj.startswith('$'):
elif isinstance(obj, six.string_types) and obj.startswith('$'):
value = _convert_macro_parameter(obj[1:], mappings)
if value is not None:
return value

View File

@ -55,8 +55,16 @@ def check_python3_no_itervalues(logical_line):
yield(0, msg)
def check_no_basestring(logical_line):
if re.search(r"\bbasestring\b", logical_line):
msg = ("M326: basestring is not Python3-compatible, use "
"six.string_types instead.")
yield(0, msg)
def factory(register):
register(no_mutable_default_args)
register(check_python3_no_iteritems)
register(check_python3_no_iterkeys)
register(check_python3_no_itervalues)
register(check_no_basestring)

View File

@ -252,7 +252,7 @@ class HotPackage(package_base.PackageBase):
@staticmethod
def _format_value(value):
if isinstance(value, basestring):
if isinstance(value, six.string_types):
return str("'" + value + "'")
return str(value)

View File

@ -49,7 +49,7 @@ class Runner(object):
class DslObjectWrapper(object):
def __init__(self, obj, runner):
self._runner = runner
if isinstance(obj, basestring):
if isinstance(obj, six.string_types):
self._object_id = obj
elif isinstance(obj, (object_model.Object, object_model.Ref)):
self._object_id = obj.id
@ -69,7 +69,7 @@ class Runner(object):
return call
def __init__(self, model, package_loader, functions):
if isinstance(model, basestring):
if isinstance(model, six.string_types):
model = object_model.Object(model)
model = object_model.build_model(model)
if 'Objects' not in model:

View File

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
from murano.dsl import dsl
from murano.dsl import exceptions
from murano.tests.unit.dsl.foundation import object_model as om
@ -34,12 +36,12 @@ class TestContracts(test_case.DslTestCase):
def test_string_contract(self):
result = self._runner.testStringContract('qwerty')
self.assertIsInstance(result, basestring)
self.assertIsInstance(result, six.string_types)
self.assertEqual('qwerty', result)
def test_string_from_number_contract(self):
result = self._runner.testStringContract(123)
self.assertIsInstance(result, basestring)
self.assertIsInstance(result, six.string_types)
self.assertEqual('123', result)
def test_string_null_contract(self):

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
from testtools import matchers
from yaql.language import exceptions as yaql_exceptions
@ -159,8 +160,8 @@ class TestEngineYaqlFunctions(test_case.DslTestCase):
name1 = self._runner.testRandomName()
name2 = self._runner.testRandomName()
self.assertIsInstance(name1, basestring)
self.assertIsInstance(name2, basestring)
self.assertIsInstance(name1, six.string_types)
self.assertIsInstance(name2, six.string_types)
self.assertThat(len(name1), matchers.GreaterThan(12))
self.assertThat(len(name2), matchers.GreaterThan(12))
self.assertThat(name1, matchers.NotEquals(name2))

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
from testtools import matchers
from murano.dsl import serializer
@ -86,7 +87,7 @@ class TestResultsSerializer(test_case.DslTestCase):
serialized['Objects']['?'].get('_actions'), dict)
for action in serialized['Objects']['?']['_actions'].values():
self.assertIsInstance(action.get('enabled'), bool)
self.assertIsInstance(action.get('name'), basestring)
self.assertIsInstance(action.get('name'), six.string_types)
self.assertThat(
action['name'],
matchers.StartsWith('test'))

View File

@ -51,7 +51,7 @@ class TestMockContextManager(mock_context_manager.MockContextManager):
class MockRunner(runner.Runner):
def __init__(self, model, package_loader, functions):
if isinstance(model, basestring):
if isinstance(model, six.string_types):
model = om.Object(model)
model = om.build_model(model)
if 'Objects' not in model: