]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Moving mail stuff to pp_lib/cfg_app.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 22 Mar 2017 15:22:00 +0000 (16:22 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 22 Mar 2017 15:22:00 +0000 (16:22 +0100)
pp_lib/cfg_app.py
pp_lib/ldap_app.py
pp_lib/test_home_app.py

index 3ebaee9492afacec06f8a9a5325042c25605563f..e2ea0d2837ca28b83c2a4e685b8ed133f529debf 100644 (file)
@@ -18,6 +18,7 @@ import re
 import traceback
 import textwrap
 import datetime
+import copy
 import json
 
 # Third party modules
@@ -39,11 +40,15 @@ from .common import pp, terminal_can_colors, to_bytes, to_bool
 
 from .merge import merge_structure
 
+from .mailaddress import MailAddress
+
 from .app import PpApplication
 
-__version__ = '0.4.4'
+__version__ = '0.5.1'
 LOG = logging.getLogger(__name__)
 
+VALID_MAIL_METHODS = ('smtp', 'sendmail')
+
 
 # =============================================================================
 class PpCfgAppError(PpAppError):
@@ -59,6 +64,19 @@ class PpConfigApplication(PpApplication):
     Class for configured application objects.
     """
 
+    default_mail_recipients = [
+        'frank.brehm@pixelpark.com'
+    ]
+    default_mail_cc = [
+        'thomas.kotschok@pixelpark.com',
+    ]
+
+    default_reply_to = 'frank.brehm@pixelpark.com'
+
+    default_mail_server = 'mx.pixelpark.net'
+
+    whitespace_re = re.compile(r'(?:[,;]+|\s*[,;]*\s+)')
+
     # -------------------------------------------------------------------------
     def __init__(
         self, appname=None, verbose=0, version=__version__, base_dir=None,
@@ -76,6 +94,12 @@ class PpConfigApplication(PpApplication):
         self.cfg_files = []
         self.log_cfg_files = []
 
+        self.mail_recipients = copy.copy(self.default_mail_recipients)
+        self.mail_cc = copy.copy(self.default_mail_cc)
+        self.reply_to = self.default_reply_to
+        self.mail_server = self.default_mail_server
+        self.mail_method = 'smtp'
+
         super(PpConfigApplication, self).__init__(
             appname=appname, verbose=verbose, version=version, base_dir=base_dir,
             initialized=False, usage=usage, description=description,
@@ -436,6 +460,68 @@ class PpConfigApplication(PpApplication):
                 root_logger = logging.getLogger()
                 root_logger.setLevel(logging.DEBUG)
 
+        for section_name in self.cfg.keys():
+
+            if self.verbose > 3:
+                LOG.debug("Checking config section {!r} ...".format(section_name))
+
+            if section_name.lower() != "mail":
+                continue
+            section = self.cfg[section_name]
+
+            if 'mail_recipients' in section:
+                v = section['mail_recipients'].strip()
+                self.mail_recipients = []
+                tokens = self.whitespace_re.split(v)
+                for token in tokens:
+                    if MailAddress.valid_address(token):
+                        if token not in self.mail_recipients:
+                            self.mail_recipients.append(token)
+                    else:
+                        msg = "Found invalid recipient mail address {!r} in configuration.".format(
+                            token)
+                        LOG.error(msg)
+
+            if 'mail_cc' in section:
+                v = section['mail_cc'].strip()
+                self.mail_cc = []
+                tokens = self.whitespace_re.split(v)
+                for token in tokens:
+                    if MailAddress.valid_address(token):
+                        if token not in self.mail_cc:
+                            self.mail_cc.append(token)
+                    else:
+                        msg = "Found invalid cc mail address {!r} in configuration.".format(
+                            token)
+                        LOG.error(msg)
+
+            if 'reply_to' in section:
+                v = section['reply_to'].strip()
+                self.reply_to = None
+                tokens = self.whitespace_re.split(v)
+                if len(tokens):
+                    if MailAddress.valid_address(tokens[0]):
+                        self.reply_to = tokens[0]
+                    else:
+                        msg = "Found invalid reply mail address {!r} in configuration.".format(
+                            tokens[0])
+                        LOG.error(msg)
+
+            if 'mail_server' in section:
+                v = section['reply_to'].strip()
+                if v:
+                    self.mail_server = v
+
+            if 'mail_method' in section:
+                v = section['mail_method'].strip().lower()
+                if v:
+                    if v in VALID_MAIL_METHODS:
+                        self.mail_method = v
+                    else:
+                        msg = "Found invalid mail method {!r} in configuration.".format(
+                            section['mail_method'])
+                        LOG.error(msg)
+
         self.perform_config()
 
     # -------------------------------------------------------------------------
index cbdd70e34f38f9ef091118d3df2d080480761130..8ceb202176e7543c8f7dacc29a3c458ec50d55e9 100644 (file)
@@ -42,7 +42,7 @@ from .merge import merge_structure
 
 from .cfg_app import PpCfgAppError, PpConfigApplication
 
-__version__ = '0.4.4'
+__version__ = '0.4.5'
 LOG = logging.getLogger(__name__)
 
 
@@ -227,6 +227,8 @@ class PpLdapApplication(PpConfigApplication):
         if self.verbose > 1:
             LOG.debug("executing pre_run() ...")
 
+        super(PpLdapApplication, self).pre_run()
+
         LOG.debug("Binding to the LDAP servers ...")
         try:
             self.ldap_connection.bind()
index b22c2e004cf058adedb922bd7f4bf83d850cd827..7806b8cfa446efbcf078b41a4b41a53af732eff7 100644 (file)
@@ -32,7 +32,7 @@ from .common import pp, terminal_can_colors, to_bytes, to_bool
 
 from .cfg_app import PpCfgAppError, PpConfigApplication
 
-__version__ = '0.4.1'
+__version__ = '0.4.2'
 LOG = logging.getLogger(__name__)
 
 
@@ -55,17 +55,7 @@ class PpTestHomeApp(PpConfigApplication):
     # /etc/pixelpark/exclude_homes
     default_exclude_file = os.sep + os.path.join('etc', 'pixelpark', 'exclude_homes')
 
-    default_mail_recipients = [
-        'frank.brehm@pixelpark.com'
-    ]
-    default_mail_cc = [
-        'thomas.kotschok\@pixelpark.com',
-    ]
-
-    default_reply_to = 'frank.brehm@pixelpark.com'
-
     comment_re = re.compile(r'\s*#.*')
-    whitespace_re = re.compile(r'(?:[,;]+|\s*[,;]*\s+)')
 
     # -------------------------------------------------------------------------
     def __init__(self, appname=None, version=__version__):
@@ -76,10 +66,6 @@ class PpTestHomeApp(PpConfigApplication):
 
         self.exclude_file = self.default_exclude_file
 
-        self.mail_recipients = copy.copy(self.default_mail_recipients)
-        self.mail_cc = copy.copy(self.default_mail_cc)
-        self.reply_to = self.default_reply_to
-
         self.exclude_dirs = []
         self.passwd_home_dirs = []
         self.unnecessary_dirs = []
@@ -104,7 +90,7 @@ class PpTestHomeApp(PpConfigApplication):
 
         for section_name in self.cfg.keys():
 
-            if self.verbose > 2:
+            if self.verbose > 3:
                 LOG.debug("Checking config section {!r} ...".format(section_name))
 
             if section_name.lower() not in ('test-home', 'test_home', 'testhome') :