-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_validator.py
79 lines (66 loc) · 2.67 KB
/
base_validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Extension to the Cerberus Validator with common checks."""
from json import JSONDecodeError, load
from logging import NullHandler, getLogger
from os import R_OK, W_OK, X_OK, access
from os.path import isdir, isfile
from pprint import pformat
from cerberus import Validator
from cerberus.errors import UNKNOWN_FIELD
_logger = getLogger(__name__)
_logger.addHandler(NullHandler())
class BaseValidator(Validator):
"""Additional format checks."""
def error_str(self):
"""Prettier format to a list of errors."""
return '\n'.join((field + ': ' + pformat(error) for field, error in self.errors.items()))
def _isdir(self, field, value):
"""Validate value is a valid, existing directory."""
if not isdir(value):
self._error(field, "{} is not a valid directory or does not exist.".format(value))
return False
return True
def _isfile(self, field, value):
"""Validate value is a valid, existing file."""
if not isfile(value):
self._error(field, "{} is not a valid file or does not exist.".format(value))
return False
return True
def _isreadable(self, field, value):
"""Validate value is a readable file."""
if not access(value, R_OK):
self._error(field, "{} is not readable.".format(value))
return False
return True
def _iswriteable(self, field, value):
"""Validate value is a writeable file."""
if not access(value, W_OK):
self._error(field, "{} is not writeable.".format(value))
return False
return True
def _isexecutable(self, field, value):
"""Validate value is an executable file."""
if not access(value, X_OK):
self._error(field, "{} is not executable.".format(value))
return False
return True
def _isjsonfile(self, field, value):
"""Validate the JSON file is decodable."""
if self._isfile(field, value) and self._isreadable(field, value):
with open(value, "r") as file_ptr:
try:
schema = load(file_ptr)
except JSONDecodeError as ex:
self._error(field, "The file is not decodable JSON: {}".format(ex))
else:
return schema
return None
def str_errors(self, error):
"""Create an error string."""
if error.code == UNKNOWN_FIELD.code:
error.rule == 'unknown field'
str_tuple = (
'Value: ' + str(error.value),
'Rule: ' + str(error.rule),
'Constraint: ' + str(error.constraint)
)
return ', '.join(str_tuple)