generalized over.callbacks.directory -> over.callbacks.path

This commit is contained in:
Martin Sekera 2019-04-18 01:57:58 +02:00
parent 430ed6e34d
commit 1d5090b33e
2 changed files with 27 additions and 14 deletions

View file

@ -53,28 +53,41 @@ def strings(*args):
return out return out
def directory(exists=False, writable=False, gio=False): def path(exists=False, writable=False, validators=[]):
""" """
Returns a directory callback that raises hell if: Returns a path callback that takes a path (str) and verifies if it:
- the supplied directory doesn't exist and `exists` is True - exists iff `exists` is True
- isn't writable and `writable` is True - is writable iff `writable` is True
- isn't a valid Gio path and `gio` is True - goes through each (function, message) pair in `validators`, in order
@while generating a callback A validator is a function that takes a str argument and returns True or False.
If False is returned, the matching message is raised along with a RuntimeError.
The message may contain %s to be replaced by the supplied path. Example:
validators=[
(os.path.isdir, "%s is not a directory"),
(os.path.isabs, "%s is a directory, but is not an absolute path.")
]
@while generating a path callback
""" """
if gio:
raise NotImplementedError("Gio support is not yet here")
def cb(arg): def cb(arg):
path = os.path.abspath(os.path.expanduser(arg)) path = os.path.abspath(os.path.expanduser(arg))
if not os.path.isdir(path) and exists: if exists and not os.path.exists(path):
raise FileNotFoundError("%s (%s) does not exist" %(arg, path)) raise FileNotFoundError("%s does not exist" %(arg))
if writable and not os.access(path, os.W_OK | os.X_OK): if writable and not os.access(path, os.W_OK):
raise PermissionError("%s exists but is not writable" %(arg)) raise PermissionError("%s exists but is not writable" %(arg))
if validators:
for fn, msg_tpl in validators:
if not fn(path):
msg = msg_tpl %(arg) if "%s" in msg_tpl else msg_tpl
raise RuntimeError(msg)
return path return path
return cb return cb

View file

@ -4,5 +4,5 @@
major = 2 # VERSION_MAJOR_IDENTIFIER major = 2 # VERSION_MAJOR_IDENTIFIER
minor = 1 # VERSION_MINOR_IDENTIFIER minor = 1 # VERSION_MINOR_IDENTIFIER
# VERSION_LAST_MM 2.1 # VERSION_LAST_MM 2.1
patch = 1 # VERSION_PATCH_IDENTIFIER patch = 2 # VERSION_PATCH_IDENTIFIER
str = "2.1.1" # VERSION_STRING_IDENTIFIER str = "2.1.2" # VERSION_STRING_IDENTIFIER