]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Adding option class OutputFileOptionAction
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 15 Apr 2024 14:15:52 +0000 (16:15 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 15 Apr 2024 14:15:52 +0000 (16:15 +0200)
lib/pp_admintools/argparse_actions.py

index 07a4763bf7150f804721b25f4a7d1d06fd34901a..231b86839ad37f192ae0cd84a10ca1daaea9badb 100644 (file)
@@ -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."""