#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
+@summary: A module for providing a configuration for applications, which are Working with PowerDNS.
+
@author: Frank Brehm
@contact: frank.brehm@pixelpark.com
@copyright: © 2023 by Frank Brehm, Berlin
-@summary: A module for providing a configuration for applications,
- which are Working with PowerDNS.
- It's based on class MailConfigError.
"""
from __future__ import absolute_import
# Standard module
+import copy
import logging
import re
-import copy
# Third party modules
-
-# Own modules
-
from fb_tools import MAX_TIMEOUT
from fb_tools.common import is_sequence
# from .config import ConfigError, BaseConfiguration
from fb_tools.multi_config import DEFAULT_ENCODING
-from .. import __version__ as GLOBAL_VERSION
+# Own modules
+from .mail import DEFAULT_CONFIG_DIR
+from .mail import MailConfigError, MailConfiguration
from .. import MAX_PORT_NUMBER
+from .. import __version__ as GLOBAL_VERSION
from .. import pp
-
-from .mail import MailConfigError, MailConfiguration
-from .mail import DEFAULT_CONFIG_DIR
-
from ..xlate import XLATOR
-LIBRARY_NAME = "pp-pdns-api-client"
+LIBRARY_NAME = 'pp-pdns-api-client'
-__version__ = '0.2.5'
+__version__ = '0.2.6'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
# =============================================================================
class PdnsConfigError(MailConfigError):
- """Base error class for all exceptions happened during
- execution this configured application"""
+ """Base error class for all exceptions happened during execution a configured application."""
pass
# =============================================================================
class PdnsConfiguration(MailConfiguration):
"""
- A class for providing a configuration for an arbitrary PowerDNS Application
- and methods to read it from configuration files.
+ A class for providing a configuration for an arbitrary PowerDNS application.
+
+ It provides also methods to read from configuration files.
"""
valid_pdns_api_instances = ('global', 'public', 'local')
default_pdns_api_instances = {
'global': {
- 'host': "dnsmaster.pp-dns.com",
+ 'host': 'dnsmaster.pp-dns.com',
},
'public': {
- 'host': "dnsmaster-public.pixelpark.com",
+ 'host': 'dnsmaster-public.pixelpark.com',
},
'local': {
- 'host': "dnsmaster-local.pixelpark.com",
+ 'host': 'dnsmaster-local.pixelpark.com',
},
}
default_pdns_api_port = 8081
- default_pdns_api_servername = "localhost"
+ default_pdns_api_servername = 'localhost'
default_pdns_timeout = 20
default_pdns_instance = 'global'
append_appname_to_stems=True, additional_stems=None, config_dir=DEFAULT_CONFIG_DIR,
additional_config_file=None, additional_cfgdirs=None, encoding=DEFAULT_ENCODING,
ensure_privacy=True, use_chardet=True, initialized=False):
-
+ """Initialize the PdnsConfiguration object."""
self.api_user_agent = '{}/{}'.format(LIBRARY_NAME, GLOBAL_VERSION)
self.pdns_api_instances = {}
# -------------------------------------------------------------------------
def as_dict(self, short=True):
"""
- Transforms the elements of the object into a dict
+ Transform 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(PdnsConfiguration, self).as_dict(short=short)
res['default_pdns_api_instances'] = self.default_pdns_api_instances
# -------------------------------------------------------------------------
def eval_section(self, section_name):
-
+ """Evaluate a particular configuration section."""
super(PdnsConfiguration, self).eval_section(section_name)
sn = section_name.lower()
def _eval_pdns(self, section_name, section):
if self.verbose > 2:
- msg = _("Evaluating config section {!r}:").format(section_name)
+ msg = _('Evaluating config section {!r}:').format(section_name)
LOG.debug(msg + '\n' + pp(section))
re_agent = re.compile(r'^\s*(?:api[_-]?)?user[_-]?agent\s*$', re.IGNORECASE)
try:
timeout = int(val)
if timeout <= 0 or timeout > MAX_TIMEOUT:
- msg = _("A timeout has to be between 1 and {} seconds.")
+ msg = _('A timeout has to be between 1 and {} seconds.')
msg = msg.format(MAX_TIMEOUT)
raise ValueError(msg)
except (ValueError, TypeError) as e:
- msg = _("Value {!r} for PowerDNS API timeout is invalid:").format(val)
- msg += " " + str(e)
+ msg = _('Value {!r} for PowerDNS API timeout is invalid:').format(val)
+ msg += ' ' + str(e)
if self.raise_on_error:
raise PdnsConfigError(msg)
LOG.error(msg)
return
if env not in self.pdns_api_instances:
- msg = _("Found invalid PDNS environment/instance {!r} in configuration.")
+ msg = _('Found invalid PDNS environment/instance {!r} in configuration.')
msg = msg.format(section[key])
if self.raise_on_error:
raise PdnsConfigError(msg)
val = section[key].strip().lower()
if val:
if self.verbose > 2:
- msg = _("Found PDNS host: {!r}.").format(val)
+ msg = _('Found PDNS host: {!r}.').format(val)
LOG.debug(msg)
self.pdns_host = val
try:
port = int(val)
if port <= 0 or port > MAX_PORT_NUMBER:
- msg = _("A port must be greater than 0 and less than {}.")
+ msg = _('A port must be greater than 0 and less than {}.')
raise ValueError(msg.format(MAX_PORT_NUMBER))
except (TypeError, ValueError) as e:
- msg = _("Wrong PDNS port number {p!r} found: {e}").format(p=val, e=e)
+ msg = _('Wrong PDNS port number {p!r} found: {e}').format(p=val, e=e)
if self.raise_on_error:
raise PdnsConfigError(msg)
else:
if port:
if self.verbose > 2:
- msg = _("Found port number for PDNS: {}.").format(port)
+ msg = _('Found port number for PDNS: {}.').format(port)
LOG.debug(msg)
self.pdns_port = port
key_show = '******'
if self.verbose > 4:
key_show = val
- msg = _("Found API key for PDNS: {!r}.").format(key_show)
+ msg = _('Found API key for PDNS: {!r}.').format(key_show)
LOG.debug(msg)
self.pdns_key = val
val = section[key].strip()
if val:
if self.verbose > 2:
- msg = _("Found PDNS API servername: {!r}.").format(val)
+ msg = _('Found PDNS API servername: {!r}.').format(val)
LOG.debug(msg)
self.pdns_servername = val
iname = instance_name.lower()
if self.verbose > 2:
- msg = _("Evaluating PowerDNS instance {!r}:").format(iname)
+ msg = _('Evaluating PowerDNS instance {!r}:').format(iname)
LOG.debug(msg + '\n' + pp(section))
self._eval_pdns_inst_host(iname, section)
def _eval_pdns_inst_host(self, iname, section):
if self.verbose > 2:
- msg = _("Searching for host for PDNS instance {!r} ..")
+ msg = _('Searching for host for PDNS instance {!r} ..')
LOG.debug(msg.format(iname))
for key in section.keys():
host = section[key].lower().strip()
if host:
if self.verbose > 2:
- msg = _("Found host for PDNS instance {inst!r}: {host!r}.")
+ msg = _('Found host for PDNS instance {inst!r}: {host!r}.')
LOG.debug(msg.format(inst=iname, host=host))
self.pdns_api_instances[iname]['host'] = host
def _eval_pdns_inst_port(self, iname, section):
if self.verbose > 2:
- msg = _("Searching for post number for PDNS instance {!r} ..")
+ msg = _('Searching for post number for PDNS instance {!r} ..')
LOG.debug(msg.format(iname))
for key in section.keys():
try:
port = int(val)
if port <= 0 or port > MAX_PORT_NUMBER:
- msg = _("A port must be greater than 0 and less than {}.")
+ msg = _('A port must be greater than 0 and less than {}.')
raise ValueError(msg.format(MAX_PORT_NUMBER))
except (TypeError, ValueError) as e:
- msg = _("Wrong port number {p!r} for PDNS instance {inst!r} found: {e}")
+ msg = _('Wrong port number {p!r} for PDNS instance {inst!r} found: {e}')
msg = msg.format(p=val, inst=iname, e=e)
if self.raise_on_error:
raise PdnsConfigError(msg)
port = None
if port:
if self.verbose > 2:
- msg = _("Found port number for PDNS instance {inst!r}: {p}.")
+ msg = _('Found port number for PDNS instance {inst!r}: {p}.')
LOG.debug(msg.format(inst=iname, p=port))
self.pdns_api_instances[iname]['port'] = port
def _eval_pdns_inst_servername(self, iname, section):
if self.verbose > 2:
- msg = _("Searching for internal server name of PDNS instance {!r} ..")
+ msg = _('Searching for internal server name of PDNS instance {!r} ..')
LOG.debug(msg.format(iname))
re_servername = re.compile(r'^\s*server[_-]?(name|id)\s*$', re.IGNORECASE)
servername = section[key].lower().strip()
if servername:
if self.verbose > 2:
- msg = _("Found internal server name PDNS instance {inst!r}: {sn!r}.")
+ msg = _('Found internal server name PDNS instance {inst!r}: {sn!r}.')
LOG.debug(msg.format(inst=iname, sn=servername))
self.pdns_api_instances[iname]['servername'] = servername
def _eval_pdns_inst_key(self, iname, section):
if self.verbose > 2:
- msg = _("Searching for API key of PDNS instance {!r} ..")
+ msg = _('Searching for API key of PDNS instance {!r} ..')
LOG.debug(msg.format(iname))
re_key = re.compile(r'^\s*(api[_-]?)?key\s*$', re.IGNORECASE)
key_show = '******'
if self.verbose > 4:
key_show = api_key
- msg = _("Found API key of PDNS instance {inst!r}: {key!r}.")
+ msg = _('Found API key of PDNS instance {inst!r}: {key!r}.')
LOG.debug(msg.format(inst=iname, key=key_show))
self.pdns_api_instances[iname]['key'] = api_key
# -------------------------------------------------------------------------
- def eval(self):
-
+ def eval(self): # noqa: A003
+ """Evaluate the complete given configuration hash."""
super(PdnsConfiguration, self).eval()
inst = self.pdns_instance
# =============================================================================
-if __name__ == "__main__":
+if __name__ == '__main__':
pass