~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/util/configobj/docs/validate.txt

  • Committer: John Arbash Meinel
  • Date: 2005-11-23 15:44:24 UTC
  • mto: (1185.50.19 bzr-jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1512.
  • Revision ID: john@arbash-meinel.com-20051123154424-a02f8bf990a1fed5
Renamed all of the tests from selftest/foo.py to tests/test_foo.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 
9
9
 
10
10
:Authors: `Michael Foord`_, `Nicola Larosa`_, `Mark Andrews`_
11
 
:Version: Validate 0.3.2
12
 
:Date: 2008/02/24
 
11
:Version: Validate 0.2.0
 
12
:Date: 2005/08/25
13
13
:Homepage: `Validate Homepage`_
14
14
:License: `BSD License`_
15
15
:Support: `Mailing List`_
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
20
21
.. _This Document:
21
22
.. _Validate Homepage: http://www.voidspace.org.uk/python/validate.html
22
 
.. _BSD License: http://www.voidspace.org.uk/python/license.shtml
 
23
.. _BSD License: http://www.voidspace.org.uk/documents/BSD-LICENSE.txt
23
24
 
24
25
 
25
26
.. contents:: Validate Manual
26
 
.. sectnum::
27
 
 
28
27
 
29
28
Introduction
30
29
============
38
37
type.
39
38
 
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
42
41
mechanism.
43
42
 
44
43
Checks look like function calls, and map to function calls. They can include
57
56
into every validator. Additional checks are easy to write: they can be provided
58
57
when the ``Validator`` is instantiated, or added afterwards.
59
58
 
60
 
Validate was primarily written to support ConfigObj_, but is designed to be 
 
59
Validate was primarily written to support ConfigObj_, but was designed to be 
61
60
applicable to many other situations.
62
61
 
63
62
For support and bug reports please use the ConfigObj `Mailing List`_.
64
63
 
65
64
.. _ConfigObj: http://www.voidspace.org.uk/python/configobj.html
66
65
 
67
 
 
68
66
Downloading
69
67
===========
70
68
 
71
 
The current version is **0.3.2**, dated 24th February 2008. 
 
69
The current version is **0.2.0**, dated 25th August 2005. 
72
70
 
73
71
You can get obtain validate in the following ways :
74
72
 
75
 
 
76
73
Files
77
74
-----
78
75
 
79
76
* validate.py_ from Voidspace
80
77
 
81
78
* configobj.zip from Voidspace - See the homepage of ConfigObj_ for the latest 
82
 
  version and download links.
 
79
  version and downlaod links.
83
80
 
84
81
    This contains validate.py and `this document`_. (As well as ConfigObj_ and 
85
82
    the ConfigObj documentation).
86
83
    
87
84
* The latest development version can be obtained from the `Subversion Repository`_.
88
85
 
89
 
 
90
86
Documentation
91
87
-------------
92
88
 
93
 
*configobj.zip* contains `this document`_.
 
89
*configobj.zip* contains `this document`_ and full `API Docs`_, generated 
 
90
automatically by EpyDoc_.
94
91
 
95
92
* You can view `this document`_ online as the `Validate Homepage`_.
96
93
 
 
94
* You can also browse the `API Docs`_ online.
97
95
 
98
96
Pythonutils
99
97
-----------
103
101
Python Projects`_.
104
102
 
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.0.0b4.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.
143
143
 
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 : ::
146
146
 
147
147
             true, on, yes, 1
148
148
 
159
159
            dotted-quad string, i.e. '1.2.3.4'.
160
160
 
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 
163
 
         return a list.
164
 
         
165
 
:'tuple': matches any list. This check returns a tuple rather than a list.
 
162
         specify min and max sizes of the list.
166
163
 
167
164
:'int_list': Matches a list of integers. Takes the same arguments as list.
168
165
 
181
178
 
182
179
               Each position can be one of : ::
183
180
 
184
 
                   int, str, boolean, float, ip_addr
 
181
                   int, str, bool, float, ip_addr
185
182
 
186
183
               So to specify a list with two strings followed by two integers,
187
184
               you write the check as : ::
206
203
    from validate import Validator
207
204
    #
208
205
    vtor = Validator()
209
 
    newval1 = vtor.check('integer', value1)
210
 
    newval2 = vtor.check('boolean', value2)
 
206
    newval1 = vtor.check(value1, 'integer')
 
207
    newval2 = vtor.check(value2, 'bool')
211
208
    # etc ...
212
209
 
213
210
    {-coloring}
218
215
    them in ``try...except`` blocks. Better still,  use ConfigObj for a higher
219
216
    level interface.
220
217
 
221
 
 
222
218
Using Validator
223
219
===============
224
220
 
268
264
Dictionary keys/functions you pass in can override the built-in ones if you
269
265
want.
270
266
 
271
 
 
272
267
Adding functions
273
268
----------------
274
269
 
288
283
 
289
284
    {-coloring}
290
285
 
291
 
``vtor.functions`` is just a dictionary that maps names to functions, so we
 
286
``vtor.functions``is just a dictionary that maps names to functions, so we
292
287
could also have called ``vtor.functions.update(fdict)``.
293
288
 
294
 
 
295
289
Writing the check
296
290
-----------------
297
291
 
306
300
 
307
301
but the check part will be identical .
308
302
 
309
 
 
310
303
The check method
311
304
----------------
312
305
 
339
332
    Although the value can be a string, if it represents a list it should
340
333
    already have been turned into a list of strings.
341
334
 
342
 
 
343
335
Default Values
344
336
~~~~~~~~~~~~~~
345
337
 
358
350
    check1 = 'integer(default=50)'
359
351
    check2 = 'option("val 1", "val 2", "val 3", default="val 1")'
360
352
 
361
 
    assert vtor.check(check1, '', missing=True) == 50
362
 
    assert vtor.check(check2, '', missing=True) == "val 1"
 
353
    assert vtor.check('', check1, missing=True) == 50
 
354
    assert vtor.check('', check2, missing=True) == "val 1"
363
355
 
364
356
    {-coloring}
365
357
 
373
365
that this check contains no useful value when missing, i.e. the value is
374
366
optional, and may be omitted without harm.
375
367
 
376
 
 
377
 
.. note:: 
378
 
 
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'``.
381
 
 
382
 
 
383
 
List Values
384
 
~~~~~~~~~~~
385
 
 
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.
389
 
 
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 : ::
393
 
 
394
 
    checkname(default=list('val1', 'val2', 'val3'))
395
 
 
396
 
 
397
 
get_default_value
398
 
-----------------
399
 
 
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``.
404
 
 
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).
407
 
 
408
 
 
409
 
 
410
368
Validator Exceptions
411
369
====================
412
370
 
465
423
* ``VdtValueTooShortError``
466
424
* ``VdtValueTooLongError``
467
425
 
468
 
 
469
426
Writing check functions
470
427
=======================
471
428
 
489
446
 
490
447
And that's it !
491
448
 
492
 
 
493
449
Example
494
450
-------
495
451
 
578
534
If you are using validate in another circumstance you may want to create your 
579
535
own subclasses of ``ValidateError``, that convey more specific information.
580
536
 
581
 
 
582
 
Known Issues
583
 
============
584
 
 
585
 
The following parses and then blows up. The resulting error message
586
 
is confusing:
587
 
 
588
 
    ``checkname(default=list(1, 2, 3, 4)``
589
 
    
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.
593
 
    
594
 
 
595
537
TODO
596
538
====
597
539
 
598
540
* A regex check function ?
599
 
* A timestamp check function ? (Using the ``parse`` function from ``DateUtil`` perhaps).
600
 
 
 
541
* A timestamp check function ? (Using the ``parse`` function from ``DateUtil``).
 
542
* Allow triple quotes ? (getting a bit heavy for a single regex)
601
543
 
602
544
ISSUES
603
545
======
607
549
    Please file any bug reports to `Michael Foord`_ or the ConfigObj
608
550
    `Mailing List`_.
609
551
 
 
552
Lists passed as function arguments need additional quotes. Ugly, could do
 
553
with fixing this.
 
554
 
610
555
If we could pull tuples out of arguments, it would be easier
611
556
to specify arguments for 'mixed_lists'.
612
557
 
613
 
 
614
558
CHANGELOG
615
559
=========
616
560
 
617
 
2008/02/24 - Version 0.3.2
618
 
--------------------------
619
 
 
620
 
BUGFIX: Handling of None as default value fixed.
621
 
 
622
 
 
623
 
2008/02/05 - Version 0.3.1
624
 
--------------------------
625
 
 
626
 
BUGFIX: Unicode checks no longer broken.
627
 
 
628
 
 
629
 
2008/02/05 - Version 0.3.0
630
 
--------------------------
631
 
 
632
 
Improved performance with a parse cache.
633
 
 
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.
637
 
 
638
 
Added 'tuple' check and corresponding 'is_tuple' function (which always returns a tuple).
639
 
 
640
 
BUGFIX: A quoted 'None' as a default value is no longer treated as None,
641
 
but as the string 'None'.
642
 
 
643
 
BUGFIX: We weren't unquoting keyword arguments of length two, so an
644
 
empty string didn't work as a default.
645
 
 
646
 
BUGFIX: Strings no longer pass the 'is_list' check. Additionally, the
647
 
list checks always return lists.
648
 
 
649
 
A couple of documentation bug fixes.
650
 
 
651
 
Removed CHANGELOG from module.
652
 
 
653
 
 
654
 
2007/02/04      Version 0.2.3
655
 
-----------------------------
656
 
 
657
 
Release of 0.2.3
658
 
 
659
 
 
660
 
2006/12/17      Version 0.2.3-alpha1
661
 
------------------------------------
662
 
 
663
 
By Nicola Larosa
664
 
 
665
 
Fixed validate doc to talk of ``boolean`` instead of ``bool``; changed the
666
 
``is_bool`` function to ``is_boolean`` (Sourceforge bug #1531525).
667
 
 
668
 
 
669
 
2006/04/29      Version 0.2.2
670
 
-----------------------------
671
 
 
672
 
Addressed bug where a string would pass the ``is_list`` test. (Thanks to
673
 
Konrad Wojas.)
674
 
 
675
 
 
676
 
2005/12/16      Version 0.2.1
677
 
-----------------------------
678
 
 
679
 
Fixed bug so we can handle keyword argument values with commas.
680
 
 
681
 
We now use a list constructor for passing list values to keyword arguments
682
 
(including ``default``) : ::
683
 
 
684
 
    default=list("val", "val", "val")
685
 
 
686
 
Added the ``_test`` test. {sm;:-)}
687
 
 
688
 
Moved a function call outside a try...except block.
689
 
 
690
561
 
691
562
2005/08/18      Version 0.2.0
692
563
-----------------------------
695
566
 
696
567
Does type conversion as well.
697
568
 
698
 
 
699
569
2005/02/01      Version 0.1.0
700
570
-----------------------------
701
571
 
702
572
Initial version developed by `Michael Foord`_
703
 
and Mark Andrews.
 
573
and `Mark Andrews`_
704
574
 
705
575
.. note::
706
576
 
708
578
    textmacros module and the PySrc CSS stuff. See
709
579
    http://www.voidspace.org.uk/python/firedrop2/textmacros.shtml
710
580
 
711
 
 
712
581
.. raw:: html
713
582
 
714
583
    <div align="center">
715
 
        <p>
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" />
719
 
            </a>
720
 
            <a href="http://sourceforge.net">
721
 
                <img src="http://sourceforge.net/sflogo.php?group_id=123265&amp;type=1" width="88" height="31" border="0" alt="SourceForge.net Logo" />
722
 
            </a>
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"/>
726
 
            </a>
727
 
        </p>
728
 
        <p>
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" />
732
 
            </a>
733
 
        </p>
 
584
        <a href="http://www.python.org">
 
585
            <img src="images/powered_by_python.jpg" width="602" height="186" border="0" />
 
586
        </a>
 
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>
 
590
        </a>
 
591
        <br /><br />
 
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>
 
594
        <noscript>
 
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 />
 
597
            </a>
 
598
        </noscript>
734
599
    </div>
735