from . import MAX_PORT_NUMBER
from .xlate import XLATOR
-__version__ = '0.5.0'
+__version__ = '0.6.0'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
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."""