# -*- 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
# =============================================================================
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
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)
# =============================================================================
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)
# =============================================================================
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
# -------------------------------------------------------------------------
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)
# =============================================================================
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
# -------------------------------------------------------------------------
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)
# =============================================================================
-if __name__ == "__main__":
+if __name__ == '__main__':
pass