+
+ self._domains = domains
+
+ def RR(self, owner: str, recordType: str, data: str) -> str:
+ '''generate given RR, in textual representation'''
+ assert re.match(r'^[A-Z]+$', recordType), "got invalid record type"
+ # figure out TTL
+ attrname = "_"+recordType+"_TTL"
+ TTL = None # type: int
+ if hasattr(self, attrname):
+ TTL = getattr(self, attrname)
+ if TTL is None:
+ TTL = self._other_TTL
+ # be done
+ return "{0}\t{1}\t{2}\t{3}".format(self.abs_hostname(owner), TTL, recordType, data)
+
+ def abs_hostname(self, name):
+ if name.endswith('.'):
+ return name
+ return name+"."+self._name
+
+ def inc_serial(self) -> int:
+ # get serial
+ cur_serial = 0
+ try:
+ with open(self._serialfile) as f:
+ cur_serial = int(f.read())
+ except FileNotFoundError:
+ pass
+ # increment serial
+ cur_serial += 1
+ # save serial
+ with open(self._serialfile, 'w') as f:
+ f.write(str(cur_serial))
+ # be done
+ return cur_serial
+
+ def generate_rrs(self) -> Iterator:
+ # SOA record
+ serial = self.inc_serial()
+ yield self.RR(self._name, 'SOA',
+ ('{NS} {mail} ({serial} {refresh} {retry} {expire} {NX_TTL}) ; '+
+ '(serial refresh retry expire NX_TTL)').format(
+ NS=self.abs_hostname(self._NS[0]), mail=self._mail, serial=serial,
+ refresh=time(self._refresh), retry=time(self._retry), expire=time(self._expire),
+ NX_TTL=time(self._NX_TTL))
+ )
+ # NS records
+ for ns in self._NS:
+ yield self.RR(self._name, 'NS', self.abs_hostname(ns))
+
+ # all the rest
+ for name in sorted(self._domains.keys(), key=lambda s: list(reversed(s.split('.')))):
+ print(name)
+ #for rr in self._domains[name].generate_rrs(self):
+ #yield rr