From 1d5090b33e195dac9b62af7a6bf3d37903cdf6d8 Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Thu, 18 Apr 2019 01:57:58 +0200 Subject: [PATCH] generalized over.callbacks.directory -> over.callbacks.path --- over/callback.py | 37 +++++++++++++++++++++++++------------ over/version.py | 4 ++-- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/over/callback.py b/over/callback.py index 591af05..43c0146 100644 --- a/over/callback.py +++ b/over/callback.py @@ -53,28 +53,41 @@ def strings(*args): 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: - - the supplied directory doesn't exist and `exists` is True - - isn't writable and `writable` is True - - isn't a valid Gio path and `gio` is True + Returns a path callback that takes a path (str) and verifies if it: + - exists iff `exists` is True + - is writable iff `writable` 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): path = os.path.abspath(os.path.expanduser(arg)) - if not os.path.isdir(path) and exists: - raise FileNotFoundError("%s (%s) does not exist" %(arg, path)) + if exists and not os.path.exists(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)) + 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 cb diff --git a/over/version.py b/over/version.py index c9378ec..26e8c61 100644 --- a/over/version.py +++ b/over/version.py @@ -4,5 +4,5 @@ major = 2 # VERSION_MAJOR_IDENTIFIER minor = 1 # VERSION_MINOR_IDENTIFIER # VERSION_LAST_MM 2.1 -patch = 1 # VERSION_PATCH_IDENTIFIER -str = "2.1.1" # VERSION_STRING_IDENTIFIER +patch = 2 # VERSION_PATCH_IDENTIFIER +str = "2.1.2" # VERSION_STRING_IDENTIFIER