Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use f-strings instead of string concatenation #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions LXMF/Handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class LXMFDeliveryAnnounceHandler:
def __init__(self, lxmrouter):
self.aspect_filter = APP_NAME+".delivery"
self.aspect_filter = f"{APP_NAME}.delivery"
self.receive_path_responses = True
self.lxmrouter = lxmrouter

Expand All @@ -33,7 +33,7 @@ def received_announce(self, destination_hash, announced_identity, app_data):

class LXMFPropagationAnnounceHandler:
def __init__(self, lxmrouter):
self.aspect_filter = APP_NAME+".propagation"
self.aspect_filter = f"{APP_NAME}.propagation"
self.receive_path_responses = False
self.lxmrouter = lxmrouter

Expand All @@ -60,4 +60,4 @@ def received_announce(self, destination_hash, announced_identity, app_data):

except Exception as e:
RNS.log("Error while evaluating propagation node announce, ignoring announce.", RNS.LOG_DEBUG)
RNS.log("The contained exception was: "+str(e), RNS.LOG_DEBUG)
RNS.log(f"The contained exception was: {e}", RNS.LOG_DEBUG)
44 changes: 22 additions & 22 deletions LXMF/LXMPeer.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __init__(self, router, destination_hash):
self.unhandled_messages = {}
self.handled_messages = {}
self.last_offer = []

self.router = router
self.destination_hash = destination_hash
self.identity = RNS.Identity.recall(destination_hash)
Expand All @@ -114,18 +114,18 @@ def __init__(self, router, destination_hash):
RNS.log(f"Could not recall identity for LXMF propagation peer {RNS.prettyhexrep(self.destination_hash)}, will retry identity resolution on next sync", RNS.LOG_WARNING)

def sync(self):
RNS.log("Initiating LXMF Propagation Node sync with peer "+RNS.prettyhexrep(self.destination_hash), RNS.LOG_DEBUG)
RNS.log(f"Initiating LXMF Propagation Node sync with peer {RNS.prettyhexrep(self.destination_hash)}", RNS.LOG_DEBUG)
self.last_sync_attempt = time.time()

if time.time() > self.next_sync_attempt:
if not RNS.Transport.has_path(self.destination_hash):
RNS.log("No path to peer "+RNS.prettyhexrep(self.destination_hash)+" exists, requesting...", RNS.LOG_DEBUG)
RNS.log(f"No path to peer {RNS.prettyhexrep(self.destination_hash)} exists, requesting...", RNS.LOG_DEBUG)
RNS.Transport.request_path(self.destination_hash)
time.sleep(LXMPeer.PATH_REQUEST_GRACE)

if not RNS.Transport.has_path(self.destination_hash):
RNS.log("Path request was not answered, retrying sync with peer "+RNS.prettyhexrep(self.destination_hash)+" later", RNS.LOG_DEBUG)
RNS.log(f"Path request was not answered, retrying sync with peer {RNS.prettyhexrep(self.destination_hash)} later", RNS.LOG_DEBUG)

else:
if self.identity == None:
self.identity = RNS.Identity.recall(destination_hash)
Expand All @@ -135,7 +135,7 @@ def sync(self):
if self.destination != None:
if len(self.unhandled_messages) > 0:
if self.state == LXMPeer.IDLE:
RNS.log("Establishing link for sync to peer "+RNS.prettyhexrep(self.destination_hash)+"...", RNS.LOG_DEBUG)
RNS.log(f"Establishing link for sync to peer {RNS.prettyhexrep(self.destination_hash)}...", RNS.LOG_DEBUG)
self.sync_backoff += LXMPeer.SYNC_BACKOFF_STEP
self.next_sync_attempt = time.time() + self.sync_backoff
self.link = RNS.Link(self.destination, established_callback = self.link_established, closed_callback = self.link_closed)
Expand All @@ -147,7 +147,7 @@ def sync(self):
self.last_heard = time.time()
self.sync_backoff = 0

RNS.log("Synchronisation link to peer "+RNS.prettyhexrep(self.destination_hash)+" established, preparing request...", RNS.LOG_DEBUG)
RNS.log(f"Synchronisation link to peer {RNS.prettyhexrep(self.destination_hash)} established, preparing request...", RNS.LOG_DEBUG)
unhandled_entries = []
unhandled_ids = []
purged_ids = []
Expand All @@ -163,7 +163,7 @@ def sync(self):
purged_ids.append(transient_id)

for transient_id in purged_ids:
RNS.log("Dropping unhandled message "+RNS.prettyhexrep(transient_id)+" for peer "+RNS.prettyhexrep(self.destination_hash)+" since it no longer exists in the message store.", RNS.LOG_DEBUG)
RNS.log(f"Dropping unhandled message {RNS.prettyhexrep(transient_id)} for peer {RNS.prettyhexrep(self.destination_hash)} since it no longer exists in the message store.", RNS.LOG_DEBUG)
self.unhandled_messages.pop(transient_id)

