neatest way to write multiline f-strings (not fast though)

This commit is contained in:
Fedor Lyanguzov
2024-12-28 19:35:37 +03:00
parent 9e3f16175a
commit 1b37f1b57d
+18 -21
View File
@@ -1,5 +1,4 @@
from dataclasses import dataclass
from textwrap import dedent
@dataclass
@@ -12,22 +11,20 @@ class Peer:
def format_static_peer(static_peer, routes, keepalive=30):
return f"""
[Peer]
PublicKey = {static_peer.public_key}
AllowedIPs = {routes}
Endpoint = {static_peer.endpoint}:{static_peer.port}
PersistentKeepAlive = {keepalive}
""".strip()
return (
"[Peer]\n"
f"PublicKey = {static_peer.public_key}\n"
f"AllowedIPs = {routes}\n"
f"Endpoint = {static_peer.endpoint}:{static_peer.port}\n"
f"PersistentKeepAlive = {keepalive}\n"
).strip()
def format_interface(peer, dns, forward=False):
if forward:
forward = dedent(
"""\
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
"""
forward = (
"PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE\n"
"PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE\n"
)
# wg0 always?
# eth0 always?
@@ -38,11 +35,11 @@ def format_interface(peer, dns, forward=False):
else:
port = ""
dns = f"DNS = {dns}\n"
return f"""
[Interface]
PrivateKey = {peer.private_key}
Address = {peer.address_cidr}
{port}\
{dns}\
{forward}\
""".strip()
return (
"[Interface]\n"
f"PrivateKey = {peer.private_key}\n"
f"Address = {peer.address_cidr}\n"
f"{port}"
f"{dns}"
f"{forward}"
).strip()