Fix several python3 incompatibilities

Change-Id: Ibf5dd6c0b6bcd161364daf35d618641f6079acf5
This commit is contained in:
Ivan A. Melnikov
2013-10-10 15:06:44 +04:00
parent 10f3cfca48
commit c1bee55192
7 changed files with 20 additions and 17 deletions

View File

@@ -17,12 +17,12 @@
# under the License. # under the License.
import logging import logging
import urlparse from six.moves import urllib_parse as urlparse # noqa
from stevedore import driver from stevedore import driver
from taskflow import exceptions as exc from taskflow import exceptions as exc
# NOTE(harlowja): this is the entrypoint namespace, not the module namespace. # NOTE(harlowja): this is the entrypoint namespace, not the module namespace.
BACKEND_NAMESPACE = 'taskflow.persistence' BACKEND_NAMESPACE = 'taskflow.persistence'

View File

@@ -21,6 +21,7 @@ import errno
import logging import logging
import os import os
import shutil import shutil
import six
import threading import threading
import weakref import weakref
@@ -103,11 +104,13 @@ class Connection(base.Connection):
cache_info = self._file_cache.setdefault(filename, {}) cache_info = self._file_cache.setdefault(filename, {})
if not cache_info or mtime > cache_info.get('mtime', 0): if not cache_info or mtime > cache_info.get('mtime', 0):
with open(filename, 'rb') as fp: with open(filename, 'rb') as fp:
cache_info['data'] = fp.read() cache_info['data'] = fp.read().decode('utf-8')
cache_info['mtime'] = mtime cache_info['mtime'] = mtime
return cache_info['data'] return cache_info['data']
def _write_to(self, filename, contents): def _write_to(self, filename, contents):
if isinstance(contents, six.text_type):
contents = contents.encode('utf-8')
with open(filename, 'wb') as fp: with open(filename, 'wb') as fp:
fp.write(contents) fp.write(contents)
self._file_cache.pop(filename, None) self._file_cache.pop(filename, None)
@@ -405,7 +408,7 @@ def _str_2_datetime(text):
"""Converts an iso8601 string/text into a datetime object (or none)""" """Converts an iso8601 string/text into a datetime object (or none)"""
if text is None: if text is None:
return None return None
if not isinstance(text, basestring): if not isinstance(text, six.string_types):
raise ValueError("Can only convert strings into a datetime object and" raise ValueError("Can only convert strings into a datetime object and"
" not %r" % (text)) " not %r" % (text))
if not len(text): if not len(text):

View File

@@ -18,7 +18,6 @@
import contextlib import contextlib
import logging import logging
import six import six
from taskflow import exceptions from taskflow import exceptions
@@ -171,7 +170,7 @@ class Storage(object):
result_mapping = self._result_mappings.get(uuid, None) result_mapping = self._result_mappings.get(uuid, None)
if result_mapping is None: if result_mapping is None:
return return
for name, index in result_mapping.items(): for name, index in six.iteritems(result_mapping):
try: try:
_item_from_result(data, index, name) _item_from_result(data, index, name)
except exceptions.NotFound: except exceptions.NotFound:
@@ -220,7 +219,7 @@ class Storage(object):
self.save(injector_uuid, pairs) self.save(injector_uuid, pairs)
self.set_result_mapping(injector_uuid, self.set_result_mapping(injector_uuid,
dict((name, name) dict((name, name)
for name in pairs.iterkeys())) for name in six.iterkeys(pairs)))
def set_result_mapping(self, uuid, mapping): def set_result_mapping(self, uuid, mapping):
"""Set mapping for naming task results """Set mapping for naming task results
@@ -233,7 +232,7 @@ class Storage(object):
if not mapping: if not mapping:
return return
self._result_mappings[uuid] = mapping self._result_mappings[uuid] = mapping
for name, index in mapping.iteritems(): for name, index in six.iteritems(mapping):
entries = self._reverse_mapping.setdefault(name, []) entries = self._reverse_mapping.setdefault(name, [])
entries.append((uuid, index)) entries.append((uuid, index))
if len(entries) > 1: if len(entries) > 1:
@@ -271,7 +270,7 @@ class Storage(object):
def fetch_mapped_args(self, args_mapping): def fetch_mapped_args(self, args_mapping):
"""Fetch arguments for the task using arguments mapping""" """Fetch arguments for the task using arguments mapping"""
return dict((key, self.fetch(name)) return dict((key, self.fetch(name))
for key, name in args_mapping.iteritems()) for key, name in six.iteritems(args_mapping))
def set_flow_state(self, state): def set_flow_state(self, state):
"""Set flowdetails state and save it""" """Set flowdetails state and save it"""

