From 9d574c6c7bd87ddab91b70342a9ae8764231c774 Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Tue, 9 Sep 2014 14:36:47 +0200 Subject: [PATCH] fix some pep8 problems --- bloomfilter.py | 2 +- candidate.py | 17 +++-- crypto.py | 42 +++++------ discovery/bootstrap.py | 2 +- discovery/community.py | 2 +- endpoint.py | 22 +++--- tests/debugcommunity/node.py | 130 +++++++++++++++++++++-------------- timeline.py | 67 ++++++++++-------- util.py | 16 +++-- 9 files changed, 176 insertions(+), 124 deletions(-) diff --git a/bloomfilter.py b/bloomfilter.py index 14c0f1b1..449b48a5 100644 --- a/bloomfilter.py +++ b/bloomfilter.py @@ -309,5 +309,5 @@ def bytes(self): """ # hex should be m_size/4, hex is 16 instead of 8 -> hence half the number of "hexes" in m_size hex_ = '%x' % self._filter - padding = '0' * (self._m_size /4 - len(hex_)) + padding = '0' * (self._m_size / 4 - len(hex_)) return unhexlify(padding + hex_)[::-1] diff --git a/candidate.py b/candidate.py index f8ebaac9..43a74e28 100644 --- a/candidate.py +++ b/candidate.py @@ -199,7 +199,8 @@ def age(self, now, category=u""): - stumble :: NOW - candidate.last_stumble - intro :: NOW - candidate.last_intro - discovered :: NOW - candidate.last_discovered - - none :: NOW - max(candidate.last_walk, candidate.last_stumble, candidate.last_intro, candidate.last_discovered) + - none :: NOW - max(candidate.last_walk, candidate.last_stumble, + candidate.last_intro, candidate.last_discovered) """ if not category: category = self.get_category(now) @@ -220,7 +221,7 @@ def is_eligible_for_walk(self, now): - SELF is either walk, stumble, or intro; and - the previous step is more than CANDIDATE_ELIGIBLE_DELAY ago. """ - return (self._last_walk + CANDIDATE_ELIGIBLE_DELAY <= now and self.get_category(now) != u"none") + return self._last_walk + CANDIDATE_ELIGIBLE_DELAY <= now and self.get_category(now) != u"none" @property def last_walk(self): @@ -310,20 +311,24 @@ def update(self, tunnel, lan_address, wan_address, connection_type): self._wan_address = wan_address # someone can also reset from a known connection_type to unknown (i.e. it now believes it is # no longer public nor symmetric NAT) - self._connection_type = u"public" if connection_type == u"unknown" and lan_address == wan_address else connection_type + self._connection_type = u"public" if connection_type == u"unknown" and lan_address == wan_address\ + else connection_type if __debug__: if not (self.sock_addr == self._lan_address or self.sock_addr == self._wan_address): - self._logger.error("Either LAN %s or the WAN %s should be SOCK_ADDR %s", self._lan_address, self._wan_address, self.sock_addr) + self._logger.error("Either LAN %s or the WAN %s should be SOCK_ADDR %s", self._lan_address, + self._wan_address, self.sock_addr) def __str__(self): if self._sock_addr == self._lan_address == self._wan_address: return "{%s:%d}" % self._lan_address elif self._sock_addr in (self._lan_address, self._wan_address): - return "{%s:%d %s:%d}" % (self._lan_address[0], self._lan_address[1], self._wan_address[0], self._wan_address[1]) + return "{%s:%d %s:%d}" % (self._lan_address[0], self._lan_address[1], self._wan_address[0], + self._wan_address[1]) else: # should not occur - return "{%s:%d %s:%d %s:%d}" % (self._sock_addr[0], self._sock_addr[1], self._lan_address[0], self._lan_address[1], self._wan_address[0], self._wan_address[1]) + return "{%s:%d %s:%d %s:%d}" % (self._sock_addr[0], self._sock_addr[1], self._lan_address[0], + self._lan_address[1], self._wan_address[0], self._wan_address[1]) class LoopbackCandidate(Candidate): diff --git a/crypto.py b/crypto.py index 9691b4e8..500c8f16 100644 --- a/crypto.py +++ b/crypto.py @@ -43,39 +43,39 @@ def generate_key(self, security_level): raise NotImplementedError() def key_to_bin(self, key): - "Convert a key to the binary format." + """Convert a key to the binary format.""" raise NotImplementedError() def key_to_hash(self, key): - "Get a hash representation from a key." + """Get a hash representation from a key.""" raise NotImplementedError() def key_from_public_bin(self, string): - "Convert a public key stored in the binary format to a key object." + """Convert a public key stored in the binary format to a key object.""" raise NotImplementedError() def key_from_private_bin(self, string): - "Convert a public/private keypair stored in the binary format to a key object." + """Convert a public/private keypair stored in the binary format to a key object.""" raise NotImplementedError() def is_valid_public_bin(self, string): - "Verify if this binary string contains a public key." + """Verify if this binary string contains a public key.""" raise NotImplementedError() def is_valid_private_bin(self, string): - "Verify if this binary string contains a public/private keypair." + """Verify if this binary string contains a public/private keypair.""" raise NotImplementedError() def is_valid_signature(self, key, string, signature): - "Verify if the signature matches the one generated by key/string pair." + """Verify if the signature matches the one generated by key/string pair.""" raise NotImplementedError() def create_signature(self, key, string): - "Create a signature using this key for this string." + """Create a signature using this key for this string.""" raise NotImplementedError() def get_signature_length(self, key): - "Get the length of a signature created using this key in bytes." + """Get the length of a signature created using this key in bytes.""" raise NotImplementedError() @@ -89,7 +89,7 @@ class ECCrypto(DispersyCrypto): """ def _progress(self, *args): - "Called when no feedback needs to be given." + """Called when no feedback needs to be given.""" pass @property @@ -135,7 +135,7 @@ def pem_to_bin(self, pem): @attach_runtime_statistics(u"{0.__class__.__name__}.{function_name}") def key_to_pem(self, ec): - "Convert a key to the PEM format." + """Convert a key to the PEM format.""" bio = BIO.MemoryBuffer() if isinstance(ec, EC_pub): ec.save_pub_key_bio(bio) @@ -145,19 +145,19 @@ def key_to_pem(self, ec): @attach_runtime_statistics(u"{0.__class__.__name__}.{function_name}") def key_from_private_pem(self, pem, password=None): - "Get the EC from a public/private keypair stored in the PEM." + """Get the EC from a public/private keypair stored in the PEM.""" def get_password(*args): return password or "" return EC.load_key_bio(BIO.MemoryBuffer(pem), get_password) @attach_runtime_statistics(u"{0.__class__.__name__}.{function_name}") def key_from_public_pem(self, pem): - "Get the EC from a public PEM." + """Get the EC from a public PEM.""" return EC.load_pub_key_bio(BIO.MemoryBuffer(pem)) @attach_runtime_statistics(u"{0.__class__.__name__}.{function_name}") def is_valid_private_pem(self, pem): - "Returns True if the input is a valid public/private keypair" + """Returns True if the input is a valid public/private keypair""" try: self.key_from_private_pem(pem) except: @@ -166,7 +166,7 @@ def is_valid_private_pem(self, pem): @attach_runtime_statistics(u"{0.__class__.__name__}.{function_name}") def is_valid_public_pem(self, pem): - "Returns True if the input is a valid public key" + """Returns True if the input is a valid public key""" try: self.key_from_public_pem(pem) except: @@ -175,18 +175,18 @@ def is_valid_public_pem(self, pem): @attach_runtime_statistics(u"{0.__class__.__name__}.{function_name}") def key_to_bin(self, ec): - "Convert the key to a binary format." + """Convert the key to a binary format.""" assert isinstance(ec, (EC.EC, EC_pub)), ec return self.pem_to_bin(self.key_to_pem(ec)) @attach_runtime_statistics(u"{0.__class__.__name__}.{function_name}") def key_to_hash(self, ec): - "Get a hash representation from a key." + """Get a hash representation from a key.""" return sha1(self.key_to_bin(ec.pub())).digest() @attach_runtime_statistics(u"{0.__class__.__name__}.{function_name}") def is_valid_private_bin(self, string): - "Returns True if the input is a valid public/private keypair stored in a binary format" + """Returns True if the input is a valid public/private keypair stored in a binary format""" try: self.key_from_private_bin(string) except: @@ -195,7 +195,7 @@ def is_valid_private_bin(self, string): @attach_runtime_statistics(u"{0.__class__.__name__}.{function_name}") def is_valid_public_bin(self, string): - "Returns True if the input is a valid public key" + """Returns True if the input is a valid public key""" try: self.key_from_public_bin(string) except: @@ -204,14 +204,14 @@ def is_valid_public_bin(self, string): @attach_runtime_statistics(u"{0.__class__.__name__}.{function_name}") def key_from_private_bin(self, string): - "Get the EC from a public/private keypair stored in a binary format." + """Get the EC from a public/private keypair stored in a binary format.""" return self.key_from_private_pem("".join(("-----BEGIN EC PRIVATE KEY-----\n", string.encode("BASE64"), "-----END EC PRIVATE KEY-----\n"))) @attach_runtime_statistics(u"{0.__class__.__name__}.{function_name}") def key_from_public_bin(self, string): - "Get the EC from a public key in binary format." + """Get the EC from a public key in binary format.""" return self.key_from_public_pem("".join(("-----BEGIN PUBLIC KEY-----\n", string.encode("BASE64"), "-----END PUBLIC KEY-----\n"))) diff --git a/discovery/bootstrap.py b/discovery/bootstrap.py index fd6c9799..21910791 100644 --- a/discovery/bootstrap.py +++ b/discovery/bootstrap.py @@ -52,7 +52,7 @@ def load_addresses_from_file(filename): """ Reads FILENAME and returns the hosts therein, otherwise returns an empty list. """ - addresses = [] + addresses = list() try: for line in open(filename, "r"): line = line.strip() diff --git a/discovery/community.py b/discovery/community.py index cdb35930..4b08ceed 100644 --- a/discovery/community.py +++ b/discovery/community.py @@ -1,7 +1,7 @@ # Written by Niels Zeilemaker, Egbert Bouman import logging import os -from random import shuffle, random, choice as random_choice +from random import shuffle, random from time import time from twisted.internet import reactor diff --git a/endpoint.py b/endpoint.py index e26d91aa..d809fdbd 100644 --- a/endpoint.py +++ b/endpoint.py @@ -168,7 +168,8 @@ def dispersythread_data_came_in(self, packets, timestamp, cache=True): def send(self, candidates, packets): assert self._dispersy, "Should not be called before open(...)" assert isinstance(candidates, (tuple, list, set)), type(candidates) - assert all(isinstance(candidate, Candidate) for candidate in candidates), [type(candidate) for candidate in candidates] + assert all(isinstance(candidate, Candidate) for candidate in candidates),\ + [type(candidate) for candidate in candidates] assert isinstance(packets, (tuple, list, set)), type(packets) assert all(isinstance(packet, str) for packet in packets), [type(packet) for packet in packets] assert all(len(packet) > 0 for packet in packets), [len(packet) for packet in packets] @@ -258,7 +259,7 @@ def __init__(self, port, ip="0.0.0.0"): self._running = False self._add_task = lambda task, delay = 0.0, id = "": None self._sendqueue_lock = threading.RLock() - self._sendqueue = [] + self._sendqueue = list() # _THREAD and _THREAD are set during open(...) self._thread = None @@ -300,7 +301,8 @@ def close(self, timeout=10.0): else: if self._thread.is_alive(): - self._logger.debug("the endpoint thread is still running (use timeout > 0.0 to ensure the thread stops)") + self._logger.debug("the endpoint thread is still running" + " (use timeout > 0.0 to ensure the thread stops)") result = False try: @@ -346,9 +348,11 @@ def _loop(self): finally: if packets: - self._logger.debug('%d came in, %d bytes in total', len(packets), sum(len(packet) for _, packet in packets)) + self._logger.debug('%d came in, %d bytes in total', + len(packets), sum(len(packet) for _, packet in packets)) self.data_came_in(packets) + class ManualEnpoint(StandaloneEndpoint): def __init__(self, *args, **kwargs): @@ -357,7 +361,8 @@ def __init__(self, *args, **kwargs): self.received_packets = [] def data_came_in(self, packets): - self._logger.debug('added %d packets to receivequeue, %d packets are queued in total', len(packets), len(packets) + len(self.received_packets)) + self._logger.debug('added %d packets to receivequeue, %d packets are queued in total', + len(packets), len(packets) + len(self.received_packets)) with self.receive_lock: self.received_packets.extend(packets) @@ -365,11 +370,11 @@ def data_came_in(self, packets): def clear_receive_queue(self): with self.receive_lock: packets = self.received_packets - self.received_packets = [] + self.received_packets = list() if packets: self._logger.debug('returning %d packets, %d bytes in total', - len(packets), sum(len(packet) for _, packet in packets)) + len(packets), sum(len(packet) for _, packet in packets)) return packets def process_receive_queue(self): @@ -381,6 +386,7 @@ def process_packets(self, packets, cache=True): self._logger.debug('processing %d packets', len(packets)) StandaloneEndpoint.data_came_in(self, packets, cache=cache) + class TunnelEndpoint(Endpoint): def __init__(self, swift_process): @@ -398,7 +404,7 @@ def close(self, timeout=0.0): return super(TunnelEndpoint, self).close(timeout) def get_address(self): - return ("0.0.0.0", self._swift.listenport) + return "0.0.0.0", self._swift.listenport def send(self, candidates, packets, prefix=None): assert self._dispersy, "Should not be called before open(...)" diff --git a/tests/debugcommunity/node.py b/tests/debugcommunity/node.py index eec71328..195d467e 100644 --- a/tests/debugcommunity/node.py +++ b/tests/debugcommunity/node.py @@ -39,7 +39,7 @@ def __init__(self, testclass, dispersy, communityclass=DebugCommunity, c_master_ self._my_member = self._dispersy.get_new_member(u"low") self._my_pub_member = Member(self._dispersy, self._my_member._ec.pub(), self._my_member.database_id) - if c_master_member == None: + if c_master_member is None: self._community = communityclass.create_community(self._dispersy, self._my_member) else: mm = self._dispersy.get_member(mid=c_master_member._community._master_member.mid) @@ -115,11 +115,13 @@ def init_my_member(self, tunnel=False, store_identity=True): self.send_identity(self._central_node) # download mm identity, mm authorizing central_node._my_member - packets = self._central_node.fetch_packets([u"dispersy-identity", u"dispersy-authorize"], self._community.master_member.mid) + packets = self._central_node.fetch_packets([u"dispersy-identity", u"dispersy-authorize"], + self._community.master_member.mid) self.give_packets(packets, self._central_node) # add this node to candidate list of mm - message = self.create_introduction_request(self._central_node.my_candidate, self.lan_address, self.wan_address, False, u"unknown", None, 1, 1) + message = self.create_introduction_request(self._central_node.my_candidate, self.lan_address, + self.wan_address, False, u"unknown", None, 1, 1) yield self._central_node.give_message(message, self) # remove introduction responses from socket @@ -148,7 +150,8 @@ def give_packets(self, packets, source, cache=False): assert isinstance(cache, bool), type(cache) self._logger.debug("%s giving %d bytes", self.my_candidate, sum(len(packet) for packet in packets)) - self._dispersy.endpoint.process_packets([(source.lan_address, TUNNEL_PREFIX + packet if source.tunnel else packet) for packet in packets], cache=cache) + self._dispersy.endpoint.process_packets([(source.lan_address, TUNNEL_PREFIX + packet + if source.tunnel else packet) for packet in packets], cache=cache) def give_message(self, message, source, cache=False): self.give_messages([message], source, cache=cache) @@ -159,7 +162,8 @@ def give_messages(self, messages, source, cache=False): Returns MESSAGES """ assert isinstance(messages, list), type(messages) - assert all(isinstance(message, Message.Implementation) for message in messages), [type(message) for message in messages] + assert all(isinstance(message, Message.Implementation) for message in messages),\ + [type(message) for message in messages] assert isinstance(cache, bool), type(cache) packets = [message.packet if message.packet else self.encode_message(message) for message in messages] @@ -225,7 +229,8 @@ def receive_packet(self, addresses=None, timeout=0.5): packets = self._dispersy.endpoint.clear_receive_queue() if packets: for address, packet in packets: - if not (addresses is None or address in addresses or (address[0] == "127.0.0.1" and ("0.0.0.0", address[1]) in addresses)): + if not (addresses is None or address in addresses or + (address[0] == "127.0.0.1" and ("0.0.0.0", address[1]) in addresses)): self._logger.debug("Ignored %d bytes from %s:%d", len(packet), address[0], address[1]) continue @@ -297,11 +302,15 @@ def decode_message(self, candidate, packet): @blocking_call_on_reactor_thread def fetch_packets(self, message_names, mid=None): if mid: - return [str(packet) for packet, in list(self._dispersy.database.execute(u"SELECT packet FROM sync, member WHERE sync.member = member.id " - u"AND mid = ? AND meta_message IN (" + ", ".join("?" * len(message_names)) + ") ORDER BY global_time, packet", - [buffer(mid), ] + [self._community.get_meta_message(name).database_id for name in message_names]))] - return [str(packet) for packet, in list(self._dispersy.database.execute(u"SELECT packet FROM sync WHERE meta_message IN (" + ", ".join("?" * len(message_names)) + ") ORDER BY global_time, packet", - [self._community.get_meta_message(name).database_id for name in message_names]))] + return [str(packet) for packet, in list(self._dispersy.database.execute( + u"SELECT packet FROM sync, member WHERE sync.member = member.id " + u"AND mid = ? AND meta_message IN (" + ", ".join("?" * len(message_names)) + + u") ORDER BY global_time, packet", + [buffer(mid), ] + [self._community.get_meta_message(name).database_id for name in message_names]))] + return [str(packet) for packet, in list(self._dispersy.database.execute( + u"SELECT packet FROM sync WHERE meta_message IN (" + ", ".join("?" * len(message_names)) + + u") ORDER BY global_time, packet", + [self._community.get_meta_message(name).database_id for name in message_names]))] @blocking_call_on_reactor_thread def fetch_messages(self, message_names, mid=None): @@ -309,22 +318,30 @@ def fetch_messages(self, message_names, mid=None): Fetch all packets for MESSAGE_NAMES from the database and converts them into Message.Implementation instances. """ - return self._dispersy.convert_packets_to_messages(self.fetch_packets(message_names, mid), community=self._community, verify=False) + return self._dispersy.convert_packets_to_messages(self.fetch_packets(message_names, mid), + community=self._community, verify=False) @blocking_call_on_reactor_thread def count_messages(self, message): - packets_stored, = self._dispersy.database.execute(u"SELECT count(*) FROM sync, member, meta_message WHERE sync.member = member.id AND sync.meta_message = meta_message.id AND sync.community = ? AND mid = ? AND name = ?", (self._community.database_id, buffer(message.authentication.member.mid), message.name)).next() + packets_stored, = self._dispersy.database.execute( + u"SELECT count(*) FROM sync, member, meta_message" + u" WHERE sync.member = member.id AND sync.meta_message = meta_message.id" + u" AND sync.community = ? AND mid = ? AND name = ?", + (self._community.database_id, buffer(message.authentication.member.mid), message.name)).next() return packets_stored @blocking_call_on_reactor_thread def assert_is_stored(self, message=None, messages=None): - if messages == None: + if messages is None: messages = [message] for message in messages: try: - undone, packet = self._dispersy.database.execute(u"SELECT undone, packet FROM sync, member WHERE sync.member = member.id AND community = ? AND mid = ? AND global_time = ?", - (self._community.database_id, buffer(message.authentication.member.mid), message.distribution.global_time)).next() + undone, packet = self._dispersy.database.execute( + u"SELECT undone, packet FROM sync, member" + u" WHERE sync.member = member.id AND community = ? AND mid = ? AND global_time = ?", + (self._community.database_id, + buffer(message.authentication.member.mid), message.distribution.global_time)).next() self._testclass.assertEqual(undone, 0, "Message is undone") self._testclass.assertEqual(str(packet), message.packet) @@ -333,13 +350,16 @@ def assert_is_stored(self, message=None, messages=None): @blocking_call_on_reactor_thread def assert_not_stored(self, message=None, messages=None): - if messages == None: + if messages is None: messages = [message] for message in messages: try: - packet, = self._dispersy.database.execute(u"SELECT packet FROM sync, member WHERE sync.member = member.id AND community = ? AND mid = ? AND global_time = ?", - (self._community.database_id, buffer(message.authentication.member.mid), message.distribution.global_time)).next() + packet, = self._dispersy.database.execute( + u"SELECT packet FROM sync, member" + u" WHERE sync.member = member.id AND community = ? AND mid = ? AND global_time = ?", + (self._community.database_id, buffer(message.authentication.member.mid), + message.distribution.global_time)).next() self._testclass.assertNotEqual(str(packet), message.packet) except StopIteration: @@ -371,13 +391,16 @@ def assert_is_canceled(self, message=None, messages=None, canceled_by=None): @blocking_call_on_reactor_thread def assert_is_undone(self, message=None, messages=None, undone_by=None): - if messages == None: + if messages is None: messages = [message] for message in messages: try: - undone, = self._dispersy.database.execute(u"SELECT undone FROM sync, member WHERE sync.member = member.id AND community = ? AND mid = ? AND global_time = ?", - (self._community.database_id, buffer(message.authentication.member.mid), message.distribution.global_time)).next() + undone, = self._dispersy.database.execute( + u"SELECT undone FROM sync, member" + u" WHERE sync.member = member.id AND community = ? AND mid = ? AND global_time = ?", + (self._community.database_id, buffer(message.authentication.member.mid), + message.distribution.global_time)).next() self._testclass.assertGreater(undone, 0, "Message is not undone") if undone_by: undone, = self._dispersy.database.execute( @@ -425,9 +448,9 @@ def create_authorize(self, permission_triplets, global_time=None, sequence_numbe """ meta = self._community.get_meta_message(u"dispersy-authorize") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() - if sequence_number == None: + if sequence_number is None: sequence_number = meta.distribution.claim_sequence_number() return meta.impl(authentication=(self._my_member,), @@ -438,9 +461,9 @@ def create_authorize(self, permission_triplets, global_time=None, sequence_numbe def create_revoke(self, permission_triplets, global_time=None, sequence_number=None): meta = self._community.get_meta_message(u"dispersy-revoke") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() - if sequence_number == None: + if sequence_number is None: sequence_number = meta.distribution.claim_sequence_number() return meta.impl(authentication=(self._my_member,), @@ -451,9 +474,9 @@ def create_revoke(self, permission_triplets, global_time=None, sequence_number=N def create_dynamic_settings(self, policies, global_time=None, sequence_number=None): meta = self._community.get_meta_message(u"dispersy-dynamic-settings") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() - if sequence_number == None: + if sequence_number is None: sequence_number = meta.distribution.claim_sequence_number() message = meta.impl(authentication=(self.my_member,), @@ -465,7 +488,7 @@ def create_dynamic_settings(self, policies, global_time=None, sequence_number=No def create_destroy_community(self, degree, global_time=None): meta = self._community.get_meta_message(u"dispersy-destroy-community") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() return meta.impl(authentication=((self._my_member),), @@ -479,7 +502,7 @@ def create_identity(self, global_time=None): """ meta = self._community.get_meta_message(u"dispersy-identity") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() return meta.impl(authentication=(self._my_member,), distribution=(global_time,)) @@ -522,9 +545,9 @@ def create_undo_own(self, message, global_time=None, sequence_number=None): assert message.authentication.member == self._my_member, "use create_dispersy_undo_other" meta = self._community.get_meta_message(u"dispersy-undo-own") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() - if sequence_number == None: + if sequence_number is None: sequence_number = meta.distribution.claim_sequence_number() return meta.impl(authentication=(self._my_member,), @@ -538,9 +561,9 @@ def create_undo_other(self, message, global_time=None, sequence_number=None): """ meta = self._community.get_meta_message(u"dispersy-undo-other") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() - if sequence_number == None: + if sequence_number is None: sequence_number = meta.distribution.claim_sequence_number() return meta.impl(authentication=(self._my_member,), @@ -555,14 +578,15 @@ def create_missing_identity(self, dummy_member=None, global_time=None): assert isinstance(dummy_member, Member), type(dummy_member) meta = self._community.get_meta_message(u"dispersy-missing-identity") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() return meta.impl(distribution=(global_time,), payload=(dummy_member.mid,)) @blocking_call_on_reactor_thread - def create_missing_sequence(self, missing_member, missing_message, missing_sequence_low, missing_sequence_high, global_time=None): + def create_missing_sequence(self, missing_member, missing_message, missing_sequence_low, missing_sequence_high, + global_time=None): """ Returns a new dispersy-missing-sequence message. """ @@ -572,7 +596,7 @@ def create_missing_sequence(self, missing_member, missing_message, missing_seque assert isinstance(missing_sequence_high, (int, long)) meta = self._community.get_meta_message(u"dispersy-missing-sequence") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() return meta.impl(distribution=(global_time,), @@ -586,7 +610,7 @@ def create_signature_request(self, identifier, message, global_time=None): assert isinstance(message, Message.Implementation) meta = self._community.get_meta_message(u"dispersy-signature-request") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() return meta.impl(distribution=(global_time,), payload=(identifier, message,)) @@ -601,7 +625,7 @@ def create_signature_response(self, identifier, message, global_time=None): meta = self._community.get_meta_message(u"dispersy-signature-response") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() return meta.impl(distribution=(global_time,), @@ -616,7 +640,7 @@ def create_missing_message(self, missing_member, missing_global_times, global_ti assert isinstance(missing_global_times, list) meta = self._community.get_meta_message(u"dispersy-missing-message") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() return meta.impl(distribution=(global_time,), @@ -630,13 +654,14 @@ def create_missing_proof(self, member, global_time=None): assert isinstance(member, Member) meta = self._community.get_meta_message(u"dispersy-missing-proof") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() return meta.impl(distribution=(global_time,), payload=(member, global_time)) @blocking_call_on_reactor_thread - def create_introduction_request(self, destination, source_lan, source_wan, advice, connection_type, sync, identifier, global_time=None): + def create_introduction_request(self, destination, source_lan, source_wan, advice, connection_type, sync, + identifier, global_time=None): """ Returns a new dispersy-introduction-request message. """ @@ -663,15 +688,17 @@ def create_introduction_request(self, destination, source_lan, source_wan, advic meta = self._community.get_meta_message(u"dispersy-introduction-request") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() return meta.impl(authentication=(self._my_member,), distribution=(global_time,), - payload=(destination.sock_addr, source_lan, source_wan, advice, connection_type, sync, identifier)) + payload=(destination.sock_addr, source_lan, source_wan, advice, connection_type, sync, + identifier)) @blocking_call_on_reactor_thread - def create_introduction_response(self, destination, source_lan, source_wan, introduction_lan, introduction_wan, connection_type, tunnel, identifier, global_time=None): + def create_introduction_response(self, destination, source_lan, source_wan, introduction_lan, introduction_wan, + connection_type, tunnel, identifier, global_time=None): """ Returns a new dispersy-introduction-request message. """ @@ -686,13 +713,14 @@ def create_introduction_response(self, destination, source_lan, source_wan, intr meta = self._community.get_meta_message(u"dispersy-introduction-response") - if global_time == None: + if global_time is None: global_time = self.claim_global_time() return meta.impl(authentication=(self._my_member,), destination=(destination,), distribution=(global_time,), - payload=(destination.sock_addr, source_lan, source_wan, introduction_lan, introduction_wan, connection_type, tunnel, identifier)) + payload=(destination.sock_addr, source_lan, source_wan, introduction_lan, introduction_wan, + connection_type, tunnel, identifier)) @blocking_call_on_reactor_thread def _create_text(self, message_name, text, global_time=None, resolution=(), destination=()): @@ -703,7 +731,7 @@ def _create_text(self, message_name, text, global_time=None, resolution=(), dest meta = self._community.get_meta_message(message_name) - if global_time == None: + if global_time is None: global_time = self.claim_global_time() return meta.impl(authentication=(self._my_member,), @@ -719,9 +747,9 @@ def _create_sequence_text(self, message_name, text, global_time=None, sequence_n meta = self._community.get_meta_message(message_name) - if global_time == None: + if global_time is None: global_time = self.claim_global_time() - if sequence_number == None: + if sequence_number is None: sequence_number = meta.distribution.claim_sequence_number() return meta.impl(authentication=(self._my_member,), @@ -741,7 +769,7 @@ def _create_doublemember_text(self, message_name, other, text, sign, global_time meta = self._community.get_meta_message(message_name) - if global_time == None: + if global_time is None: global_time = self.claim_global_time() return meta.impl(authentication=([self._my_member, my_other],), diff --git a/timeline.py b/timeline.py index 03be00c4..f8d256d5 100644 --- a/timeline.py +++ b/timeline.py @@ -24,11 +24,11 @@ def __init__(self, community): # _members contains the permission grants and revokes per member # Member / [(global_time, {u"permission^message-name":(True/False, [Message.Implementation])})] - self._members = {} + self._members = dict() # _policies contains the policies that the community is currently using (dynamic settings) # [(global_time, {u"resolution^message-name":(resolution-policy, [Message.Implementation])})] - self._policies = [] + self._policies = list() if __debug__: def printer(self): @@ -67,7 +67,8 @@ def check(self, message, permission=u"permit"): """ from .message import Message assert isinstance(message, Message.Implementation), message - assert isinstance(message.authentication, (MemberAuthentication.Implementation, DoubleMemberAuthentication.Implementation)), message.authentication + assert isinstance(message.authentication, (MemberAuthentication.Implementation, + DoubleMemberAuthentication.Implementation)), message.authentication assert isinstance(permission, unicode) assert permission in (u"permit", u"authorize", u"revoke", u"undo", u"cancel") if isinstance(message.authentication, MemberAuthentication.Implementation): @@ -85,7 +86,7 @@ def check(self, message, permission=u"permit"): # if one or more of the contained permission_triplets are allowed, we will allow the # entire message. when the message is processed only the permission_triplets that # are still valid will be used - all_allowed = [] + all_allowed = list() all_proofs = set() # question: is message.authentication.member allowed to authorize or revoke one or @@ -95,7 +96,8 @@ def check(self, message, permission=u"permit"): key = lambda member_sub_message__: member_sub_message__[1] for sub_message, iterator in groupby(message.payload.permission_triplets, key=key): permission_pairs = [(sub_message, sub_permission) for _, _, sub_permission in iterator] - allowed, proofs = self._check(message.authentication.member, message.distribution.global_time, sub_message.resolution, permission_pairs) + allowed, proofs = self._check(message.authentication.member, message.distribution.global_time, + sub_message.resolution, permission_pairs) all_allowed.append(allowed) all_proofs.update(proofs) @@ -120,7 +122,8 @@ def check(self, message, permission=u"permit"): message.payload.packet.name, message.payload.packet.resolution) self.printer() - return self._check(message.authentication.member, message.distribution.global_time, message.resolution, [(message.payload.packet.meta, u"undo")]) + return self._check(message.authentication.member, message.distribution.global_time, message.resolution, + [(message.payload.packet.meta, u"undo")]) elif message.name == u"dispersy-cancel-other": assert isinstance(message.resolution, LinearResolution.Implementation), message @@ -141,16 +144,18 @@ def check(self, message, permission=u"permit"): [(message.payload.packet.meta, u"cancel")]) else: - return self._check(message.authentication.member, message.distribution.global_time, message.resolution, [(message.meta, permission)]) + return self._check(message.authentication.member, message.distribution.global_time, message.resolution, + [(message.meta, permission)]) else: # DoubleMemberAuthentication all_proofs = set() for member in message.authentication.members: - allowed, proofs = self._check(member, message.distribution.global_time, message.resolution, [(message.meta, permission)]) + allowed, proofs = self._check(member, message.distribution.global_time, message.resolution, + [(message.meta, permission)]) all_proofs.update(proofs) if not allowed: - return (False, [proof for proof in all_proofs]) - return (True, [proof for proof in all_proofs]) + return False, [proof for proof in all_proofs] + return True, [proof for proof in all_proofs] def allowed(self, meta, global_time=0, permission=u"permit"): """ @@ -162,7 +167,8 @@ def allowed(self, meta, global_time=0, permission=u"permit"): assert global_time >= 0 assert isinstance(permission, unicode) assert permission in (u"permit", u"authorize", u"revoke", u"undo", u"cancel") - return self._check(self._community.my_member, global_time if global_time else self._community.global_time, meta.resolution, [(meta, permission)]) + return self._check(self._community.my_member, global_time if global_time else self._community.global_time, + meta.resolution, [(meta, permission)]) def _check(self, member, global_time, resolution, permission_pairs): """ @@ -184,14 +190,16 @@ def _check(self, member, global_time, resolution, permission_pairs): assert isinstance(pair[0], Message), "Requires meta message" assert isinstance(pair[1], unicode) assert pair[1] in (u"permit", u"authorize", u"revoke", u"undo", u"cancel") - assert isinstance(resolution, (PublicResolution.Implementation, LinearResolution.Implementation, DynamicResolution.Implementation, PublicResolution, LinearResolution, DynamicResolution)), resolution + assert isinstance(resolution, (PublicResolution.Implementation, LinearResolution.Implementation, + DynamicResolution.Implementation, PublicResolution, LinearResolution, + DynamicResolution)), resolution # TODO: we can make this more efficient by changing the loop a bit. make a shallow copy of # the permission_pairs and remove one after another as they succeed. key is to loop though # the self._members[member] once (currently looping over the timeline for every item in # permission_pairs). - all_proofs = [] + all_proofs = list() for message, permission in permission_pairs: # the master member can do anything @@ -214,7 +222,7 @@ def _check(self, member, global_time, resolution, permission_pairs): if not resolution.policy.meta == local_resolution: self._logger.debug("FAIL time:%d user:%d (conflicting resolution policy %s %s)", global_time, member.database_id, resolution.policy.meta, local_resolution) - return (False, all_proofs) + return False, all_proofs resolution = resolution.policy self._logger.debug("APPLY time:%d resolution^%s -> %s", @@ -257,18 +265,18 @@ def _check(self, member, global_time, resolution, permission_pairs): else: self._logger.warning("DENIED time:%d user:%d -> %s (revoked)", global_time, member.database_id, key) - return (False, [proofs]) + return False, [proofs] time, permissions = iterator.next() except StopIteration: self._logger.warning("FAIL time:%d user:%d -> %s (not authorized)", global_time, member.database_id, key) - return (False, []) + return False, list() else: self._logger.warning("FAIL time:%d user:%d -> %s (no authorization)", global_time, member.database_id, key) - return (False, []) + return False, list() # accept with proof assert len(all_proofs) > 0 @@ -276,7 +284,7 @@ def _check(self, member, global_time, resolution, permission_pairs): else: raise NotImplementedError("Unknown Resolution") - return (True, all_proofs) + return True, all_proofs def authorize(self, author, global_time, permission_triplets, proof): from .member import Member @@ -301,18 +309,20 @@ def authorize(self, author, global_time, permission_triplets, proof): # TODO: we must remove duplicates in the below permission_pairs list # check that AUTHOR is allowed to perform these authorizations - authorize_allowed, authorize_proofs = self._check(author, global_time, LinearResolution(), [(message, u"authorize") for _, message, __ in permission_triplets]) + authorize_allowed, authorize_proofs = self._check(author, global_time, LinearResolution(), + [(message, u"authorize") for _, message, __ + in permission_triplets]) if not authorize_allowed: self._logger.debug("the author is NOT allowed to perform authorisations" " for one or more of the given permission triplets") self._logger.debug("-- the author is... the master member? %s; my member? %s", author == self._community.master_member, author == self._community.my_member) - return (False, authorize_proofs) + return False, authorize_proofs for member, message, permission in permission_triplets: if isinstance(message.resolution, (PublicResolution, LinearResolution, DynamicResolution)): if not member in self._members: - self._members[member] = [] + self._members[member] = list() key = permission + "^" + message.name @@ -359,7 +369,7 @@ def authorize(self, author, global_time, permission_triplets, proof): else: raise NotImplementedError(message.resolution) - return (True, authorize_proofs) + return True, authorize_proofs def revoke(self, author, global_time, permission_triplets, proof): from .member import Member @@ -384,18 +394,19 @@ def revoke(self, author, global_time, permission_triplets, proof): # TODO: we must remove duplicates in the below permission_pairs list # check that AUTHOR is allowed to perform these authorizations - revoke_allowed, revoke_proofs = self._check(author, global_time, LinearResolution(), [(message, u"revoke") for _, message, __ in permission_triplets]) + revoke_allowed, revoke_proofs = self._check(author, global_time, LinearResolution(), + [(message, u"revoke") for _, message, __ in permission_triplets]) if not revoke_allowed: self._logger.debug("the author is NOT allowed to perform authorizations" " for one or more of the given permission triplets") self._logger.debug("-- the author is... the master member? %s; my member? %s", author == self._community.master_member, author == self._community.my_member) - return (False, revoke_proofs) + return False, revoke_proofs for member, message, permission in permission_triplets: if isinstance(message.resolution, (PublicResolution, LinearResolution, DynamicResolution)): if not member in self._members: - self._members[member] = [] + self._members[member] = list() key = permission + "^" + message.name @@ -442,7 +453,7 @@ def revoke(self, author, global_time, permission_triplets, proof): else: raise NotImplementedError(message.resolution) - return (True, revoke_proofs) + return True, revoke_proofs def get_resolution_policy(self, message, global_time): """ @@ -461,7 +472,7 @@ def get_resolution_policy(self, message, global_time): return policies[key] self._logger.debug("using %s for time %d (default)", message.resolution.default.__class__.__name__, global_time) - return message.resolution.default, [] + return message.resolution.default, list() def change_resolution_policy(self, message, global_time, policy, proof): from .message import Message @@ -474,7 +485,7 @@ def change_resolution_policy(self, message, global_time, policy, proof): if policy_time == global_time: break else: - policies = {} + policies = dict() self._policies.append((global_time, policies)) self._policies.sort() diff --git a/util.py b/util.py index 4a1448f8..8e7fef82 100644 --- a/util.py +++ b/util.py @@ -53,7 +53,8 @@ def helper(func): prefix = documented_func.__doc__ + "\n" else: prefix = "" - func.__doc__ = prefix + "\n @note: This documentation is copied from " + documented_func.__class__.__name__ + "." + documented_func.__name__ + func.__doc__ = prefix + "\n @note: This documentation is copied from "\ + + documented_func.__class__.__name__ + "." + documented_func.__name__ return func return helper @@ -117,27 +118,27 @@ def __init__(self): @property def count(self): - " Returns the number of times a method was called. " + """ Returns the number of times a method was called. """ return self._count @property def duration(self): - " Returns the cumulative time spent in a method. " + """ Returns the cumulative time spent in a method. """ return self._duration @property def average(self): - " Returns the average time spent in a method. " + """ Returns the average time spent in a method. """ return self._duration / self._count def increment(self, duration): - " Increase self.count with 1 and self.duration with DURATION. " + """ Increase self.count with 1 and self.duration with DURATION. """ assert isinstance(duration, float), type(duration) self._duration += duration self._count += 1 def get_dict(self, **kargs): - " Returns a dictionary with the statistics. " + """ Returns a dictionary with the statistics. """ return dict(count=self.count, duration=self.duration, average=self.average, **kargs) _runtime_statistics = defaultdict(RuntimeStatistic) @@ -229,7 +230,8 @@ def start_memory_dumper(): start = time() from meliae import scanner LoopingCall(lambda: scanner.dump_all_objects("memory-%d.out" % (time() - start))).start(MEMORY_DUMP_INTERVAL, now=True) - reactor.addSystemEventTrigger("before", "shutdown", lambda: scanner.dump_all_objects("memory-%d-shutdown.out" % (time() - start))) + reactor.addSystemEventTrigger("before", "shutdown", + lambda: scanner.dump_all_objects("memory-%d-shutdown.out" % (time() - start))) # # Other utils