diff options
| author | Daniel Cooper <daniel@cooper.ro> | 2010-11-25 16:22:30 +1100 |
|---|---|---|
| committer | Daniel Cooper <daniel@cooper.ro> | 2010-11-25 16:22:30 +1100 |
| commit | fcc144ab7342bd6bb7c3382d855a3c0772a97e5d (patch) | |
| tree | 0d352c68dffbcf54582a31bb71202e1f91ecf93c | |
| parent | ea3528a7f1fe1bb850e0fd7a86b0646b1a4524b0 (diff) | |
| download | voluptuous-fcc144ab7342bd6bb7c3382d855a3c0772a97e5d.tar.gz | |
Added path-based validation.
- isdir(): verifies if the specified path exists and is a directory.
- isfile(): verifies if the specified path exists and is a file.
- path_exists(): verifies if the specified path exists.
| -rw-r--r-- | voluptuous.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/voluptuous.py b/voluptuous.py index b377560..be450a8 100644 --- a/voluptuous.py +++ b/voluptuous.py @@ -82,6 +82,7 @@ Validate like so: 'Users': {'snmp_community': 'monkey'}}}} """ +import os import re import string import types @@ -630,6 +631,36 @@ def url(msg=None): return f +def isfile(msg=None): + """Verify the file exists.""" + def f(v): + if os.path.isfile(v): + return v + else: + raise Invalid(msg or 'not a file') + return f + + +def isdir(msg=None): + """Verify the directory exists.""" + def f(v): + if os.path.isdir(v): + return v + else: + raise Invalid(msg or 'not a directory') + return f + + +def path_exists(msg=None): + """Verify the path exists, regardless of its type.""" + def f(v): + if os.path.exists(v): + return v + else: + raise Invalid(msg or 'path does not exist') + return f + + def range(min=None, max=None, msg=None): """Limit a value to a range. |
