From f963039bd76a6e35629cac8fcb7a48593521e15a Mon Sep 17 00:00:00 2001 From: Ravi Shekhar Jethani Date: Mon, 18 Jan 2016 23:25:53 -0800 Subject: [PATCH] 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 --- HACKING.rst | 1 + murano/common/helpers/token_sanitizer.py | 2 +- murano/common/utils.py | 6 +++--- murano/common/wsgi.py | 2 +- murano/dsl/dsl.py | 6 +++--- murano/dsl/executor.py | 2 +- murano/dsl/helpers.py | 8 +++++--- murano/dsl/macros.py | 2 +- murano/dsl/serializer.py | 3 ++- murano/dsl/type_scheme.py | 4 ++-- murano/dsl/yaql_expression.py | 4 ++-- murano/engine/system/status_reporter.py | 3 ++- murano/engine/system/yaql_functions.py | 4 ++-- murano/hacking/checks.py | 8 ++++++++ murano/packages/hot_package.py | 2 +- murano/tests/unit/dsl/foundation/runner.py | 4 ++-- murano/tests/unit/dsl/test_contracts.py | 6 ++++-- murano/tests/unit/dsl/test_engine_yaql_functions.py | 5 +++-- murano/tests/unit/dsl/test_results_serializer.py | 3 ++- murano/tests/unit/engine/test_mock_context_manager.py | 2 +- 20 files changed, 47 insertions(+), 30 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index 760bcb18e..4846b9c53 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -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. diff --git a/murano/common/helpers/token_sanitizer.py b/murano/common/helpers/token_sanitizer.py index 0fb1308ed..558222751 100644 --- a/murano/common/helpers/token_sanitizer.py +++ b/murano/common/helpers/token_sanitizer.py @@ -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: diff --git a/murano/common/utils.py b/murano/common/utils.py index b7f1eae31..64a064d53 100644 --- a/murano/common/utils.py +++ b/murano/common/utils.py @@ -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() diff --git a/murano/common/wsgi.py b/murano/common/wsgi.py index a9e33b0f3..da9fbb866 100644 --- a/murano/common/wsgi.py +++ b/murano/common/wsgi.py @@ -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} diff --git a/murano/dsl/dsl.py b/murano/dsl/dsl.py index 91466f5a0..51da1960e 100644 --- a/murano/dsl/dsl.py +++ b/murano/dsl/dsl.py @@ -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) diff --git a/murano/dsl/executor.py b/murano/dsl/executor.py index 2fde7ad64..616ecedac 100644 --- a/murano/dsl/executor.py +++ b/murano/dsl/executor.py @@ -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 diff --git a/murano/dsl/helpers.py b/murano/dsl/helpers.py index 947e1c61d..e4107028d 100644 --- a/murano/dsl/helpers.py +++ b/murano/dsl/helpers.py @@ -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: diff --git a/murano/dsl/macros.py b/murano/dsl/macros.py index ba52383e8..d8fe91dff 100644 --- a/murano/dsl/macros.py +++ b/murano/dsl/macros.py @@ -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) diff --git a/murano/dsl/serializer.py b/murano/dsl/serializer.py index 35129424e..3dfb6999a 100644 --- a/murano/dsl/serializer.py +++ b/murano/dsl/serializer.py @@ -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: diff --git a/murano/dsl/type_scheme.py b/murano/dsl/type_scheme.py index 7d9de564c..3cd67be34 100644 --- a/murano/dsl/type_scheme.py +++ b/murano/dsl/type_scheme.py @@ -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) diff --git a/murano/dsl/yaql_expression.py b/murano/dsl/yaql_expression.py index 62905b72e..bd06e14b5 100644 --- a/murano/dsl/yaql_expression.py +++ b/murano/dsl/yaql_expression.py @@ -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 diff --git a/murano/engine/system/status_reporter.py b/murano/engine/system/status_reporter.py index 5c835d310..ff9f3ba57 100644 --- a/murano/engine/system/status_reporter.py +++ b/murano/engine/system/status_reporter.py @@ -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 diff --git a/murano/engine/system/yaql_functions.py b/murano/engine/system/yaql_functions.py index c5fd10fd4..203d38107 100644 --- a/murano/engine/system/yaql_functions.py +++ b/murano/engine/system/yaql_functions.py @@ -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 diff --git a/murano/hacking/checks.py b/murano/hacking/checks.py index f5805e375..f6e0c358b 100644 --- a/murano/hacking/checks.py +++ b/murano/hacking/checks.py @@ -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) diff --git a/murano/packages/hot_package.py b/murano/packages/hot_package.py index a0776e50d..35e949cb4 100644 --- a/murano/packages/hot_package.py +++ b/murano/packages/hot_package.py @@ -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) diff --git a/murano/tests/unit/dsl/foundation/runner.py b/murano/tests/unit/dsl/foundation/runner.py index 683123780..bbc14d309 100644 --- a/murano/tests/unit/dsl/foundation/runner.py +++ b/murano/tests/unit/dsl/foundation/runner.py @@ -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: diff --git a/murano/tests/unit/dsl/test_contracts.py b/murano/tests/unit/dsl/test_contracts.py index 511aab76b..cf71d6d7c 100644 --- a/murano/tests/unit/dsl/test_contracts.py +++ b/murano/tests/unit/dsl/test_contracts.py @@ -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): diff --git a/murano/tests/unit/dsl/test_engine_yaql_functions.py b/murano/tests/unit/dsl/test_engine_yaql_functions.py index 8a3e59814..4aa6be932 100644 --- a/murano/tests/unit/dsl/test_engine_yaql_functions.py +++ b/murano/tests/unit/dsl/test_engine_yaql_functions.py @@ -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)) diff --git a/murano/tests/unit/dsl/test_results_serializer.py b/murano/tests/unit/dsl/test_results_serializer.py index 813cba9c7..0c6c33314 100644 --- a/murano/tests/unit/dsl/test_results_serializer.py +++ b/murano/tests/unit/dsl/test_results_serializer.py @@ -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')) diff --git a/murano/tests/unit/engine/test_mock_context_manager.py b/murano/tests/unit/engine/test_mock_context_manager.py index 76ff54b43..10583a1a0 100644 --- a/murano/tests/unit/engine/test_mock_context_manager.py +++ b/murano/tests/unit/engine/test_mock_context_manager.py @@ -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: