scripts: migrate format strings to f-strings

This commit is contained in:
Martin Weinelt 2025-05-08 06:25:55 +02:00
parent ddc6ce61db
commit 4839fa6614
No known key found for this signature in database
GPG key ID: 87C1E9888F856759
2 changed files with 19 additions and 27 deletions

View file

@ -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,