From 4839fa6614de277dbde21c417ae6ff4db9aa2240 Mon Sep 17 00:00:00 2001 From: Martin Weinelt Date: Thu, 8 May 2025 06:25:55 +0200 Subject: [PATCH] scripts: migrate format strings to f-strings --- scripts/generate-options.py | 14 +++++++------- scripts/mail-check.py | 32 ++++++++++++-------------------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/scripts/generate-options.py b/scripts/generate-options.py index ab1227d..3cfc0b0 100644 --- a/scripts/generate-options.py +++ b/scripts/generate-options.py @@ -42,9 +42,10 @@ def render_option_value(opt, attr): if isinstance(opt[attr], dict) and "_type" in opt[attr]: if opt[attr]["_type"] == "literalExpression": if "\n" in opt[attr]["text"]: - res = "\n```nix\n" + opt[attr]["text"].rstrip("\n") + "\n```" + text = opt[attr]["text"].rstrip("\n") + res = f"\n```nix\n{text}\n```" else: - res = "```{}```".format(opt[attr]["text"]) + res = f"```{opt[attr]["text"]}```" elif opt[attr]["_type"] == "literalMD": res = opt[attr]["text"] else: @@ -54,9 +55,9 @@ def render_option_value(opt, attr): if s == "": res = '`""`' elif "\n" in s: - res = "\n```\n" + s.rstrip("\n") + "\n```" + res = f"\n```\n{s.rstrip("\n")}\n```" else: - res = "```{}```".format(s) + res = f"```{s}```" return "- " + attr + ": " + res # type: ignore @@ -70,7 +71,7 @@ def print_option(opt): template.format( key=opt["name"], description=description or "", - type="- type: ```{}```".format(opt["type"]), + type=f"- type: ```{opt["type"]}```", default=render_option_value(opt, "default"), example=render_option_value(opt, "example"), ) @@ -84,8 +85,7 @@ for opt in options: print_option(opt) for c in groups: - print("## `{}`".format(c)) - print() + print(f"## `{c}`\n") for opt in options: if opt["name"].startswith(c): print_option(opt) diff --git a/scripts/mail-check.py b/scripts/mail-check.py index db36bc9..39b2688 100644 --- a/scripts/mail-check.py +++ b/scripts/mail-check.py @@ -14,23 +14,17 @@ RETRY = 100 def _send_mail( smtp_host, smtp_port, smtp_username, from_addr, from_pwd, to_addr, subject, starttls ): - print("Sending mail with subject '{}'".format(subject)) + print(f"Sending mail with subject '{subject}'") message = "\n".join( [ - "From: {from_addr}", - "To: {to_addr}", - "Subject: {subject}", - "Message-ID: {random}@mail-check.py", - "Date: {date}", + f"From: {from_addr}", + f"To: {to_addr}", + f"Subject: {subject}", + f"Message-ID: {uuid.uuid4()}@mail-check.py", + f"Date: {email.utils.formatdate()}", "", "This validates our mail server can send to Gmail :/", ] - ).format( - from_addr=from_addr, - to_addr=to_addr, - subject=subject, - random=str(uuid.uuid4()), - date=email.utils.formatdate(), ) retry = RETRY @@ -79,7 +73,7 @@ def _read_mail( show_body=False, delete=True, ): - print("Reading mail from %s" % imap_username) + print("Reading mail from {imap_username}") message = None @@ -93,7 +87,7 @@ def _read_mail( for _ in range(0, RETRY): print("Retrying") obj.select() - _, data = obj.search(None, '(SINCE %s) (SUBJECT "%s")' % (dt, subject)) + _, data = obj.search(None, f'(SINCE {dt}) (SUBJECT "{subject}")') if data == [b""]: time.sleep(1) continue @@ -101,8 +95,7 @@ def _read_mail( uids = data[0].decode("utf-8").split(" ") if len(uids) != 1: print( - "Warning: %d messages have been found with subject containing %s " - % (len(uids), subject) + f"Warning: {len(uids)} messages have been found with subject containing {subject}" ) # FIXME: we only consider the first matching message... @@ -113,7 +106,7 @@ def _read_mail( obj.expunge() assert raw[0] and raw[0][1] message = email.message_from_bytes(cast(bytes, raw[0][1])) - print("Message with subject '%s' has been found" % message["subject"]) + print(f"Message with subject '{message['subject']}' has been found") if show_body: if message.is_multipart(): for part in message.walk(): @@ -130,8 +123,7 @@ def _read_mail( if message is None: print( - "Error: no message with subject '%s' has been found in INBOX of %s" - % (subject, imap_username) + f"Error: no message with subject '{subject}' has been found in INBOX of {imap_username}" ) exit(1) @@ -168,7 +160,7 @@ def send_and_read(args): else: imap_username = args.to_addr - subject = "{}".format(uuid.uuid4()) + subject = f"{uuid.uuid4()}" _send_mail( smtp_host=args.smtp_host,