From: Ralf Jung Date: Wed, 14 Oct 2015 20:02:30 +0000 (+0200) Subject: allow adding subgroups of dicts X-Git-Url: https://git.ralfj.de/zonemaker.git/commitdiff_plain/f036ee7868886116f0aab76a4cf80dae72b4cd19?ds=sidebyside;hp=80ad8ab8737f7e43f12ced20d504a3fa47cf6e10 allow adding subgroups of dicts --- diff --git a/db.example.com.py b/db.example.com.py index daf5327..ca620ef 100644 --- a/db.example.com.py +++ b/db.example.com.py @@ -42,4 +42,9 @@ __zone__ = Zone('example.com.', serialfile = 'db.example.com.srl', # 'sub1': Delegation('ns.example.org.'), # this adds an NS record 'sub2': SecureDelegation('ns.example.com.', 12345, Algorithm.RSA_SHA256, Digest.SHA256, '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'), # this adds an NS and a DS record + # + 'local': { + 'one': Name(one4), + 'stuff': CName('one'), + }, }) diff --git a/zone.py b/zone.py index 1e1251a..5cd0d72 100644 --- a/zone.py +++ b/zone.py @@ -101,6 +101,15 @@ def column_widths(datas: 'Sequence', widths: 'Sequence[int]'): # last data point return result+str(datas[-1]) +def concatenate(root, path): + if path == '' or root == '': + raise Exception("Empty domain name is not valid") + if path == '@': + return root + if root == '@' or path.endswith('.'): + return path + return path+"."+root + ## Enums class Protocol: @@ -131,15 +140,7 @@ class RR: return self def relativize(self, root): - def _relativize(path): - if path == '' or root == '': - raise Exception("Empty domain name is not valid") - if path == '@': - return root - if root == '@' or path.endswith('.'): - return path - return path+"."+root - return self.mapPath(_relativize) + return self.mapPath(lambda path: concatenate(root, path)) def mapTTL(self, f): '''Run the current TTL and the recordType through f.''' @@ -346,6 +347,20 @@ class Zone: # be done return cur_serial + @staticmethod + def generate_rrs_from_dict(root, domains): + for name in sorted(domains.keys(), key=lambda s: s.split('.')): + if name.endswith('.'): + raise Exception("You are trying to add a record outside of your zone. This is not supported. Use '@' for the zone root.") + domain = domains[name] + name = concatenate(root, name) + if isinstance(domain, dict): + for rr in Zone.generate_rrs_from_dict(name, domain): + yield rr + else: + for rr in domain.generate_rrs(): + yield rr.relativize(name) + def generate_rrs(self) -> 'Iterator': # SOA record serial = self.inc_serial() @@ -360,11 +375,8 @@ class Zone: for name in self._NS: yield NS(name).generate_rr() # all the rest - for name in sorted(self._domains.keys(), key=lambda s: list(reversed(s.split('.')))): - if name.endswith('.'): - raise Exception("You are trying to add a record outside of your zone. This is not supported. Use '@' for the zone root.") - for rr in self._domains[name].generate_rrs(): - yield rr.relativize(name) + for rr in Zone.generate_rrs_from_dict('@', self._domains): + yield rr def write(self) -> None: print(";; {} zone file, generated by zonemaker on {}".format(self._name, datetime.datetime.now()))