OSDN Git Service

updated jtalk dic binary
[nvdajp/nvdajpmiscdep.git] / source / validate.py
1 # validate.py
2 # A Validator object
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
7
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.
12
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.
18
19 """
20     The Validator object is used to check that supplied values 
21     conform to a specification.
22     
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.
29     
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
33     added afterwards.
34     
35     The standard functions work with the following basic data types :
36     
37     * integers
38     * floats
39     * booleans
40     * strings
41     * ip_addr
42     
43     plus lists of these datatypes
44     
45     Adding additional checks is done through coding simple functions.
46     
47     The full set of standard checks are : 
48     
49     * 'integer': matches integer values (including negative)
50                  Takes optional 'min' and 'max' arguments : ::
51     
52                    integer()
53                    integer(3, 9)  # any value from 3 to 9
54                    integer(min=0) # any positive value
55                    integer(max=9)
56     
57     * 'float': matches float values
58                Has the same parameters as the integer check.
59     
60     * 'boolean': matches boolean values - ``True`` or ``False``
61                  Acceptable string values for True are :
62                    true, on, yes, 1
63                  Acceptable string values for False are :
64                    false, off, no, 0
65     
66                  Any other value raises an error.
67     
68     * 'ip_addr': matches an Internet Protocol address, v.4, represented
69                  by a dotted-quad string, i.e. '1.2.3.4'.
70     
71     * 'string': matches any string.
72                 Takes optional keyword args 'min' and 'max'
73                 to specify min and max lengths of the string.
74     
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.)
78     
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.)
82     
83     * 'int_list': Matches a list of integers.
84                   Takes the same arguments as list.
85     
86     * 'float_list': Matches a list of floats.
87                     Takes the same arguments as list.
88     
89     * 'bool_list': Matches a list of boolean values.
90                    Takes the same arguments as list.
91     
92     * 'ip_addr_list': Matches a list of IP addresses.
93                      Takes the same arguments as list.
94     
95     * 'string_list': Matches a list of strings.
96                      Takes the same arguments as list.
97     
98     * 'mixed_list': Matches a list with different types in 
99                     specific positions. List size must match
100                     the number of arguments.
101     
102                     Each position can be one of :
103                     'integer', 'float', 'ip_addr', 'string', 'boolean'
104     
105                     So to specify a list with two strings followed
106                     by two integers, you write the check as : ::
107     
108                       mixed_list('string', 'string', 'integer', 'integer')
109     
110     * 'pass': This check matches everything ! It never fails
111               and the value is unchanged.
112     
113               It is also the default if no check is specified.
114     
115     * 'option': This check matches any from a list of options.
116                 You specify this check with : ::
117     
118                   option('option 1', 'option 2', 'option 3')
119     
120     You can supply a default value (returned if no value is supplied)
121     using the default keyword argument.
122     
123     You specify a list argument for default using a list constructor syntax in
124     the check : ::
125     
126         checkname(arg1, arg2, default=list('val 1', 'val 2', 'val 3'))
127     
128     A badly formatted set of arguments will raise a ``VdtParamError``.
129 """
130
131 __docformat__ = "restructuredtext en"
132
133 __version__ = '1.0.0'
134
135 __revision__ = '$Id: validate.py 123 2005-09-08 08:54:28Z fuzzyman $'
136
137 __all__ = (
138     '__version__',
139     'dottedQuadToNum',
140     'numToDottedQuad',
141     'ValidateError',
142     'VdtUnknownCheckError',
143     'VdtParamError',
144     'VdtTypeError',
145     'VdtValueError',
146     'VdtValueTooSmallError',
147     'VdtValueTooBigError',
148     'VdtValueTooShortError',
149     'VdtValueTooLongError',
150     'VdtMissingValue',
151     'Validator',
152     'is_integer',
153     'is_float',
154     'is_boolean',
155     'is_list',
156     'is_tuple',
157     'is_ip_addr',
158     'is_string',
159     'is_int_list',
160     'is_bool_list',
161     'is_float_list',
162     'is_string_list',
163     'is_ip_addr_list',
164     'is_mixed_list',
165     'is_option',
166     '__docformat__',
167 )
168
169
170 import re
171
172
173 _list_arg = re.compile(r'''
174     (?:
175         ([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*list\(
176             (
177                 (?:
178                     \s*
179                     (?:
180                         (?:".*?")|              # double quotes
181                         (?:'.*?')|              # single quotes
182                         (?:[^'",\s\)][^,\)]*?)  # unquoted
183                     )
184                     \s*,\s*
185                 )*
186                 (?:
187                     (?:".*?")|              # double quotes
188                     (?:'.*?')|              # single quotes
189                     (?:[^'",\s\)][^,\)]*?)  # unquoted
190                 )?                          # last one
191             )
192         \)
193     )
194 ''', re.VERBOSE | re.DOTALL)    # two groups
195
196 _list_members = re.compile(r'''
197     (
198         (?:".*?")|              # double quotes
199         (?:'.*?')|              # single quotes
200         (?:[^'",\s=][^,=]*?)       # unquoted
201     )
202     (?:
203     (?:\s*,\s*)|(?:\s*$)            # comma
204     )
205 ''', re.VERBOSE | re.DOTALL)    # one group
206
207 _paramstring = r'''
208     (?:
209         (
210             (?:
211                 [a-zA-Z_][a-zA-Z0-9_]*\s*=\s*list\(
212                     (?:
213                         \s*
214                         (?:
215                             (?:".*?")|              # double quotes
216                             (?:'.*?')|              # single quotes
217                             (?:[^'",\s\)][^,\)]*?)       # unquoted
218                         )
219                         \s*,\s*
220                     )*
221                     (?:
222                         (?:".*?")|              # double quotes
223                         (?:'.*?')|              # single quotes
224                         (?:[^'",\s\)][^,\)]*?)       # unquoted
225                     )?                              # last one
226                 \)
227             )|
228             (?:
229                 (?:".*?")|              # double quotes
230                 (?:'.*?')|              # single quotes
231                 (?:[^'",\s=][^,=]*?)|       # unquoted
232                 (?:                         # keyword argument
233                     [a-zA-Z_][a-zA-Z0-9_]*\s*=\s*
234                     (?:
235                         (?:".*?")|              # double quotes
236                         (?:'.*?')|              # single quotes
237                         (?:[^'",\s=][^,=]*?)       # unquoted
238                     )
239                 )
240             )
241         )
242         (?:
243             (?:\s*,\s*)|(?:\s*$)            # comma
244         )
245     )
246     '''
247
248 _matchstring = '^%s*' % _paramstring
249
250 # Python pre 2.2.1 doesn't have bool
251 try:
252     bool
253 except NameError:
254     def bool(val):
255         """Simple boolean equivalent function. """
256         if val:
257             return 1
258         else:
259             return 0
260
261
262 def dottedQuadToNum(ip):
263     """
264     Convert decimal dotted quad string to long integer
265     
266     >>> int(dottedQuadToNum('1 '))
267     1
268     >>> int(dottedQuadToNum(' 1.2'))
269     16777218
270     >>> int(dottedQuadToNum(' 1.2.3 '))
271     16908291
272     >>> int(dottedQuadToNum('1.2.3.4'))
273     16909060
274     >>> dottedQuadToNum('1.2.3. 4')
275     16909060
276     >>> dottedQuadToNum('255.255.255.255')
277     4294967295L
278     >>> dottedQuadToNum('255.255.255.256')
279     Traceback (most recent call last):
280     ValueError: Not a good dotted-quad IP: 255.255.255.256
281     """
282     
283     # import here to avoid it when ip_addr values are not used
284     import socket, struct
285     
286     try:
287         return struct.unpack('!L',
288             socket.inet_aton(ip.strip()))[0]
289     except socket.error:
290         # bug in inet_aton, corrected in Python 2.3
291         if ip.strip() == '255.255.255.255':
292             return 0xFFFFFFFFL
293         else:
294             raise ValueError('Not a good dotted-quad IP: %s' % ip)
295     return
296
297
298 def numToDottedQuad(num):
299     """
300     Convert long int to dotted quad string
301     
302     >>> numToDottedQuad(-1L)
303     Traceback (most recent call last):
304     ValueError: Not a good numeric IP: -1
305     >>> numToDottedQuad(1L)
306     '0.0.0.1'
307     >>> numToDottedQuad(16777218L)
308     '1.0.0.2'
309     >>> numToDottedQuad(16908291L)
310     '1.2.0.3'
311     >>> numToDottedQuad(16909060L)
312     '1.2.3.4'
313     >>> numToDottedQuad(4294967295L)
314     '255.255.255.255'
315     >>> numToDottedQuad(4294967296L)
316     Traceback (most recent call last):
317     ValueError: Not a good numeric IP: 4294967296
318     """
319     
320     # import here to avoid it when ip_addr values are not used
321     import socket, struct
322     
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)
326     try:
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)
331
332
333 class ValidateError(Exception):
334     """
335     This error indicates that the check failed.
336     It can be the base class for more specific errors.
337     
338     Any check function that fails ought to raise this error.
339     (or a subclass)
340     
341     >>> raise ValidateError
342     Traceback (most recent call last):
343     ValidateError
344     """
345
346
347 class VdtMissingValue(ValidateError):
348     """No value was supplied to a check that needed one."""
349
350
351 class VdtUnknownCheckError(ValidateError):
352     """An unknown check function was requested"""
353
354     def __init__(self, value):
355         """
356         >>> raise VdtUnknownCheckError('yoda')
357         Traceback (most recent call last):
358         VdtUnknownCheckError: the check "yoda" is unknown.
359         """
360         ValidateError.__init__(self, 'the check "%s" is unknown.' % (value,))
361
362
363 class VdtParamError(SyntaxError):
364     """An incorrect parameter was passed"""
365
366     def __init__(self, name, value):
367         """
368         >>> raise VdtParamError('yoda', 'jedi')
369         Traceback (most recent call last):
370         VdtParamError: passed an incorrect value "jedi" for parameter "yoda".
371         """
372         SyntaxError.__init__(self, 'passed an incorrect value "%s" for parameter "%s".' % (value, name))
373
374
375 class VdtTypeError(ValidateError):
376     """The value supplied was of the wrong type"""
377
378     def __init__(self, value):
379         """
380         >>> raise VdtTypeError('jedi')
381         Traceback (most recent call last):
382         VdtTypeError: the value "jedi" is of the wrong type.
383         """
384         ValidateError.__init__(self, 'the value "%s" is of the wrong type.' % (value,))
385
386
387 class VdtValueError(ValidateError):
388     """The value supplied was of the correct type, but was not an allowed value."""
389     
390     def __init__(self, value):
391         """
392         >>> raise VdtValueError('jedi')
393         Traceback (most recent call last):
394         VdtValueError: the value "jedi" is unacceptable.
395         """
396         ValidateError.__init__(self, 'the value "%s" is unacceptable.' % (value,))
397
398
399 class VdtValueTooSmallError(VdtValueError):
400     """The value supplied was of the correct type, but was too small."""
401
402     def __init__(self, value):
403         """
404         >>> raise VdtValueTooSmallError('0')
405         Traceback (most recent call last):
406         VdtValueTooSmallError: the value "0" is too small.
407         """
408         ValidateError.__init__(self, 'the value "%s" is too small.' % (value,))
409
410
411 class VdtValueTooBigError(VdtValueError):
412     """The value supplied was of the correct type, but was too big."""
413
414     def __init__(self, value):
415         """
416         >>> raise VdtValueTooBigError('1')
417         Traceback (most recent call last):
418         VdtValueTooBigError: the value "1" is too big.
419         """
420         ValidateError.__init__(self, 'the value "%s" is too big.' % (value,))
421
422
423 class VdtValueTooShortError(VdtValueError):
424     """The value supplied was of the correct type, but was too short."""
425
426     def __init__(self, value):
427         """
428         >>> raise VdtValueTooShortError('jed')
429         Traceback (most recent call last):
430         VdtValueTooShortError: the value "jed" is too short.
431         """
432         ValidateError.__init__(
433             self,
434             'the value "%s" is too short.' % (value,))
435
436
437 class VdtValueTooLongError(VdtValueError):
438     """The value supplied was of the correct type, but was too long."""
439
440     def __init__(self, value):
441         """
442         >>> raise VdtValueTooLongError('jedie')
443         Traceback (most recent call last):
444         VdtValueTooLongError: the value "jedie" is too long.
445         """
446         ValidateError.__init__(self, 'the value "%s" is too long.' % (value,))
447
448
449 class Validator(object):
450     """
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.
453     
454     This can also involve converting the value from a string into
455     the correct datatype.
456     
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.
459     
460     An example input string would be:
461     'int_range(param1, param2)'
462     
463     You would then provide something like:
464     
465     >>> def int_range_check(value, min, max):
466     ...     # turn min and max from strings to integers
467     ...     min = int(min)
468     ...     max = int(max)
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
477     ...         try:
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)
486     ...     return value
487     
488     >>> fdict = {'int_range': int_range_check}
489     >>> vtr1 = Validator(fdict)
490     >>> vtr1.check('int_range(20, 40)', '30')
491     30
492     >>> vtr1.check('int_range(20, 40)', '60')
493     Traceback (most recent call last):
494     VdtValueTooBigError: the value "60" is too big.
495     
496     New functions can be added with : ::
497     
498     >>> vtr2 = Validator()       
499     >>> vtr2.functions['int_range'] = int_range_check
500     
501     Or by passing in a dictionary of functions when Validator 
502     is instantiated.
503     
504     Your functions *can* use keyword arguments,
505     but the first argument should always be 'value'.
506     
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 : ::
510     
511         keyword = function_name
512         keyword = function_name()
513     
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
519     """
520
521     # this regex does the initial parsing of the checks
522     _func_re = re.compile(r'(.+?)\((.*)\)', re.DOTALL)
523
524     # this regex takes apart keyword arguments
525     _key_arg = re.compile(r'^([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*(.*)$',  re.DOTALL)
526
527
528     # this regex finds keyword=list(....) type values
529     _list_arg = _list_arg
530
531     # this regex takes individual values out of lists - in one pass
532     _list_members = _list_members
533
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)
538
539
540     def __init__(self, functions=None):
541         """
542         >>> vtri = Validator()
543         """
544         self.functions = {
545             '': self._pass,
546             'integer': is_integer,
547             'float': is_float,
548             'boolean': is_boolean,
549             'ip_addr': is_ip_addr,
550             'string': is_string,
551             'list': is_list,
552             'tuple': is_tuple,
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,
559             'pass': self._pass,
560             'option': is_option,
561             'force_list': force_list,
562         }
563         if functions is not None:
564             self.functions.update(functions)
565         # tekNico: for use by ConfigObj
566         self.baseErrorClass = ValidateError
567         self._cache = {}
568
569
570     def check(self, check, value, missing=False):
571         """
572         Usage: check(check, value)
573         
574         Arguments:
575             check: string representing check to apply (including arguments)
576             value: object to be checked
577         Returns value, converted to correct type if necessary
578         
579         If the check fails, raises a ``ValidateError`` subclass.
580         
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.
587         
588         >>> vtor.check('string(default="")', '', missing=True)
589         ''
590         """
591         fun_name, fun_args, fun_kwargs, default = self._parse_with_caching(check)
592             
593         if missing:
594             if default is None:
595                 # no information needed here - to be handled by caller
596                 raise VdtMissingValue()
597             value = self._handle_none(default)
598                 
599         if value is None:
600             return None
601         
602         return self._check_value(value, fun_name, fun_args, fun_kwargs)
603
604
605     def _handle_none(self, value):
606         if value == 'None':
607             value = None
608         elif value in ("'None'", '"None"'):
609             # Special case a quoted None
610             value = self._unquote(value)
611         return value
612
613
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)
621         else:
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
626         
627         
628     def _check_value(self, value, fun_name, fun_args, fun_kwargs):
629         try:
630             fun = self.functions[fun_name]
631         except KeyError:
632             raise VdtUnknownCheckError(fun_name)
633         else:
634             return fun(value, *fun_args, **fun_kwargs)
635
636
637     def _parse_check(self, check):
638         fun_match = self._func_re.match(check)
639         if fun_match:
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:
644                 # Bad syntax
645                 raise VdtParamError('Bad syntax in check "%s".' % check)
646             fun_args = []
647             fun_kwargs = {}
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)
651                 arg = arg.strip()
652                 listmatch = self._list_arg.match(arg)
653                 if listmatch:
654                     key, val = self._list_handle(listmatch)
655                     fun_kwargs[key] = val
656                     continue
657                 keymatch = self._key_arg.match(arg)
658                 if keymatch:
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
664                     continue
665                 
666                 fun_args.append(self._unquote(arg))
667         else:
668             # allows for function names without (args)
669             return check, (), {}, None
670
671         # Default must be deleted if the value is specified too,
672         # otherwise the check function will get a spurious "default" keyword arg
673         try:
674             default = fun_kwargs.pop('default', None)
675         except AttributeError:
676             # Python 2.2 compatibility
677             default = None
678             try:
679                 default = fun_kwargs['default']
680                 del fun_kwargs['default']
681             except KeyError:
682                 pass
683             
684         return fun_name, fun_args, fun_kwargs, default
685
686
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]):
690             val = val[1:-1]
691         return val
692
693
694     def _list_handle(self, listmatch):
695         """Take apart a ``keyword=list('val, 'val')`` type string."""
696         out = []
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))
701         return name, out
702
703
704     def _pass(self, value):
705         """
706         Dummy check that always passes
707         
708         >>> vtor.check('', 0)
709         0
710         >>> vtor.check('', '0')
711         '0'
712         """
713         return value
714     
715     
716     def get_default_value(self, check):
717         """
718         Given a check, return the default value for the check
719         (converted to the right type).
720         
721         If the check doesn't specify a default value then a
722         ``KeyError`` will be raised.
723         """
724         fun_name, fun_args, fun_kwargs, default = self._parse_with_caching(check)
725         if default is None:
726             raise KeyError('Check "%s" has no default value.' % check)
727         value = self._handle_none(default)
728         if value is None:
729             return value
730         return self._check_value(value, fun_name, fun_args, fun_kwargs)
731
732
733 def _is_num_param(names, values, to_float=False):
734     """
735     Return numbers from inputs or raise VdtParamError.
736     
737     Lets ``None`` pass through.
738     Pass in keyword argument ``to_float=True`` to
739     use float for the conversion rather than int.
740     
741     >>> _is_num_param(('', ''), (0, 1.0))
742     [0, 1]
743     >>> _is_num_param(('', ''), (0, 1.0), to_float=True)
744     [0.0, 1.0]
745     >>> _is_num_param(('a'), ('a'))
746     Traceback (most recent call last):
747     VdtParamError: passed an incorrect value "a" for parameter "a".
748     """
749     fun = to_float and float or int
750     out_params = []
751     for (name, val) in zip(names, values):
752         if val is None:
753             out_params.append(val)
754         elif isinstance(val, (int, long, float, basestring)):
755             try:
756                 out_params.append(fun(val))
757             except ValueError, e:
758                 raise VdtParamError(name, val)
759         else:
760             raise VdtParamError(name, val)
761     return out_params
762
763
764 # built in checks
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.
769
770 def is_integer(value, min=None, max=None):
771     """
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
774     a float will fail.
775     
776     If the value is a string, then the conversion is done - if possible.
777     Otherwise a VdtError is raised.
778     
779     >>> vtor.check('integer', '-1')
780     -1
781     >>> vtor.check('integer', '0')
782     0
783     >>> vtor.check('integer', 9)
784     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')
792     20
793     >>> vtor.check('integer(max=20)', '15')
794     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)
808     0
809     """
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 ?
815         try:
816             value = int(value)
817         except ValueError:
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)
823     return value
824
825
826 def is_float(value, min=None, max=None):
827     """
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.
830     
831     If the value is a string, then the conversion is done - if possible.
832     Otherwise a VdtError is raised.
833     
834     This can accept negative values.
835     
836     >>> vtor.check('float', '2')
837     2.0
838     
839     From now on we multiply the value to avoid comparing decimals
840     
841     >>> vtor.check('float', '-6.8') * 10
842     -68.0
843     >>> vtor.check('float', '12.2') * 10
844     122.0
845     >>> vtor.check('float', 8.4) * 10
846     84.0
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
851     102.0
852     >>> vtor.check('float(max=20.2)', '15.1') * 10
853     151.0
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.
860     """
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 ?
867         try:
868             value = float(value)
869         except ValueError:
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)
875     return value
876
877
878 bool_dict = {
879     True: True, 'on': True, '1': True, 'true': True, 'yes': True, 
880     False: False, 'off': False, '0': False, 'false': False, 'no': False,
881 }
882
883
884 def is_boolean(value):
885     """
886     Check if the value represents a boolean.
887     
888     >>> vtor.check('boolean', 0)
889     0
890     >>> vtor.check('boolean', False)
891     0
892     >>> vtor.check('boolean', '0')
893     0
894     >>> vtor.check('boolean', 'off')
895     0
896     >>> vtor.check('boolean', 'false')
897     0
898     >>> vtor.check('boolean', 'no')
899     0
900     >>> vtor.check('boolean', 'nO')
901     0
902     >>> vtor.check('boolean', 'NO')
903     0
904     >>> vtor.check('boolean', 1)
905     1
906     >>> vtor.check('boolean', True)
907     1
908     >>> vtor.check('boolean', '1')
909     1
910     >>> vtor.check('boolean', 'on')
911     1
912     >>> vtor.check('boolean', 'true')
913     1
914     >>> vtor.check('boolean', 'yes')
915     1
916     >>> vtor.check('boolean', 'Yes')
917     1
918     >>> vtor.check('boolean', 'YES')
919     1
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.
926     
927     """
928     if isinstance(value, basestring):
929         try:
930             return bool_dict[value.lower()]
931         except KeyError:
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
936     if value == False:
937         return False
938     elif value == True:
939         return True
940     else:
941         raise VdtTypeError(value)
942
943
944 def is_ip_addr(value):
945     """
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'.
948     
949     >>> vtor.check('ip_addr', '1 ')
950     '1'
951     >>> vtor.check('ip_addr', ' 1.2')
952     '1.2'
953     >>> vtor.check('ip_addr', ' 1.2.3 ')
954     '1.2.3'
955     >>> vtor.check('ip_addr', '1.2.3.4')
956     '1.2.3.4'
957     >>> vtor.check('ip_addr', '0.0.0.0')
958     '0.0.0.0'
959     >>> vtor.check('ip_addr', '255.255.255.255')
960     '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.
970     """
971     if not isinstance(value, basestring):
972         raise VdtTypeError(value)
973     value = value.strip()
974     try:
975         dottedQuadToNum(value)
976     except ValueError:
977         raise VdtValueError(value)
978     return value
979
980
981 def is_list(value, min=None, max=None):
982     """
983     Check that the value is a list of values.
984     
985     You can optionally specify the minimum and maximum number of members.
986     
987     It does no check on list members.
988     
989     >>> vtor.check('list', ())
990     []
991     >>> vtor.check('list', [])
992     []
993     >>> vtor.check('list', (1, 2))
994     [1, 2]
995     >>> vtor.check('list', [1, 2])
996     [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))
1004     [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.
1011     """
1012     (min_len, max_len) = _is_num_param(('min', 'max'), (min, max))
1013     if isinstance(value, basestring):
1014         raise VdtTypeError(value)
1015     try:
1016         num_members = len(value)
1017     except TypeError:
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)
1023     return list(value)
1024
1025
1026 def is_tuple(value, min=None, max=None):
1027     """
1028     Check that the value is a tuple of values.
1029     
1030     You can optionally specify the minimum and maximum number of members.
1031     
1032     It does no check on members.
1033     
1034     >>> vtor.check('tuple', ())
1035     ()
1036     >>> vtor.check('tuple', [])
1037     ()
1038     >>> vtor.check('tuple', (1, 2))
1039     (1, 2)
1040     >>> vtor.check('tuple', [1, 2])
1041     (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))
1049     (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.
1056     """
1057     return tuple(is_list(value, min, max))
1058
1059
1060 def is_string(value, min=None, max=None):
1061     """
1062     Check that the supplied value is a string.
1063     
1064     You can optionally specify the minimum and maximum number of members.
1065     
1066     >>> vtor.check('string', '0')
1067     '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')
1072     '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')
1077     '123'
1078     >>> vtor.check('string(min=2, max=3)', '1234')
1079     Traceback (most recent call last):
1080     VdtValueTooLongError: the value "1234" is too long.
1081     """
1082     if not isinstance(value, basestring):
1083         raise VdtTypeError(value)
1084     (min_len, max_len) = _is_num_param(('min', 'max'), (min, max))
1085     try:
1086         num_members = len(value)
1087     except TypeError:
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)
1093     return value
1094
1095
1096 def is_int_list(value, min=None, max=None):
1097     """
1098     Check that the value is a list of integers.
1099     
1100     You can optionally specify the minimum and maximum number of members.
1101     
1102     Each list member is checked that it is an integer.
1103     
1104     >>> vtor.check('int_list', ())
1105     []
1106     >>> vtor.check('int_list', [])
1107     []
1108     >>> vtor.check('int_list', (1, 2))
1109     [1, 2]
1110     >>> vtor.check('int_list', [1, 2])
1111     [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.
1115     """
1116     return [is_integer(mem) for mem in is_list(value, min, max)]
1117
1118
1119 def is_bool_list(value, min=None, max=None):
1120     """
1121     Check that the value is a list of booleans.
1122     
1123     You can optionally specify the minimum and maximum number of members.
1124     
1125     Each list member is checked that it is a boolean.
1126     
1127     >>> vtor.check('bool_list', ())
1128     []
1129     >>> vtor.check('bool_list', [])
1130     []
1131     >>> check_res = vtor.check('bool_list', (True, False))
1132     >>> check_res == [True, False]
1133     1
1134     >>> check_res = vtor.check('bool_list', [True, False])
1135     >>> check_res == [True, False]
1136     1
1137     >>> vtor.check('bool_list', [True, 'a'])
1138     Traceback (most recent call last):
1139     VdtTypeError: the value "a" is of the wrong type.
1140     """
1141     return [is_boolean(mem) for mem in is_list(value, min, max)]
1142
1143
1144 def is_float_list(value, min=None, max=None):
1145     """
1146     Check that the value is a list of floats.
1147     
1148     You can optionally specify the minimum and maximum number of members.
1149     
1150     Each list member is checked that it is a float.
1151     
1152     >>> vtor.check('float_list', ())
1153     []
1154     >>> vtor.check('float_list', [])
1155     []
1156     >>> vtor.check('float_list', (1, 2.0))
1157     [1.0, 2.0]
1158     >>> vtor.check('float_list', [1, 2.0])
1159     [1.0, 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.
1163     """
1164     return [is_float(mem) for mem in is_list(value, min, max)]
1165
1166
1167 def is_string_list(value, min=None, max=None):
1168     """
1169     Check that the value is a list of strings.
1170     
1171     You can optionally specify the minimum and maximum number of members.
1172     
1173     Each list member is checked that it is a string.
1174     
1175     >>> vtor.check('string_list', ())
1176     []
1177     >>> vtor.check('string_list', [])
1178     []
1179     >>> vtor.check('string_list', ('a', 'b'))
1180     ['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.
1187     """
1188     if isinstance(value, basestring):
1189         raise VdtTypeError(value)
1190     return [is_string(mem) for mem in is_list(value, min, max)]
1191
1192
1193 def is_ip_addr_list(value, min=None, max=None):
1194     """
1195     Check that the value is a list of IP addresses.
1196     
1197     You can optionally specify the minimum and maximum number of members.
1198     
1199     Each list member is checked that it is an IP address.
1200     
1201     >>> vtor.check('ip_addr_list', ())
1202     []
1203     >>> vtor.check('ip_addr_list', [])
1204     []
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.
1210     """
1211     return [is_ip_addr(mem) for mem in is_list(value, min, max)]
1212
1213
1214 def force_list(value, min=None, max=None):
1215     """
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.
1219     
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
1222     string.
1223     
1224     >>> vtor.check('force_list', ())
1225     []
1226     >>> vtor.check('force_list', [])
1227     []
1228     >>> vtor.check('force_list', 'hello')
1229     ['hello']
1230     """
1231     if not isinstance(value, (list, tuple)):
1232         value = [value]
1233     return is_list(value, min, max)
1234     
1235     
1236
1237 fun_dict = {
1238     'integer': is_integer,
1239     'float': is_float,
1240     'ip_addr': is_ip_addr,
1241     'string': is_string,
1242     'boolean': is_boolean,
1243 }
1244
1245
1246 def is_mixed_list(value, *args):
1247     """
1248     Check that the value is a list.
1249     Allow specifying the type of each member.
1250     Work on lists of specific lengths.
1251     
1252     You specify each member as a positional argument specifying type
1253     
1254     Each type should be one of the following strings :
1255       'integer', 'float', 'ip_addr', 'string', 'boolean'
1256     
1257     So you can specify a list of two strings, followed by
1258     two integers as :
1259     
1260       mixed_list('string', 'string', 'integer', 'integer')
1261     
1262     The length of the list must match the number of positional
1263     arguments you supply.
1264     
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]
1268     1
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]
1271     1
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.
1284     
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 .
1287     
1288     >>> res_seq = (
1289     ...     'passed an incorrect value "',
1290     ...     'yoda',
1291     ...     '" for parameter "mixed_list".',
1292     ... )
1293     >>> res_str = "'".join(res_seq)
1294     >>> try:
1295     ...     vtor.check('mixed_list("yoda")', ('a'))
1296     ... except VdtParamError, err:
1297     ...     str(err) == res_str
1298     1
1299     """
1300     try:
1301         length = len(value)
1302     except TypeError:
1303         raise VdtTypeError(value)
1304     if length < len(args):
1305         raise VdtValueTooShortError(value)
1306     elif length > len(args):
1307         raise VdtValueTooLongError(value)
1308     try:
1309         return [fun_dict[arg](val) for arg, val in zip(args, value)]
1310     except KeyError, e:
1311         raise VdtParamError('mixed_list', e)
1312
1313
1314 def is_option(value, *options):
1315     """
1316     This check matches the value to any of a set of options.
1317     
1318     >>> vtor.check('option("yoda", "jedi")', 'yoda')
1319     '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.
1326     """
1327     if not isinstance(value, basestring):
1328         raise VdtTypeError(value)
1329     if not value in options:
1330         raise VdtValueError(value)
1331     return value
1332
1333
1334 def _test(value, *args, **keywargs):
1335     """
1336     A function that exists for test purposes.
1337     
1338     >>> checks = [
1339     ...     '3, 6, min=1, max=3, test=list(a, b, c)',
1340     ...     '3',
1341     ...     '3, 6',
1342     ...     '3,',
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',
1347     ...     'min=1, max=3',
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)'",
1353     ...    ]
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'})
1358     (3, ('3',), {})
1359     (3, ('3', '6'), {})
1360     (3, ('3',), {})
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)'})
1371     
1372     >>> v = Validator()
1373     >>> v.check('integer(default=6)', '3')
1374     3
1375     >>> v.check('integer(default=6)', None, True)
1376     6
1377     >>> v.get_default_value('integer(default=6)')
1378     6
1379     >>> v.get_default_value('float(default=6)')
1380     6.0
1381     >>> v.get_default_value('pass(default=None)')
1382     >>> v.get_default_value("string(default='None')")
1383     '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']
1389     
1390     >>> v = Validator()
1391     >>> v.check("pass(default=None)", None, True)
1392     >>> v.check("pass(default='None')", None, True)
1393     'None'
1394     >>> v.check('pass(default="None")', None, True)
1395     'None'
1396     >>> v.check('pass(default=list(1, 2, 3, 4))', None, True)
1397     ['1', '2', '3', '4']
1398     
1399     Bug test for unicode arguments
1400     >>> v = Validator()
1401     >>> v.check(u'string(min=4)', u'test')
1402     u'test'
1403     
1404     >>> v = Validator()
1405     >>> v.get_default_value(u'string(min=4, default="1234")')
1406     u'1234'
1407     >>> v.check(u'string(min=4, default="1234")', u'test')
1408     u'test'
1409     
1410     >>> v = Validator()
1411     >>> default = v.get_default_value('string(default=None)')
1412     >>> default == None
1413     1
1414     """
1415     return (value, args, keywargs)
1416
1417
1418 def _test2():
1419     """
1420     >>> 
1421     >>> v = Validator()
1422     >>> v.get_default_value('string(default="#ff00dd")')
1423     '#ff00dd'
1424     >>> v.get_default_value('integer(default=3) # comment')
1425     3
1426     """
1427
1428 def _test3():
1429     r"""
1430     >>> vtor.check('string(default="")', '', missing=True)
1431     ''
1432     >>> vtor.check('string(default="\n")', '', missing=True)
1433     '\n'
1434     >>> print vtor.check('string(default="\n")', '', missing=True),
1435     <BLANKLINE>
1436     >>> vtor.check('string()', '\n')
1437     '\n'
1438     >>> vtor.check('string(default="\n\n\n")', '', missing=True)
1439     '\n\n\n'
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)
1446     '\n\n\n'
1447     >>> vtor.check("option('\n','a','b',default='\n')", '', missing=True)
1448     '\n'
1449     >>> vtor.check("string_list()", ['foo', '\n', 'bar'])
1450     ['foo', '\n', 'bar']
1451     >>> vtor.check("string_list(default=list('\n'))", '', missing=True)
1452     ['\n']
1453     """
1454     
1455     
1456 if __name__ == '__main__':
1457     # run the code tests in doctest format
1458     import sys
1459     import doctest
1460     m = sys.modules.get('__main__')
1461     globs = m.__dict__.copy()
1462     globs.update({
1463         'vtor': Validator(),
1464     })
1465     doctest.testmod(m, globs=globs)