Python3: Replace dict.itervalues with six.itervalues

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: I50a8b2f98ddbc2819ae180b68468e0a51c13281b
This commit is contained in:
Ravi Shekhar Jethani 2016-01-18 03:04:23 -08:00
parent d99904e38b
commit e633fd004a
8 changed files with 18 additions and 9 deletions

View File

@ -9,3 +9,4 @@ Murano Specific Commandments
- [M322] Method's default argument shouldn't be mutable. - [M322] Method's default argument shouldn't be mutable.
- [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.

View File

@ -118,7 +118,7 @@ class MuranoTestRunner(object):
m for m in class_to_methods[class_to_test] m for m in class_to_methods[class_to_test]
if m == test_method] if m == test_method]
continue continue
methods_count = sum(len(v) for v in methods_to_run.itervalues()) methods_count = sum(len(v) for v in six.itervalues(methods_to_run))
methods = [k + '.' + method methods = [k + '.' + method
for k, v in six.iteritems(methods_to_run) for method in v] for k, v in six.iteritems(methods_to_run) for method in v]
LOG.debug('{0} method(s) is(are) going to be executed: ' LOG.debug('{0} method(s) is(are) going to be executed: '

View File

@ -18,6 +18,7 @@ import functools as func
import eventlet import eventlet
import jsonschema import jsonschema
from oslo_log import log as logging from oslo_log import log as logging
import six
from murano.common.i18n import _, _LE from murano.common.i18n import _, _LE
@ -198,7 +199,7 @@ def build_entity_map(value):
if isinstance(value, dict): if isinstance(value, dict):
if '?' in value and 'id' in value['?']: if '?' in value and 'id' in value['?']:
id_map[value['?']['id']] = value id_map[value['?']['id']] = value
for v in value.itervalues(): for v in six.itervalues(value):
build_entity_map_recursive(v, id_map) build_entity_map_recursive(v, id_map)
if isinstance(value, list): if isinstance(value, list):
for item in value: for item in value:

View File

@ -221,7 +221,7 @@ class MuranoDslExecutor(object):
if (isinstance(sys_dict, dict) and if (isinstance(sys_dict, dict) and
sys_dict.get('id') and sys_dict.get('type')): sys_dict.get('id') and sys_dict.get('type')):
yield sys_dict['id'] yield sys_dict['id']
for val in data.itervalues(): for val in six.itervalues(data):
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(

View File

@ -179,7 +179,7 @@ class MuranoClass(dsl_types.MuranoClass):
def find_methods(self, predicate): def find_methods(self, predicate):
result = [] result = []
for c in self.ancestors(): for c in self.ancestors():
for method in c.methods.itervalues(): for method in six.itervalues(c.methods):
if predicate(method) and method not in result: if predicate(method) and method not in result:
result.append(method) result.append(method)
return result return result
@ -286,7 +286,7 @@ class MuranoClass(dsl_types.MuranoClass):
(parent.package, requirement)) (parent.package, requirement))
package_bindings = {} package_bindings = {}
for versions in aggregation.itervalues(): for versions in six.itervalues(aggregation):
mappings = self._remap_package(versions) mappings = self._remap_package(versions)
package_bindings.update(mappings) package_bindings.update(mappings)

View File

@ -252,7 +252,7 @@ def get_class_factory_definition(cls, murano_class):
def filter_parameters(__fd, *args, **kwargs): def filter_parameters(__fd, *args, **kwargs):
if '*' not in __fd.parameters: if '*' not in __fd.parameters:
position_args = 0 position_args = 0
for p in __fd.parameters.itervalues(): for p in six.itervalues(__fd.parameters):
if p.position is not None: if p.position is not None:
position_args += 1 position_args += 1
args = args[:position_args] args = args[:position_args]
@ -261,7 +261,7 @@ def filter_parameters(__fd, *args, **kwargs):
if not helpers.is_keyword(name): if not helpers.is_keyword(name):
del kwargs[name] del kwargs[name]
if '**' not in __fd.parameters: if '**' not in __fd.parameters:
names = {p.alias or p.name for p in __fd.parameters.itervalues()} names = {p.alias or p.name for p in six.itervalues(__fd.parameters)}
for name in kwargs.keys(): for name in kwargs.keys():
if name not in names: if name not in names:
del kwargs[name] del kwargs[name]

View File

@ -261,8 +261,8 @@ class DirectoryPackageLoader(package_loader.MuranoPackageLoader):
@property @property
def packages(self): def packages(self):
for package_versions in self._packages_by_name.itervalues(): for package_versions in six.itervalues(self._packages_by_name):
for package in package_versions.itervalues(): for package in six.itervalues(package_versions):
yield package yield package
@staticmethod @staticmethod

View File

@ -49,7 +49,14 @@ def check_python3_no_iterkeys(logical_line):
yield(0, msg) yield(0, msg)
def check_python3_no_itervalues(logical_line):
if re.search(r".*\.itervalues\(\)", logical_line):
msg = ("M325: Use six.itervalues() instead of dict.itervalues().")
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)