From 9c7dbf8fd009a9be3d6b7d64abeb105c869e8660 Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Sat, 19 Oct 2024 23:27:48 +0900 Subject: [PATCH] Run pyupgrade to clean up Python 2 syntaxes Update all .py source files by $ pyupgrade --py3-only $(git ls-files | grep ".py$") to modernize the code according to Python 3 syntaxes. pep8 errors are fixed by $ autopep8 --select=E127,E128,E501 --max-line-length 79 -r \ --in-place tooz Also add the pyupgrade hook to pre-commit to avoid merging additional Python 2 syntaxes. Change-Id: I18b9e51964afe739ddabd3d074431c27a09a8508 --- .pre-commit-config.yaml | 11 ++++-- doc/source/conf.py | 5 +-- releasenotes/source/conf.py | 17 ++++----- tools/compat-matrix.py | 2 - tooz/__init__.py | 3 +- tooz/_retry.py | 1 - tooz/coordination.py | 53 +++++++++++++-------------- tooz/drivers/consul.py | 5 +-- tooz/drivers/etcd.py | 7 ++-- tooz/drivers/etcd3gw.py | 7 ++-- tooz/drivers/file.py | 53 +++++++++++++-------------- tooz/drivers/ipc.py | 11 +++--- tooz/drivers/memcached.py | 18 ++++----- tooz/drivers/mysql.py | 5 +-- tooz/drivers/pgsql.py | 11 +++--- tooz/drivers/redis.py | 18 ++++----- tooz/drivers/zake.py | 2 +- tooz/drivers/zookeeper.py | 13 +++---- tooz/hashring.py | 5 +-- tooz/locking.py | 7 ++-- tooz/partitioner.py | 3 +- tooz/tests/__init__.py | 5 +-- tooz/tests/drivers/test_etcd.py | 3 +- tooz/tests/drivers/test_etcd3gw.py | 1 - tooz/tests/drivers/test_file.py | 2 - tooz/tests/drivers/test_memcache.py | 2 - tooz/tests/drivers/test_mysql.py | 2 - tooz/tests/drivers/test_postgresql.py | 2 - tooz/tests/test_coordination.py | 3 +- tooz/tests/test_hashring.py | 6 +-- tooz/tests/test_partitioner.py | 11 +++--- tooz/tests/test_utils.py | 2 - tooz/utils.py | 6 +-- 33 files changed, 134 insertions(+), 168 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b1b958c0..abf463c2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v5.0.0 hooks: - id: trailing-whitespace # Replaces or checks mixed line ending @@ -19,12 +19,17 @@ repos: - id: check-yaml files: .*\.(yaml|yml)$ - repo: https://opendev.org/openstack/hacking - rev: 6.1.0 + rev: 7.0.0 hooks: - id: hacking additional_dependencies: [] exclude: '^(doc|releasenotes|tools)/.*$' - repo: https://github.com/PyCQA/doc8 - rev: v1.1.1 + rev: v1.1.2 hooks: - id: doc8 + - repo: https://github.com/asottile/pyupgrade + rev: v3.18.0 + hooks: + - id: pyupgrade + args: [--py3-only] diff --git a/doc/source/conf.py b/doc/source/conf.py index e4672a8d..b6818237 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright (C) 2020 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -80,8 +79,8 @@ latex_elements = {} # (source start file, target name, title, author, # documentclass [howto/manual]). latex_documents = [ - ('index', 'tooz.tex', u'tooz Documentation', - u'eNovance', 'manual'), + ('index', 'tooz.tex', 'tooz Documentation', + 'eNovance', 'manual'), ] # Grouping the document tree into Texinfo files. List of tuples diff --git a/releasenotes/source/conf.py b/releasenotes/source/conf.py index 19a358cf..893d448e 100644 --- a/releasenotes/source/conf.py +++ b/releasenotes/source/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright (C) 2020 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -60,8 +59,8 @@ source_suffix = '.rst' master_doc = 'index' # General information about the project. -project = u'tooz Release Notes' -copyright = u'2016, tooz Developers' +project = 'tooz Release Notes' +copyright = '2016, tooz Developers' # Release do not need a version number in the title, they # cover multiple versions. @@ -208,8 +207,8 @@ latex_elements = { # author, documentclass [howto, manual, or own class]). latex_documents = [ ('index', 'toozReleaseNotes.tex', - u'tooz Release Notes Documentation', - u'tooz Developers', 'manual'), + 'tooz Release Notes Documentation', + 'tooz Developers', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of @@ -239,8 +238,8 @@ latex_documents = [ # (source start file, name, description, authors, manual section). man_pages = [ ('index', 'toozReleaseNotes', - u'tooz Release Notes Documentation', - [u'tooz Developers'], 1) + 'tooz Release Notes Documentation', + ['tooz Developers'], 1) ] # If true, show URL addresses after external links. @@ -254,8 +253,8 @@ man_pages = [ # dir menu entry, description, category) texinfo_documents = [ ('index', 'toozReleaseNotes', - u'tooz Release Notes Documentation', - u'tooz Developers', 'toozReleaseNotes', + 'tooz Release Notes Documentation', + 'tooz Developers', 'toozReleaseNotes', 'One line description of project.', 'Miscellaneous'), ] diff --git a/tools/compat-matrix.py b/tools/compat-matrix.py index 7cbc9986..8d37a980 100644 --- a/tools/compat-matrix.py +++ b/tools/compat-matrix.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # Copyright (C) 2015 Yahoo! Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may diff --git a/tooz/__init__.py b/tooz/__init__.py index f2657597..34a754c6 100644 --- a/tooz/__init__.py +++ b/tooz/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2014 eNovance Inc. All Rights Reserved. # @@ -28,7 +27,7 @@ class ToozError(Exception): """ def __init__(self, message, cause=None): - super(ToozError, self).__init__(message) + super().__init__(message) self.cause = cause diff --git a/tooz/_retry.py b/tooz/_retry.py index e799e79d..b95dc433 100644 --- a/tooz/_retry.py +++ b/tooz/_retry.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright © 2016 Red Hat, Inc. # diff --git a/tooz/coordination.py b/tooz/coordination.py index 942fa025..24136c1d 100644 --- a/tooz/coordination.py +++ b/tooz/coordination.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2016 Red Hat, Inc. # Copyright (C) 2013-2014 eNovance Inc. All Rights Reserved. @@ -120,7 +119,7 @@ class Hooks(list): return list(map(lambda cb: cb(*args, **kwargs), self)) -class Event(object): +class Event: """Base class for events.""" @@ -132,9 +131,9 @@ class MemberJoinedGroup(Event): self.member_id = member_id def __repr__(self): - return "<%s: group %s: +member %s>" % (self.__class__.__name__, - self.group_id, - self.member_id) + return "<{}: group {}: +member {}>".format(self.__class__.__name__, + self.group_id, + self.member_id) class MemberLeftGroup(Event): @@ -145,9 +144,9 @@ class MemberLeftGroup(Event): self.member_id = member_id def __repr__(self): - return "<%s: group %s: -member %s>" % (self.__class__.__name__, - self.group_id, - self.member_id) + return "<{}: group {}: -member {}>".format(self.__class__.__name__, + self.group_id, + self.member_id) class LeaderElected(Event): @@ -158,7 +157,7 @@ class LeaderElected(Event): self.member_id = member_id -class Heart(object): +class Heart: """Coordination drivers main liveness pump (its heart).""" def __init__(self, driver, thread_cls=threading.Thread, @@ -228,7 +227,7 @@ class Heart(object): return self._runner.is_alive() -class CoordinationDriver(object): +class CoordinationDriver: requires_beating = False """ @@ -245,7 +244,7 @@ class CoordinationDriver(object): """ def __init__(self, member_id, parsed_url, options): - super(CoordinationDriver, self).__init__() + super().__init__() self._member_id = member_id self._started = False self._hooks_join_group = collections.defaultdict(Hooks) @@ -629,7 +628,7 @@ class CoordinationDriver(object): pass -class CoordAsyncResult(object, metaclass=abc.ABCMeta): +class CoordAsyncResult(metaclass=abc.ABCMeta): """Representation of an asynchronous task. Every call API returns an CoordAsyncResult object on which the result or @@ -681,15 +680,15 @@ class CoordinationDriverWithExecutor(CoordinationDriver): self._options = utils.collapse(options, exclude=self.EXCLUDE_OPTIONS) self._executor = utils.ProxyExecutor.build( self.__class__.__name__, self._options) - super(CoordinationDriverWithExecutor, self).__init__( + super().__init__( member_id, parsed_url, options) def start(self, start_heart=False): self._executor.start() - super(CoordinationDriverWithExecutor, self).start(start_heart) + super().start(start_heart) def stop(self): - super(CoordinationDriverWithExecutor, self).stop() + super().stop() self._executor.stop() @@ -703,7 +702,7 @@ class CoordinationDriverCachedRunWatchers(CoordinationDriver): """ def __init__(self, member_id, parsed_url, options): - super(CoordinationDriverCachedRunWatchers, self).__init__( + super().__init__( member_id, parsed_url, options) # A cache for group members self._group_members = collections.defaultdict(set) @@ -716,11 +715,11 @@ class CoordinationDriverCachedRunWatchers(CoordinationDriver): def watch_join_group(self, group_id, callback): self._init_watch_group(group_id) - super(CoordinationDriverCachedRunWatchers, self).watch_join_group( + super().watch_join_group( group_id, callback) def unwatch_join_group(self, group_id, callback): - super(CoordinationDriverCachedRunWatchers, self).unwatch_join_group( + super().unwatch_join_group( group_id, callback) if (not self._has_hooks_for_group(group_id) and @@ -729,11 +728,11 @@ class CoordinationDriverCachedRunWatchers(CoordinationDriver): def watch_leave_group(self, group_id, callback): self._init_watch_group(group_id) - super(CoordinationDriverCachedRunWatchers, self).watch_leave_group( + super().watch_leave_group( group_id, callback) def unwatch_leave_group(self, group_id, callback): - super(CoordinationDriverCachedRunWatchers, self).unwatch_leave_group( + super().unwatch_leave_group( group_id, callback) if (not self._has_hooks_for_group(group_id) and @@ -840,7 +839,7 @@ class GroupNotCreated(tooz.ToozError): """Exception raised when the caller request an nonexistent group.""" def __init__(self, group_id): self.group_id = group_id - super(GroupNotCreated, self).__init__( + super().__init__( "Group %s does not exist" % group_id) @@ -848,7 +847,7 @@ class GroupAlreadyExist(tooz.ToozError): """Exception raised trying to create an already existing group.""" def __init__(self, group_id): self.group_id = group_id - super(GroupAlreadyExist, self).__init__( + super().__init__( "Group %s already exists" % group_id) @@ -857,7 +856,7 @@ class MemberAlreadyExist(tooz.ToozError): def __init__(self, group_id, member_id): self.group_id = group_id self.member_id = member_id - super(MemberAlreadyExist, self).__init__( + super().__init__( "Member %s has already joined %s" % (member_id, group_id)) @@ -867,15 +866,15 @@ class MemberNotJoined(tooz.ToozError): def __init__(self, group_id, member_id): self.group_id = group_id self.member_id = member_id - super(MemberNotJoined, self).__init__("Member %s has not joined %s" % - (member_id, group_id)) + super().__init__("Member %s has not joined %s" % + (member_id, group_id)) class GroupNotEmpty(tooz.ToozError): "Exception raised when the caller try to delete a group with members." def __init__(self, group_id): self.group_id = group_id - super(GroupNotEmpty, self).__init__("Group %s is not empty" % group_id) + super().__init__("Group %s is not empty" % group_id) class WatchCallbackNotFound(tooz.ToozError): @@ -888,7 +887,7 @@ class WatchCallbackNotFound(tooz.ToozError): def __init__(self, group_id, callback): self.group_id = group_id self.callback = callback - super(WatchCallbackNotFound, self).__init__( + super().__init__( 'Callback %s is not registered on group %s' % (callback.__name__, group_id)) diff --git a/tooz/drivers/consul.py b/tooz/drivers/consul.py index 7da21ad6..88d30f86 100644 --- a/tooz/drivers/consul.py +++ b/tooz/drivers/consul.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright © 2015 Yahoo! Inc. # @@ -57,7 +56,7 @@ def _translate_failures(func): class ConsulLock(locking.Lock): def __init__(self, name, node, address, session_id, client, token=None): - super(ConsulLock, self).__init__(name) + super().__init__(name) self._name = name self._node = node self._address = address @@ -218,7 +217,7 @@ class ConsulDriver(coordination.CoordinationDriverCachedRunWatchers, """ def __init__(self, member_id, parsed_url, options): - super(ConsulDriver, self).__init__(member_id, parsed_url, options) + super().__init__(member_id, parsed_url, options) options = utils.collapse(options) self._host = parsed_url.hostname self._port = parsed_url.port or self.DEFAULT_PORT diff --git a/tooz/drivers/etcd.py b/tooz/drivers/etcd.py index 34181b0b..84f935bb 100644 --- a/tooz/drivers/etcd.py +++ b/tooz/drivers/etcd.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -50,7 +49,7 @@ def _translate_failures(func): return wrapper -class _Client(object): +class _Client: def __init__(self, host, port, protocol): self.host = host self.port = port @@ -88,7 +87,7 @@ class EtcdLock(locking.Lock): _TOOZ_LOCK_PREFIX = "tooz_locks" def __init__(self, lock_url, name, coord, client, ttl=60): - super(EtcdLock, self).__init__(name) + super().__init__(name) self.client = client self.coord = coord self.ttl = ttl @@ -242,7 +241,7 @@ class EtcdDriver(coordination.CoordinationDriver): ) def __init__(self, member_id, parsed_url, options): - super(EtcdDriver, self).__init__(member_id, parsed_url, options) + super().__init__(member_id, parsed_url, options) host = parsed_url.hostname or self.DEFAULT_HOST port = parsed_url.port or self.DEFAULT_PORT options = utils.collapse(options) diff --git a/tooz/drivers/etcd3gw.py b/tooz/drivers/etcd3gw.py index da6fbb17..6f9b7c26 100644 --- a/tooz/drivers/etcd3gw.py +++ b/tooz/drivers/etcd3gw.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -69,7 +68,7 @@ class Etcd3Lock(locking.Lock): LOCK_PREFIX = b"/tooz/locks" def __init__(self, coord, name, timeout): - super(Etcd3Lock, self).__init__(name) + super().__init__(name) self._timeout = timeout self._coord = coord self._key = self.LOCK_PREFIX + name @@ -214,7 +213,7 @@ class Etcd3Driver(coordination.CoordinationDriverCachedRunWatchers, ) def __init__(self, member_id, parsed_url, options): - super(Etcd3Driver, self).__init__(member_id, parsed_url, options) + super().__init__(member_id, parsed_url, options) protocol = 'https' if parsed_url.scheme.endswith('https') else 'http' host = parsed_url.hostname or self.DEFAULT_HOST port = parsed_url.port or self.DEFAULT_PORT @@ -243,7 +242,7 @@ class Etcd3Driver(coordination.CoordinationDriverCachedRunWatchers, self._membership_lease = None def _start(self): - super(Etcd3Driver, self)._start() + super()._start() self._membership_lease = self.client.lease(self.membership_timeout) def get_lock(self, name): diff --git a/tooz/drivers/file.py b/tooz/drivers/file.py index f022ee05..60615fbc 100644 --- a/tooz/drivers/file.py +++ b/tooz/drivers/file.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright © 2015 eNovance # @@ -42,7 +41,7 @@ from tooz import utils LOG = logging.getLogger(__name__) -class _Barrier(object): +class _Barrier: def __init__(self): self.cond = threading.Condition() self.owner = None @@ -54,7 +53,7 @@ class _Barrier(object): def _translate_failures(): try: yield - except (EnvironmentError, voluptuous.Invalid) as e: + except (OSError, voluptuous.Invalid) as e: utils.raise_with_cause(tooz.ToozError, encodeutils.exception_to_unicode(e), cause=e) @@ -74,7 +73,7 @@ def _convert_from_old_format(data): # {u"member_id": b"member"} # {u"member_id": u"member"} if b"member_id" in data or b"group_id" in data: - data = dict((k.decode("utf8"), v) for k, v in data.items()) + data = {k.decode("utf8"): v for k, v in data.items()} # About member_id and group_id valuse if the file have been written # with python2 and in the old format, we can't known with python3 # if we need to decode the value or not. Python3 see bytes blob @@ -104,7 +103,7 @@ class FileLock(locking.Lock): """A file based lock.""" def __init__(self, path, barrier, member_id): - super(FileLock, self).__init__(path) + super().__init__(path) self.acquired = False self._lock = fasteners.InterProcessLock(path) self._barrier = barrier @@ -226,7 +225,7 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, def __init__(self, member_id, parsed_url, options): """Initialize the file driver.""" - super(FileDriver, self).__init__(member_id, parsed_url, options) + super().__init__(member_id, parsed_url, options) self._dir = self._normalize_path(parsed_url.path) self._group_dir = os.path.join(self._dir, 'groups') self._tmpdir = os.path.join(self._dir, 'tmp') @@ -271,7 +270,7 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, return hashlib.new(cls.HASH_ROUTINE, item).hexdigest() def _start(self): - super(FileDriver, self)._start() + super()._start() for a_dir in self._reserved_dirs: try: fileutils.ensure_tree(a_dir) @@ -280,9 +279,9 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, def _update_group_metadata(self, path, group_id): details = { - u'group_id': utils.to_binary(group_id, encoding="utf8") + 'group_id': utils.to_binary(group_id, encoding="utf8") } - details[u'encoded'] = details[u"group_id"] != group_id + details['encoded'] = details["group_id"] != group_id details_blob = utils.dumps(details) fd, name = tempfile.mkstemp("tooz", dir=self._tmpdir) with os.fdopen(fd, "wb") as fh: @@ -321,12 +320,12 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, raise coordination.MemberAlreadyExist(group_id, self._member_id) details = { - u'capabilities': capabilities, - u'joined_on': datetime.datetime.now(), - u'member_id': utils.to_binary(self._member_id, - encoding="utf-8") + 'capabilities': capabilities, + 'joined_on': datetime.datetime.now(), + 'member_id': utils.to_binary(self._member_id, + encoding="utf-8") } - details[u'encoded'] = details[u"member_id"] != self._member_id + details['encoded'] = details["member_id"] != self._member_id details_blob = utils.dumps(details) with open(me_path, "wb") as fh: fh.write(details_blob) @@ -346,7 +345,7 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, raise coordination.GroupNotCreated(group_id) try: os.unlink(me_path) - except EnvironmentError as e: + except OSError as e: if e.errno != errno.ENOENT: raise else: @@ -382,8 +381,8 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, with open(path, 'rb') as fh: details = self._load_and_validate(fh.read(), 'member') if details.get("encoded"): - return details[u'member_id'].decode("utf-8") - return details[u'member_id'] + return details['member_id'].decode("utf-8") + return details['member_id'] def get_members(self, group_id): safe_group_id = self._make_filesystem_safe(group_id) @@ -396,7 +395,7 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, members = set() try: entries = os.listdir(group_dir) - except EnvironmentError as e: + except OSError as e: # Did someone manage to remove it before we got here... if e.errno != errno.ENOENT: raise @@ -415,7 +414,7 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, member_id = self._read_member_id(entry_path) else: continue - except EnvironmentError as e: + except OSError as e: if e.errno != errno.ENOENT: raise else: @@ -436,7 +435,7 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, try: with open(member_path, "rb") as fh: contents = fh.read() - except EnvironmentError as e: + except OSError as e: if e.errno == errno.ENOENT: if not os.path.isdir(group_dir): raise coordination.GroupNotCreated(group_id) @@ -447,7 +446,7 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, raise else: details = self._load_and_validate(contents, 'member') - return details.get(u"capabilities") + return details.get("capabilities") fut = self._executor.submit(_do_get_member_capabilities) return FileFutureResult(fut) @@ -460,7 +459,7 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, def _do_delete_group(): try: entries = os.listdir(group_dir) - except EnvironmentError as e: + except OSError as e: if e.errno == errno.ENOENT: raise coordination.GroupNotCreated(group_id) else: @@ -476,7 +475,7 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, else: try: shutil.rmtree(group_dir) - except EnvironmentError as e: + except OSError as e: if e.errno != errno.ENOENT: raise @@ -487,8 +486,8 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, with open(path, 'rb') as fh: details = self._load_and_validate(fh.read(), 'group') if details.get("encoded"): - return details[u'group_id'].decode("utf-8") - return details[u'group_id'] + return details['group_id'].decode("utf-8") + return details['group_id'] def get_groups(self): @@ -498,7 +497,7 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, path = os.path.join(self._group_dir, entry, '.metadata') try: groups.append(self._read_group_id(path)) - except EnvironmentError as e: + except OSError as e: if e.errno != errno.ENOENT: raise return groups @@ -517,7 +516,7 @@ class FileDriver(coordination.CoordinationDriverCachedRunWatchers, def _do_heartbeat(): try: os.utime(member_path, None) - except EnvironmentError as err: + except OSError as err: if err.errno != errno.ENOENT: raise _do_heartbeat() diff --git a/tooz/drivers/ipc.py b/tooz/drivers/ipc.py index 95654e4b..769ce5a7 100644 --- a/tooz/drivers/ipc.py +++ b/tooz/drivers/ipc.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright © 2014 eNovance # @@ -58,7 +57,7 @@ class IPCLock(locking.Lock): _LOCK_PROJECT = b'__TOOZ_LOCK_' def __init__(self, name): - super(IPCLock, self).__init__(name) + super().__init__(name) self.key = ftok(name, self._LOCK_PROJECT) self._lock = None @@ -173,7 +172,7 @@ class IPCDriver(coordination.CoordinationDriverWithExecutor): _INTERNAL_LOCK_NAME = "TOOZ_INTERNAL_LOCK" def _start(self): - super(IPCDriver, self)._start() + super()._start() self._group_list = sysv_ipc.SharedMemory( ftok(self._GROUP_LIST_KEY, self._GROUP_PROJECT), sysv_ipc.IPC_CREAT, @@ -181,7 +180,7 @@ class IPCDriver(coordination.CoordinationDriverWithExecutor): self._lock = self.get_lock(self._INTERNAL_LOCK_NAME) def _stop(self): - super(IPCDriver, self)._stop() + super()._stop() try: self._group_list.detach() self._group_list.remove() @@ -230,12 +229,12 @@ class IPCDriver(coordination.CoordinationDriverWithExecutor): def watch_join_group(self, group_id, callback): # Check the group exist self.get_members(group_id).get() - super(IPCDriver, self).watch_join_group(group_id, callback) + super().watch_join_group(group_id, callback) def watch_leave_group(self, group_id, callback): # Check the group exist self.get_members(group_id).get() - super(IPCDriver, self).watch_leave_group(group_id, callback) + super().watch_leave_group(group_id, callback) def _get_groups_handler(self): with self._lock: diff --git a/tooz/drivers/memcached.py b/tooz/drivers/memcached.py index f18017a2..adb174c5 100644 --- a/tooz/drivers/memcached.py +++ b/tooz/drivers/memcached.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright © 2014 eNovance # @@ -45,15 +44,14 @@ def _failure_translator(): utils.raise_with_cause(coordination.ToozConnectionError, encodeutils.exception_to_unicode(e), cause=e) - except (socket.timeout, socket.error, - socket.gaierror, socket.herror) as e: + except (socket.timeout, OSError, socket.gaierror, socket.herror) as e: # TODO(harlowja): get upstream pymemcache to produce a better # exception for these, using socket (vs. a memcache specific # error) seems sorta not right and/or the best approach... msg = encodeutils.exception_to_unicode(e) if e.errno is not None: - msg += " (with errno %s [%s])" % (errno.errorcode[e.errno], - e.errno) + msg += " (with errno {} [{}])".format(errno.errorcode[e.errno], + e.errno) utils.raise_with_cause(coordination.ToozConnectionError, msg, cause=e) except pymemcache_client.MemcacheError as e: @@ -76,7 +74,7 @@ class MemcachedLock(locking.Lock): _LOCK_PREFIX = b'__TOOZ_LOCK_' def __init__(self, coord, name, timeout): - super(MemcachedLock, self).__init__(self._LOCK_PREFIX + name) + super().__init__(self._LOCK_PREFIX + name) self.coord = coord self.timeout = timeout @@ -252,7 +250,7 @@ class MemcachedDriver(coordination.CoordinationDriverCachedRunWatchers, STILL_ALIVE = b"It's alive!" def __init__(self, member_id, parsed_url, options): - super(MemcachedDriver, self).__init__(member_id, parsed_url, options) + super().__init__(member_id, parsed_url, options) self.host = (parsed_url.hostname or "localhost", parsed_url.port or 11211) default_timeout = self._options.get('timeout', self.DEFAULT_TIMEOUT) @@ -287,7 +285,7 @@ class MemcachedDriver(coordination.CoordinationDriverCachedRunWatchers, @_translate_failures def _start(self): - super(MemcachedDriver, self)._start() + super()._start() self.client = pymemcache_client.PooledClient( self.host, serializer=self._msgpack_serializer, @@ -301,7 +299,7 @@ class MemcachedDriver(coordination.CoordinationDriverCachedRunWatchers, @_translate_failures def _stop(self): - super(MemcachedDriver, self)._stop() + super()._stop() for lock in list(self._acquired_locks): lock.release() self.client.delete(self._encode_member_id(self._member_id)) @@ -529,7 +527,7 @@ class MemcachedDriver(coordination.CoordinationDriverCachedRunWatchers, self._member_id)) def run_watchers(self, timeout=None): - result = super(MemcachedDriver, self).run_watchers(timeout=timeout) + result = super().run_watchers(timeout=timeout) self.run_elect_coordinator() return result diff --git a/tooz/drivers/mysql.py b/tooz/drivers/mysql.py index ddb6fcb0..bade0a1c 100644 --- a/tooz/drivers/mysql.py +++ b/tooz/drivers/mysql.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright © 2014 eNovance # @@ -34,7 +33,7 @@ class MySQLLock(locking.Lock): MYSQL_DEFAULT_PORT = 3306 def __init__(self, name, parsed_url, options): - super(MySQLLock, self).__init__(name) + super().__init__(name) self.acquired = False self._conn = MySQLDriver.get_connection(parsed_url, options, True) @@ -156,7 +155,7 @@ class MySQLDriver(coordination.CoordinationDriver): def __init__(self, member_id, parsed_url, options): """Initialize the MySQL driver.""" - super(MySQLDriver, self).__init__(member_id, parsed_url, options) + super().__init__(member_id, parsed_url, options) self._parsed_url = parsed_url self._options = utils.collapse(options) diff --git a/tooz/drivers/pgsql.py b/tooz/drivers/pgsql.py index 4d2601b9..45d8213e 100644 --- a/tooz/drivers/pgsql.py +++ b/tooz/drivers/pgsql.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright © 2014 eNovance # @@ -55,8 +54,8 @@ _DIAGNOSTICS_ATTRS = tuple([ def _format_exception(e): lines = [ - "%s: %s" % (type(e).__name__, - encodeutils.exception_to_unicode(e).strip()), + "{}: {}".format(type(e).__name__, + encodeutils.exception_to_unicode(e).strip()), ] if hasattr(e, 'pgcode') and e.pgcode is not None: lines.append("Error code: %s" % e.pgcode) @@ -71,7 +70,7 @@ def _format_exception(e): attr_value = getattr(e.diag, attr_name) if attr_value is None: continue - diagnostic_lines.append(" %s = %s" % (attr_name, attr_value)) + diagnostic_lines.append(" {} = {}".format(attr_name, attr_value)) if diagnostic_lines: lines.append('Diagnostics:') lines.extend(diagnostic_lines) @@ -93,7 +92,7 @@ class PostgresLock(locking.Lock): """A PostgreSQL based lock.""" def __init__(self, name, parsed_url, options): - super(PostgresLock, self).__init__(name) + super().__init__(name) self.acquired = False self._conn = None self._parsed_url = parsed_url @@ -192,7 +191,7 @@ class PostgresDriver(coordination.CoordinationDriver): def __init__(self, member_id, parsed_url, options): """Initialize the PostgreSQL driver.""" - super(PostgresDriver, self).__init__(member_id, parsed_url, options) + super().__init__(member_id, parsed_url, options) self._parsed_url = parsed_url self._options = utils.collapse(options) diff --git a/tooz/drivers/redis.py b/tooz/drivers/redis.py index 6eaf52c3..45a7618a 100644 --- a/tooz/drivers/redis.py +++ b/tooz/drivers/redis.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # Copyright (C) 2014 Yahoo! Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -79,8 +77,8 @@ def _handle_failures(n_tries=15): class RedisLock(locking.Lock): def __init__(self, coord, client, name, timeout): - name = "%s_%s_lock" % (coord.namespace, str(name)) - super(RedisLock, self).__init__(name) + name = "{}_{}_lock".format(coord.namespace, str(name)) + super().__init__(name) # NOTE(jd) Make sure we don't release and heartbeat at the same time by # using a exclusive access lock (LP#1557593) self._exclusive_access = threading.Lock() @@ -373,7 +371,7 @@ return 1 EXCLUDE_OPTIONS = CLIENT_LIST_ARGS def __init__(self, member_id, parsed_url, options): - super(RedisDriver, self).__init__(member_id, parsed_url, options) + super().__init__(member_id, parsed_url, options) self._parsed_url = parsed_url self._encoding = self._options.get('encoding', self.DEFAULT_ENCODING) timeout = self._options.get('timeout', self.CLIENT_DEFAULT_SOCKET_TO) @@ -492,7 +490,7 @@ return 1 @_handle_failures() def _start(self): - super(RedisDriver, self)._start() + super()._start() try: self._client = self._make_client(self._parsed_url, self._options, self.timeout) @@ -586,7 +584,7 @@ return 1 lock.release() except tooz.ToozError: LOG.warning("Unable to release lock '%s'", lock, exc_info=True) - super(RedisDriver, self)._stop() + super()._stop() if self._client is not None: # Make sure we no longer exist... beat_id = self._encode_beat_id(self._member_id) @@ -703,8 +701,8 @@ return 1 for m in gone_members) p.hdel(encoded_group, *encoded_gone_members) p.execute() - return set(m for m in potential_members - if m not in gone_members) + return {m for m in potential_members + if m not in gone_members} return potential_members return RedisFutureResult(self._submit(self._client.transaction, @@ -805,7 +803,7 @@ return 1 self._member_id)) def run_watchers(self, timeout=None): - result = super(RedisDriver, self).run_watchers(timeout=timeout) + result = super().run_watchers(timeout=timeout) self.run_elect_coordinator() return result diff --git a/tooz/drivers/zake.py b/tooz/drivers/zake.py index e70a35cd..feb5c119 100644 --- a/tooz/drivers/zake.py +++ b/tooz/drivers/zake.py @@ -50,7 +50,7 @@ class ZakeDriver(zookeeper.KazooDriver): fake_client.k_threading.SequentialThreadingHandler()) def __init__(self, member_id, parsed_url, options): - super(ZakeDriver, self).__init__(member_id, parsed_url, options) + super().__init__(member_id, parsed_url, options) warnings.warn( "The zake driver is deprecated, and will be removed in " "a future release." diff --git a/tooz/drivers/zookeeper.py b/tooz/drivers/zookeeper.py index 490328dd..a654d9b7 100644 --- a/tooz/drivers/zookeeper.py +++ b/tooz/drivers/zookeeper.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2013-2014 eNovance Inc. All Rights Reserved. # @@ -34,7 +33,7 @@ from tooz import utils class ZooKeeperLock(locking.Lock): def __init__(self, name, lock): - super(ZooKeeperLock, self).__init__(name) + super().__init__(name) self._lock = lock self._client = lock.client @@ -144,7 +143,7 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers): """ def __init__(self, member_id, parsed_url, options): - super(KazooDriver, self).__init__(member_id, parsed_url, options) + super().__init__(member_id, parsed_url, options) options = utils.collapse(options, exclude=['hosts']) self.timeout = int(options.get('timeout', '10')) self._namespace = options.get('namespace', self.TOOZ_NAMESPACE) @@ -317,7 +316,7 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers): encodeutils.exception_to_unicode(e), cause=e) else: - return set(m.encode('ascii') for m in members_ids) + return {m.encode('ascii') for m in members_ids} def get_members(self, group_id): group_path = self._paths_join("/", self._namespace, group_id) @@ -427,7 +426,7 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers): encodeutils.exception_to_unicode(e), cause=e) else: - return set(g.encode('ascii') for g in group_ids) + return {g.encode('ascii') for g in group_ids} def get_groups(self): tooz_namespace = self._paths_join("/", self._namespace) @@ -464,7 +463,7 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers): username = parsed_url.username password = parsed_url.password - digest_auth = "%s:%s" % (username, password) + digest_auth = "{}:{}".format(username, password) digest_acl = security.make_digest_acl(username, password, all=True) default_acl = (digest_acl,) auth_data = [('digest', digest_auth)] @@ -547,7 +546,7 @@ class KazooDriver(coordination.CoordinationDriverCachedRunWatchers): self._member_id)) def run_watchers(self, timeout=None): - results = super(KazooDriver, self).run_watchers(timeout) + results = super().run_watchers(timeout) self.run_elect_coordinator() return results diff --git a/tooz/hashring.py b/tooz/hashring.py index 581bc9c2..b8deb031 100644 --- a/tooz/hashring.py +++ b/tooz/hashring.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2016 Red Hat, Inc. # @@ -25,11 +24,11 @@ from tooz import utils class UnknownNode(tooz.ToozError): """Node is unknown.""" def __init__(self, node): - super(UnknownNode, self).__init__("Unknown node `%s'" % node) + super().__init__("Unknown node `%s'" % node) self.node = node -class HashRing(object): +class HashRing: """Map objects onto nodes based on their consistent hash.""" DEFAULT_PARTITION_NUMBER = 2**5 diff --git a/tooz/locking.py b/tooz/locking.py index f2590690..6dfe61bd 100644 --- a/tooz/locking.py +++ b/tooz/locking.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2014 eNovance Inc. All Rights Reserved. # @@ -19,7 +18,7 @@ import tooz from tooz import coordination -class _LockProxy(object): +class _LockProxy: def __init__(self, lock, *args, **kwargs): self.lock = lock self.args = args @@ -32,7 +31,7 @@ class _LockProxy(object): self.lock.__exit__(exc_type, exc_val, exc_tb) -class Lock(object, metaclass=abc.ABCMeta): +class Lock(metaclass=abc.ABCMeta): def __init__(self, name): if not name: raise ValueError("Locks must be provided a name") @@ -48,7 +47,7 @@ class Lock(object, metaclass=abc.ABCMeta): def __enter__(self, *args, **kwargs): acquired = self.acquire(*args, **kwargs) if not acquired: - msg = u'Acquiring lock %s failed' % self.name + msg = 'Acquiring lock %s failed' % self.name raise coordination.LockAcquireFailed(msg) return self diff --git a/tooz/partitioner.py b/tooz/partitioner.py index 3a23f9ea..8ee0dbe7 100644 --- a/tooz/partitioner.py +++ b/tooz/partitioner.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2016 Red Hat, Inc. # @@ -17,7 +16,7 @@ from tooz import hashring -class Partitioner(object): +class Partitioner: """Partition set of objects across several members. Objects to be partitioned should implement the __tooz_hash__ method to diff --git a/tooz/tests/__init__.py b/tooz/tests/__init__.py index 45f68bec..d6165b5b 100644 --- a/tooz/tests/__init__.py +++ b/tooz/tests/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright (C) 2014 eNovance Inc. All Rights Reserved. # @@ -53,7 +52,7 @@ class TestWithCoordinator(testcase.TestCase, metaclass=SkipNotImplementedMeta): url = os.getenv("TOOZ_TEST_URL") def setUp(self): - super(TestWithCoordinator, self).setUp() + super().setUp() if self.url is None: raise RuntimeError("No URL set for this driver") if os.getenv("TOOZ_TEST_ETCD3GW"): @@ -72,4 +71,4 @@ class TestWithCoordinator(testcase.TestCase, metaclass=SkipNotImplementedMeta): def tearDown(self): self._coord.stop() - super(TestWithCoordinator, self).tearDown() + super().tearDown() diff --git a/tooz/tests/drivers/test_etcd.py b/tooz/tests/drivers/test_etcd.py index fead02a6..be3f0b4a 100644 --- a/tooz/tests/drivers/test_etcd.py +++ b/tooz/tests/drivers/test_etcd.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright 2016 Red Hat, Inc. # @@ -26,7 +25,7 @@ class TestEtcd(testcase.TestCase): FAKE_MEMBER_ID = "mocked-not-really-member" def setUp(self): - super(TestEtcd, self).setUp() + super().setUp() self._coord = tooz.coordination.get_coordinator(self.FAKE_URL, self.FAKE_MEMBER_ID) diff --git a/tooz/tests/drivers/test_etcd3gw.py b/tooz/tests/drivers/test_etcd3gw.py index 483f5d91..a9c33043 100644 --- a/tooz/tests/drivers/test_etcd3gw.py +++ b/tooz/tests/drivers/test_etcd3gw.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright 2020 Red Hat, Inc. # diff --git a/tooz/tests/drivers/test_file.py b/tooz/tests/drivers/test_file.py index f8c106d6..f6f5a1a5 100644 --- a/tooz/tests/drivers/test_file.py +++ b/tooz/tests/drivers/test_file.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # Copyright 2016 Cloudbase Solutions Srl # All Rights Reserved. # diff --git a/tooz/tests/drivers/test_memcache.py b/tooz/tests/drivers/test_memcache.py index d3b6d8a4..54707d5e 100644 --- a/tooz/tests/drivers/test_memcache.py +++ b/tooz/tests/drivers/test_memcache.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # Copyright (c) 2015 OpenStack Foundation # All Rights Reserved. # diff --git a/tooz/tests/drivers/test_mysql.py b/tooz/tests/drivers/test_mysql.py index b64d7306..a18ec773 100644 --- a/tooz/tests/drivers/test_mysql.py +++ b/tooz/tests/drivers/test_mysql.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # Copyright (c) 2015 OpenStack Foundation # All Rights Reserved. # diff --git a/tooz/tests/drivers/test_postgresql.py b/tooz/tests/drivers/test_postgresql.py index 8671e540..3b71a1f6 100644 --- a/tooz/tests/drivers/test_postgresql.py +++ b/tooz/tests/drivers/test_postgresql.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # Copyright (C) 2014 Yahoo! Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may diff --git a/tooz/tests/test_coordination.py b/tooz/tests/test_coordination.py index 20f42550..3d78796b 100644 --- a/tooz/tests/test_coordination.py +++ b/tooz/tests/test_coordination.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright © 2013-2015 eNovance Inc. All Rights Reserved. # @@ -988,7 +987,7 @@ class TestAPI(tests.TestWithCoordinator): class TestHook(testcase.TestCase): def setUp(self): - super(TestHook, self).setUp() + super().setUp() self.hooks = tooz.coordination.Hooks() self.triggered = False diff --git a/tooz/tests/test_hashring.py b/tooz/tests/test_hashring.py index 01dd642f..f2ba20fe 100644 --- a/tooz/tests/test_hashring.py +++ b/tooz/tests/test_hashring.py @@ -92,8 +92,8 @@ class HashRingTestCase(testcase.TestCase): ring = hashring.HashRing(nodes) self.assertEqual(nodes, set(ring.nodes.keys())) self.assertEqual(2 ** 5 * len(nodes), len(ring)) - nodes.add(u'\u0634\u0628\u06a9\u0647') - ring.add_node(u'\u0634\u0628\u06a9\u0647') + nodes.add('\u0634\u0628\u06a9\u0647') + ring.add_node('\u0634\u0628\u06a9\u0647') self.assertEqual(nodes, set(ring.nodes.keys())) self.assertEqual(2 ** 5 * len(nodes), len(ring)) @@ -114,7 +114,7 @@ class HashRingTestCase(testcase.TestCase): self.assertEqual(2 ** 5 * len(nodes), len(ring)) nodes.add('baz') nodes.add('baz2') - ring.add_nodes(set(['baz', 'baz2']), weight=10) + ring.add_nodes({'baz', 'baz2'}, weight=10) self.assertEqual(nodes, set(ring.nodes.keys())) self.assertEqual(2 ** 5 * 22, len(ring)) diff --git a/tooz/tests/test_partitioner.py b/tooz/tests/test_partitioner.py index 39c13b61..f9a88507 100644 --- a/tooz/tests/test_partitioner.py +++ b/tooz/tests/test_partitioner.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Copyright © 2016 Red Hat, Inc. # @@ -21,13 +20,13 @@ from tooz import tests class TestPartitioner(tests.TestWithCoordinator): def setUp(self): - super(TestPartitioner, self).setUp() + super().setUp() self._extra_coords = [] def tearDown(self): for c in self._extra_coords: c.stop() - super(TestPartitioner, self).tearDown() + super().tearDown() def _add_members(self, number_of_members, weight=1): groups = [] @@ -66,8 +65,8 @@ class TestPartitioner(tests.TestWithCoordinator): p = self._coord.join_partitioned_group(self.group_id, weight=5) self.assertEqual([5], list(p.ring.nodes.values())) p2 = self._add_members(1, weight=10)[0] - self.assertEqual(set([5, 10]), set(p.ring.nodes.values())) - self.assertEqual(set([5, 10]), set(p2.ring.nodes.values())) + self.assertEqual({5, 10}, set(p.ring.nodes.values())) + self.assertEqual({5, 10}, set(p2.ring.nodes.values())) p.stop() def test_stop(self): @@ -79,7 +78,7 @@ class TestPartitioner(tests.TestWithCoordinator): def test_members_of_object_and_others(self): p = self._coord.join_partitioned_group(self.group_id) self._add_members(3) - o = str(u"чупакабра") + o = "чупакабра" m = p.members_for_object(o) self.assertEqual(1, len(m)) m = m.pop() diff --git a/tooz/tests/test_utils.py b/tooz/tests/test_utils.py index 717f4fdb..3babd561 100644 --- a/tooz/tests/test_utils.py +++ b/tooz/tests/test_utils.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # Copyright (c) 2015 OpenStack Foundation # All Rights Reserved. # diff --git a/tooz/utils.py b/tooz/utils.py index 1e4dc1c1..f3cc4e1d 100644 --- a/tooz/utils.py +++ b/tooz/utils.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # Copyright (C) 2014 Yahoo! Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -28,7 +26,7 @@ from oslo_utils import excutils import tooz -class Base64LockEncoder(object): +class Base64LockEncoder: def __init__(self, keyspace_url, prefix=''): self.keyspace_url = keyspace_url if prefix: @@ -52,7 +50,7 @@ class Base64LockEncoder(object): return self.keyspace_url + "/" + enc_name.decode("ascii") -class ProxyExecutor(object): +class ProxyExecutor: KIND_TO_FACTORY = { 'threaded': (lambda: futurist.ThreadPoolExecutor(max_workers=1)),