mirror of
https://github.com/github/octodns.git
synced 2024-05-11 05:55:00 +00:00
80 lines
2.0 KiB
Python
80 lines
2.0 KiB
Python
#
|
|
#
|
|
#
|
|
|
|
from fqdn import FQDN
|
|
|
|
from ..idna import idna_encode
|
|
|
|
|
|
class _TargetValue(str):
|
|
@classmethod
|
|
def parse_rdata_text(self, value):
|
|
return value
|
|
|
|
@classmethod
|
|
def validate(cls, data, _type):
|
|
reasons = []
|
|
if data == '':
|
|
reasons.append('empty value')
|
|
elif not data:
|
|
reasons.append('missing value')
|
|
else:
|
|
data = idna_encode(data)
|
|
if not FQDN(str(data), allow_underscores=True).is_valid:
|
|
reasons.append(f'{_type} value "{data}" is not a valid FQDN')
|
|
elif not data.endswith('.'):
|
|
reasons.append(f'{_type} value "{data}" missing trailing .')
|
|
return reasons
|
|
|
|
@classmethod
|
|
def process(cls, value):
|
|
if value:
|
|
return cls(value)
|
|
return None
|
|
|
|
def __new__(cls, v):
|
|
v = idna_encode(v)
|
|
return super().__new__(cls, v)
|
|
|
|
@property
|
|
def rdata_text(self):
|
|
return self
|
|
|
|
|
|
#
|
|
# much like _TargetValue, but geared towards multiple values
|
|
class _TargetsValue(str):
|
|
@classmethod
|
|
def parse_rdata_text(cls, value):
|
|
return value
|
|
|
|
@classmethod
|
|
def validate(cls, data, _type):
|
|
if not data:
|
|
return ['missing value(s)']
|
|
elif not isinstance(data, (list, tuple)):
|
|
data = (data,)
|
|
reasons = []
|
|
for value in data:
|
|
value = idna_encode(value)
|
|
if not FQDN(value, allow_underscores=True).is_valid:
|
|
reasons.append(
|
|
f'Invalid {_type} value "{value}" is not a valid FQDN.'
|
|
)
|
|
elif not value.endswith('.'):
|
|
reasons.append(f'{_type} value "{value}" missing trailing .')
|
|
return reasons
|
|
|
|
@classmethod
|
|
def process(cls, values):
|
|
return [cls(v) for v in values]
|
|
|
|
def __new__(cls, v):
|
|
v = idna_encode(v)
|
|
return super().__new__(cls, v)
|
|
|
|
@property
|
|
def rdata_text(self):
|
|
return self
|