From 1398013f20d4e5c83a3665617dc2ce8ea4a5dcd0 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 13 Jun 2023 14:23:01 +0200 Subject: [PATCH] Make the linter happy with lib/pp_admintools/argparse_actions.py --- lib/pp_admintools/argparse_actions.py | 57 ++++++++++++++++----------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/lib/pp_admintools/argparse_actions.py b/lib/pp_admintools/argparse_actions.py index 1ebd3cc..05a8709 100644 --- a/lib/pp_admintools/argparse_actions.py +++ b/lib/pp_admintools/argparse_actions.py @@ -1,22 +1,22 @@ # -*- coding: utf-8 -*- """ +@summary: A module containing some useful argparse actions. + @author: Frank Brehm @contact: frank.brehm@pixelpark.com @copyright: © 2023 by Frank Brehm, Berlin -@summary: A module containing some useful argparse actions. """ from __future__ import absolute_import # Standard modules -import logging import argparse +import logging # Own modules from . import MAX_PORT_NUMBER - from .xlate import XLATOR -__version__ = '0.3.1' +__version__ = '0.3.2' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -24,17 +24,23 @@ _ = XLATOR.gettext # ============================================================================= class PortOptionAction(argparse.Action): + """ + It's an argparse action class to ensure a valid IP port number. + + It ensures, that the given port number is an integer value in the range + between 1 and MAX_PORT_NUMBER (2**16 - 1 == 65535). + """ # ------------------------------------------------------------------------- def __init__(self, option_strings, what, *args, **kwargs): - + """Initialize the PortOptionAction object.""" self.what = what super(PortOptionAction, self).__init__(option_strings=option_strings, *args, **kwargs) # ------------------------------------------------------------------------- def __call__(self, parser, namespace, values, option_string=None): - + """Check the given value from command line for type and the valid range.""" if values is None: setattr(namespace, self.dest, None) return @@ -42,12 +48,12 @@ class PortOptionAction(argparse.Action): try: port = int(values) except (ValueError, TypeError) as e: - msg = _("Value {v!r} for a {what} port is invalid:").format(v=values, what=self.what) + msg = _('Value {v!r} for a {what} port is invalid:').format(v=values, what=self.what) msg += ' ' + str(e) raise argparse.ArgumentError(self, msg) if port <= 0 or port > MAX_PORT_NUMBER: - msg = _("Value {v!r} for a {what} port must be greater than 0 and less than {max}.") + msg = _('Value {v!r} for a {what} port must be greater than 0 and less than {max}.') msg = msg.format(v=values, what=self.what, max=(MAX_PORT_NUMBER + 1)) raise argparse.ArgumentError(self, msg) @@ -55,19 +61,24 @@ class PortOptionAction(argparse.Action): # ============================================================================= class NonNegativeItegerOptionAction(argparse.Action): + """ + It's an argparse action class to ensure a positive integer value. + + It ensures, that the given value is an integer value, which ist greater or equal to 0. + """ # ------------------------------------------------------------------------- def __call__(self, parser, namespace, value, option_string=None): - + """Check the given value from command line for type and the valid range.""" try: val = int(value) except Exception as e: - msg = _("Got a {c} for converting {v!r} into an integer value: {e}").format( + msg = _('Got a {c} for converting {v!r} into an integer value: {e}').format( c=e.__class__.__name__, v=value, e=e) raise argparse.ArgumentError(self, msg) if val < 0: - msg = _("The option must not be negative (given: {}).").format(value) + msg = _('The option must not be negative (given: {}).').format(value) raise argparse.ArgumentError(self, msg) setattr(namespace, self.dest, val) @@ -75,10 +86,11 @@ class NonNegativeItegerOptionAction(argparse.Action): # ============================================================================= class LimitedIntegerOptionAction(argparse.Action): + """It's an argparse action class to ensure an integer value in a defined range.""" # ------------------------------------------------------------------------- def __init__(self, option_strings, min_val=None, max_val=None, *args, **kwargs): - + """Initialize the LimitedIntegerOptionAction object.""" self._min_val = min_val self._max_val = max_val @@ -87,24 +99,24 @@ class LimitedIntegerOptionAction(argparse.Action): # ------------------------------------------------------------------------- def __call__(self, parser, namespace, value, option_string=None): - + """Check the given value from command line for type and the valid range.""" val = 0 try: val = int(value) except Exception as e: - msg = _("Got a {c} for converting {v!r} into an integer value: {e}").format( + msg = _('Got a {c} for converting {v!r} into an integer value: {e}').format( c=e.__class__.__name__, v=value, e=e) raise argparse.ArgumentError(self, msg) if self._min_val is not None: if val < self._min_val: - msg = _("The option must be greater or equal to {m} (given: {v}).").format( + msg = _('The option must be greater or equal to {m} (given: {v}).').format( m=self._min_val, v=val) raise argparse.ArgumentError(self, msg) if self._max_val is not None: if val > self._max_val: - msg = _("The option must be less or equal to {m} (given: {v}).").format( + msg = _('The option must be less or equal to {m} (given: {v}).').format( m=self._max_val, v=val) raise argparse.ArgumentError(self, msg) @@ -113,10 +125,11 @@ class LimitedIntegerOptionAction(argparse.Action): # ============================================================================= class LimitedFloatOptionAction(argparse.Action): + """It's an argparse action class to ensure an float value in a defined range.""" # ------------------------------------------------------------------------- def __init__(self, option_strings, min_val=None, max_val=None, *args, **kwargs): - + """Initialize the LimitedFloatOptionAction object.""" self._min_val = min_val self._max_val = max_val @@ -125,22 +138,22 @@ class LimitedFloatOptionAction(argparse.Action): # ------------------------------------------------------------------------- def __call__(self, parser, namespace, value, option_string=None): - + """Check the given value from command line for type and the valid range.""" val = 0 try: val = float(value) except Exception as e: - msg = _("Got a {c} for converting {v!r} into a float value: {e}").format( + msg = _('Got a {c} for converting {v!r} into a float value: {e}').format( c=e.__class__.__name__, v=value, e=e) raise argparse.ArgumentError(self, msg) if self._min_val is not None and val < self._min_val: - msg = _("The option must be greater or equal to {m} (given: {v}).").format( + msg = _('The option must be greater or equal to {m} (given: {v}).').format( m=self._min_val, v=val) raise argparse.ArgumentError(self, msg) if self._max_val is not None and val > self._max_val: - msg = _("The option must be less or equal to {m} (given: {v}).").format( + msg = _('The option must be less or equal to {m} (given: {v}).').format( m=self._max_val, v=val) raise argparse.ArgumentError(self, msg) @@ -148,7 +161,7 @@ class LimitedFloatOptionAction(argparse.Action): # ============================================================================= -if __name__ == "__main__": +if __name__ == '__main__': pass -- 2.39.5