From 5a70c6afa4faaa7d9ca0ad36b9b196ee315060fb Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Sun, 27 Mar 2022 16:32:17 +0200 Subject: [PATCH] Fixing syntax errors and applying flake8 rules --- lib/pp_admintools/mail_app.py | 18 +++++++++------- lib/pp_admintools/mail_config.py | 16 ++++++++------- lib/pp_admintools/pdns_config.py | 35 +++++++++++++++----------------- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/lib/pp_admintools/mail_app.py b/lib/pp_admintools/mail_app.py index daa264d..0f70748 100644 --- a/lib/pp_admintools/mail_app.py +++ b/lib/pp_admintools/mail_app.py @@ -11,6 +11,7 @@ from __future__ import absolute_import import logging import copy import pipes +import os from email.mime.text import MIMEText from email import charset @@ -32,10 +33,12 @@ from fb_tools.xlate import format_list from .xlate import XLATOR -from .mail_config import MailConfigError, MailConfiguration +from .mail_config import MailConfiguration from .mail_config import VALID_MAIL_METHODS, MAX_PORT_NUMBER -__version__ = '0.2.1' +from .mailaddress import MailAddress + +__version__ = '0.2.2' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -122,7 +125,7 @@ class BaseMailApplication(FbConfigApplication): def _perform_cmdline_mail_rcpt(self): v = getattr(self.args, 'mail_recipients', None) - if v not None: + if v is None: return recipients = [] @@ -136,7 +139,7 @@ class BaseMailApplication(FbConfigApplication): if bad_rcpts: msg = _("Got invalid recipient mail addresses:") - msg += " " + format_list(bad_rcpts, , do_repr=True) + msg += " " + format_list(bad_rcpts, do_repr=True) LOG.error(msg) self.exit(1) @@ -164,7 +167,7 @@ class BaseMailApplication(FbConfigApplication): if bad_cc: msg = _("Got invalid cc mail addresses:") - msg += " " + format_list(bad_cc, , do_repr=True) + msg += " " + format_list(bad_cc, do_repr=True) LOG.error(msg) self.exit(1) @@ -196,7 +199,7 @@ class BaseMailApplication(FbConfigApplication): mail_group.add_argument( '--from', '--mail-from', - metavar=_("ADDRESS"), dest="mail_from", + metavar=_("ADDRESS"), dest="mail_from", help=_( "Sender mail address for mails generated by this script. " "Default: {!r}").format(self.cfg.mail_from), @@ -244,7 +247,8 @@ class BaseMailApplication(FbConfigApplication): 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.cfg.smtp_port) + "the mail method of this script is 'smtp'. Default: {}.").format( + self.cfg.smtp_port) ) # ------------------------------------------------------------------------- diff --git a/lib/pp_admintools/mail_config.py b/lib/pp_admintools/mail_config.py index d650762..0363b59 100644 --- a/lib/pp_admintools/mail_config.py +++ b/lib/pp_admintools/mail_config.py @@ -14,14 +14,14 @@ import logging import pwd import re import copy - -from numbers import Number +import os +import socket # Third party modules # Own modules -from fb_tools.common import is_sequence +from fb_tools.common import is_sequence, pp # from .config import ConfigError, BaseConfiguration from fb_tools.multi_config import MultiConfigError, BaseMultiConfig @@ -33,14 +33,14 @@ from .mailaddress import MailAddress from .xlate import XLATOR -__version__ = '0.1.3' +__version__ = '0.1.4' LOG = logging.getLogger(__name__) _ = XLATOR.gettext DEFAULT_CONFIG_DIR = 'pixelpark' VALID_MAIL_METHODS = ('smtp', 'sendmail') -MAX_PORT_NUMBER = (2 ** 16) -1 +MAX_PORT_NUMBER = (2 ** 16) - 1 # ============================================================================= @@ -293,7 +293,7 @@ class MailConfiguration(BaseMultiConfig): re_method = re.compile(r'^\s*(mail[_-]?)?method\s*$', re.IGNORECASE) for key in section.keys(): - if not re_reply.search(key): + if not re_method.search(key): continue val = section[key].strip().lower() @@ -331,11 +331,12 @@ class MailConfiguration(BaseMultiConfig): if not re_server.search(key): continue - val = section[[key] + val = section[key] try: port = int(val) except (ValueError, TypeError) as e: msg = _("Value {!r} for SMTP port is invalid:").format(val) + msg += ' ' + str(e) LOG.error(msg) continue if port <= 0 or port > MAX_PORT_NUMBER: @@ -345,6 +346,7 @@ class MailConfiguration(BaseMultiConfig): self.smtp_port = port + # ============================================================================= if __name__ == "__main__": diff --git a/lib/pp_admintools/pdns_config.py b/lib/pp_admintools/pdns_config.py index 0b6de91..276e2a5 100644 --- a/lib/pp_admintools/pdns_config.py +++ b/lib/pp_admintools/pdns_config.py @@ -12,20 +12,16 @@ from __future__ import absolute_import # Standard module import logging -import pwd import re import copy -from numbers import Number - # Third party modules # Own modules -from fb_tools.common import is_sequence +from fb_tools.common import is_sequence, pp # from .config import ConfigError, BaseConfiguration -from fb_tools.multi_config import MultiConfigError from fb_tools.multi_config import DEFAULT_ENCODING from .mail_config import MailConfigError, MailConfiguration @@ -35,7 +31,7 @@ from .xlate import XLATOR MAX_PDNS_API_TIMEOUT = 3600 -__version__ = '0.1.1' +__version__ = '0.1.2' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -183,12 +179,13 @@ class PdnsConfiguration(MailConfiguration): val = section[key] try: timeout = int(val) + if timeout <= 0 or timeout > MAX_PDNS_API_TIMEOUT: + msg = _("A timeout has to be between 1 and {} seconds.") + msg = msg.format(MAX_PDNS_API_TIMEOUT) + raise ValueError(msg) except (ValueError, TypeError) as e: msg = _("Value {!r} for PowerDNS API timeout is invalid:").format(val) - LOG.error(msg) - continue - if timeout <= 0 or port > MAX_PDNS_API_TIMEOUT: - msg = _("Value {!r} for PowerDNS API timeout is invalid:").format(timeout) + msg += " " + str(e) LOG.error(msg) continue @@ -223,9 +220,9 @@ class PdnsConfiguration(MailConfiguration): # ------------------------------------------------------------------------- def _eval_pdns_inst_host(self, iname, section): - if self.verbose > 2 + if self.verbose > 2: msg = _("Searching for host for PDNS instance {!r} ..") - LOG.debug(msg.format(iname) + LOG.debug(msg.format(iname)) for key in section.keys(): if key.lower() == 'host': @@ -239,9 +236,9 @@ class PdnsConfiguration(MailConfiguration): # ------------------------------------------------------------------------- def _eval_pdns_inst_port(self, iname, section): - if self.verbose > 2 + if self.verbose > 2: msg = _("Searching for post number for PDNS instance {!r} ..") - LOG.debug(msg.format(iname) + LOG.debug(msg.format(iname)) for key in section.keys(): if key.lower() == 'port': @@ -251,7 +248,7 @@ class PdnsConfiguration(MailConfiguration): port = int(val) if port <= 0 or port > MAX_PORT_NUMBER: msg = _("A port must be greater than 0 and less than {}.") - raise ValueError(msg.format(MAX_PORT_NUMBER) + 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 = msg.format(p=val, inst=iname, e=e) @@ -269,9 +266,9 @@ class PdnsConfiguration(MailConfiguration): # ------------------------------------------------------------------------- def _eval_pdns_inst_servername(self, iname, section): - if self.verbose > 2 + if self.verbose > 2: msg = _("Searching for internal server name of PDNS instance {!r} ..") - LOG.debug(msg.format(iname) + LOG.debug(msg.format(iname)) re_servername = re.compile(r'^\s*server[_-]?(name|id)\s*$', re.IGNORECASE) @@ -287,9 +284,9 @@ class PdnsConfiguration(MailConfiguration): # ------------------------------------------------------------------------- def _eval_pdns_inst_key(self, iname, section): - if self.verbose > 2 + if self.verbose > 2: msg = _("Searching for API key of PDNS instance {!r} ..") - LOG.debug(msg.format(iname) + LOG.debug(msg.format(iname)) re_key = re.compile(r'^\s*(api[_-]?)?key\s*$', re.IGNORECASE) -- 2.39.5