17
17
.. _Mailing List: http://lists.sourceforge.net/lists/listinfo/configobj-develop
18
18
.. _Michael Foord: fuzzyman@voidspace.org.uk
19
19
.. _Nicola Larosa: nico@teknico.net
20
.. _Mark Andrews: mark@la-la.com
22
21
.. _Validate Homepage: http://www.voidspace.org.uk/python/validate.html
23
.. _BSD License: http://www.voidspace.org.uk/documents/BSD-LICENSE.txt
22
.. _BSD License: http://www.voidspace.org.uk/python/license.shtml
26
25
.. contents:: Validate Manual
39
40
Checks are also strings, and are easy to write. One generic system can be used
40
to validate information from different sources, via a single consistent
41
to validate information from different sources via a single consistent
43
44
Checks look like function calls, and map to function calls. They can include
56
57
into every validator. Additional checks are easy to write: they can be provided
57
58
when the ``Validator`` is instantiated, or added afterwards.
59
Validate was primarily written to support ConfigObj_, but was designed to be
60
Validate was primarily written to support ConfigObj_, but is designed to be
60
61
applicable to many other situations.
62
63
For support and bug reports please use the ConfigObj `Mailing List`_.
64
65
.. _ConfigObj: http://www.voidspace.org.uk/python/configobj.html
69
The current version is **0.2.0**, dated 25th August 2005.
71
The current version is **0.3.2**, dated 24th February 2008.
71
73
You can get obtain validate in the following ways :
76
79
* validate.py_ from Voidspace
78
81
* configobj.zip from Voidspace - See the homepage of ConfigObj_ for the latest
79
version and downlaod links.
82
version and download links.
81
84
This contains validate.py and `this document`_. (As well as ConfigObj_ and
82
85
the ConfigObj documentation).
84
87
* The latest development version can be obtained from the `Subversion Repository`_.
89
*configobj.zip* contains `this document`_ and full `API Docs`_, generated
90
automatically by EpyDoc_.
93
*configobj.zip* contains `this document`_.
92
95
* You can view `this document`_ online as the `Validate Homepage`_.
94
* You can also browse the `API Docs`_ online.
101
103
Python Projects`_.
103
105
.. _configobj.py: http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj.py
104
.. _configobj.zip: http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj-4.0.0b4.zip
106
.. _configobj.zip: http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj-4.5.2.zip
105
107
.. _validate.py: http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=validate.py
106
.. _API Docs: http://www.voidspace.org.uk/python/configobj-api/
107
.. _Subversion Repository: http://svn.rest2web.python-hosting.com/branches/configobj4/
108
.. _Subversion Repository: http://svn.pythonutils.python-hosting.com/trunk/pythonutils/
108
109
.. _Sourceforge: http://sourceforge.net/projects/configobj
109
.. _EpyDoc: http://epydoc.sourceforge.net
110
110
.. _pythonutils: http://www.voidspace.org.uk/python/pythonutils.html
111
111
.. _Voidspace Python Projects: http://www.voidspace.org.uk/python
112
112
.. _validate: http://www.voidspace.org.uk/python/validate.html
141
141
:'float': matches float values
142
142
Has the same parameters as the integer check.
144
:'bool': matches boolean values: ``True`` or ``False``.
145
Acceptable string values for True are : ::
144
:'boolean': matches boolean values: ``True`` or ``False``.
145
Acceptable string values for True are : ::
159
159
dotted-quad string, i.e. '1.2.3.4'.
161
161
:'list': matches any list. Takes optional keyword args 'min', and 'max' to
162
specify min and max sizes of the list.
162
specify min and max sizes of the list. The list checks always
165
:'tuple': matches any list. This check returns a tuple rather than a list.
164
167
:'int_list': Matches a list of integers. Takes the same arguments as list.
179
182
Each position can be one of : ::
181
int, str, bool, float, ip_addr
184
int, str, boolean, float, ip_addr
183
186
So to specify a list with two strings followed by two integers,
184
187
you write the check as : ::
203
206
from validate import Validator
205
208
vtor = Validator()
206
newval1 = vtor.check(value1, 'integer')
207
newval2 = vtor.check(value2, 'bool')
209
newval1 = vtor.check('integer', value1)
210
newval2 = vtor.check('boolean', value2)
350
358
check1 = 'integer(default=50)'
351
359
check2 = 'option("val 1", "val 2", "val 3", default="val 1")'
353
assert vtor.check('', check1, missing=True) == 50
354
assert vtor.check('', check2, missing=True) == "val 1"
361
assert vtor.check(check1, '', missing=True) == 50
362
assert vtor.check(check2, '', missing=True) == "val 1"
365
373
that this check contains no useful value when missing, i.e. the value is
366
374
optional, and may be omitted without harm.
379
As of version 0.3.0, if you specify ``default='None'`` (note the quote marks
380
around ``None``) then it will be interpreted as the string ``'None'``.
386
It's possible that you would like your default value to be a list. It's even
387
possible that you will write your own check functions - and would like to pass
388
them keyword arguments as lists from within the check.
390
To avoid confusing syntax with commas and quotes you use a list constructor to
391
specify that keyword arguments are lists. This includes the ``default`` value.
392
This makes checks look something like : ::
394
checkname(default=list('val1', 'val2', 'val3'))
400
``Validator`` instances have a ``get_default_value`` method. It takes a ``check`` string
401
(the same string you would pass to the ``check`` method) and returns the default value,
402
converted to the right type. If the check doesn't define a default value then this method
403
raises a ``KeyError``.
405
If the ``check`` has been seen before then it will have been parsed and cached already,
406
so this method is not expensive to call (however the conversion is done each time).
368
410
Validator Exceptions
369
411
====================
534
578
If you are using validate in another circumstance you may want to create your
535
579
own subclasses of ``ValidateError``, that convey more specific information.
585
The following parses and then blows up. The resulting error message
588
``checkname(default=list(1, 2, 3, 4)``
590
This is because it parses as: ``checkname(default="list(1", 2, 3, 4)``.
591
That isn't actually unreasonable, but the error message won't help you
592
work out what has happened.
540
598
* A regex check function ?
541
* A timestamp check function ? (Using the ``parse`` function from ``DateUtil``).
542
* Allow triple quotes ? (getting a bit heavy for a single regex)
599
* A timestamp check function ? (Using the ``parse`` function from ``DateUtil`` perhaps).
549
607
Please file any bug reports to `Michael Foord`_ or the ConfigObj
552
Lists passed as function arguments need additional quotes. Ugly, could do
555
610
If we could pull tuples out of arguments, it would be easier
556
611
to specify arguments for 'mixed_lists'.
617
2008/02/24 - Version 0.3.2
618
--------------------------
620
BUGFIX: Handling of None as default value fixed.
623
2008/02/05 - Version 0.3.1
624
--------------------------
626
BUGFIX: Unicode checks no longer broken.
629
2008/02/05 - Version 0.3.0
630
--------------------------
632
Improved performance with a parse cache.
634
New ``get_default_value`` method. Given a check it returns the default
635
value (converted to the correct type) or raises a ``KeyError`` if the
636
check doesn't specify a default.
638
Added 'tuple' check and corresponding 'is_tuple' function (which always returns a tuple).
640
BUGFIX: A quoted 'None' as a default value is no longer treated as None,
641
but as the string 'None'.
643
BUGFIX: We weren't unquoting keyword arguments of length two, so an
644
empty string didn't work as a default.
646
BUGFIX: Strings no longer pass the 'is_list' check. Additionally, the
647
list checks always return lists.
649
A couple of documentation bug fixes.
651
Removed CHANGELOG from module.
654
2007/02/04 Version 0.2.3
655
-----------------------------
660
2006/12/17 Version 0.2.3-alpha1
661
------------------------------------
665
Fixed validate doc to talk of ``boolean`` instead of ``bool``; changed the
666
``is_bool`` function to ``is_boolean`` (Sourceforge bug #1531525).
669
2006/04/29 Version 0.2.2
670
-----------------------------
672
Addressed bug where a string would pass the ``is_list`` test. (Thanks to
676
2005/12/16 Version 0.2.1
677
-----------------------------
679
Fixed bug so we can handle keyword argument values with commas.
681
We now use a list constructor for passing list values to keyword arguments
682
(including ``default``) : ::
684
default=list("val", "val", "val")
686
Added the ``_test`` test. {sm;:-)}
688
Moved a function call outside a try...except block.
562
691
2005/08/18 Version 0.2.0
563
692
-----------------------------
578
708
textmacros module and the PySrc CSS stuff. See
579
709
http://www.voidspace.org.uk/python/firedrop2/textmacros.shtml
583
714
<div align="center">
584
<a href="http://www.python.org">
585
<img src="images/powered_by_python.jpg" width="602" height="186" border="0" />
587
<a href="http://www.opensource.org">
588
<img src="images/osi-certified-120x100.gif" width="120" height="100" border="0" />
589
<br /><strong>Certified Open Source</strong>
592
<script type="text/javascript" language="JavaScript">var site="s16atlantibots"</script>
593
<script type="text/javascript" language="JavaScript1.2" src="http://s16.sitemeter.com/js/counter.js?site=s16atlantibots"></script>
595
<a href="http://s16.sitemeter.com/stats.asp?site=s16atlantibots">
596
<img src="http://s16.sitemeter.com/meter.asp?site=s16atlantibots" alt="Site Meter" border=0 />
716
<a href="http://www.python.org">
717
<img src="images/new_python.gif" width="100" height="103" border="0"
718
alt="Powered by Python" />
720
<a href="http://sourceforge.net">
721
<img src="http://sourceforge.net/sflogo.php?group_id=123265&type=1" width="88" height="31" border="0" alt="SourceForge.net Logo" />
723
<a href="http://www.opensource.org">
724
<img src="images/osi-certified-120x100.gif" width="120" height="100" border="0"
725
alt="Certified Open Source"/>
729
<a href="http://www.voidspace.org.uk/python/index.shtml">
730
<img src="images/pythonbanner.gif" width="468" height="60"
731
alt="Python on Voidspace" border="0" />