+week = 7*day
+
+REGEX_label = r'[a-zA-Z90-9]([a-zA-Z90-9-]{0,61}[a-zA-Z90-9])?' # max. 63 characters; must not start or end with hyphen
+REGEX_ipv4 = r'^\d{1,3}(\.\d{1,3}){3}$'
+REGEX_ipv6 = r'^[a-fA-F0-9]{1,4}(:[a-fA-F0-9]{1,4}){7}$'
+
+def check_label(label: str) -> str:
+ label = str(label)
+ pattern = r'^{0}$'.format(REGEX_label)
+ if re.match(pattern, label):
+ return label
+ raise Exception(label+" is not a valid label")
+
+def check_hostname(name: str) -> str:
+ name = str(name)
+ # check hostname for validity
+ pattern = r'^{0}(\.{0})*\.?$'.format(REGEX_label)
+ if re.match(pattern, name):
+ return name
+ raise Exception(name+" is not a valid hostname")
+
+def check_hex(data: str) -> str:
+ data = str(data)
+ if re.match('^[a-fA-F0-9]+$', data):
+ return data
+ raise Exception(data+" is not valid hex data")
+
+def check_ipv4(address: str) -> str:
+ address = str(address)
+ if re.match(REGEX_ipv4, address):
+ return address
+ raise Exception(address+" is not a valid IPv4 address")
+
+def check_ipv6(address: str) -> str:
+ address = str(address)
+ if re.match(REGEX_ipv6, address):
+ return address
+ raise Exception(address+" is not a valid IPv6 address")
+
+def time(time: int) -> str:
+ if time == 0:
+ return "0"
+ elif time % week == 0:
+ return str(time//week)+"w"
+ elif time % day == 0:
+ return str(time//day)+"d"
+ elif time % hour == 0:
+ return str(time//hour)+"h"
+ elif time % minute == 0:
+ return str(time//minute)+"m"
+ else:
+ return str(time)
+
+def column_widths(datas: 'Sequence', widths: 'Sequence[int]'):
+ assert len(datas) == len(widths)+1, "There must be as one more data points as widths"
+ result = ""
+ width_sum = 0
+ for data, width in zip(datas, widths): # will *not* cover the last point
+ result += str(data)+" " # add data point, and a minimal space
+ width_sum += width
+ if len(result) < width_sum: # add padding
+ result += (width_sum - len(result))*" "
+ # last data point
+ return result+str(datas[-1])
+
+
+## Enums
+class Protocol:
+ TCP = 'tcp'
+ UDP = 'udp'
+
+class Algorithm:
+ RSA_SHA256 = 8
+
+class Digest:
+ SHA1 = 1
+ SHA256 = 2
+
+
+## Record types
+class A:
+ def __init__(self, address: str) -> None:
+ self._address = check_ipv4(address)
+
+ def generate_rr(self, owner: str, zone: 'Zone') -> 'Any':
+ return zone.RR(owner, 'A', self._address)
+
+
+class AAAA:
+ def __init__(self, address: str) -> None:
+ self._address = check_ipv6(address)
+
+ def generate_rr(self, owner: str, zone: 'Zone') -> 'Any':
+ return zone.RR(owner, 'AAAA', self._address)
+
+
+class MX:
+ def __init__(self, name: str, prio: int = 10) -> None:
+ self._priority = int(prio)
+ self._name = check_hostname(name)
+
+ def generate_rr(self, owner: str, zone: 'Zone') -> 'Any':
+ return zone.RR(owner, 'MX', '{0} {1}'.format(self._priority, zone.abs_hostname(self._name)))
+