View File

@@ -116,7 +116,7 @@ class FailureObjectTestCase(test.TestCase):
traceback_str=None, traceback_str=None,
exc_type_names=['Exception'], exc_type_names=['Exception'],
hi='hi there') hi='hi there')
expected = "Failure.__init__ got unexpected keyword argument: 'hi'" expected = "Failure.__init__ got unexpected keyword argument(s): hi"
self.assertEquals(str(ctx.exception), expected) self.assertEquals(str(ctx.exception), expected)
def test_empty_does_not_reraise(self): def test_empty_does_not_reraise(self):

View File

@@ -85,7 +85,7 @@ class MultiLock(object):
return lock.locked() return lock.locked()
return False return False
for i in xrange(0, len(self._locked)): for i in range(0, len(self._locked)):
if self._locked[i] or is_locked(self._locks[i]): if self._locked[i] or is_locked(self._locks[i]):
raise threading.ThreadError("Lock %s not previously released" raise threading.ThreadError("Lock %s not previously released"
% (i + 1)) % (i + 1))

View File

@@ -21,16 +21,15 @@ import collections
import copy import copy
import errno import errno
import functools import functools
import itertools
import logging import logging
import os import os
import six
import sys import sys
import traceback import traceback
from taskflow import exceptions from taskflow import exceptions
from taskflow.utils import reflection from taskflow.utils import reflection
import six
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@@ -66,7 +65,7 @@ def get_version_string(obj):
def get_duplicate_keys(iterable, key=None): def get_duplicate_keys(iterable, key=None):
if key is not None: if key is not None:
iterable = itertools.imap(key, iterable) iterable = six.moves.map(key, iterable)
keys = set() keys = set()
duplicates = set() duplicates = set()
for item in iterable: for item in iterable:
@@ -254,8 +253,9 @@ class Failure(object):
self._exc_type_names = kwargs.pop('exc_type_names', []) self._exc_type_names = kwargs.pop('exc_type_names', [])
self._traceback_str = kwargs.pop('traceback_str', None) self._traceback_str = kwargs.pop('traceback_str', None)
if kwargs: if kwargs:
raise TypeError('Failure.__init__ got unexpected keyword ' raise TypeError(
'argument: %r' % kwargs.keys()[0]) 'Failure.__init__ got unexpected keyword argument(s): %s'
% ', '.join(six.iterkeys(kwargs)))
def _matches(self, other): def _matches(self, other):
if self is other: if self is other:

View File

@@ -18,6 +18,7 @@
import logging import logging
import multiprocessing import multiprocessing
import six
import threading import threading
import time import time
import types import types
@@ -98,7 +99,7 @@ class ThreadSafeMeta(type):
"""Metaclass that adds locking to all pubic methods of a class""" """Metaclass that adds locking to all pubic methods of a class"""
def __new__(cls, name, bases, attrs): def __new__(cls, name, bases, attrs):
for attr_name, attr_value in attrs.iteritems(): for attr_name, attr_value in six.iteritems(attrs):
if isinstance(attr_value, types.FunctionType): if isinstance(attr_value, types.FunctionType):
if attr_name[0] != '_': if attr_name[0] != '_':
attrs[attr_name] = lock_utils.locked(attr_value) attrs[attr_name] = lock_utils.locked(attr_value)