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:
parent
b8d3ce1b73
commit
f963039bd7
|
@ -10,3 +10,4 @@ Murano Specific Commandments
|
||||||
- [M323] Python 3: do not use dict.iteritems.
|
- [M323] Python 3: do not use dict.iteritems.
|
||||||
- [M324] Python 3: do not use dict.iterkeys.
|
- [M324] Python 3: do not use dict.iterkeys.
|
||||||
- [M325] Python 3: do not use dict.itervalues.
|
- [M325] Python 3: do not use dict.itervalues.
|
||||||
|
- [M326] Python 3: do not use basestring.
|
||||||
|
|
|
@ -57,7 +57,7 @@ class TokenSanitizer(object):
|
||||||
return [self.sanitize(item) for item in obj]
|
return [self.sanitize(item) for item in obj]
|
||||||
elif isinstance(obj, tuple):
|
elif isinstance(obj, tuple):
|
||||||
k, v = obj
|
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.message
|
||||||
return k, self.sanitize(v)
|
return k, self.sanitize(v)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -27,7 +27,7 @@ LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class TraverseHelper(object):
|
class TraverseHelper(object):
|
||||||
value_type = (basestring, int, float, bool)
|
value_type = (six.string_types, int, float, bool)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get(path, source):
|
def get(path, source):
|
||||||
|
@ -165,8 +165,8 @@ def is_different(obj1, obj2):
|
||||||
|
|
||||||
if o1 is o2:
|
if o1 is o2:
|
||||||
return
|
return
|
||||||
elif (isinstance(o1, basestring) and
|
elif (isinstance(o1, six.string_types) and
|
||||||
isinstance(o2, basestring)) and o1 == o2:
|
isinstance(o2, six.string_types)) and o1 == o2:
|
||||||
return
|
return
|
||||||
elif type(o1) != type(o2):
|
elif type(o1) != type(o2):
|
||||||
raise Difference()
|
raise Difference()
|
||||||
|
|
|
@ -1040,6 +1040,6 @@ class FormDataDeserializer(TextDeserializer):
|
||||||
def default(self, request):
|
def default(self, request):
|
||||||
form_data_parts = request.POST
|
form_data_parts = request.POST
|
||||||
for key, value in six.iteritems(form_data_parts):
|
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)
|
form_data_parts[key] = self._from_json(value)
|
||||||
return {'body': form_data_parts}
|
return {'body': form_data_parts}
|
||||||
|
|
|
@ -52,7 +52,7 @@ class MuranoType(yaqltypes.PythonType):
|
||||||
if value is None or isinstance(value, yaql_expressions.Expression):
|
if value is None or isinstance(value, yaql_expressions.Expression):
|
||||||
return True
|
return True
|
||||||
murano_class = self.murano_class
|
murano_class = self.murano_class
|
||||||
if isinstance(murano_class, basestring):
|
if isinstance(murano_class, six.string_types):
|
||||||
murano_class_name = murano_class
|
murano_class_name = murano_class
|
||||||
else:
|
else:
|
||||||
murano_class_name = murano_class.name
|
murano_class_name = murano_class.name
|
||||||
|
@ -95,7 +95,7 @@ class MuranoTypeName(yaqltypes.LazyParameterType, yaqltypes.PythonType):
|
||||||
def __init__(self, nullable=False, context=None):
|
def __init__(self, nullable=False, context=None):
|
||||||
self._context = context
|
self._context = context
|
||||||
super(MuranoTypeName, self).__init__(
|
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,
|
def convert(self, value, sender, context, function_spec, engine,
|
||||||
*args, **kwargs):
|
*args, **kwargs):
|
||||||
|
@ -104,7 +104,7 @@ class MuranoTypeName(yaqltypes.LazyParameterType, yaqltypes.PythonType):
|
||||||
value = value(utils.NO_VALUE, context, engine)
|
value = value(utils.NO_VALUE, context, engine)
|
||||||
value = super(MuranoTypeName, self).convert(
|
value = super(MuranoTypeName, self).convert(
|
||||||
value, sender, context, function_spec, engine)
|
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):
|
if function_spec.meta.get(constants.META_MURANO_METHOD):
|
||||||
context = helpers.get_caller_context(context)
|
context = helpers.get_caller_context(context)
|
||||||
murano_type = helpers.get_type(context)
|
murano_type = helpers.get_type(context)
|
||||||
|
|
|
@ -225,7 +225,7 @@ class MuranoDslExecutor(object):
|
||||||
for res in self._list_potential_object_ids(val):
|
for res in self._list_potential_object_ids(val):
|
||||||
yield res
|
yield res
|
||||||
elif isinstance(data, collections.Iterable) and not isinstance(
|
elif isinstance(data, collections.Iterable) and not isinstance(
|
||||||
data, basestring):
|
data, six.string_types):
|
||||||
for val in data:
|
for val in data:
|
||||||
for res in self._list_potential_object_ids(val):
|
for res in self._list_potential_object_ids(val):
|
||||||
yield res
|
yield res
|
||||||
|
|
|
@ -83,8 +83,10 @@ def merge_dicts(dict1, dict2, max_levels=0):
|
||||||
if key in dict2:
|
if key in dict2:
|
||||||
value2 = dict2[key]
|
value2 = dict2[key]
|
||||||
if type(value2) != type(value1):
|
if type(value2) != type(value1):
|
||||||
if ((isinstance(value1, basestring) or value1 is None) and
|
if ((isinstance(value1,
|
||||||
(isinstance(value2, basestring) or value2 is None)):
|
six.string_types) or value1 is None) and
|
||||||
|
(isinstance(value2,
|
||||||
|
six.string_types) or value2 is None)):
|
||||||
continue
|
continue
|
||||||
raise TypeError()
|
raise TypeError()
|
||||||
if max_levels != 1 and isinstance(value2, dict):
|
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
|
obj = obj.object
|
||||||
if isinstance(pov_or_version_spec, dsl_types.MuranoClass):
|
if isinstance(pov_or_version_spec, dsl_types.MuranoClass):
|
||||||
pov_or_version_spec = pov_or_version_spec.package
|
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)
|
pov_or_version_spec = parse_version_spec(pov_or_version_spec)
|
||||||
if isinstance(murano_class, dsl_types.MuranoClass):
|
if isinstance(murano_class, dsl_types.MuranoClass):
|
||||||
if pov_or_version_spec is None:
|
if pov_or_version_spec is None:
|
||||||
|
|
|
@ -141,7 +141,7 @@ class WhileDoMacro(expressions.DslExpression):
|
||||||
|
|
||||||
class ForMacro(expressions.DslExpression):
|
class ForMacro(expressions.DslExpression):
|
||||||
def __init__(self, For, In, Do):
|
def __init__(self, For, In, Do):
|
||||||
if not isinstance(For, basestring):
|
if not isinstance(For, six.string_types):
|
||||||
raise exceptions.DslSyntaxError(
|
raise exceptions.DslSyntaxError(
|
||||||
'For value must be of string type')
|
'For value must be of string type')
|
||||||
self._code = CodeBlock(Do)
|
self._code = CodeBlock(Do)
|
||||||
|
|
|
@ -86,7 +86,8 @@ def _pass12_serialize(value, parent, serialized_objects,
|
||||||
designer_attributes_getter):
|
designer_attributes_getter):
|
||||||
if isinstance(value, dsl.MuranoObjectInterface):
|
if isinstance(value, dsl.MuranoObjectInterface):
|
||||||
value = value.object
|
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
|
return value, False
|
||||||
if isinstance(value, dsl_types.MuranoObject):
|
if isinstance(value, dsl_types.MuranoObject):
|
||||||
if value.owner is not parent or value.object_id in serialized_objects:
|
if value.owner is not parent or value.object_id in serialized_objects:
|
||||||
|
|
|
@ -175,7 +175,7 @@ class TypeScheme(object):
|
||||||
|
|
||||||
obj = object_store.load(
|
obj = object_store.load(
|
||||||
value, owner, root_context, defaults=default)
|
value, owner, root_context, defaults=default)
|
||||||
elif isinstance(value, basestring):
|
elif isinstance(value, six.string_types):
|
||||||
obj = object_store.get(value)
|
obj = object_store.get(value)
|
||||||
if obj is None:
|
if obj is None:
|
||||||
if not object_store.initializing:
|
if not object_store.initializing:
|
||||||
|
@ -317,6 +317,6 @@ class TypeScheme(object):
|
||||||
|
|
||||||
|
|
||||||
def format_scalar(value):
|
def format_scalar(value):
|
||||||
if isinstance(value, basestring):
|
if isinstance(value, six.string_types):
|
||||||
return "'{0}'".format(value)
|
return "'{0}'".format(value)
|
||||||
return six.text_type(value)
|
return six.text_type(value)
|
||||||
|
|
|
@ -26,7 +26,7 @@ from murano.dsl import yaql_integration
|
||||||
class YaqlExpression(dsl_types.YaqlExpression):
|
class YaqlExpression(dsl_types.YaqlExpression):
|
||||||
def __init__(self, expression, version):
|
def __init__(self, expression, version):
|
||||||
self._version = version
|
self._version = version
|
||||||
if isinstance(expression, basestring):
|
if isinstance(expression, six.string_types):
|
||||||
self._expression = six.text_type(expression)
|
self._expression = six.text_type(expression)
|
||||||
self._parsed_expression = yaql_integration.parse(
|
self._parsed_expression = yaql_integration.parse(
|
||||||
self._expression, version)
|
self._expression, version)
|
||||||
|
@ -66,7 +66,7 @@ class YaqlExpression(dsl_types.YaqlExpression):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def is_expression(expression, version):
|
def is_expression(expression, version):
|
||||||
if not isinstance(expression, basestring):
|
if not isinstance(expression, six.string_types):
|
||||||
return False
|
return False
|
||||||
if re.match('^[\s\w\d.:]*$', expression):
|
if re.match('^[\s\w\d.:]*$', expression):
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
from oslo_config import cfg
|
from oslo_config import cfg
|
||||||
import oslo_messaging as messaging
|
import oslo_messaging as messaging
|
||||||
|
import six
|
||||||
|
|
||||||
from murano.common import uuidutils
|
from murano.common import uuidutils
|
||||||
from murano.dsl import dsl
|
from murano.dsl import dsl
|
||||||
|
@ -34,7 +35,7 @@ class StatusReporter(object):
|
||||||
StatusReporter.transport,
|
StatusReporter.transport,
|
||||||
publisher_id=uuidutils.generate_uuid(),
|
publisher_id=uuidutils.generate_uuid(),
|
||||||
topic='murano')
|
topic='murano')
|
||||||
if isinstance(environment, basestring):
|
if isinstance(environment, six.string_types):
|
||||||
self._environment_id = environment
|
self._environment_id = environment
|
||||||
else:
|
else:
|
||||||
self._environment_id = environment.id
|
self._environment_id = environment.id
|
||||||
|
|
|
@ -59,7 +59,7 @@ def pselect(collection, composer):
|
||||||
@specs.parameter('mappings', collections.Mapping)
|
@specs.parameter('mappings', collections.Mapping)
|
||||||
@specs.extension_method
|
@specs.extension_method
|
||||||
def bind(obj, mappings):
|
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)
|
value = _convert_macro_parameter(obj[1:], mappings)
|
||||||
if value is not None:
|
if value is not None:
|
||||||
return value
|
return value
|
||||||
|
@ -70,7 +70,7 @@ def bind(obj, mappings):
|
||||||
for key, value in six.iteritems(obj):
|
for key, value in six.iteritems(obj):
|
||||||
result[bind(key, mappings)] = bind(value, mappings)
|
result[bind(key, mappings)] = bind(value, mappings)
|
||||||
return result
|
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)
|
value = _convert_macro_parameter(obj[1:], mappings)
|
||||||
if value is not None:
|
if value is not None:
|
||||||
return value
|
return value
|
||||||
|
|
|
@ -55,8 +55,16 @@ def check_python3_no_itervalues(logical_line):
|
||||||
yield(0, msg)
|
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):
|
def factory(register):
|
||||||
register(no_mutable_default_args)
|
register(no_mutable_default_args)
|
||||||
register(check_python3_no_iteritems)
|
register(check_python3_no_iteritems)
|
||||||
register(check_python3_no_iterkeys)
|
register(check_python3_no_iterkeys)
|
||||||
register(check_python3_no_itervalues)
|
register(check_python3_no_itervalues)
|
||||||
|
register(check_no_basestring)
|
||||||
|
|
|
@ -252,7 +252,7 @@ class HotPackage(package_base.PackageBase):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _format_value(value):
|
def _format_value(value):
|
||||||
if isinstance(value, basestring):
|
if isinstance(value, six.string_types):
|
||||||
return str("'" + value + "'")
|
return str("'" + value + "'")
|
||||||
return str(value)
|
return str(value)
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ class Runner(object):
|
||||||
class DslObjectWrapper(object):
|
class DslObjectWrapper(object):
|
||||||
def __init__(self, obj, runner):
|
def __init__(self, obj, runner):
|
||||||
self._runner = runner
|
self._runner = runner
|
||||||
if isinstance(obj, basestring):
|
if isinstance(obj, six.string_types):
|
||||||
self._object_id = obj
|
self._object_id = obj
|
||||||
elif isinstance(obj, (object_model.Object, object_model.Ref)):
|
elif isinstance(obj, (object_model.Object, object_model.Ref)):
|
||||||
self._object_id = obj.id
|
self._object_id = obj.id
|
||||||
|
@ -69,7 +69,7 @@ class Runner(object):
|
||||||
return call
|
return call
|
||||||
|
|
||||||
def __init__(self, model, package_loader, functions):
|
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.Object(model)
|
||||||
model = object_model.build_model(model)
|
model = object_model.build_model(model)
|
||||||
if 'Objects' not in model:
|
if 'Objects' not in model:
|
||||||
|
|
|
@ -12,6 +12,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 murano.dsl import dsl
|
from murano.dsl import dsl
|
||||||
from murano.dsl import exceptions
|
from murano.dsl import exceptions
|
||||||
from murano.tests.unit.dsl.foundation import object_model as om
|
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):
|
def test_string_contract(self):
|
||||||
result = self._runner.testStringContract('qwerty')
|
result = self._runner.testStringContract('qwerty')
|
||||||
self.assertIsInstance(result, basestring)
|
self.assertIsInstance(result, six.string_types)
|
||||||
self.assertEqual('qwerty', result)
|
self.assertEqual('qwerty', result)
|
||||||
|
|
||||||
def test_string_from_number_contract(self):
|
def test_string_from_number_contract(self):
|
||||||
result = self._runner.testStringContract(123)
|
result = self._runner.testStringContract(123)
|
||||||
self.assertIsInstance(result, basestring)
|
self.assertIsInstance(result, six.string_types)
|
||||||
self.assertEqual('123', result)
|
self.assertEqual('123', result)
|
||||||
|
|
||||||
def test_string_null_contract(self):
|
def test_string_null_contract(self):
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
# 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 testtools import matchers
|
from testtools import matchers
|
||||||
from yaql.language import exceptions as yaql_exceptions
|
from yaql.language import exceptions as yaql_exceptions
|
||||||
|
|
||||||
|
@ -159,8 +160,8 @@ class TestEngineYaqlFunctions(test_case.DslTestCase):
|
||||||
name1 = self._runner.testRandomName()
|
name1 = self._runner.testRandomName()
|
||||||
name2 = self._runner.testRandomName()
|
name2 = self._runner.testRandomName()
|
||||||
|
|
||||||
self.assertIsInstance(name1, basestring)
|
self.assertIsInstance(name1, six.string_types)
|
||||||
self.assertIsInstance(name2, basestring)
|
self.assertIsInstance(name2, six.string_types)
|
||||||
self.assertThat(len(name1), matchers.GreaterThan(12))
|
self.assertThat(len(name1), matchers.GreaterThan(12))
|
||||||
self.assertThat(len(name2), matchers.GreaterThan(12))
|
self.assertThat(len(name2), matchers.GreaterThan(12))
|
||||||
self.assertThat(name1, matchers.NotEquals(name2))
|
self.assertThat(name1, matchers.NotEquals(name2))
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
# 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 testtools import matchers
|
from testtools import matchers
|
||||||
|
|
||||||
from murano.dsl import serializer
|
from murano.dsl import serializer
|
||||||
|
@ -86,7 +87,7 @@ class TestResultsSerializer(test_case.DslTestCase):
|
||||||
serialized['Objects']['?'].get('_actions'), dict)
|
serialized['Objects']['?'].get('_actions'), dict)
|
||||||
for action in serialized['Objects']['?']['_actions'].values():
|
for action in serialized['Objects']['?']['_actions'].values():
|
||||||
self.assertIsInstance(action.get('enabled'), bool)
|
self.assertIsInstance(action.get('enabled'), bool)
|
||||||
self.assertIsInstance(action.get('name'), basestring)
|
self.assertIsInstance(action.get('name'), six.string_types)
|
||||||
self.assertThat(
|
self.assertThat(
|
||||||
action['name'],
|
action['name'],
|
||||||
matchers.StartsWith('test'))
|
matchers.StartsWith('test'))
|
||||||
|
|
|
@ -51,7 +51,7 @@ class TestMockContextManager(mock_context_manager.MockContextManager):
|
||||||
|
|
||||||
class MockRunner(runner.Runner):
|
class MockRunner(runner.Runner):
|
||||||
def __init__(self, model, package_loader, functions):
|
def __init__(self, model, package_loader, functions):
|
||||||
if isinstance(model, basestring):
|
if isinstance(model, six.string_types):
|
||||||
model = om.Object(model)
|
model = om.Object(model)
|
||||||
model = om.build_model(model)
|
model = om.build_model(model)
|
||||||
if 'Objects' not in model:
|
if 'Objects' not in model:
|
||||||
|
|
Loading…
Reference in New Issue