/usr/lib/python2.7/dist-packages/staticconf/validation.py is in python-staticconf 0.10.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | """
Validate a configuration value by converting it to a specific type.
These functions are used by :mod:`staticconf.readers` and
:mod:`staticconf.schema` to coerce config values to a type.
"""
import datetime
import logging
import re
import time
import six
from staticconf.errors import ValidationError
def validate_string(value):
return None if value is None else six.text_type(value)
def validate_bool(value):
return None if value is None else bool(value)
def validate_numeric(type_func, value):
try:
return type_func(value)
except ValueError:
raise ValidationError("Invalid %s: %s" % (type_func.__name__, value))
def validate_int(value):
return validate_numeric(int, value)
def validate_float(value):
return validate_numeric(float, value)
date_formats = [
"%Y-%m-%d %H:%M:%S",
"%Y-%m-%d %I:%M:%S %p",
"%Y-%m-%d",
"%y-%m-%d",
"%m/%d/%y",
"%m/%d/%Y",
]
def validate_datetime(value):
if isinstance(value, datetime.datetime):
return value
for format_ in date_formats:
try:
return datetime.datetime.strptime(value, format_)
except ValueError:
pass
raise ValidationError("Invalid date format: %s" % value)
def validate_date(value):
if isinstance(value, datetime.date):
return value
return validate_datetime(value).date()
time_formats = [
"%I %p",
"%H:%M",
"%I:%M %p",
"%H:%M:%S",
"%I:%M:%S %p"
]
def validate_time(value):
if isinstance(value, datetime.time):
return value
for format_ in time_formats:
try:
return datetime.time(*time.strptime(value, format_)[3:6])
except ValueError:
pass
raise ValidationError("Invalid time format: %s" % value)
def _validate_iterable(iterable_type, value):
"""Convert the iterable to iterable_type, or raise a Configuration
exception.
"""
if isinstance(value, six.string_types):
msg = "Invalid iterable of type(%s): %s"
raise ValidationError(msg % (type(value), value))
try:
return iterable_type(value)
except TypeError:
raise ValidationError("Invalid iterable: %s" % (value))
def validate_list(value):
return _validate_iterable(list, value)
def validate_set(value):
return _validate_iterable(set, value)
def validate_tuple(value):
return _validate_iterable(tuple, value)
def validate_regex(value):
try:
return re.compile(value)
except (re.error, TypeError) as e:
raise ValidationError("Invalid regex: %s, %s" % (e, value))
def build_list_type_validator(item_validator):
"""Return a function which validates that the value is a list of items
which are validated using item_validator.
"""
def validate_list_of_type(value):
return [item_validator(item) for item in validate_list(value)]
return validate_list_of_type
def build_map_type_validator(item_validator):
"""Return a function which validates that the value is a mapping of
items. The function should return pairs of items that will be
passed to the `dict` constructor.
"""
def validate_mapping(value):
return dict(item_validator(item) for item in validate_list(value))
return validate_mapping
def validate_log_level(value):
"""Validate a log level from a string value. Returns a constant from
the :mod:`logging` module.
"""
try:
return getattr(logging, value)
except AttributeError:
raise ValidationError("Unknown log level: %s" % value)
def validate_any(value):
return value
validators = {
'': validate_any,
'bool': validate_bool,
'date': validate_date,
'datetime': validate_datetime,
'float': validate_float,
'int': validate_int,
'list': validate_list,
'set': validate_set,
'string': validate_string,
'time': validate_time,
'tuple': validate_tuple,
'regex': validate_regex,
'log_level': validate_log_level,
}
def get_validators():
"""Return an iterator of (validator_name, validator) pairs."""
return six.iteritems(validators)
|