From e8bf051cd0a1b3ab8265b24f8b12200a3b5be49b Mon Sep 17 00:00:00 2001 From: link2xt Date: Mon, 15 Jul 2024 19:37:03 +0000 Subject: [PATCH] refactor: use f-string in logging where it is easy % is only interpreted if there are two or more arguments: So it is safe to pass a single argument with already formatted string. --- chatmaild/src/chatmaild/doveauth.py | 2 +- chatmaild/src/chatmaild/echo.py | 8 ++++---- chatmaild/src/chatmaild/filedict.py | 2 +- chatmaild/src/chatmaild/filtermail.py | 6 +++--- chatmaild/src/chatmaild/metadata.py | 4 ++-- chatmaild/src/chatmaild/notifier.py | 6 +++--- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/chatmaild/src/chatmaild/doveauth.py b/chatmaild/src/chatmaild/doveauth.py index f56decc5..7a0cbaac 100644 --- a/chatmaild/src/chatmaild/doveauth.py +++ b/chatmaild/src/chatmaild/doveauth.py @@ -233,7 +233,7 @@ def handle_dovecot_protocol(rfile, wfile, db: Database, config: Config): try: res = handle_dovecot_request(msg, db, config) except UnknownCommand: - logging.warning("unknown command: %r", msg) + logging.warning(f"unknown command: {msg!r}") else: if res: wfile.write(res.encode("ascii")) diff --git a/chatmaild/src/chatmaild/echo.py b/chatmaild/src/chatmaild/echo.py index 4a09809b..765e2dbd 100644 --- a/chatmaild/src/chatmaild/echo.py +++ b/chatmaild/src/chatmaild/echo.py @@ -21,9 +21,9 @@ hooks = events.HookCollection() @hooks.on(events.RawEvent) def log_event(event): if event.kind == EventType.INFO: - logging.info("%s", event.msg) + logging.info(event.msg) elif event.kind == EventType.WARNING: - logging.warning("%s", event.msg) + logging.warning(event.msg) @hooks.on(events.RawEvent(EventType.ERROR)) @@ -45,7 +45,7 @@ def on_group_image_changed(event): @hooks.on(events.GroupNameChanged) def on_group_name_changed(event): - logging.info("group name changed, old name: %s", event.old_name) + logging.info(f"group name changed, old name: {event.old_name}") @hooks.on(events.NewMessage(func=lambda e: not e.command)) @@ -72,7 +72,7 @@ def main(): with Rpc() as rpc: deltachat = DeltaChat(rpc) system_info = deltachat.get_system_info() - logging.info("Running deltachat core %s", system_info.deltachat_core_version) + logging.info(f"Running deltachat core {system_info.deltachat_core_version}") accounts = deltachat.get_all_accounts() account = accounts[0] if accounts else deltachat.add_account() diff --git a/chatmaild/src/chatmaild/filedict.py b/chatmaild/src/chatmaild/filedict.py index c86b94a0..0e98c310 100644 --- a/chatmaild/src/chatmaild/filedict.py +++ b/chatmaild/src/chatmaild/filedict.py @@ -32,5 +32,5 @@ class FileDict: except FileNotFoundError: return {} except Exception: - logging.warning("corrupt serialization state at: %r", self.path) + logging.warning(f"corrupt serialization state at: {self.path!r}") return {} diff --git a/chatmaild/src/chatmaild/filtermail.py b/chatmaild/src/chatmaild/filtermail.py index eaa7d912..39b51226 100644 --- a/chatmaild/src/chatmaild/filtermail.py +++ b/chatmaild/src/chatmaild/filtermail.py @@ -152,7 +152,7 @@ class BeforeQueueHandler: self.send_rate_limiter = SendRateLimiter() async def handle_MAIL(self, server, session, envelope, address, mail_options): - logging.info("handle_MAIL from %s", address) + logging.info(f"handle_MAIL from {address}") envelope.mail_from = address max_sent = self.config.max_user_send_per_minute if not self.send_rate_limiter.is_sending_allowed(address, max_sent): @@ -176,13 +176,13 @@ class BeforeQueueHandler: def check_DATA(self, envelope): """the central filtering function for e-mails.""" - logging.info("Processing DATA message from %s", envelope.mail_from) + logging.info(f"Processing DATA message from {envelope.mail_from}") message = BytesParser(policy=policy.default).parsebytes(envelope.content) mail_encrypted = check_encrypted(message) _, from_addr = parseaddr(message.get("from").strip()) - logging.info("mime-from: %s envelope-from: %r", from_addr, envelope.mail_from) + logging.info(f"mime-from: {from_addr} envelope-from: {envelope.mail_from!r}") if envelope.mail_from.lower() != from_addr.lower(): return f"500 Invalid FROM <{from_addr!r}> for <{envelope.mail_from!r}>" diff --git a/chatmaild/src/chatmaild/metadata.py b/chatmaild/src/chatmaild/metadata.py index 8ef66f1e..c8726ea5 100644 --- a/chatmaild/src/chatmaild/metadata.py +++ b/chatmaild/src/chatmaild/metadata.py @@ -82,7 +82,7 @@ def handle_dovecot_request(msg, transactions, notifier, metadata, iroh_relay=Non ): # Handle `GETMETADATA "" /shared/vendor/deltachat/irohrelay` return f"O{iroh_relay}\n" - logging.warning("lookup ignored: %r", msg) + logging.warning(f"lookup ignored: {msg!r}") return "N\n" elif short_command == DICTPROXY_ITERATE_CHAR: # Empty line means ITER_FINISHED. @@ -92,7 +92,7 @@ def handle_dovecot_request(msg, transactions, notifier, metadata, iroh_relay=Non return # no version checking if short_command not in (DICTPROXY_TRANSACTION_CHARS): - logging.warning("unknown dictproxy request: %r", msg) + logging.warning(f"unknown dictproxy request: {msg!r}") return transaction_id = parts[0] diff --git a/chatmaild/src/chatmaild/notifier.py b/chatmaild/src/chatmaild/notifier.py index 6800cce8..6a2136fa 100644 --- a/chatmaild/src/chatmaild/notifier.py +++ b/chatmaild/src/chatmaild/notifier.py @@ -92,7 +92,7 @@ class Notifier: def requeue_persistent_queue_items(self): for queue_path in self.queue_dir.iterdir(): if queue_path.name.endswith(".tmp"): - logging.warning("removing spurious queue item: %r", queue_path) + logging.warning(f"removing spurious queue item: {queue_path!r}") queue_path.unlink() continue queue_item = PersistentQueueItem.read_from_path(queue_path) @@ -104,7 +104,7 @@ class Notifier: deadline = queue_item.start_ts + self.DROP_DEADLINE if retry_num >= len(self.retry_queues) or when > deadline: queue_item.delete() - logging.error("notification exceeded deadline: %r", queue_item.token) + logging.error(f"notification exceeded deadline: {queue_item.token!r}") return self.retry_queues[retry_num].put((when, queue_item)) @@ -162,5 +162,5 @@ class NotifyThread(Thread): queue_item.delete() return - logging.warning("Notification request failed: %r", res) + logging.warning(f"Notification request failed: {res!r}") self.notifier.queue_for_retry(queue_item, retry_num=self.retry_num + 1)