# Third party modules
# Own modules
-from .common import pp, compare_fqdn, to_bytes, to_utf8
+from .common import pp, compare_fqdn, to_bytes, to_utf8, to_str
from .errors import PpError
from .obj import PpBaseObjectError, PpBaseObject
-__version__ = '0.3.2'
+__version__ = '0.4.1'
LOG = logging.getLogger(__name__)
pass
+# =============================================================================
+class PdnsWrongSoaDataError(PdnsApiRrsetError):
+
+ # -------------------------------------------------------------------------
+ def __init__(self, data):
+ self.data = str(data)
+
+ # -------------------------------------------------------------------------
+ def __str__(self):
+
+ msg = "Could not interprete SOA data: {!r}.".format(self.data)
+ return msg
+
+
# =============================================================================
def compare_rrsets(x, y):
out += ", ".join(fields) + ")>"
return out
+
+# =============================================================================
+class PdnsSoaData(PpBaseObject):
+
+ re_soa_data = re.compile(r'^\s*(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*$')
+ re_ws = re.compile(r'\s+')
+
+ # -------------------------------------------------------------------------
+ def __init__(
+ self, primary=None, email=None, serial=None, refresh=None, retry=None, expire=None,
+ ttl=None, appname=None, verbose=0, version=__version__,
+ base_dir=None):
+
+ self._primary = None
+ self._email = None
+ self._serial = None
+ self._refresh = None
+ self._retry = None
+ self._expire = None
+ self._ttl = None
+
+ super(PdnsSoaData, self).__init__(
+ appname=appname, verbose=verbose, version=version, base_dir=base_dir,
+ initialized=False)
+
+ self.primary = primary
+ self.email = email
+ self.serial = serial
+ self.refresh = refresh
+ self.retry = retry
+ self.expire = expire
+ self.ttl = ttl
+
+ if (self.primary and self.email and self.serial is not None and self.refresh and
+ self.retry and self.expire and self.ttl):
+ self.initialized = True
+ else:
+ self.initialized = False
+
+ # -----------------------------------------------------------
+ @property
+ def primary(self):
+ "The primary name server of this SOA"
+ return self._primary
+
+ @primary.setter
+ def primary(self, value):
+ if value is None:
+ self._primary = None
+ return
+ self._primary = str(value).strip()
+
+ # -----------------------------------------------------------
+ @property
+ def email(self):
+ "The E-Mail-address of the hostmaster of this zone."
+ return self._email
+
+ @email.setter
+ def email(self, value):
+ if value is None:
+ self._email = None
+ return
+ self._email = str(value).strip()
+
+ # -----------------------------------------------------------
+ @property
+ def serial(self):
+ "The serial number of this SOA."
+ return self._serial
+
+ @serial.setter
+ def serial(self, value):
+ if value is None:
+ self._serial = None
+ return
+ self._serial = int(value)
+
+ # -----------------------------------------------------------
+ @property
+ def refresh(self):
+ "The time in seconds when slaves should ask master for changes."
+ return self._refresh
+
+ @refresh.setter
+ def refresh(self, value):
+ if value is None:
+ self._refresh = None
+ return
+ self._refresh = int(value)
+
+ # -----------------------------------------------------------
+ @property
+ def retry(self):
+ """The time in seconds when slaves should retry getting changes from master,
+ if an attemt to get it was not successful."""
+ return self._retry
+
+ @retry.setter
+ def retry(self, value):
+ if value is None:
+ self._retry = None
+ return
+ self._retry = int(value)
+
+ # -----------------------------------------------------------
+ @property
+ def expire(self):
+ """The time in seconds when slaves should expiring the zone,
+ if an attemt to get it was not successful."""
+ return self._expire
+
+ @expire.setter
+ def expire(self, value):
+ if value is None:
+ self._expire = None
+ return
+ self._expire = int(value)
+
+ # -----------------------------------------------------------
+ @property
+ def ttl(self):
+ "The defaul TTL of this zone."
+ return self._ttl
+
+ @ttl.setter
+ def ttl(self, value):
+ if value is None:
+ self._ttl = None
+ return
+ self._ttl = int(value)
+
+ # -------------------------------------------------------------------------
+ def as_dict(self, short=True):
+ """
+ Transforms the elements of the object into a dict
+
+ @param short: don't include local properties in resulting dict.
+ @type short: bool
+
+ @return: structure as dict
+ @rtype: dict
+ """
+
+ res = super(PdnsSoaData, self).as_dict(short=short)
+ res['primary'] = self.primary
+ res['email'] = self.email
+ res['serial'] = self.serial
+ res['refresh'] = self.refresh
+ res['retry'] = self.retry
+ res['expire'] = self.expire
+ res['ttl'] = self.ttl
+
+ return res
+
+ # -------------------------------------------------------------------------
+ @classmethod
+ def init_from_data(cls, data, appname=None, verbose=0, base_dir=None):
+
+ line = cls.re_ws.sub(' ', to_str(data))
+ match = cls.re_soa_data.match(line)
+ if not match:
+ raise PdnsWrongSoaDataError(data)
+
+ soa = cls(
+ primary=match.group(1), email=match.group(2), serial=match.group(3),
+ refresh=match.group(4), retry=match.group(5), expire=match.group(6),
+ ttl=match.group(7), appname=appname, verbose=verbose, base_dir=base_dir)
+ return soa
+
# =============================================================================
class PdnsApiRrset(PpBaseObject):
# -------------------------------------------------------------------------
def __init__(
- self, appname=None, verbose=0, version=__version__, base_dir=None, initialized=None):
+ self, appname=None, verbose=0, version=__version__, base_dir=None, initialized=None):
# { 'comments': [],
# 'name': 'www.bmwi.tv.',