]> git.ipfire.org Git - location/libloc.git/commitdiff
export: Refactor writing zones
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 16 Dec 2025 17:44:28 +0000 (17:44 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 16 Dec 2025 17:44:28 +0000 (17:44 +0000)
We have too many different types to make this super plain.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/location/export.py

index fd3bad6f03af64c8005000a69ff7896fed437137..6310f7a611f6200d21ca9392494481579bea7b9a 100644 (file)
@@ -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,
        }