Drop types module usage

Since python 2.2, use of types module is not prefered way to
access the type hence it is replaced by using names of built-in
factory functions.

Change-Id: Ibdcc1ae75204f67076edab5edcffe94a9f1f1d8e
Implements: blueprint replace-types-module-with-new-style-classes
This commit is contained in:
enthurohini 2015-10-29 20:02:36 +05:30
parent 8ee1335b5e
commit f49bdb6218
21 changed files with 55 additions and 84 deletions

View File

@ -13,8 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import types
class TokenSanitizer(object):
"""Helper class for cleaning some object from different passwords/tokens.
@ -48,16 +46,16 @@ class TokenSanitizer(object):
def sanitize(self, obj):
"""Replaces each token found in object by message.
:param obj: types.DictType, types.ListType, types.Tuple, object
:param obj: dict, list, tuple, object
:return: Sanitized object
"""
if isinstance(obj, types.DictType):
if isinstance(obj, dict):
return dict([self.sanitize(item) for item in obj.iteritems()])
elif isinstance(obj, types.ListType):
elif isinstance(obj, list):
return [self.sanitize(item) for item in obj]
elif isinstance(obj, types.TupleType):
elif isinstance(obj, tuple):
k, v = obj
if self._contains_token(k) and isinstance(v, types.StringTypes):
if self._contains_token(k) and isinstance(v, basestring):
return k, self.message
return k, self.sanitize(v)
else:

View File

@ -14,7 +14,6 @@
import collections
import functools as func
import types
import eventlet
import jsonschema
@ -27,8 +26,7 @@ LOG = logging.getLogger(__name__)
class TraverseHelper(object):
value_type = (types.StringTypes, types.IntType, types.FloatType,
types.BooleanType)
value_type = (basestring, int, float, bool)
@staticmethod
def get(path, source):
@ -60,7 +58,7 @@ class TraverseHelper(object):
while len(queue):
path = queue.popleft()
if isinstance(source, types.ListType):
if isinstance(source, list):
idx_source = source
iterator = (
i for i in source
@ -69,7 +67,7 @@ class TraverseHelper(object):
source = next(iterator, None)
if source is None and path.isdigit():
source = idx_source[int(path)]
elif isinstance(source, types.DictionaryType):
elif isinstance(source, dict):
source = source[path]
elif isinstance(source, TraverseHelper.value_type):
break
@ -126,14 +124,14 @@ class TraverseHelper(object):
node = TraverseHelper.get(parent_path, source)
key = path[1:].split('/')[-1]
if isinstance(node, types.ListType):
if isinstance(node, list):
iterator = (i for i in node if i.get('?', {}).get('id') == key)
item = next(iterator, None)
if item is None and key.isdigit():
del node[int(key)]
else:
node.remove(item)
elif isinstance(node, types.DictionaryType):
elif isinstance(node, dict):
del node[key]
else:
raise ValueError(_('Source object or path is malformed'))
@ -197,12 +195,12 @@ def is_different(obj1, obj2):
def build_entity_map(value):
def build_entity_map_recursive(value, id_map):
if isinstance(value, types.DictionaryType):
if isinstance(value, dict):
if '?' in value and 'id' in value['?']:
id_map[value['?']['id']] = value
for v in value.itervalues():
build_entity_map_recursive(v, id_map)
if isinstance(value, types.ListType):
if isinstance(value, list):
for item in value:
build_entity_map_recursive(item, id_map)

View File

@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import types
from oslo_log import log as logging
from oslo_utils import timeutils
from webob import exc
@ -119,7 +117,7 @@ class CoreServices(object):
temp_description['services'] = []
if path == '/services':
if isinstance(data, types.ListType):
if isinstance(data, list):
utils.TraverseHelper.extend(path, data, temp_description)
else:
utils.TraverseHelper.insert(path, data, temp_description)
@ -149,7 +147,7 @@ class CoreServices(object):
temp_description['services'] = []
if path == '/services':
if isinstance(data, types.ListType):
if isinstance(data, list):
utils.TraverseHelper.extend(path, data, temp_description)
else:
utils.TraverseHelper.insert(path, data, temp_description)
@ -175,7 +173,7 @@ class CoreServices(object):
env_description['services'] = []
if path == '/services':
if isinstance(data, types.ListType):
if isinstance(data, list):
utils.TraverseHelper.extend(path, data, env_description)
else:
utils.TraverseHelper.insert(path, data, env_description)

View File

@ -14,7 +14,6 @@
import inspect
import os.path
import types
from yaql.language import expressions as yaql_expressions
from yaql.language import utils
@ -51,7 +50,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, types.StringTypes):
if isinstance(murano_class, basestring):
murano_class_name = murano_class
else:
murano_class_name = murano_class.name
@ -94,7 +93,7 @@ class MuranoTypeName(yaqltypes.LazyParameterType, yaqltypes.PythonType):
def __init__(self, nullable=False, context=None):
self._context = context
super(MuranoTypeName, self).__init__(
(dsl_types.MuranoClassReference,) + types.StringTypes, nullable)
(dsl_types.MuranoClassReference, basestring), nullable)
def convert(self, value, sender, context, function_spec, engine,
*args, **kwargs):
@ -103,7 +102,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, types.StringTypes):
if isinstance(value, basestring):
murano_type = helpers.get_type(context)
value = dsl_types.MuranoClassReference(
helpers.get_class(

View File

@ -15,7 +15,6 @@
import collections
import contextlib
import itertools
import types
import weakref
import eventlet
@ -184,7 +183,7 @@ class MuranoDslExecutor(object):
return tuple(), parameter_values
def load(self, data):
if not isinstance(data, types.DictionaryType):
if not isinstance(data, dict):
raise TypeError()
self._attribute_store.load(data.get(constants.DM_ATTRIBUTES) or [])
result = self._object_store.load(data.get(constants.DM_OBJECTS), None)
@ -216,16 +215,16 @@ class MuranoDslExecutor(object):
'on {0}: {1}').format(obj, e), exc_info=True)
def _list_potential_object_ids(self, data):
if isinstance(data, types.DictionaryType):
if isinstance(data, dict):
sys_dict = data.get('?')
if (isinstance(sys_dict, types.DictionaryType) and
if (isinstance(sys_dict, dict) and
sys_dict.get('id') and sys_dict.get('type')):
yield sys_dict['id']
for val in data.itervalues():
for res in self._list_potential_object_ids(val):
yield res
elif isinstance(data, collections.Iterable) and not isinstance(
data, types.StringTypes):
data, basestring):
for val in data:
for res in self._list_potential_object_ids(val):
yield res

View File

@ -12,7 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import types
from murano.dsl import dsl_exception
from murano.dsl import helpers
@ -45,7 +44,7 @@ class Statement(DslExpression):
if isinstance(statement, yaql_expression.YaqlExpression):
key = None
value = statement
elif isinstance(statement, types.DictionaryType):
elif isinstance(statement, dict):
if len(statement) != 1:
raise SyntaxError()
key = statement.keys()[0]
@ -81,7 +80,7 @@ def parse_expression(expr):
result = None
if isinstance(expr, yaql_expression.YaqlExpression):
result = Statement(expr)
elif isinstance(expr, types.DictionaryType):
elif isinstance(expr, dict):
kwds = {}
for key, value in expr.iteritems():
if isinstance(key, yaql_expression.YaqlExpression):

View File

@ -18,7 +18,6 @@ import functools
import re
import string
import sys
import types
import uuid
import eventlet.greenpool
@ -83,15 +82,15 @@ def merge_dicts(dict1, dict2, max_levels=0):
if key in dict2:
value2 = dict2[key]
if type(value2) != type(value1):
if (isinstance(value1, types.StringTypes) and
isinstance(value2, types.StringTypes)):
if (isinstance(value1, basestring) and
isinstance(value2, basestring)):
continue
raise TypeError()
if max_levels != 1 and isinstance(value2, types.DictionaryType):
if max_levels != 1 and isinstance(value2, dict):
result[key] = merge_dicts(
value1, value2,
0 if max_levels == 0 else max_levels - 1)
elif max_levels != 1 and isinstance(value2, types.ListType):
elif max_levels != 1 and isinstance(value2, list):
result[key] = merge_lists(value1, value2)
else:
result[key] = value2
@ -285,7 +284,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, types.StringTypes):
elif isinstance(pov_or_version_spec, basestring):
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

@ -13,7 +13,6 @@
# under the License.
import itertools
import types
from yaql.language import specs
from yaql.language import utils
@ -109,7 +108,7 @@ class LhsExpression(object):
if utils.is_sequence(src):
src_property.set(src[:index] + (value,) + src[index + 1:])
if isinstance(index, types.IntType):
if isinstance(index, int):
return LhsExpression.Property(
lambda: getter(this.get()),
lambda value: setter(this, value))

View File

@ -12,7 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import types
from murano.dsl import constants
from murano.dsl import dsl_exception
@ -24,7 +23,7 @@ from murano.dsl import yaql_expression
class CodeBlock(expressions.DslExpression):
def __init__(self, body):
if not isinstance(body, types.ListType):
if not isinstance(body, list):
body = [body]
self.code_block = map(expressions.parse_expression, body)
@ -140,7 +139,7 @@ class WhileDoMacro(expressions.DslExpression):
class ForMacro(expressions.DslExpression):
def __init__(self, For, In, Do):
if not isinstance(For, types.StringTypes):
if not isinstance(For, basestring):
raise exceptions.DslSyntaxError(
'For value must be of string type')
self._code = CodeBlock(Do)
@ -180,7 +179,7 @@ class RepeatMacro(expressions.DslExpression):
class MatchMacro(expressions.DslExpression):
def __init__(self, Match, Value, Default=None):
if not isinstance(Match, types.DictionaryType):
if not isinstance(Match, dict):
raise exceptions.DslSyntaxError(
'Match value must be of dictionary type')
self._switch = Match
@ -199,7 +198,7 @@ class MatchMacro(expressions.DslExpression):
class SwitchMacro(expressions.DslExpression):
def __init__(self, Switch, Default=None):
if not isinstance(Switch, types.DictionaryType):
if not isinstance(Switch, dict):
raise exceptions.DslSyntaxError(
'Switch value must be of dictionary type')
self._switch = Switch

View File

@ -13,7 +13,6 @@
# under the License.
import collections
import types
import weakref
from yaql.language import specs
@ -59,12 +58,12 @@ class MuranoMethod(dsl_types.MuranoMethod):
self._body = macros.MethodBlock(payload.get('Body') or [], name)
self._usage = payload.get('Usage') or MethodUsages.Runtime
arguments_scheme = payload.get('Arguments') or []
if isinstance(arguments_scheme, types.DictionaryType):
if isinstance(arguments_scheme, dict):
arguments_scheme = [{key: value} for key, value in
arguments_scheme.iteritems()]
self._arguments_scheme = collections.OrderedDict()
for record in arguments_scheme:
if (not isinstance(record, types.DictionaryType) or
if (not isinstance(record, dict) or
len(record) > 1):
raise ValueError()
name = record.keys()[0]

View File

@ -12,7 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import types
from yaql import utils
@ -86,8 +85,7 @@ def _pass12_serialize(value, parent, serialized_objects,
designer_attributes_getter):
if isinstance(value, dsl.MuranoObjectInterface):
value = value.object
if isinstance(value, (types.StringTypes, types.IntType, types.FloatType,
types.BooleanType, types.NoneType)):
if isinstance(value, (basestring, 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

@ -13,7 +13,6 @@
# under the License.
import sys
import types
import uuid
from yaql.language import specs
@ -175,7 +174,7 @@ class TypeScheme(object):
obj = object_store.load(
value, owner, root_context, defaults=default)
elif isinstance(value, types.StringTypes):
elif isinstance(value, basestring):
obj = object_store.get(value)
if obj is None:
if not object_store.initializing:
@ -255,10 +254,10 @@ class TypeScheme(object):
shift = 0
max_length = sys.maxint
min_length = 0
if isinstance(spec[-1], types.IntType):
if isinstance(spec[-1], int):
min_length = spec[-1]
shift += 1
if len(spec) >= 2 and isinstance(spec[-2], types.IntType):
if len(spec) >= 2 and isinstance(spec[-2], int):
max_length = min_length
min_length = spec[-2]
shift += 1
@ -317,6 +316,6 @@ class TypeScheme(object):
def format_scalar(value):
if isinstance(value, types.StringTypes):
if isinstance(value, basestring):
return "'{0}'".format(value)
return unicode(value)

View File

@ -13,7 +13,6 @@
# under the License.
import re
import types
from yaql.language import exceptions as yaql_exceptions
from yaql.language import expressions
@ -26,7 +25,7 @@ from murano.dsl import yaql_integration
class YaqlExpression(dsl_types.YaqlExpression):
def __init__(self, expression, version):
self._version = version
if isinstance(expression, types.StringTypes):
if isinstance(expression, basestring):
self._expression = unicode(expression)
self._parsed_expression = yaql_integration.parse(
self._expression, version)
@ -66,7 +65,7 @@ class YaqlExpression(dsl_types.YaqlExpression):
@staticmethod
def is_expression(expression, version):
if not isinstance(expression, types.StringTypes):
if not isinstance(expression, basestring):
return False
if re.match('^[\s\w\d.:]*$', expression):
return False

View File

@ -16,7 +16,6 @@
import copy
import datetime
import os
import types
import urlparse
import uuid
@ -207,7 +206,7 @@ class Agent(object):
def build_execution_plan(self, template, resources):
template = copy.deepcopy(template)
if not isinstance(template, types.DictionaryType):
if not isinstance(template, dict):
raise ValueError('Incorrect execution plan ')
format_version = template.get('FormatVersion')
if not format_version or format_version.startswith('1.'):

View File

@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import types
from oslo_config import cfg
import oslo_messaging as messaging
@ -35,7 +34,7 @@ class StatusReporter(object):
StatusReporter.transport,
publisher_id=uuidutils.generate_uuid(),
topic='murano')
if isinstance(environment, types.StringTypes):
if isinstance(environment, basestring):
self._environment_id = environment
else:
self._environment_id = environment.id

View File

@ -19,7 +19,6 @@ import random
import re
import string
import time
import types
import jsonpatch
import jsonpointer
@ -59,7 +58,7 @@ def pselect(collection, composer):
@specs.parameter('mappings', collections.Mapping)
@specs.extension_method
def bind(obj, mappings):
if isinstance(obj, types.StringTypes) and obj.startswith('$'):
if isinstance(obj, basestring) and obj.startswith('$'):
value = _convert_macro_parameter(obj[1:], mappings)
if value is not None:
return value
@ -70,7 +69,7 @@ def bind(obj, mappings):
for key, value in obj.iteritems():
result[bind(key, mappings)] = bind(value, mappings)
return result
elif isinstance(obj, types.StringTypes) and obj.startswith('$'):
elif isinstance(obj, basestring) and obj.startswith('$'):
value = _convert_macro_parameter(obj[1:], mappings)
if value is not None:
return value

View File

@ -15,7 +15,6 @@
import os
import shutil
import sys
import types
import yaml
@ -249,7 +248,7 @@ class HotPackage(package_base.PackageBase):
@staticmethod
def _format_value(value):
if isinstance(value, types.StringTypes):
if isinstance(value, basestring):
return str("'" + value + "'")
return str(value)

View File

@ -14,7 +14,6 @@
import sys
import types
from murano.dsl import context_manager
from murano.dsl import dsl
@ -48,7 +47,7 @@ class Runner(object):
class DslObjectWrapper(object):
def __init__(self, obj, runner):
self._runner = runner
if isinstance(obj, types.StringTypes):
if isinstance(obj, basestring):
self._object_id = obj
elif isinstance(obj, (object_model.Object, object_model.Ref)):
self._object_id = obj.id
@ -68,7 +67,7 @@ class Runner(object):
return call
def __init__(self, model, package_loader, functions):
if isinstance(model, types.StringTypes):
if isinstance(model, basestring):
model = object_model.Object(model)
model = object_model.build_model(model)
if 'Objects' not in model:

View File

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

View File

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

View File

@ -12,9 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import types
from testtools import matchers
from murano.dsl import serializer
@ -89,7 +86,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'), types.StringTypes)
self.assertIsInstance(action.get('name'), basestring)
self.assertThat(
action['name'],
matchers.StartsWith('test'))