From: Frank Brehm Date: Wed, 19 Apr 2017 16:06:03 +0000 (+0200) Subject: Adding command line options for mailing and a check for the given encoding to pp_lib... X-Git-Tag: 0.1.2~204 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=6dbc0632a8e5b8332742dfc6658b836d884383f5;p=pixelpark%2Fadmin-tools.git Adding command line options for mailing and a check for the given encoding to pp_lib/cfg_app.py --- diff --git a/pp_lib/cfg_app.py b/pp_lib/cfg_app.py index 9c9b0c7..be1b7eb 100644 --- a/pp_lib/cfg_app.py +++ b/pp_lib/cfg_app.py @@ -23,6 +23,7 @@ import json import socket import pwd import pipes +import codecs from subprocess import Popen, PIPE @@ -55,7 +56,7 @@ from .mailaddress import MailAddress from .app import PpApplication -__version__ = '0.5.4' +__version__ = '0.6.1' LOG = logging.getLogger(__name__) VALID_MAIL_METHODS = ('smtp', 'sendmail') @@ -101,7 +102,7 @@ class PpConfigApplication(PpApplication): argparse_epilog=None, argparse_prefix_chars='-', env_prefix=None, cfg_dir=None, cfg_stems=None, cfg_encoding='utf-8', need_config_file=False): - self._cfg_encoding = cfg_encoding + self.cfg_encoding = cfg_encoding self._need_config_file = bool(need_config_file) self.cfg = {} @@ -157,9 +158,7 @@ class PpConfigApplication(PpApplication): enc = getattr(self.args, 'cfg_encoding', None) if enc: - enc = enc.lower() - if enc != self.cfg_encoding: - self._cfg_encoding = enc + self.cfg_encoding = enc self.perform_arg_parser() self.init_logging() @@ -185,6 +184,17 @@ class PpConfigApplication(PpApplication): """The encoding character set of the configuration files.""" return self._cfg_encoding + @cfg_encoding.setter + def cfg_encoding(self, value): + try: + codec = codecs.lookup(value) + except Exception as e: + msg = "{c} on setting encoding {v!r}: {e}".format( + c=e.__class__.__name__, v=value, e=e) + LOG.error(msg) + else: + self._cfg_encoding = codec.name + # ----------------------------------------------------------- @property def cfg_dir(self): @@ -218,21 +228,69 @@ class PpConfigApplication(PpApplication): This method should be explicitely called by all init_arg_parser() methods in descendant classes. """ - self.arg_parser.add_argument( + + mail_group = self.arg_parser.add_argument_group('Mailing options') + + mail_group.add_argument( + '--recipients', '--mail-recipients', + metavar="ADDRESS", nargs='+', dest="mail_recipients", + help="Mail addresses of all recipients for mails generated by this script." + ) + + mail_group.add_argument( + '--cc', '--mail-cc', + metavar="ADDRESS", nargs='*', dest="mail_cc", + help="Mail addresses of all CC recipients for mails generated by this script." + ) + + mail_group.add_argument( + '--reply-to', '--mail-reply-to', + metavar="ADDRESS", dest="mail_reply_to", + help="Reply mail address for mails generated by this script." + ) + + mail_group.add_argument( + '--mail-method', + metavar="METHOD", choices=VALID_MAIL_METHODS, dest="mail_method", + help=("Method for sending the mails generated by this script. " + "Valid values: {v}, default: {d!r}.".format( + v=', '.join(map(lambda x: repr(x), VALID_MAIL_METHODS)), + d=self.mail_method)) + ) + + mail_group.add_argument( + '--mail-server', + metavar="SERVER", dest="mail_server", + help=("Mail server for submitting generated by this script if " + "the mail method of this script is 'smtp'. Default: {!r}.".format( + self.mail_server)) + ) + + mail_group.add_argument( + '--smtp-port', + metavar="PORT", type=int, dest='smtp_port', + help=("The port to use for submitting generated by this script if " + "the mail method of this script is 'smtp'. Default: {}.".format(self.smtp_port)) + ) + + cfgfile_group = self.arg_parser.add_argument_group('Config file options') + + cfgfile_group.add_argument( "-C", "--cfgfile", "--cfg-file", "--config", metavar="FILE", nargs='+', dest="cfg_file", help="Configuration files to use additional to the standard configuration files.", ) - self.arg_parser.add_argument( + cfgfile_group.add_argument( "--log-cfgfile", metavar="FILE", dest="log_cfgfile", - help=("Configuration file for logging in JSON format. " - "See https://docs.python.org/3/library/logging.config.html#logging-config-dictschema " - "how the structures has to be defined.") + help=( + "Configuration file for logging in JSON format. " + "See https://docs.python.org/3/library/logging.config.html" + "#logging-config-dictschema how the structures has to be defined.") ) - self.arg_parser.add_argument( + cfgfile_group.add_argument( "--cfg-encoding", metavar="ENCODING", dest="cfg_encoding", default=self.cfg_encoding, help=("The encoding character set of the configuration files " @@ -557,16 +615,78 @@ class PpConfigApplication(PpApplication): except (ValueError, TypeError) as e: msg = "Found invalid SMTP port number {!r} in configuration.".format(v) LOG.error(msg) - if port <= 0: - msg = "Found invalid SMTP port number {!r} in configuration.".format(port) - LOG.error(msg) - self.smtp_port = port + else: + if port <= 0: + msg = "Found invalid SMTP port number {!r} in configuration.".format(port) + LOG.error(msg) + else: + self.smtp_port = port self.perform_config() + self._perform_mail_cmdline_options() + # ------------------------------------------------------------------------- - def perform_config(self): + def _perform_mail_cmdline_options(self): + + v = getattr(self.args, 'mail_recipients', None) + if v is not None: + self.mail_recipients = [] + for addr in v: + tokens = self.whitespace_re.split(addr) + for token in tokens: + if MailAddress.valid_address(token): + if token not in self.mail_recipients: + self.mail_recipients.append(token) + else: + msg = "Got invalid recipient mail address {!r}.".format(token) + LOG.error(msg) + if not self.mail_recipients: + msg = "Did not found any valid recipient mail addresses." + LOG.error(msg) + + v = getattr(self.args, 'mail_cc', None) + if v is not None: + self.mail_cc = [] + for addr in v: + tokens = self.whitespace_re.split(addr) + for token in tokens: + if MailAddress.valid_address(token): + if token not in self.mail_cc: + self.mail_cc.append(token) + else: + msg = "Got invalid CC mail address {!r}.".format(token) + LOG.error(msg) + + v = getattr(self.args, 'mail_reply_to', None) + if v: + tokens = self.whitespace_re.split(v) + if len(tokens): + if MailAddress.valid_address(tokens[0]): + self.reply_to = tokens[0] + else: + msg = "Got invalid reply mail address {!r}.".format( + tokens[0]) + LOG.error(msg) + + v = getattr(self.args, 'mail_method', None) + if v: + self.mail_method = v + + v = getattr(self.args, 'mail_server', None) + if v: + self.mail_server = v + v = getattr(self.args, 'smtp_port', None) + if v is not None: + if v <= 0: + msg = "Got invalid SMTP port number {!r}.".format(v) + LOG.error(msg) + else: + self.smtp_port = v + + # ------------------------------------------------------------------------- + def perform_config(self): """ Execute some actions after reading the configuration.