Skip to content

Commit

Permalink
Merge pull request #446 from python-zk/feat/issue-445
Browse files Browse the repository at this point in the history
feat: pep8 all the things
  • Loading branch information
bbangert authored Jun 2, 2017
2 parents b0fbd35 + 9288034 commit bd1ee41
Show file tree
Hide file tree
Showing 19 changed files with 160 additions and 87 deletions.
3 changes: 3 additions & 0 deletions .hound.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fail_on_violations: true
python:
enabled: true
2 changes: 1 addition & 1 deletion kazoo/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .version import __version__
from .version import __version__ # noqa
6 changes: 4 additions & 2 deletions kazoo/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,8 @@ def reconfig(self, joining, leaving, new_members, from_config=-1):
joining=joining, leaving=None, new_members=None)
# wait and then remove it (just by using its id) (incremental)
data, _ = zk.reconfig(joining=None, leaving='100', new_members=None)
data, _ = zk.reconfig(joining=None, leaving='100',
new_members=None)
# now do a full change of the cluster (non-incremental)
new = [
Expand Down Expand Up @@ -1418,7 +1419,8 @@ def reconfig(self, joining, leaving, new_members, from_config=-1):
returns a non-zero error code.
"""
result = self.reconfig_async(joining, leaving, new_members, from_config)
result = self.reconfig_async(joining, leaving, new_members,
from_config)
return result.get()

def reconfig_async(self, joining, leaving, new_members, from_config):
Expand Down
7 changes: 5 additions & 2 deletions kazoo/handlers/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,11 @@ def select(self, *args, **kwargs):
end = (time.time() + timeout) if timeout else None
while end is None or time.time() < end:
if end is not None:
args = list(args) # make a list, since tuples aren't mutable
args[3] = end - time.time() # set the timeout to the remaining time
# make a list, since tuples aren't mutable
args = list(args)

# set the timeout to the remaining time
args[3] = end - time.time()
try:
return select.select(*args, **kwargs)
except select.error as ex:
Expand Down
9 changes: 6 additions & 3 deletions kazoo/protocol/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,8 @@ def _expand_client_hosts(self):
host_ports = []
for host, port in self.client.hosts:
try:
for rhost in socket.getaddrinfo(host.strip(), port, 0, 0, socket.IPPROTO_TCP):
for rhost in socket.getaddrinfo(host.strip(), port, 0, 0,
socket.IPPROTO_TCP):
host_ports.append((rhost[4][0], rhost[4][1]))
except socket.gaierror as e:
# Skip hosts that don't resolve
Expand Down Expand Up @@ -618,7 +619,8 @@ def _connect(self, host, port):
client._session_id or 0, client._session_passwd,
client.read_only)

# save the client's last_zxid before it gets overwritten by the server's.
# save the client's last_zxid before it gets overwritten by the
# server's.
# we'll need this to reset watches via SetWatches further below.
last_zxid = client.last_zxid

Expand Down Expand Up @@ -667,7 +669,8 @@ def _connect(self, host, port):
client._data_watchers.keys(),
client._data_watchers.keys(),
client._child_watchers.keys())
zxid = self._invoke(connect_timeout / 1000.0, sw, xid=SET_WATCHES_XID)
zxid = self._invoke(connect_timeout / 1000.0, sw,
xid=SET_WATCHES_XID)
if zxid:
client.last_zxid = zxid

Expand Down
3 changes: 2 additions & 1 deletion kazoo/protocol/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ def unchroot(client, response):
return resp


class Reconfig(namedtuple('Reconfig', 'joining leaving new_members config_id')):
class Reconfig(namedtuple('Reconfig',
'joining leaving new_members config_id')):
type = 16

def serialize(self):
Expand Down
2 changes: 1 addition & 1 deletion kazoo/protocol/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class EventType(object):
NONE = 'NONE'

EVENT_TYPE_MAP = {
-1:EventType.NONE,
-1: EventType.NONE,
1: EventType.CREATED,
2: EventType.DELETED,
3: EventType.CHANGED,
Expand Down
62 changes: 39 additions & 23 deletions kazoo/recipe/lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@
class NonBlockingLease(object):
"""Exclusive lease that does not block.
An exclusive lease ensures that only one client at a time owns the lease. The client may
renew the lease without losing it by obtaining a new lease with the same path and same
identity. The lease object evaluates to True if the lease was obtained.
An exclusive lease ensures that only one client at a time owns the lease.
The client may renew the lease without losing it by obtaining a new lease
with the same path and same identity. The lease object evaluates to True
if the lease was obtained.
A common use case is a situation where a task should only run on a single host. In this
case, the clients that did not obtain the lease should exit without performing the protected
task.
A common use case is a situation where a task should only run on a single
host. In this case, the clients that did not obtain the lease should exit
without performing the protected task.
The lease stores time stamps using client clocks, and will therefore only work if client clocks
are roughly synchronised. It uses UTC, and works across time zones and daylight savings.
The lease stores time stamps using client clocks, and will therefore only
work if client clocks are roughly synchronised. It uses UTC, and works
across time zones and daylight savings.
Example usage: with a :class:`~kazoo.client.KazooClient` instance::
zk = KazooClient()
zk.start()
# Hold lease over an hour in order to keep job on same machine, with failover if it dies.
lease = zk.NonBlockingLease("/db_leases/hourly_cleanup", datetime.timedelta(minutes = 70),
identifier = "DB hourly cleanup on " + socket.gethostname())
# Hold lease over an hour in order to keep job on same machine,
# with failover if it dies.
lease = zk.NonBlockingLease(
"/db_leases/hourly_cleanup", datetime.timedelta(minutes = 70),
identifier = "DB hourly cleanup on " + socket.gethostname())
if lease:
do_hourly_database_cleanup()
"""
Expand All @@ -42,15 +46,20 @@ class NonBlockingLease(object):
_date_format = "%Y-%m-%dT%H:%M:%S"
_byte_encoding = 'utf-8'

def __init__(self, client, path, duration, identifier=None, utcnow=datetime.datetime.utcnow):
def __init__(self, client, path, duration, identifier=None,
utcnow=datetime.datetime.utcnow):
"""Create a non-blocking lease.
:param client: A :class:`~kazoo.client.KazooClient` instance.
:param path: The lease path to use.
:param duration: Duration during which the lease is reserved. A :class:`~datetime.timedelta` instance.
:param identifier: Unique name to use for this lease holder. Reuse in order to renew the lease.
Defaults do :meth:`socket.gethostname()`.
:param utcnow: Clock function, by default returning :meth:`datetime.datetime.utcnow()`. Used for testing.
:param duration: Duration during which the lease is reserved. A
:class:`~datetime.timedelta` instance.
:param identifier: Unique name to use for this lease holder. Reuse in
order to renew the lease. Defaults to
:meth:`socket.gethostname()`.
:param utcnow: Clock function, by default returning
:meth:`datetime.datetime.utcnow()`. Used for testing.
"""
ident = identifier or socket.gethostname()
self.obtained = False
Expand All @@ -69,13 +78,15 @@ def _attempt_obtaining(self, client, path, duration, ident, utcnow):
if data["version"] != self._version:
# We need an upgrade, let someone else take the lease
return
current_end = datetime.datetime.strptime(data['end'], self._date_format)
current_end = datetime.datetime.strptime(data['end'],
self._date_format)
if data['holder'] != ident and now < current_end:
# Another client is still holding the lease
return
client.delete(holder_path)
end_lease = (now + duration).strftime(self._date_format)
new_data = {'version': self._version, 'holder': ident, 'end': end_lease}
new_data = {'version': self._version, 'holder': ident,
'end': end_lease}
client.create(holder_path, self._encode(new_data))
self.obtained = True

Expand All @@ -100,16 +111,21 @@ def __bool__(self):
class MultiNonBlockingLease(object):
"""Exclusive lease for multiple clients.
This type of lease is useful when a limited set of hosts should run a particular task.
It will attempt to obtain leases trying a sequence of ZooKeeper lease paths.
This type of lease is useful when a limited set of hosts should run a
particular task. It will attempt to obtain leases trying a sequence of
ZooKeeper lease paths.
:param client: A :class:`~kazoo.client.KazooClient` instance.
:param count: Number of host leases allowed.
:param path: ZooKeeper path under which lease files are stored.
:param duration: Duration during which the lease is reserved. A :class:`~datetime.timedelta` instance.
:param identifier: Unique name to use for this lease holder. Reuse in order to renew the lease.
:param duration: Duration during which the lease is reserved. A
:class:`~datetime.timedelta` instance.
:param identifier: Unique name to use for this lease holder. Reuse in order
to renew the lease.
Defaults do :meth:`socket.gethostname()`.
:param utcnow: Clock function, by default returning :meth:`datetime.datetime.utcnow()`. Used for testing.
:param utcnow: Clock function, by default returning
:meth:`datetime.datetime.utcnow()`. Used for testing.
"""

def __init__(self, client, count, path, duration, identifier=None,
Expand Down
8 changes: 4 additions & 4 deletions kazoo/recipe/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class Lock(object):
Note: This lock is not *re-entrant*. Repeated calls after already
acquired will block.
This is an exclusive lock. For a read/write lock, see :class:`WriteLock` and
:class:`ReadLock`.
This is an exclusive lock. For a read/write lock, see :class:`WriteLock`
and :class:`ReadLock`.
"""

Expand Down Expand Up @@ -267,8 +267,8 @@ def _get_sorted_children(self):

# Node names are prefixed by a type: strip the prefix first, which may
# be one of multiple values in case of a read-write lock, and return
# only the sequence number (as a string since it is padded and will sort
# correctly anyway).
# only the sequence number (as a string since it is padded and will
# sort correctly anyway).
#
# In some cases, the lock path may contain nodes with other prefixes
# (eg. in case of a lease), just sort them last ('~' sorts after all
Expand Down
6 changes: 3 additions & 3 deletions kazoo/recipe/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _inner_get(self):
# get another one
self._children = []
raise ForceRetryError()

self._children.pop(0)
return data

Expand Down Expand Up @@ -269,8 +269,9 @@ def release(self):
:returns: True if the lock was removed successfully, False otherwise.
:rtype: bool
"""
if not self.processing_element is None and self.holds_lock:
if self.processing_element is not None and self.holds_lock:
id_, value = self.processing_element
with self.client.transaction() as transaction:
transaction.delete("{path}/{id}".format(
Expand All @@ -281,7 +282,6 @@ def release(self):
else:
return False


def _inner_get(self, timeout):
flag = self.client.handler.event_object()
lock = self.client.handler.lock_object()
Expand Down
3 changes: 2 additions & 1 deletion kazoo/testing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ def __init__(self, install_path=None, classpath=None,
peer_type = 'observer'
else:
peer_type = 'participant'
info = ServerInfo(server_id, port, port + 1, port + 2, port + 3, peer_type)
info = ServerInfo(server_id, port, port + 1, port + 2, port + 3,
peer_type)
peers.append(info)
port += 10

Expand Down
11 changes: 7 additions & 4 deletions kazoo/testing/harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from kazoo import python2atexit as atexit

from kazoo.client import KazooClient
from kazoo.exceptions import KazooException, NotEmptyError
from kazoo.exceptions import KazooException
from kazoo.protocol.states import (
KazooState
)
Expand All @@ -27,7 +27,8 @@ def get_global_cluster():
ZK_CLASSPATH = os.environ.get("ZOOKEEPER_CLASSPATH")
ZK_PORT_OFFSET = int(os.environ.get("ZOOKEEPER_PORT_OFFSET", 20000))
ZK_CLUSTER_SIZE = int(os.environ.get("ZOOKEEPER_CLUSTER_SIZE", 3))
ZK_OBSERVER_START_ID = int(os.environ.get("ZOOKEEPER_OBSERVER_START_ID", -1))
ZK_OBSERVER_START_ID = int(
os.environ.get("ZOOKEEPER_OBSERVER_START_ID", -1))

assert ZK_HOME or ZK_CLASSPATH, (
"Either ZOOKEEPER_PATH or ZOOKEEPER_CLASSPATH environment "
Expand Down Expand Up @@ -96,11 +97,13 @@ def _get_client(self, **kwargs):

def lose_connection(self, event_factory):
"""Force client to lose connection with server"""
self.__break_connection(_CONNECTION_DROP, KazooState.SUSPENDED, event_factory)
self.__break_connection(_CONNECTION_DROP, KazooState.SUSPENDED,
event_factory)

def expire_session(self, event_factory):
"""Force ZK to expire a client session"""
self.__break_connection(_SESSION_EXPIRED, KazooState.LOST, event_factory)
self.__break_connection(_SESSION_EXPIRED, KazooState.LOST,
event_factory)

def setup_zookeeper(self, **client_options):
"""Create a ZK cluster and chrooted :class:`KazooClient`
Expand Down
2 changes: 1 addition & 1 deletion kazoo/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ def test_watch(event):
client.get("/test/", watch=test_watch)
self.expire_session(self.make_event)


cv.wait(3)
assert cv.is_set()

Expand Down Expand Up @@ -980,6 +979,7 @@ def w(we):
# force a reconnect
states = []
rc = client.handler.event_object()

@client.add_listener
def listener(state):
if state == KazooState.CONNECTED:
Expand Down
Loading

0 comments on commit bd1ee41

Please sign in to comment.