From: Michael Tremer Date: Tue, 16 Dec 2025 17:44:28 +0000 (+0000) Subject: export: Refactor writing zones X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=99f7305c5d5fe4499ab472c81bf11fe2fcadcf34;p=location%2Flibloc.git export: Refactor writing zones We have too many different types to make this super plain. Signed-off-by: Michael Tremer --- diff --git a/src/python/location/export.py b/src/python/location/export.py index fd3bad6..6310f7a 100644 --- a/src/python/location/export.py +++ b/src/python/location/export.py @@ -309,7 +309,7 @@ class ZoneExporter(object): self.ttl = ttl try: - self.type, self._format = self.formats[format] + self.write = self.formats[format] except KeyError as e: raise ValueError("Unsupported format for a zone: %s" % format) @@ -325,51 +325,53 @@ class ZoneExporter(object): if self.db.license: f.write("License: %s\n" % self.db.license) if self.db.description: - f.write("%s" % self.db.description) - f.write("Updated At: %s" % created_at.isoformat()) + f.write("%s\n" % self.db.description) + f.write("Updated At: %s\n" % created_at.isoformat()) f.write(";##############################################################################\n") f.write("$ORIGIN %s" % self.origin) if self.ttl: f.write("$TTL %s" % self.ttl) - if self.format == "bogons": - networks = self.db.list_bogons() - else: - networks = self.db.networks - - # Write all networks - for network in networks: - self.write(f, network) - - def write(self, f, network): - rp = network.reverse_pointer(suffix="") + # Write all records + self.write(self, f) - # If we don't have a reverse pointer, we will have to split the network - # into its subnets and call ourselves again. - if rp is None: + def _write_record(self, f, network, type, content): + """ + Writes a single record to the output file + """ + name = network.reverse_pointer(suffix="") + if name is None: for subnet in network.subnets: - self.write(f, subnet) + self._write_record(f, subnet, type, content) return - # Fetch the payload - content = self._format(self, network) + f.write("%s IN %s %s\n" % (name, type, content)) - # Skip the network if there is no payload - if content is None: - return + # Bogons - f.write("%s IN %s %s\n" % (rp, self.type, content)) + def _write_bogons(self, f): + for network in self.db.list_bogons(): + self._write_record(f, network, "A", "127.0.0.1") - # DNSBL + # Origin - def _format_dnsbl(self, network): - return "127.0.0.2" + def _write_origin(self, f): + for network in self.db.networks: + rp = network.reverse_pointer(suffix="") - # Origin + # If we don't have a reverse pointer, we will have to split the network + # into its subnets and call ourselves again. + if rp is None: + for subnet in network.subnets: + self._write_origin_network(f, subnet) + + continue + + self._write_origin_network(f, network) - def _format_origin(self, network): + def _write_origin_network(self, f, network): # Skip the network if it does not belong to an AS if network.asn is None: return @@ -379,9 +381,9 @@ class ZoneExporter(object): if asn is None: return - return "\"%s\"" % asn + self._write_record(f, network, "TXT", "\"%s\"" % asn) formats = { - "origin" : ("TXT", _format_asn), - "bogons" : ("A", _format_dnsbl), + "bogons" : _write_bogons, + "origin" : _write_origin, }