From 91f23e215f654e2834388c105bb47c2ea9e8128f Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 15 Apr 2024 16:15:52 +0200 Subject: [PATCH] Adding option class OutputFileOptionAction --- lib/pp_admintools/argparse_actions.py | 60 ++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/lib/pp_admintools/argparse_actions.py b/lib/pp_admintools/argparse_actions.py index 07a4763..231b868 100644 --- a/lib/pp_admintools/argparse_actions.py +++ b/lib/pp_admintools/argparse_actions.py @@ -21,7 +21,7 @@ except ImportError: from . import MAX_PORT_NUMBER from .xlate import XLATOR -__version__ = '0.5.0' +__version__ = '0.6.0' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -285,6 +285,64 @@ class PasswordFileOptionAction(argparse.Action): setattr(namespace, self.dest, path) +# ============================================================================= +class OutputFileOptionAction(argparse.Action): + """Argparse action for a password file.""" + + # ------------------------------------------------------------------------- + def __init__(self, option_strings, must_exists=False, must_absolute=False, *args, **kwargs): + """Construct the action object.""" + self.must_exists = bool(must_exists) + self.must_absolute = bool(must_absolute) + + super(OutputFileOptionAction, self).__init__( + option_strings=option_strings, *args, **kwargs) + + # ------------------------------------------------------------------------- + def __call__(self, parser, namespace, given_path, option_string=None): + """Call the option action.""" + path = Path(given_path) + if self.must_absolute: + if not path.is_absolute(): + msg = _('The path {!r} must be an absolute path.').format(given_path) + raise argparse.ArgumentError(self, msg) + + if self.must_exists: + + if not path.exists(): + msg = _('The file {!r} does not exists.').format(str(path)) + raise argparse.ArgumentError(self, msg) + + if path.exists(): + if not path.is_file(): + msg = _('The given path {!r} exists, but is not a regular file.').format(str(path)) + raise argparse.ArgumentError(self, msg) + + if not os.access(str(path), os.R_OK): + msg = _('The given file {!r} is not readable.').format(str(path)) + raise argparse.ArgumentError(self, msg) + + if not os.access(str(path), os.W_OK): + msg = _('The given file {!r} is not writeable.').format(str(path)) + raise argparse.ArgumentError(self, msg) + + else: + parent_dir = path.parent + if not parent_dir.exists(): + msg = _('Directory {!r} does not exists.').format(str(parent_dir)) + raise argparse.ArgumentError(self, msg) + + if not parent_dir.is_dir(): + msg = _('Path {!r} exists, bu is not a directory.').format(str(parent_dir)) + raise argparse.ArgumentError(self, msg) + + if not os.access(str(parent_dir), os.W_OK): + msg = _('The directory {!r} is not writeable.').format(str(parent_dir)) + raise argparse.ArgumentError(self, msg) + + setattr(namespace, self.dest, path) + + # ============================================================================= class LdapPortOptionAction(argparse.Action): """Argparse action for the LDAP TCP (UDP?) port.""" -- 2.39.5