# 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:
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.'''
# 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()
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 <https://www.ralfj.de/projects/zonemaker> on {}".format(self._name, datetime.datetime.now()))