Apply consistent Python and Django coding standards

This commit is contained in:
parkel committed 2026-11-21 02:12:32 +01:00
1 parent 1d0b5adeee
commit d7314fa927
61 files changed
+1829 -603

No files matched your search

+38 -12
View File
@@ -65,7 +65,9 @@ def txt_chunks(text):
def quote_txt_chunk(chunk):
escaped = "".join(
chr(byte) if 32 <= byte <= 126 and byte not in (34, 92) else f"\\{byte:03d}"
chr(byte)
if 32 <= byte <= 126 and byte not in (34, 92)
else f"\\{byte:03d}"
for byte in chunk
)
return f'"{escaped}"'
@@ -95,7 +97,9 @@ class AddressRecord:
@classmethod
def from_manifest(cls, data):
if not isinstance(data, dict) or data.keys() != {"name", "address"}:
raise ValueError("Expected an address record with a name and IP address.")
raise ValueError(
"Expected an address record with a name and IP address."
)
return cls(data["name"], data["address"])
@@ -144,7 +148,9 @@ class TxtRecord:
try:
chunks = txt_chunks(self.text)
except UnicodeEncodeError:
raise ValueError("The TXT record value must be valid Unicode.") from None
raise ValueError(
"The TXT record value must be valid Unicode."
) from None
if sum(len(chunk) + 1 for chunk in chunks) > 65535:
raise ValueError("The TXT record value is too long for DNS.")
@@ -155,7 +161,9 @@ class TxtRecord:
return cls(data["name"], data["text"])
def render(self):
text = " ".join(quote_txt_chunk(chunk) for chunk in txt_chunks(self.text))
text = " ".join(
quote_txt_chunk(chunk) for chunk in txt_chunks(self.text)
)
return f"{self.name}. IN TXT {text}"
@@ -178,12 +186,21 @@ class DnsZone:
def __post_init__(self):
object.__setattr__(self, "domain", dns_name(self.domain))
object.__setattr__(self, "responsible_name", dns_name(self.responsible_name))
if type(self.serial) is not int or not 0 <= self.serial <= MAX_SOA_SERIAL:
raise ValueError("The SOA serial must be an unsigned 32-bit integer.")
object.__setattr__(
self, "responsible_name", dns_name(self.responsible_name)
)
if (
type(self.serial) is not int
or not 0 <= self.serial <= MAX_SOA_SERIAL
):
raise ValueError(
"The SOA serial must be an unsigned 32-bit integer."
)
nameservers = tuple(dns_name(name) for name in self.nameservers)
if not nameservers or len(set(nameservers)) != len(nameservers):
raise ValueError("Supply at least one nameserver and list each name once.")
raise ValueError(
"Supply at least one nameserver and list each name once."
)
object.__setattr__(self, "nameservers", nameservers)
address_names = {record.name for record in self.addresses}
@@ -193,7 +210,9 @@ class DnsZone:
in_zone(name, self.domain) and name not in address_names
for name in nameservers
):
raise ValueError("Each in-zone nameserver needs an address record.")
raise ValueError(
"Each in-zone nameserver needs an address record."
)
self.check_mail_records(address_names)
def check_mail_records(self, address_names):
@@ -302,9 +321,13 @@ class DnsZone:
def main(argv=None):
parser = argparse.ArgumentParser(prog="coulobre-dns-node")
commands = parser.add_subparsers(dest="command", required=True)
render = commands.add_parser("render", help="Render a BIND9 zone from a manifest.")
render = commands.add_parser(
"render", help="Render a BIND9 zone from a manifest."
)
render.add_argument("manifest", type=Path)
apply = commands.add_parser("apply", help="Install or update a primary BIND9 zone.")
apply = commands.add_parser(
"apply", help="Install or update a primary BIND9 zone."
)
apply.add_argument("manifest", type=Path)
commands.add_parser(
"apply-request",
@@ -330,7 +353,10 @@ def main(argv=None):
from .dns_node_apply import apply_zone
apply_zone(zone)
print(f"Applied BIND9 zone {zone.domain} at SOA serial {zone.serial}.")
print(
f"Applied BIND9 zone {zone.domain} "
f"at SOA serial {zone.serial}."
)
else:
sys.stdout.write(zone.render())
except (OSError, ValueError) as error: