From 5b353910982ab310434f01296b26df8c85d31a00 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 6 Sep 2022 13:51:07 +0200 Subject: [PATCH] Refactoring modules pp_admintools.config and pp_admintools.config.mail. --- lib/pp_admintools/config/__init__.py | 64 ++++++++++++++++++---- lib/pp_admintools/config/mail.py | 80 ++++++++-------------------- 2 files changed, 75 insertions(+), 69 deletions(-) diff --git a/lib/pp_admintools/config/__init__.py b/lib/pp_admintools/config/__init__.py index fc925ff..d272baf 100644 --- a/lib/pp_admintools/config/__init__.py +++ b/lib/pp_admintools/config/__init__.py @@ -14,6 +14,7 @@ import pwd import socket import os import copy +import re # Third party modules @@ -29,9 +30,10 @@ from ..errors import PpError from ..xlate import XLATOR CONFIG_DIR = 'pixelpark' -__version__ = '0.1.4' +__version__ = '0.2.1' LOG = logging.getLogger(__name__) VALID_MAIL_METHODS = ('smtp', 'sendmail') +DEFAULT_DOMAIN = 'pixelpark.com' _ = XLATOR.gettext @@ -59,42 +61,84 @@ class PpBaseConfiguration(BaseMultiConfig): default_reply_to = 'solution@pixelpark.com' default_mail_server = 'mx.pixelpark.com' + default_smtp_port = 25 current_user_name = pwd.getpwuid(os.getuid()).pw_name current_user_gecos = pwd.getpwuid(os.getuid()).pw_gecos + current_user_gecos = current_user_gecos.strip() + current_user_gecos = re.sub(r',+$', '', current_user_gecos) + if current_user_gecos == '': + current_user_gecos = current_user_name + default_domain = socket.getfqdn() - if not MailAddress.re_valid_domain.search(default_domain): - default_domain = 'pixelpark.com' + if default_domain is None: + default_domain = DEFAULT_DOMAIN + else: + default_domain = default_domain.strip() + if not MailAddress.re_valid_domain.match(default_domain): + default_domain = DEFAULT_DOMAIN default_mail_from = MailAddress(user=current_user_name, domain=default_domain) + default_mail_from_complete = '{n} <{m}>'.format(n=current_user_gecos, m=default_mail_from) + + valid_mail_methods = VALID_MAIL_METHODS + default_mail_method = 'smtp' # ------------------------------------------------------------------------- def __init__( self, appname=None, verbose=0, version=__version__, base_dir=None, append_appname_to_stems=True, config_dir=CONFIG_DIR, additional_stems=None, additional_cfgdirs=None, encoding=DEFAULT_ENCODING, additional_config_file=None, - use_chardet=True, raise_on_error=True, initialized=False): + ensure_privacy=False, use_chardet=True, raise_on_error=True, initialized=False): self.mail_recipients = copy.copy(self.default_mail_recipients) - self.mail_from = '{n} <{m}>'.format( - n=self.current_user_gecos, m=self.default_mail_from) + self.mail_from = self.default_mail_from_complete self.mail_cc = copy.copy(self.default_mail_cc) self.reply_to = self.default_reply_to - self.mail_method = 'smtp' + self.mail_method = self.default_mail_method self.mail_server = self.default_mail_server - self.smtp_port = 25 - self._config_has_errors = None + self.smtp_port = self.default_smtp_port super(PpBaseConfiguration, self).__init__( appname=appname, verbose=verbose, version=version, base_dir=base_dir, append_appname_to_stems=append_appname_to_stems, config_dir=config_dir, additional_stems=additional_stems, additional_cfgdirs=additional_cfgdirs, encoding=encoding, additional_config_file=additional_config_file, - use_chardet=use_chardet, raise_on_error=raise_on_error, initialized=False) + ensure_privacy=ensure_privacy, use_chardet=use_chardet, + raise_on_error=raise_on_error, initialized=False) if initialized: self.initialized = True + # ------------------------------------------------------------------------- + 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(PpBaseConfiguration, self).as_dict(short=short) + + res['default_mail_recipients'] = self.default_mail_recipients + res['default_mail_cc'] = self.default_mail_cc + res['default_reply_to'] = self.default_reply_to + res['default_mail_server'] = self.default_mail_server + res['default_smtp_port'] = self.default_smtp_port + res['current_user_name'] = self.current_user_name + res['current_user_gecos'] = self.current_user_gecos + res['default_mail_from'] = self.default_mail_from + res['default_domain'] = self.default_domain + res['default_mail_from_complete'] = self.default_mail_from_complete + res['default_mail_method'] = self.default_mail_method + res['valid_mail_methods'] = self.valid_mail_methods + + return res + # ============================================================================= diff --git a/lib/pp_admintools/config/mail.py b/lib/pp_admintools/config/mail.py index 5f8527c..3f82d3d 100644 --- a/lib/pp_admintools/config/mail.py +++ b/lib/pp_admintools/config/mail.py @@ -11,20 +11,15 @@ from __future__ import absolute_import # Standard module import logging -import pwd import re import copy -import os -import socket # Third party modules # Own modules -from fb_tools.common import is_sequence, pp +from fb_tools.common import is_sequence, pp, to_bool -# from .config import ConfigError, BaseConfiguration -from fb_tools.multi_config import MultiConfigError, BaseMultiConfig from fb_tools.multi_config import DEFAULT_ENCODING from fb_tools import MailAddress @@ -32,19 +27,20 @@ from fb_tools import MailAddress from .. import __version__ as GLOBAL_VERSION from .. import MAX_PORT_NUMBER, DEFAULT_CONFIG_DIR +from . import PpConfigurationError, PpBaseConfiguration + from ..xlate import XLATOR -__version__ = '0.1.11' +__version__ = '0.2.1' LOG = logging.getLogger(__name__) _ = XLATOR.gettext -VALID_MAIL_METHODS = ('smtp', 'sendmail') DEFAULT_DOMAIN = 'pixelpark.com' # ============================================================================= -class MailConfigError(MultiConfigError): +class MailConfigError(PpConfigurationError): """Base error class for all exceptions happened during execution this configured application""" @@ -52,40 +48,12 @@ class MailConfigError(MultiConfigError): # ============================================================================= -class MailConfiguration(BaseMultiConfig): +class MailConfiguration(PpBaseConfiguration): """ A class for providing a configuration for an arbitrary PowerDNS Application and methods to read it from configuration files. """ - default_mail_recipients = [ - 'frank.brehm@pixelpark.com' - ] - default_mail_cc = [ - 'thomas.dalichow@pixelpark.com', - ] - - default_reply_to = 'solution@pixelpark.com' - - default_mail_server = 'localhost' - default_smtp_port = 25 - - default_domain = socket.getfqdn() - if default_domain is None: - default_domain = DEFAULT_DOMAIN - else: - default_domain = default_domain.strip() - if not MailAddress.re_valid_domain.match(default_domain): - default_domain = DEFAULT_DOMAIN - - current_user_name = pwd.getpwuid(os.getuid()).pw_name - current_user_gecos = pwd.getpwuid(os.getuid()).pw_gecos - default_mail_from = MailAddress(user=current_user_name, domain=default_domain) - default_mail_from_complete = '{n} <{m}>'.format(n=current_user_gecos, m=default_mail_from) - - valid_mail_methods = VALID_MAIL_METHODS - default_mail_method = 'smtp' - whitespace_re = re.compile(r'(?:[,;]+|\s*[,;]*\s+)+') # ------------------------------------------------------------------------- @@ -93,7 +61,7 @@ class MailConfiguration(BaseMultiConfig): self, appname=None, verbose=0, version=__version__, base_dir=None, 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=False, use_chardet=True, initialized=False): + ensure_privacy=False, raise_on_error=True, use_chardet=True, initialized=False): add_stems = [] if additional_stems: @@ -106,13 +74,6 @@ class MailConfiguration(BaseMultiConfig): if 'mail' not in add_stems: add_stems.append('mail') - self.mail_recipients = copy.copy(self.default_mail_recipients) - self.mail_from = self.default_mail_from_complete - self.mail_cc = copy.copy(self.default_mail_cc) - self.reply_to = self.default_reply_to - self.mail_method = self.default_mail_method - self.mail_server = self.default_mail_server - self.smtp_port = self.default_smtp_port self._mail_cc_configured = False super(MailConfiguration, self).__init__( @@ -120,7 +81,7 @@ class MailConfiguration(BaseMultiConfig): append_appname_to_stems=append_appname_to_stems, config_dir=config_dir, additional_stems=add_stems, additional_config_file=additional_config_file, additional_cfgdirs=additional_cfgdirs, encoding=encoding, use_chardet=use_chardet, - ensure_privacy=ensure_privacy, initialized=False, + raise_on_error=raise_on_error, ensure_privacy=ensure_privacy, initialized=False, ) self.xmailer = "{a} (Admin Tools version {v})".format( @@ -129,6 +90,16 @@ class MailConfiguration(BaseMultiConfig): if initialized: self.initialized = True + # ----------------------------------------------------------- + @property + def mail_cc_configured(self): + """Should there be used LDAPS for communicating with the LDAP server?""" + return self._mail_cc_configured + + @mail_cc_configured.setter + def mail_cc_configured(self, value): + self._mail_cc_configured = to_bool(value) + # ------------------------------------------------------------------------- def as_dict(self, short=True): """ @@ -143,16 +114,7 @@ class MailConfiguration(BaseMultiConfig): res = super(MailConfiguration, self).as_dict(short=short) - res['default_mail_recipients'] = self.default_mail_recipients - res['default_mail_cc'] = self.default_mail_cc - res['default_reply_to'] = self.default_reply_to - res['default_mail_server'] = self.default_mail_server - res['default_smtp_port'] = self.default_smtp_port - res['current_user_name'] = self.current_user_name - res['current_user_gecos'] = self.current_user_gecos - res['default_mail_from'] = self.default_mail_from - res['default_mail_from_complete'] = self.default_mail_from_complete - res['default_mail_method'] = self.default_mail_method + res['mail_cc_configured'] = self.mail_cc_configured return res @@ -167,7 +129,7 @@ class MailConfiguration(BaseMultiConfig): if not self.mail_recipients: self.mail_recipients = copy.copy(self.default_mail_recipients) - if not self.mail_cc and not self._mail_cc_configured: + if not self.mail_cc and not self.mail_cc_configured: self.mail_cc = copy.copy(self.default_mail_cc) # ------------------------------------------------------------------------- @@ -261,7 +223,7 @@ class MailConfiguration(BaseMultiConfig): for key in section.keys(): - self._mail_cc_configured = True + self.mail_cc_configured = True if not re_cc.search(key): continue -- 2.39.5