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
21
22
.. _Validate Homepage: http://www.voidspace.org.uk/python/validate.html
22
23
.. _BSD License: http://www.voidspace.org.uk/python/license.shtml
25
26
.. contents:: Validate Manual
40
39
Checks are also strings, and are easy to write. One generic system can be used
41
to validate information from different sources via a single consistent
40
to validate information from different sources, via a single consistent
44
43
Checks look like function calls, and map to function calls. They can include
65
64
.. _ConfigObj: http://www.voidspace.org.uk/python/configobj.html
71
The current version is **0.3.2**, dated 24th February 2008.
69
The current version is **0.2.1**, dated 16th December 2005.
73
71
You can get obtain validate in the following ways :
87
84
* The latest development version can be obtained from the `Subversion Repository`_.
93
*configobj.zip* contains `this document`_.
89
*configobj.zip* contains `this document`_ and full `API Docs`_, generated
90
automatically by EpyDoc_.
95
92
* You can view `this document`_ online as the `Validate Homepage`_.
94
* You can also browse the `API Docs`_ online.
103
101
Python Projects`_.
105
103
.. _configobj.py: http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj.py
106
.. _configobj.zip: http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj-4.5.2.zip
104
.. _configobj.zip: http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=configobj-4.1.0.zip
107
105
.. _validate.py: http://www.voidspace.org.uk/cgi-bin/voidspace/downman.py?file=validate.py
108
.. _Subversion Repository: http://svn.pythonutils.python-hosting.com/trunk/pythonutils/
106
.. _API Docs: http://www.voidspace.org.uk/python/configobj-api/
107
.. _Subversion Repository: http://svn.rest2web.python-hosting.com/branches/configobj4/
109
108
.. _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
:'boolean': matches boolean values: ``True`` or ``False``.
145
Acceptable string values for True are : ::
144
:'bool': 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. The list checks always
165
:'tuple': matches any list. This check returns a tuple rather than a list.
162
specify min and max sizes of the list.
167
164
:'int_list': Matches a list of integers. Takes the same arguments as list.
182
179
Each position can be one of : ::
184
int, str, boolean, float, ip_addr
181
int, str, bool, float, ip_addr
186
183
So to specify a list with two strings followed by two integers,
187
184
you write the check as : ::
208
205
vtor = Validator()
209
206
newval1 = vtor.check('integer', value1)
210
newval2 = vtor.check('boolean', value2)
207
newval2 = vtor.check('bool', value2)
291
``vtor.functions`` is just a dictionary that maps names to functions, so we
287
``vtor.functions``is just a dictionary that maps names to functions, so we
292
288
could also have called ``vtor.functions.update(fdict)``.
295
290
Writing the check
296
291
-----------------
358
352
check1 = 'integer(default=50)'
359
353
check2 = 'option("val 1", "val 2", "val 3", default="val 1")'
361
assert vtor.check(check1, '', missing=True) == 50
362
assert vtor.check(check2, '', missing=True) == "val 1"
355
assert vtor.check('', check1, missing=True) == 50
356
assert vtor.check('', check2, missing=True) == "val 1"
394
382
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).
410
384
Validator Exceptions
411
385
====================
578
550
If you are using validate in another circumstance you may want to create your
579
551
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.
598
556
* A regex check function ?
599
* A timestamp check function ? (Using the ``parse`` function from ``DateUtil`` perhaps).
557
* A timestamp check function ? (Using the ``parse`` function from ``DateUtil``).
558
* Allow triple quotes ? (getting a bit heavy for a regex)
610
568
If we could pull tuples out of arguments, it would be easier
611
569
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
574
2005/12/16 Version 0.2.1
677
575
-----------------------------
708
604
textmacros module and the PySrc CSS stuff. See
709
605
http://www.voidspace.org.uk/python/firedrop2/textmacros.shtml
714
609
<div align="center">
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" />
610
<a href="http://www.python.org">
611
<img src="images/powered_by_python.jpg" width="602" height="186" border="0" />
613
<a href="http://www.opensource.org">
614
<img src="images/osi-certified-120x100.gif" width="120" height="100" border="0" />
615
<br /><strong>Certified Open Source</strong>
618
<script type="text/javascript" language="JavaScript">var site="s16atlantibots"</script>
619
<script type="text/javascript" language="JavaScript1.2" src="http://s16.sitemeter.com/js/counter.js?site=s16atlantibots"></script>
621
<a href="http://s16.sitemeter.com/stats.asp?site=s16atlantibots">
622
<img src="http://s16.sitemeter.com/meter.asp?site=s16atlantibots" alt="Site Meter" border=0 />