mirror of
https://gitlab.com/simple-nixos-mailserver/nixos-mailserver.git
synced 2025-05-17 16:10:49 +05:00
scripts: migrate format strings to f-strings
This commit is contained in:
parent
ddc6ce61db
commit
4839fa6614
@ -42,9 +42,10 @@ def render_option_value(opt, attr):
|
|||||||
if isinstance(opt[attr], dict) and "_type" in opt[attr]:
|
if isinstance(opt[attr], dict) and "_type" in opt[attr]:
|
||||||
if opt[attr]["_type"] == "literalExpression":
|
if opt[attr]["_type"] == "literalExpression":
|
||||||
if "\n" in opt[attr]["text"]:
|
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:
|
else:
|
||||||
res = "```{}```".format(opt[attr]["text"])
|
res = f"```{opt[attr]["text"]}```"
|
||||||
elif opt[attr]["_type"] == "literalMD":
|
elif opt[attr]["_type"] == "literalMD":
|
||||||
res = opt[attr]["text"]
|
res = opt[attr]["text"]
|
||||||
else:
|
else:
|
||||||
@ -54,9 +55,9 @@ def render_option_value(opt, attr):
|
|||||||
if s == "":
|
if s == "":
|
||||||
res = '`""`'
|
res = '`""`'
|
||||||
elif "\n" in s:
|
elif "\n" in s:
|
||||||
res = "\n```\n" + s.rstrip("\n") + "\n```"
|
res = f"\n```\n{s.rstrip("\n")}\n```"
|
||||||
else:
|
else:
|
||||||
res = "```{}```".format(s)
|
res = f"```{s}```"
|
||||||
|
|
||||||
return "- " + attr + ": " + res # type: ignore
|
return "- " + attr + ": " + res # type: ignore
|
||||||
|
|
||||||
@ -70,7 +71,7 @@ def print_option(opt):
|
|||||||
template.format(
|
template.format(
|
||||||
key=opt["name"],
|
key=opt["name"],
|
||||||
description=description or "",
|
description=description or "",
|
||||||
type="- type: ```{}```".format(opt["type"]),
|
type=f"- type: ```{opt["type"]}```",
|
||||||
default=render_option_value(opt, "default"),
|
default=render_option_value(opt, "default"),
|
||||||
example=render_option_value(opt, "example"),
|
example=render_option_value(opt, "example"),
|
||||||
)
|
)
|
||||||
@ -84,8 +85,7 @@ for opt in options:
|
|||||||
print_option(opt)
|
print_option(opt)
|
||||||
|
|
||||||
for c in groups:
|
for c in groups:
|
||||||
print("## `{}`".format(c))
|
print(f"## `{c}`\n")
|
||||||
print()
|
|
||||||
for opt in options:
|
for opt in options:
|
||||||
if opt["name"].startswith(c):
|
if opt["name"].startswith(c):
|
||||||
print_option(opt)
|
print_option(opt)
|
||||||
|
@ -14,23 +14,17 @@ RETRY = 100
|
|||||||
def _send_mail(
|
def _send_mail(
|
||||||
smtp_host, smtp_port, smtp_username, from_addr, from_pwd, to_addr, subject, starttls
|
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(
|
message = "\n".join(
|
||||||
[
|
[
|
||||||
"From: {from_addr}",
|
f"From: {from_addr}",
|
||||||
"To: {to_addr}",
|
f"To: {to_addr}",
|
||||||
"Subject: {subject}",
|
f"Subject: {subject}",
|
||||||
"Message-ID: {random}@mail-check.py",
|
f"Message-ID: {uuid.uuid4()}@mail-check.py",
|
||||||
"Date: {date}",
|
f"Date: {email.utils.formatdate()}",
|
||||||
"",
|
"",
|
||||||
"This validates our mail server can send to Gmail :/",
|
"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
|
retry = RETRY
|
||||||
@ -79,7 +73,7 @@ def _read_mail(
|
|||||||
show_body=False,
|
show_body=False,
|
||||||
delete=True,
|
delete=True,
|
||||||
):
|
):
|
||||||
print("Reading mail from %s" % imap_username)
|
print("Reading mail from {imap_username}")
|
||||||
|
|
||||||
message = None
|
message = None
|
||||||
|
|
||||||
@ -93,7 +87,7 @@ def _read_mail(
|
|||||||
for _ in range(0, RETRY):
|
for _ in range(0, RETRY):
|
||||||
print("Retrying")
|
print("Retrying")
|
||||||
obj.select()
|
obj.select()
|
||||||
_, data = obj.search(None, '(SINCE %s) (SUBJECT "%s")' % (dt, subject))
|
_, data = obj.search(None, f'(SINCE {dt}) (SUBJECT "{subject}")')
|
||||||
if data == [b""]:
|
if data == [b""]:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
continue
|
continue
|
||||||
@ -101,8 +95,7 @@ def _read_mail(
|
|||||||
uids = data[0].decode("utf-8").split(" ")
|
uids = data[0].decode("utf-8").split(" ")
|
||||||
if len(uids) != 1:
|
if len(uids) != 1:
|
||||||
print(
|
print(
|
||||||
"Warning: %d messages have been found with subject containing %s "
|
f"Warning: {len(uids)} messages have been found with subject containing {subject}"
|
||||||
% (len(uids), subject)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# FIXME: we only consider the first matching message...
|
# FIXME: we only consider the first matching message...
|
||||||
@ -113,7 +106,7 @@ def _read_mail(
|
|||||||
obj.expunge()
|
obj.expunge()
|
||||||
assert raw[0] and raw[0][1]
|
assert raw[0] and raw[0][1]
|
||||||
message = email.message_from_bytes(cast(bytes, 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 show_body:
|
||||||
if message.is_multipart():
|
if message.is_multipart():
|
||||||
for part in message.walk():
|
for part in message.walk():
|
||||||
@ -130,8 +123,7 @@ def _read_mail(
|
|||||||
|
|
||||||
if message is None:
|
if message is None:
|
||||||
print(
|
print(
|
||||||
"Error: no message with subject '%s' has been found in INBOX of %s"
|
f"Error: no message with subject '{subject}' has been found in INBOX of {imap_username}"
|
||||||
% (subject, imap_username)
|
|
||||||
)
|
)
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
@ -168,7 +160,7 @@ def send_and_read(args):
|
|||||||
else:
|
else:
|
||||||
imap_username = args.to_addr
|
imap_username = args.to_addr
|
||||||
|
|
||||||
subject = "{}".format(uuid.uuid4())
|
subject = f"{uuid.uuid4()}"
|
||||||
|
|
||||||
_send_mail(
|
_send_mail(
|
||||||
smtp_host=args.smtp_host,
|
smtp_host=args.smtp_host,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user