unhandled_entries.sort(key=lambda e: e[1], reverse=False)
Expand All @@ -180,24 +180,24 @@ def sync(self):
cumulative_size += (lxm_size+per_message_overhead)
unhandled_ids.append(transient_id)

RNS.log("Sending sync request to peer "+str(self.destination), RNS.LOG_DEBUG)
RNS.log(f"Sending sync request to peer {self.destination}", RNS.LOG_DEBUG)
self.last_offer = unhandled_ids
self.link.request(LXMPeer.OFFER_REQUEST_PATH, self.last_offer, response_callback=self.offer_response, failed_callback=self.request_failed)
self.state = LXMPeer.REQUEST_SENT

else:
RNS.log("Could not request sync to peer "+RNS.prettyhexrep(self.destination_hash)+" since its identity could not be recalled.", RNS.LOG_ERROR)
RNS.log(f"Could not request sync to peer {RNS.prettyhexrep(self.destination_hash)} since its identity could not be recalled.", RNS.LOG_ERROR)

else:
RNS.log("Postponing sync with peer "+RNS.prettyhexrep(self.destination_hash)+" for "+RNS.prettytime(self.next_sync_attempt-time.time())+" due to previous failures", RNS.LOG_DEBUG)
RNS.log(f"Postponing sync with peer {RNS.prettyhexrep(self.destination_hash)} for {RNS.prettytime(self.next_sync_attempt - time.time())} due to previous failures", RNS.LOG_DEBUG)
if self.last_sync_attempt > self.last_heard:
self.alive = False

def request_failed(self, request_receipt):
RNS.log("Sync request to peer "+str(self.destination)+" failed", RNS.LOG_DEBUG)
RNS.log(f"Sync request to peer {self.destination} failed", RNS.LOG_DEBUG)
if self.link != None:
self.link.teardown()

self.state = LXMPeer.IDLE

def offer_response(self, request_receipt):
Expand All @@ -220,7 +220,7 @@ def offer_response(self, request_receipt):
for transient_id in self.last_offer:
if transient_id in self.unhandled_messages:
self.handled_messages[transient_id] = self.unhandled_messages.pop(transient_id)


elif response == True:
# Peer wants all advertised messages
Expand All @@ -242,7 +242,7 @@ def offer_response(self, request_receipt):
wanted_message_ids.append(transient_id)

if len(wanted_messages) > 0:
RNS.log("Peer wanted "+str(len(wanted_messages))+" of the available messages", RNS.LOG_DEBUG)
RNS.log(f"Peer wanted {len(wanted_messages)} of the available messages", RNS.LOG_DEBUG)

lxm_list = []

Expand All @@ -260,16 +260,16 @@ def offer_response(self, request_receipt):
self.state = LXMPeer.RESOURCE_TRANSFERRING

else:
RNS.log("Peer "+RNS.prettyhexrep(self.destination_hash)+" did not request any of the available messages, sync completed", RNS.LOG_DEBUG)
RNS.log(f"Peer {RNS.prettyhexrep(self.destination_hash)} did not request any of the available messages, sync completed", RNS.LOG_DEBUG)
if self.link != None:
self.link.teardown()

self.link = None
self.state = LXMPeer.IDLE

except Exception as e:
RNS.log("Error while handling offer response from peer "+str(self.destination), RNS.LOG_ERROR)
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR)
RNS.log(f"Error while handling offer response from peer {self.destination}", RNS.LOG_ERROR)
RNS.log(f"The contained exception was: {e}", RNS.LOG_ERROR)

if self.link != None:
self.link.teardown()
Expand All @@ -282,19 +282,19 @@ def resource_concluded(self, resource):
for transient_id in resource.transferred_messages:
message = self.unhandled_messages.pop(transient_id)
self.handled_messages[transient_id] = message

if self.link != None:
self.link.teardown()

self.link = None
self.state = LXMPeer.IDLE

RNS.log("Sync to peer "+RNS.prettyhexrep(self.destination_hash)+" completed", RNS.LOG_DEBUG)
RNS.log(f"Sync to peer {RNS.prettyhexrep(self.destination_hash)} completed", RNS.LOG_DEBUG)
self.alive = True
self.last_heard = time.time()

else:
RNS.log("Resource transfer for LXMF peer sync failed to "+str(self.destination), RNS.LOG_DEBUG)
RNS.log(f"Resource transfer for LXMF peer sync failed to {self.destination}", RNS.LOG_DEBUG)
if self.link != None:
self.link.teardown()

Expand Down
Loading