import dns.name
import dns.rdataclass
import dns.zone
+import io
class Exporter(abc.ABC):
"""
self.backend = backend
self.list = list
+ def __call__(self, f, **kwargs):
+ """
+ The main entry point to export something with this exporter...
+ """
+ # Export!
+ self.export(f, **kwargs)
+
@abc.abstractmethod
def export(self, f, **kwargs):
"""
"""
raise NotImplementedError
+
+class TextExporter(Exporter):
+ def __call__(self, f, **kwargs):
+ detach = False
+
+ # Convert any file handles to handle plain text
+ if not isinstance(f, io.TextIOBase):
+ f = io.TextIOWrapper(f, encoding="utf-8")
+ detach = True
+
+ # Export!
+ super().__call__(f, **kwargs)
+
+ # Detach the underlying stream. That way, the wrapper won't close
+ # the underlying file handle.
+ if detach:
+ f.detach()
+
def write_header(self, f, delim="#"):
"""
Writes a header
f.write("%s%s\n" % (delim, line))
-class DomainsExporter(Exporter):
+class DomainsExporter(TextExporter):
"""
Exports the plain domains
"""
f.write("%s\n" % domain)
-class HostsExporter(Exporter):
+class HostsExporter(TextExporter):
"""
Exports a file like /etc/hosts
"""
f.write("0.0.0.0 %s\n" % domain)
-class ZoneExporter(Exporter):
+class ZoneExporter(TextExporter):
def export(self, f, ttl=60, rpz_action="."):
# Write the header
self.write_header(f, ";")
"""
Exports the list
"""
- detach = False
-
- # Convert any file handles to handle plain text
- if not isinstance(f, io.TextIOBase):
- f = io.TextIOWrapper(f, encoding="utf-8")
- detach = True
-
formats = {
"domains" : exporters.DomainsExporter,
"dnsbl" : exporters.BlocklistExporter,
raise ValueError("Unknown output format: %s" % format) from e
# Run the export
- exporter.export(f, **kwargs)
-
- # Detach the underlying stream. That way, the wrapper won't close
- # the underlying file handle.
- if detach:
- f.detach()
+ exporter(f, **kwargs)
# export
export = subparsers.add_parser("export", help=_("Exports a list"))
export.add_argument("list", help=_("The name of the list"))
- export.add_argument("output", type=argparse.FileType("w"),
+ export.add_argument("output", type=argparse.FileType("wb"),
help=_("The output file"))
export.add_argument("--format", default="domains",
choices=("domains", "dnsbl", "hosts", "rpz",), help=_("Output Format"))