changed the over.callback.path API a bit

This commit is contained in:
Martin Sekera 2019-04-18 02:08:20 +02:00
parent 1d5090b33e
commit cf9fda165f
2 changed files with 11 additions and 6 deletions

View file

@ -53,11 +53,11 @@ def strings(*args):
return out
def path(exists=False, writable=False, validators=[]):
def path(exists=False, permissions=None, validators=None):
"""
Returns a path callback that takes a path (str) and verifies if it:
- exists iff `exists` is True
- is writable iff `writable` is True
- its permissions match those in `permissions` ("rwx" or any subset, e.g. "r--", "-w-", dashes are optional)
- goes through each (function, message) pair in `validators`, in order
A validator is a function that takes a str argument and returns True or False.
@ -78,8 +78,13 @@ def path(exists=False, writable=False, validators=[]):
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):
raise PermissionError("%s exists but is not writable" %(arg))
if permissions:
if "r" in permissions and not os.access(path, os.R_OK):
raise PermissionError("%s is not readable" %(arg))
if "w" in permissions and not os.access(path, os.W_OK):
raise PermissionError("%s is not writable" %(arg))
if "x" in permissions and not os.access(path, os.X_OK):
raise PermissionError("%s is not executable" %(arg))
if validators:
for fn, msg_tpl in validators:

View file

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