/usr/lib/python2.7/dist-packages/kazoo/hosts.py is in python-kazoo 1.2.1-1ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import random
class HostIterator(object):
"""An iterator that returns selected hosts in order.
A host is guaranteed to not be selected twice unless there is only
one host in the collection.
"""
def __init__(self, hosts):
self.hosts = hosts
def __iter__(self):
for host in self.hosts[:]:
yield host
def __len__(self):
return len(self.hosts)
class RandomHostIterator(HostIterator):
"""An iterator that returns a randomly selected host."""
def __iter__(self):
hostslist = self.hosts[:]
random.shuffle(hostslist)
for host in hostslist:
yield host
def collect_hosts(hosts, randomize=True):
"""Collect a set of hosts and an optional chroot from a string."""
host_ports, chroot = hosts.partition("/")[::2]
chroot = "/" + chroot if chroot else None
result = []
for host_port in host_ports.split(","):
host, port = host_port.partition(":")[::2]
port = int(port.strip()) if port else 2181
result.append((host.strip(), port))
if randomize:
return (RandomHostIterator(result), chroot)
return (HostIterator(result), chroot)
|