3 # Copyright (C) 2005 Michael Foord, Mark Andrews, Nicola Larosa
4 # E-mail: fuzzyman AT voidspace DOT org DOT uk
5 # mark AT la-la DOT com
6 # nico AT tekNico DOT net
8 # This software is licensed under the terms of the BSD license.
9 # http://www.voidspace.org.uk/python/license.shtml
10 # Basically you're free to copy, modify, distribute and relicense it,
11 # So long as you keep a copy of the license with it.
13 # Scripts maintained at http://www.voidspace.org.uk/python/index.shtml
14 # For information about bugfixes, updates and support, please join the
15 # ConfigObj mailing list:
16 # http://lists.sourceforge.net/lists/listinfo/configobj-develop
17 # Comments, suggestions and bug reports welcome.
20 The Validator object is used to check that supplied values
21 conform to a specification.
23 The value can be supplied as a string - e.g. from a config file.
24 In this case the check will also *convert* the value to
25 the required type. This allows you to add validation
26 as a transparent layer to access data stored as strings.
27 The validation checks that the data is correct *and*
28 converts it to the expected type.
30 Some standard checks are provided for basic data types.
31 Additional checks are easy to write. They can be
32 provided when the ``Validator`` is instantiated or
35 The standard functions work with the following basic data types :
43 plus lists of these datatypes
45 Adding additional checks is done through coding simple functions.
47 The full set of standard checks are :
49 * 'integer': matches integer values (including negative)
50 Takes optional 'min' and 'max' arguments : ::
53 integer(3, 9) # any value from 3 to 9
54 integer(min=0) # any positive value
57 * 'float': matches float values
58 Has the same parameters as the integer check.
60 * 'boolean': matches boolean values - ``True`` or ``False``
61 Acceptable string values for True are :
63 Acceptable string values for False are :
66 Any other value raises an error.
68 * 'ip_addr': matches an Internet Protocol address, v.4, represented
69 by a dotted-quad string, i.e. '1.2.3.4'.
71 * 'string': matches any string.
72 Takes optional keyword args 'min' and 'max'
73 to specify min and max lengths of the string.
75 * 'list': matches any list.
76 Takes optional keyword args 'min', and 'max' to specify min and
77 max sizes of the list. (Always returns a list.)
79 * 'tuple': matches any tuple.
80 Takes optional keyword args 'min', and 'max' to specify min and
81 max sizes of the tuple. (Always returns a tuple.)
83 * 'int_list': Matches a list of integers.
84 Takes the same arguments as list.
86 * 'float_list': Matches a list of floats.
87 Takes the same arguments as list.
89 * 'bool_list': Matches a list of boolean values.
90 Takes the same arguments as list.
92 * 'ip_addr_list': Matches a list of IP addresses.
93 Takes the same arguments as list.
95 * 'string_list': Matches a list of strings.
96 Takes the same arguments as list.
98 * 'mixed_list': Matches a list with different types in
99 specific positions. List size must match
100 the number of arguments.
102 Each position can be one of :
103 'integer', 'float', 'ip_addr', 'string', 'boolean'
105 So to specify a list with two strings followed
106 by two integers, you write the check as : ::
108 mixed_list('string', 'string', 'integer', 'integer')
110 * 'pass': This check matches everything ! It never fails
111 and the value is unchanged.
113 It is also the default if no check is specified.
115 * 'option': This check matches any from a list of options.
116 You specify this check with : ::
118 option('option 1', 'option 2', 'option 3')
120 You can supply a default value (returned if no value is supplied)
121 using the default keyword argument.
123 You specify a list argument for default using a list constructor syntax in
126 checkname(arg1, arg2, default=list('val 1', 'val 2', 'val 3'))
128 A badly formatted set of arguments will raise a ``VdtParamError``.
131 __docformat__ = "restructuredtext en"
133 __version__ = '1.0.0'
135 __revision__ = '$Id: validate.py 123 2005-09-08 08:54:28Z fuzzyman $'
142 'VdtUnknownCheckError',
146 'VdtValueTooSmallError',
147 'VdtValueTooBigError',
148 'VdtValueTooShortError',
149 'VdtValueTooLongError',
173 _list_arg = re.compile(r'''
175 ([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*list\(
180 (?:".*?")| # double quotes
181 (?:'.*?')| # single quotes
182 (?:[^'",\s\)][^,\)]*?) # unquoted
187 (?:".*?")| # double quotes
188 (?:'.*?')| # single quotes
189 (?:[^'",\s\)][^,\)]*?) # unquoted
194 ''', re.VERBOSE | re.DOTALL) # two groups
196 _list_members = re.compile(r'''
198 (?:".*?")| # double quotes
199 (?:'.*?')| # single quotes
200 (?:[^'",\s=][^,=]*?) # unquoted
203 (?:\s*,\s*)|(?:\s*$) # comma
205 ''', re.VERBOSE | re.DOTALL) # one group
211 [a-zA-Z_][a-zA-Z0-9_]*\s*=\s*list\(
215 (?:".*?")| # double quotes
216 (?:'.*?')| # single quotes
217 (?:[^'",\s\)][^,\)]*?) # unquoted
222 (?:".*?")| # double quotes
223 (?:'.*?')| # single quotes
224 (?:[^'",\s\)][^,\)]*?) # unquoted
229 (?:".*?")| # double quotes
230 (?:'.*?')| # single quotes
231 (?:[^'",\s=][^,=]*?)| # unquoted
232 (?: # keyword argument
233 [a-zA-Z_][a-zA-Z0-9_]*\s*=\s*
235 (?:".*?")| # double quotes
236 (?:'.*?')| # single quotes
237 (?:[^'",\s=][^,=]*?) # unquoted
243 (?:\s*,\s*)|(?:\s*$) # comma
248 _matchstring = '^%s*' % _paramstring
250 # Python pre 2.2.1 doesn't have bool
255 """Simple boolean equivalent function. """
262 def dottedQuadToNum(ip):
264 Convert decimal dotted quad string to long integer
266 >>> int(dottedQuadToNum('1 '))
268 >>> int(dottedQuadToNum(' 1.2'))
270 >>> int(dottedQuadToNum(' 1.2.3 '))
272 >>> int(dottedQuadToNum('1.2.3.4'))
274 >>> dottedQuadToNum('1.2.3. 4')
276 >>> dottedQuadToNum('255.255.255.255')
278 >>> dottedQuadToNum('255.255.255.256')
279 Traceback (most recent call last):
280 ValueError: Not a good dotted-quad IP: 255.255.255.256
283 # import here to avoid it when ip_addr values are not used
284 import socket, struct
287 return struct.unpack('!L',
288 socket.inet_aton(ip.strip()))[0]
290 # bug in inet_aton, corrected in Python 2.3
291 if ip.strip() == '255.255.255.255':
294 raise ValueError('Not a good dotted-quad IP: %s' % ip)
298 def numToDottedQuad(num):
300 Convert long int to dotted quad string
302 >>> numToDottedQuad(-1L)
303 Traceback (most recent call last):
304 ValueError: Not a good numeric IP: -1
305 >>> numToDottedQuad(1L)
307 >>> numToDottedQuad(16777218L)
309 >>> numToDottedQuad(16908291L)
311 >>> numToDottedQuad(16909060L)
313 >>> numToDottedQuad(4294967295L)
315 >>> numToDottedQuad(4294967296L)
316 Traceback (most recent call last):
317 ValueError: Not a good numeric IP: 4294967296
320 # import here to avoid it when ip_addr values are not used
321 import socket, struct
323 # no need to intercept here, 4294967295L is fine
324 if num > 4294967295L or num < 0:
325 raise ValueError('Not a good numeric IP: %s' % num)
327 return socket.inet_ntoa(
328 struct.pack('!L', long(num)))
329 except (socket.error, struct.error, OverflowError):
330 raise ValueError('Not a good numeric IP: %s' % num)
333 class ValidateError(Exception):
335 This error indicates that the check failed.
336 It can be the base class for more specific errors.
338 Any check function that fails ought to raise this error.
341 >>> raise ValidateError
342 Traceback (most recent call last):
347 class VdtMissingValue(ValidateError):
348 """No value was supplied to a check that needed one."""
351 class VdtUnknownCheckError(ValidateError):
352 """An unknown check function was requested"""
354 def __init__(self, value):
356 >>> raise VdtUnknownCheckError('yoda')
357 Traceback (most recent call last):
358 VdtUnknownCheckError: the check "yoda" is unknown.
360 ValidateError.__init__(self, 'the check "%s" is unknown.' % (value,))
363 class VdtParamError(SyntaxError):
364 """An incorrect parameter was passed"""
366 def __init__(self, name, value):
368 >>> raise VdtParamError('yoda', 'jedi')
369 Traceback (most recent call last):
370 VdtParamError: passed an incorrect value "jedi" for parameter "yoda".
372 SyntaxError.__init__(self, 'passed an incorrect value "%s" for parameter "%s".' % (value, name))
375 class VdtTypeError(ValidateError):
376 """The value supplied was of the wrong type"""
378 def __init__(self, value):
380 >>> raise VdtTypeError('jedi')
381 Traceback (most recent call last):
382 VdtTypeError: the value "jedi" is of the wrong type.
384 ValidateError.__init__(self, 'the value "%s" is of the wrong type.' % (value,))
387 class VdtValueError(ValidateError):
388 """The value supplied was of the correct type, but was not an allowed value."""
390 def __init__(self, value):
392 >>> raise VdtValueError('jedi')
393 Traceback (most recent call last):
394 VdtValueError: the value "jedi" is unacceptable.
396 ValidateError.__init__(self, 'the value "%s" is unacceptable.' % (value,))
399 class VdtValueTooSmallError(VdtValueError):
400 """The value supplied was of the correct type, but was too small."""
402 def __init__(self, value):
404 >>> raise VdtValueTooSmallError('0')
405 Traceback (most recent call last):
406 VdtValueTooSmallError: the value "0" is too small.
408 ValidateError.__init__(self, 'the value "%s" is too small.' % (value,))
411 class VdtValueTooBigError(VdtValueError):
412 """The value supplied was of the correct type, but was too big."""
414 def __init__(self, value):
416 >>> raise VdtValueTooBigError('1')
417 Traceback (most recent call last):
418 VdtValueTooBigError: the value "1" is too big.
420 ValidateError.__init__(self, 'the value "%s" is too big.' % (value,))
423 class VdtValueTooShortError(VdtValueError):
424 """The value supplied was of the correct type, but was too short."""
426 def __init__(self, value):
428 >>> raise VdtValueTooShortError('jed')
429 Traceback (most recent call last):
430 VdtValueTooShortError: the value "jed" is too short.
432 ValidateError.__init__(
434 'the value "%s" is too short.' % (value,))
437 class VdtValueTooLongError(VdtValueError):
438 """The value supplied was of the correct type, but was too long."""
440 def __init__(self, value):
442 >>> raise VdtValueTooLongError('jedie')
443 Traceback (most recent call last):
444 VdtValueTooLongError: the value "jedie" is too long.
446 ValidateError.__init__(self, 'the value "%s" is too long.' % (value,))
449 class Validator(object):
451 Validator is an object that allows you to register a set of 'checks'.
452 These checks take input and test that it conforms to the check.
454 This can also involve converting the value from a string into
455 the correct datatype.
457 The ``check`` method takes an input string which configures which
458 check is to be used and applies that check to a supplied value.
460 An example input string would be:
461 'int_range(param1, param2)'
463 You would then provide something like:
465 >>> def int_range_check(value, min, max):
466 ... # turn min and max from strings to integers
469 ... # check that value is of the correct type.
470 ... # possible valid inputs are integers or strings
471 ... # that represent integers
472 ... if not isinstance(value, (int, long, basestring)):
473 ... raise VdtTypeError(value)
474 ... elif isinstance(value, basestring):
475 ... # if we are given a string
476 ... # attempt to convert to an integer
478 ... value = int(value)
479 ... except ValueError:
480 ... raise VdtValueError(value)
481 ... # check the value is between our constraints
482 ... if not min <= value:
483 ... raise VdtValueTooSmallError(value)
484 ... if not value <= max:
485 ... raise VdtValueTooBigError(value)
488 >>> fdict = {'int_range': int_range_check}
489 >>> vtr1 = Validator(fdict)
490 >>> vtr1.check('int_range(20, 40)', '30')
492 >>> vtr1.check('int_range(20, 40)', '60')
493 Traceback (most recent call last):
494 VdtValueTooBigError: the value "60" is too big.
496 New functions can be added with : ::
498 >>> vtr2 = Validator()
499 >>> vtr2.functions['int_range'] = int_range_check
501 Or by passing in a dictionary of functions when Validator
504 Your functions *can* use keyword arguments,
505 but the first argument should always be 'value'.
507 If the function doesn't take additional arguments,
508 the parentheses are optional in the check.
509 It can be written with either of : ::
511 keyword = function_name
512 keyword = function_name()
514 The first program to utilise Validator() was Michael Foord's
515 ConfigObj, an alternative to ConfigParser which supports lists and
516 can validate a config file using a config schema.
517 For more details on using Validator with ConfigObj see:
518 http://www.voidspace.org.uk/python/configobj.html
521 # this regex does the initial parsing of the checks
522 _func_re = re.compile(r'(.+?)\((.*)\)', re.DOTALL)
524 # this regex takes apart keyword arguments
525 _key_arg = re.compile(r'^([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*(.*)$', re.DOTALL)
528 # this regex finds keyword=list(....) type values
529 _list_arg = _list_arg
531 # this regex takes individual values out of lists - in one pass
532 _list_members = _list_members
534 # These regexes check a set of arguments for validity
535 # and then pull the members out
536 _paramfinder = re.compile(_paramstring, re.VERBOSE | re.DOTALL)
537 _matchfinder = re.compile(_matchstring, re.VERBOSE | re.DOTALL)
540 def __init__(self, functions=None):
542 >>> vtri = Validator()
546 'integer': is_integer,
548 'boolean': is_boolean,
549 'ip_addr': is_ip_addr,
553 'int_list': is_int_list,
554 'float_list': is_float_list,
555 'bool_list': is_bool_list,
556 'ip_addr_list': is_ip_addr_list,
557 'string_list': is_string_list,
558 'mixed_list': is_mixed_list,
561 'force_list': force_list,
563 if functions is not None:
564 self.functions.update(functions)
565 # tekNico: for use by ConfigObj
566 self.baseErrorClass = ValidateError
570 def check(self, check, value, missing=False):
572 Usage: check(check, value)
575 check: string representing check to apply (including arguments)
576 value: object to be checked
577 Returns value, converted to correct type if necessary
579 If the check fails, raises a ``ValidateError`` subclass.
581 >>> vtor.check('yoda', '')
582 Traceback (most recent call last):
583 VdtUnknownCheckError: the check "yoda" is unknown.
584 >>> vtor.check('yoda()', '')
585 Traceback (most recent call last):
586 VdtUnknownCheckError: the check "yoda" is unknown.
588 >>> vtor.check('string(default="")', '', missing=True)
591 fun_name, fun_args, fun_kwargs, default = self._parse_with_caching(check)
595 # no information needed here - to be handled by caller
596 raise VdtMissingValue()
597 value = self._handle_none(default)
602 return self._check_value(value, fun_name, fun_args, fun_kwargs)
605 def _handle_none(self, value):
608 elif value in ("'None'", '"None"'):
609 # Special case a quoted None
610 value = self._unquote(value)
614 def _parse_with_caching(self, check):
615 if check in self._cache:
616 fun_name, fun_args, fun_kwargs, default = self._cache[check]
617 # We call list and dict below to work with *copies* of the data
618 # rather than the original (which are mutable of course)
619 fun_args = list(fun_args)
620 fun_kwargs = dict(fun_kwargs)
622 fun_name, fun_args, fun_kwargs, default = self._parse_check(check)
623 fun_kwargs = dict((str(key), value) for (key, value) in fun_kwargs.items())
624 self._cache[check] = fun_name, list(fun_args), dict(fun_kwargs), default
625 return fun_name, fun_args, fun_kwargs, default
628 def _check_value(self, value, fun_name, fun_args, fun_kwargs):
630 fun = self.functions[fun_name]
632 raise VdtUnknownCheckError(fun_name)
634 return fun(value, *fun_args, **fun_kwargs)
637 def _parse_check(self, check):
638 fun_match = self._func_re.match(check)
640 fun_name = fun_match.group(1)
641 arg_string = fun_match.group(2)
642 arg_match = self._matchfinder.match(arg_string)
643 if arg_match is None:
645 raise VdtParamError('Bad syntax in check "%s".' % check)
648 # pull out args of group 2
649 for arg in self._paramfinder.findall(arg_string):
650 # args may need whitespace removing (before removing quotes)
652 listmatch = self._list_arg.match(arg)
654 key, val = self._list_handle(listmatch)
655 fun_kwargs[key] = val
657 keymatch = self._key_arg.match(arg)
659 val = keymatch.group(2)
660 if not val in ("'None'", '"None"'):
661 # Special case a quoted None
662 val = self._unquote(val)
663 fun_kwargs[keymatch.group(1)] = val
666 fun_args.append(self._unquote(arg))
668 # allows for function names without (args)
669 return check, (), {}, None
671 # Default must be deleted if the value is specified too,
672 # otherwise the check function will get a spurious "default" keyword arg
674 default = fun_kwargs.pop('default', None)
675 except AttributeError:
676 # Python 2.2 compatibility
679 default = fun_kwargs['default']
680 del fun_kwargs['default']
684 return fun_name, fun_args, fun_kwargs, default
687 def _unquote(self, val):
688 """Unquote a value if necessary."""
689 if (len(val) >= 2) and (val[0] in ("'", '"')) and (val[0] == val[-1]):
694 def _list_handle(self, listmatch):
695 """Take apart a ``keyword=list('val, 'val')`` type string."""
697 name = listmatch.group(1)
698 args = listmatch.group(2)
699 for arg in self._list_members.findall(args):
700 out.append(self._unquote(arg))
704 def _pass(self, value):
706 Dummy check that always passes
708 >>> vtor.check('', 0)
710 >>> vtor.check('', '0')
716 def get_default_value(self, check):
718 Given a check, return the default value for the check
719 (converted to the right type).
721 If the check doesn't specify a default value then a
722 ``KeyError`` will be raised.
724 fun_name, fun_args, fun_kwargs, default = self._parse_with_caching(check)
726 raise KeyError('Check "%s" has no default value.' % check)
727 value = self._handle_none(default)
730 return self._check_value(value, fun_name, fun_args, fun_kwargs)
733 def _is_num_param(names, values, to_float=False):
735 Return numbers from inputs or raise VdtParamError.
737 Lets ``None`` pass through.
738 Pass in keyword argument ``to_float=True`` to
739 use float for the conversion rather than int.
741 >>> _is_num_param(('', ''), (0, 1.0))
743 >>> _is_num_param(('', ''), (0, 1.0), to_float=True)
745 >>> _is_num_param(('a'), ('a'))
746 Traceback (most recent call last):
747 VdtParamError: passed an incorrect value "a" for parameter "a".
749 fun = to_float and float or int
751 for (name, val) in zip(names, values):
753 out_params.append(val)
754 elif isinstance(val, (int, long, float, basestring)):
756 out_params.append(fun(val))
757 except ValueError, e:
758 raise VdtParamError(name, val)
760 raise VdtParamError(name, val)
765 # you can override these by setting the appropriate name
766 # in Validator.functions
767 # note: if the params are specified wrongly in your input string,
768 # you will also raise errors.
770 def is_integer(value, min=None, max=None):
772 A check that tests that a given value is an integer (int, or long)
773 and optionally, between bounds. A negative value is accepted, while
776 If the value is a string, then the conversion is done - if possible.
777 Otherwise a VdtError is raised.
779 >>> vtor.check('integer', '-1')
781 >>> vtor.check('integer', '0')
783 >>> vtor.check('integer', 9)
785 >>> vtor.check('integer', 'a')
786 Traceback (most recent call last):
787 VdtTypeError: the value "a" is of the wrong type.
788 >>> vtor.check('integer', '2.2')
789 Traceback (most recent call last):
790 VdtTypeError: the value "2.2" is of the wrong type.
791 >>> vtor.check('integer(10)', '20')
793 >>> vtor.check('integer(max=20)', '15')
795 >>> vtor.check('integer(10)', '9')
796 Traceback (most recent call last):
797 VdtValueTooSmallError: the value "9" is too small.
798 >>> vtor.check('integer(10)', 9)
799 Traceback (most recent call last):
800 VdtValueTooSmallError: the value "9" is too small.
801 >>> vtor.check('integer(max=20)', '35')
802 Traceback (most recent call last):
803 VdtValueTooBigError: the value "35" is too big.
804 >>> vtor.check('integer(max=20)', 35)
805 Traceback (most recent call last):
806 VdtValueTooBigError: the value "35" is too big.
807 >>> vtor.check('integer(0, 9)', False)
810 (min_val, max_val) = _is_num_param(('min', 'max'), (min, max))
811 if not isinstance(value, (int, long, basestring)):
812 raise VdtTypeError(value)
813 if isinstance(value, basestring):
814 # if it's a string - does it represent an integer ?
818 raise VdtTypeError(value)
819 if (min_val is not None) and (value < min_val):
820 raise VdtValueTooSmallError(value)
821 if (max_val is not None) and (value > max_val):
822 raise VdtValueTooBigError(value)
826 def is_float(value, min=None, max=None):
828 A check that tests that a given value is a float
829 (an integer will be accepted), and optionally - that it is between bounds.
831 If the value is a string, then the conversion is done - if possible.
832 Otherwise a VdtError is raised.
834 This can accept negative values.
836 >>> vtor.check('float', '2')
839 From now on we multiply the value to avoid comparing decimals
841 >>> vtor.check('float', '-6.8') * 10
843 >>> vtor.check('float', '12.2') * 10
845 >>> vtor.check('float', 8.4) * 10
847 >>> vtor.check('float', 'a')
848 Traceback (most recent call last):
849 VdtTypeError: the value "a" is of the wrong type.
850 >>> vtor.check('float(10.1)', '10.2') * 10
852 >>> vtor.check('float(max=20.2)', '15.1') * 10
854 >>> vtor.check('float(10.0)', '9.0')
855 Traceback (most recent call last):
856 VdtValueTooSmallError: the value "9.0" is too small.
857 >>> vtor.check('float(max=20.0)', '35.0')
858 Traceback (most recent call last):
859 VdtValueTooBigError: the value "35.0" is too big.
861 (min_val, max_val) = _is_num_param(
862 ('min', 'max'), (min, max), to_float=True)
863 if not isinstance(value, (int, long, float, basestring)):
864 raise VdtTypeError(value)
865 if not isinstance(value, float):
866 # if it's a string - does it represent a float ?
870 raise VdtTypeError(value)
871 if (min_val is not None) and (value < min_val):
872 raise VdtValueTooSmallError(value)
873 if (max_val is not None) and (value > max_val):
874 raise VdtValueTooBigError(value)
879 True: True, 'on': True, '1': True, 'true': True, 'yes': True,
880 False: False, 'off': False, '0': False, 'false': False, 'no': False,
884 def is_boolean(value):
886 Check if the value represents a boolean.
888 >>> vtor.check('boolean', 0)
890 >>> vtor.check('boolean', False)
892 >>> vtor.check('boolean', '0')
894 >>> vtor.check('boolean', 'off')
896 >>> vtor.check('boolean', 'false')
898 >>> vtor.check('boolean', 'no')
900 >>> vtor.check('boolean', 'nO')
902 >>> vtor.check('boolean', 'NO')
904 >>> vtor.check('boolean', 1)
906 >>> vtor.check('boolean', True)
908 >>> vtor.check('boolean', '1')
910 >>> vtor.check('boolean', 'on')
912 >>> vtor.check('boolean', 'true')
914 >>> vtor.check('boolean', 'yes')
916 >>> vtor.check('boolean', 'Yes')
918 >>> vtor.check('boolean', 'YES')
920 >>> vtor.check('boolean', '')
921 Traceback (most recent call last):
922 VdtTypeError: the value "" is of the wrong type.
923 >>> vtor.check('boolean', 'up')
924 Traceback (most recent call last):
925 VdtTypeError: the value "up" is of the wrong type.
928 if isinstance(value, basestring):
930 return bool_dict[value.lower()]
932 raise VdtTypeError(value)
933 # we do an equality test rather than an identity test
934 # this ensures Python 2.2 compatibilty
935 # and allows 0 and 1 to represent True and False
941 raise VdtTypeError(value)
944 def is_ip_addr(value):
946 Check that the supplied value is an Internet Protocol address, v.4,
947 represented by a dotted-quad string, i.e. '1.2.3.4'.
949 >>> vtor.check('ip_addr', '1 ')
951 >>> vtor.check('ip_addr', ' 1.2')
953 >>> vtor.check('ip_addr', ' 1.2.3 ')
955 >>> vtor.check('ip_addr', '1.2.3.4')
957 >>> vtor.check('ip_addr', '0.0.0.0')
959 >>> vtor.check('ip_addr', '255.255.255.255')
961 >>> vtor.check('ip_addr', '255.255.255.256')
962 Traceback (most recent call last):
963 VdtValueError: the value "255.255.255.256" is unacceptable.
964 >>> vtor.check('ip_addr', '1.2.3.4.5')
965 Traceback (most recent call last):
966 VdtValueError: the value "1.2.3.4.5" is unacceptable.
967 >>> vtor.check('ip_addr', 0)
968 Traceback (most recent call last):
969 VdtTypeError: the value "0" is of the wrong type.
971 if not isinstance(value, basestring):
972 raise VdtTypeError(value)
973 value = value.strip()
975 dottedQuadToNum(value)
977 raise VdtValueError(value)
981 def is_list(value, min=None, max=None):
983 Check that the value is a list of values.
985 You can optionally specify the minimum and maximum number of members.
987 It does no check on list members.
989 >>> vtor.check('list', ())
991 >>> vtor.check('list', [])
993 >>> vtor.check('list', (1, 2))
995 >>> vtor.check('list', [1, 2])
997 >>> vtor.check('list(3)', (1, 2))
998 Traceback (most recent call last):
999 VdtValueTooShortError: the value "(1, 2)" is too short.
1000 >>> vtor.check('list(max=5)', (1, 2, 3, 4, 5, 6))
1001 Traceback (most recent call last):
1002 VdtValueTooLongError: the value "(1, 2, 3, 4, 5, 6)" is too long.
1003 >>> vtor.check('list(min=3, max=5)', (1, 2, 3, 4))
1005 >>> vtor.check('list', 0)
1006 Traceback (most recent call last):
1007 VdtTypeError: the value "0" is of the wrong type.
1008 >>> vtor.check('list', '12')
1009 Traceback (most recent call last):
1010 VdtTypeError: the value "12" is of the wrong type.
1012 (min_len, max_len) = _is_num_param(('min', 'max'), (min, max))
1013 if isinstance(value, basestring):
1014 raise VdtTypeError(value)
1016 num_members = len(value)
1018 raise VdtTypeError(value)
1019 if min_len is not None and num_members < min_len:
1020 raise VdtValueTooShortError(value)
1021 if max_len is not None and num_members > max_len:
1022 raise VdtValueTooLongError(value)
1026 def is_tuple(value, min=None, max=None):
1028 Check that the value is a tuple of values.
1030 You can optionally specify the minimum and maximum number of members.
1032 It does no check on members.
1034 >>> vtor.check('tuple', ())
1036 >>> vtor.check('tuple', [])
1038 >>> vtor.check('tuple', (1, 2))
1040 >>> vtor.check('tuple', [1, 2])
1042 >>> vtor.check('tuple(3)', (1, 2))
1043 Traceback (most recent call last):
1044 VdtValueTooShortError: the value "(1, 2)" is too short.
1045 >>> vtor.check('tuple(max=5)', (1, 2, 3, 4, 5, 6))
1046 Traceback (most recent call last):
1047 VdtValueTooLongError: the value "(1, 2, 3, 4, 5, 6)" is too long.
1048 >>> vtor.check('tuple(min=3, max=5)', (1, 2, 3, 4))
1050 >>> vtor.check('tuple', 0)
1051 Traceback (most recent call last):
1052 VdtTypeError: the value "0" is of the wrong type.
1053 >>> vtor.check('tuple', '12')
1054 Traceback (most recent call last):
1055 VdtTypeError: the value "12" is of the wrong type.
1057 return tuple(is_list(value, min, max))
1060 def is_string(value, min=None, max=None):
1062 Check that the supplied value is a string.
1064 You can optionally specify the minimum and maximum number of members.
1066 >>> vtor.check('string', '0')
1068 >>> vtor.check('string', 0)
1069 Traceback (most recent call last):
1070 VdtTypeError: the value "0" is of the wrong type.
1071 >>> vtor.check('string(2)', '12')
1073 >>> vtor.check('string(2)', '1')
1074 Traceback (most recent call last):
1075 VdtValueTooShortError: the value "1" is too short.
1076 >>> vtor.check('string(min=2, max=3)', '123')
1078 >>> vtor.check('string(min=2, max=3)', '1234')
1079 Traceback (most recent call last):
1080 VdtValueTooLongError: the value "1234" is too long.
1082 if not isinstance(value, basestring):
1083 raise VdtTypeError(value)
1084 (min_len, max_len) = _is_num_param(('min', 'max'), (min, max))
1086 num_members = len(value)
1088 raise VdtTypeError(value)
1089 if min_len is not None and num_members < min_len:
1090 raise VdtValueTooShortError(value)
1091 if max_len is not None and num_members > max_len:
1092 raise VdtValueTooLongError(value)
1096 def is_int_list(value, min=None, max=None):
1098 Check that the value is a list of integers.
1100 You can optionally specify the minimum and maximum number of members.
1102 Each list member is checked that it is an integer.
1104 >>> vtor.check('int_list', ())
1106 >>> vtor.check('int_list', [])
1108 >>> vtor.check('int_list', (1, 2))
1110 >>> vtor.check('int_list', [1, 2])
1112 >>> vtor.check('int_list', [1, 'a'])
1113 Traceback (most recent call last):
1114 VdtTypeError: the value "a" is of the wrong type.
1116 return [is_integer(mem) for mem in is_list(value, min, max)]
1119 def is_bool_list(value, min=None, max=None):
1121 Check that the value is a list of booleans.
1123 You can optionally specify the minimum and maximum number of members.
1125 Each list member is checked that it is a boolean.
1127 >>> vtor.check('bool_list', ())
1129 >>> vtor.check('bool_list', [])
1131 >>> check_res = vtor.check('bool_list', (True, False))
1132 >>> check_res == [True, False]
1134 >>> check_res = vtor.check('bool_list', [True, False])
1135 >>> check_res == [True, False]
1137 >>> vtor.check('bool_list', [True, 'a'])
1138 Traceback (most recent call last):
1139 VdtTypeError: the value "a" is of the wrong type.
1141 return [is_boolean(mem) for mem in is_list(value, min, max)]
1144 def is_float_list(value, min=None, max=None):
1146 Check that the value is a list of floats.
1148 You can optionally specify the minimum and maximum number of members.
1150 Each list member is checked that it is a float.
1152 >>> vtor.check('float_list', ())
1154 >>> vtor.check('float_list', [])
1156 >>> vtor.check('float_list', (1, 2.0))
1158 >>> vtor.check('float_list', [1, 2.0])
1160 >>> vtor.check('float_list', [1, 'a'])
1161 Traceback (most recent call last):
1162 VdtTypeError: the value "a" is of the wrong type.
1164 return [is_float(mem) for mem in is_list(value, min, max)]
1167 def is_string_list(value, min=None, max=None):
1169 Check that the value is a list of strings.
1171 You can optionally specify the minimum and maximum number of members.
1173 Each list member is checked that it is a string.
1175 >>> vtor.check('string_list', ())
1177 >>> vtor.check('string_list', [])
1179 >>> vtor.check('string_list', ('a', 'b'))
1181 >>> vtor.check('string_list', ['a', 1])
1182 Traceback (most recent call last):
1183 VdtTypeError: the value "1" is of the wrong type.
1184 >>> vtor.check('string_list', 'hello')
1185 Traceback (most recent call last):
1186 VdtTypeError: the value "hello" is of the wrong type.
1188 if isinstance(value, basestring):
1189 raise VdtTypeError(value)
1190 return [is_string(mem) for mem in is_list(value, min, max)]
1193 def is_ip_addr_list(value, min=None, max=None):
1195 Check that the value is a list of IP addresses.
1197 You can optionally specify the minimum and maximum number of members.
1199 Each list member is checked that it is an IP address.
1201 >>> vtor.check('ip_addr_list', ())
1203 >>> vtor.check('ip_addr_list', [])
1205 >>> vtor.check('ip_addr_list', ('1.2.3.4', '5.6.7.8'))
1206 ['1.2.3.4', '5.6.7.8']
1207 >>> vtor.check('ip_addr_list', ['a'])
1208 Traceback (most recent call last):
1209 VdtValueError: the value "a" is unacceptable.
1211 return [is_ip_addr(mem) for mem in is_list(value, min, max)]
1214 def force_list(value, min=None, max=None):
1216 Check that a value is a list, coercing strings into
1217 a list with one member. Useful where users forget the
1218 trailing comma that turns a single value into a list.
1220 You can optionally specify the minimum and maximum number of members.
1221 A minumum of greater than one will fail if the user only supplies a
1224 >>> vtor.check('force_list', ())
1226 >>> vtor.check('force_list', [])
1228 >>> vtor.check('force_list', 'hello')
1231 if not isinstance(value, (list, tuple)):
1233 return is_list(value, min, max)
1238 'integer': is_integer,
1240 'ip_addr': is_ip_addr,
1241 'string': is_string,
1242 'boolean': is_boolean,
1246 def is_mixed_list(value, *args):
1248 Check that the value is a list.
1249 Allow specifying the type of each member.
1250 Work on lists of specific lengths.
1252 You specify each member as a positional argument specifying type
1254 Each type should be one of the following strings :
1255 'integer', 'float', 'ip_addr', 'string', 'boolean'
1257 So you can specify a list of two strings, followed by
1260 mixed_list('string', 'string', 'integer', 'integer')
1262 The length of the list must match the number of positional
1263 arguments you supply.
1265 >>> mix_str = "mixed_list('integer', 'float', 'ip_addr', 'string', 'boolean')"
1266 >>> check_res = vtor.check(mix_str, (1, 2.0, '1.2.3.4', 'a', True))
1267 >>> check_res == [1, 2.0, '1.2.3.4', 'a', True]
1269 >>> check_res = vtor.check(mix_str, ('1', '2.0', '1.2.3.4', 'a', 'True'))
1270 >>> check_res == [1, 2.0, '1.2.3.4', 'a', True]
1272 >>> vtor.check(mix_str, ('b', 2.0, '1.2.3.4', 'a', True))
1273 Traceback (most recent call last):
1274 VdtTypeError: the value "b" is of the wrong type.
1275 >>> vtor.check(mix_str, (1, 2.0, '1.2.3.4', 'a'))
1276 Traceback (most recent call last):
1277 VdtValueTooShortError: the value "(1, 2.0, '1.2.3.4', 'a')" is too short.
1278 >>> vtor.check(mix_str, (1, 2.0, '1.2.3.4', 'a', 1, 'b'))
1279 Traceback (most recent call last):
1280 VdtValueTooLongError: the value "(1, 2.0, '1.2.3.4', 'a', 1, 'b')" is too long.
1281 >>> vtor.check(mix_str, 0)
1282 Traceback (most recent call last):
1283 VdtTypeError: the value "0" is of the wrong type.
1285 This test requires an elaborate setup, because of a change in error string
1286 output from the interpreter between Python 2.2 and 2.3 .
1289 ... 'passed an incorrect value "',
1291 ... '" for parameter "mixed_list".',
1293 >>> res_str = "'".join(res_seq)
1295 ... vtor.check('mixed_list("yoda")', ('a'))
1296 ... except VdtParamError, err:
1297 ... str(err) == res_str
1303 raise VdtTypeError(value)
1304 if length < len(args):
1305 raise VdtValueTooShortError(value)
1306 elif length > len(args):
1307 raise VdtValueTooLongError(value)
1309 return [fun_dict[arg](val) for arg, val in zip(args, value)]
1311 raise VdtParamError('mixed_list', e)
1314 def is_option(value, *options):
1316 This check matches the value to any of a set of options.
1318 >>> vtor.check('option("yoda", "jedi")', 'yoda')
1320 >>> vtor.check('option("yoda", "jedi")', 'jed')
1321 Traceback (most recent call last):
1322 VdtValueError: the value "jed" is unacceptable.
1323 >>> vtor.check('option("yoda", "jedi")', 0)
1324 Traceback (most recent call last):
1325 VdtTypeError: the value "0" is of the wrong type.
1327 if not isinstance(value, basestring):
1328 raise VdtTypeError(value)
1329 if not value in options:
1330 raise VdtValueError(value)
1334 def _test(value, *args, **keywargs):
1336 A function that exists for test purposes.
1339 ... '3, 6, min=1, max=3, test=list(a, b, c)',
1343 ... 'min=1, test="a b c"',
1344 ... 'min=5, test="a, b, c"',
1345 ... 'min=1, max=3, test="a, b, c"',
1346 ... 'min=-100, test=-99',
1348 ... '3, 6, test="36"',
1349 ... '3, 6, test="a, b, c"',
1350 ... '3, max=3, test=list("a", "b", "c")',
1351 ... '''3, max=3, test=list("'a'", 'b', "x=(c)")''',
1352 ... "test='x=fish(3)'",
1354 >>> v = Validator({'test': _test})
1355 >>> for entry in checks:
1356 ... print v.check(('test(%s)' % entry), 3)
1357 (3, ('3', '6'), {'test': ['a', 'b', 'c'], 'max': '3', 'min': '1'})
1361 (3, (), {'test': 'a b c', 'min': '1'})
1362 (3, (), {'test': 'a, b, c', 'min': '5'})
1363 (3, (), {'test': 'a, b, c', 'max': '3', 'min': '1'})
1364 (3, (), {'test': '-99', 'min': '-100'})
1365 (3, (), {'max': '3', 'min': '1'})
1366 (3, ('3', '6'), {'test': '36'})
1367 (3, ('3', '6'), {'test': 'a, b, c'})
1368 (3, ('3',), {'test': ['a', 'b', 'c'], 'max': '3'})
1369 (3, ('3',), {'test': ["'a'", 'b', 'x=(c)'], 'max': '3'})
1370 (3, (), {'test': 'x=fish(3)'})
1373 >>> v.check('integer(default=6)', '3')
1375 >>> v.check('integer(default=6)', None, True)
1377 >>> v.get_default_value('integer(default=6)')
1379 >>> v.get_default_value('float(default=6)')
1381 >>> v.get_default_value('pass(default=None)')
1382 >>> v.get_default_value("string(default='None')")
1384 >>> v.get_default_value('pass')
1385 Traceback (most recent call last):
1386 KeyError: 'Check "pass" has no default value.'
1387 >>> v.get_default_value('pass(default=list(1, 2, 3, 4))')
1388 ['1', '2', '3', '4']
1391 >>> v.check("pass(default=None)", None, True)
1392 >>> v.check("pass(default='None')", None, True)
1394 >>> v.check('pass(default="None")', None, True)
1396 >>> v.check('pass(default=list(1, 2, 3, 4))', None, True)
1397 ['1', '2', '3', '4']
1399 Bug test for unicode arguments
1401 >>> v.check(u'string(min=4)', u'test')
1405 >>> v.get_default_value(u'string(min=4, default="1234")')
1407 >>> v.check(u'string(min=4, default="1234")', u'test')
1411 >>> default = v.get_default_value('string(default=None)')
1415 return (value, args, keywargs)
1422 >>> v.get_default_value('string(default="#ff00dd")')
1424 >>> v.get_default_value('integer(default=3) # comment')
1430 >>> vtor.check('string(default="")', '', missing=True)
1432 >>> vtor.check('string(default="\n")', '', missing=True)
1434 >>> print vtor.check('string(default="\n")', '', missing=True),
1436 >>> vtor.check('string()', '\n')
1438 >>> vtor.check('string(default="\n\n\n")', '', missing=True)
1440 >>> vtor.check('string()', 'random \n text goes here\n\n')
1441 'random \n text goes here\n\n'
1442 >>> vtor.check('string(default=" \nrandom text\ngoes \n here\n\n ")',
1443 ... '', missing=True)
1444 ' \nrandom text\ngoes \n here\n\n '
1445 >>> vtor.check("string(default='\n\n\n')", '', missing=True)
1447 >>> vtor.check("option('\n','a','b',default='\n')", '', missing=True)
1449 >>> vtor.check("string_list()", ['foo', '\n', 'bar'])
1450 ['foo', '\n', 'bar']
1451 >>> vtor.check("string_list(default=list('\n'))", '', missing=True)
1456 if __name__ == '__main__':
1457 # run the code tests in doctest format
1460 m = sys.modules.get('__main__')
1461 globs = m.__dict__.copy()
1463 'vtor': Validator(),
1465 doctest.testmod(m, globs=globs)