home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / doc / python-configobj / validate.html < prev    next >
Encoding:
Extensible Markup Language  |  2010-03-01  |  42.5 KB  |  640 lines

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.6: http://docutils.sourceforge.net/" />
  7. <title>Validation Schema with validate.py</title>
  8. <meta name="authors" content="Michael Foord  Nicola Larosa  Mark Andrews" />
  9. <meta name="date" content="2010/01/09" />
  10. <link rel="stylesheet" href="docutils.css" type="text/css" />
  11. </head>
  12. <body>
  13. <div class="document" id="validation-schema-with-validate-py">
  14. <h1 class="title">Validation Schema with validate.py</h1>
  15. <h2 class="subtitle" id="using-the-validator-class">Using the Validator class</h2>
  16. <table class="docinfo" frame="void" rules="none">
  17. <col class="docinfo-name" />
  18. <col class="docinfo-content" />
  19. <tbody valign="top">
  20. <tr><th class="docinfo-name">Authors:</th>
  21. <td>Michael Foord
  22. <br />Nicola Larosa
  23. <br />Mark Andrews</td></tr>
  24. <tr><th class="docinfo-name">Version:</th>
  25. <td>Validate 1.0.1</td></tr>
  26. <tr><th class="docinfo-name">Date:</th>
  27. <td>2010/01/09</td></tr>
  28. <tr class="field"><th class="docinfo-name">Homepage:</th><td class="field-body"><a class="reference external" href="http://www.voidspace.org.uk/python/validate.html">Validate Homepage</a></td>
  29. </tr>
  30. <tr class="field"><th class="docinfo-name">Repository:</th><td class="field-body"><a class="reference external" href="http://code.google.com/p/configobj/">Google code homepage</a></td>
  31. </tr>
  32. <tr class="field"><th class="docinfo-name">PyPI Entry:</th><td class="field-body"><a class="reference external" href="http://pypi.python.org/pypi/validate">Validate on Python Packaging Index</a></td>
  33. </tr>
  34. <tr class="field"><th class="docinfo-name">License:</th><td class="field-body"><a class="reference external" href="http://www.voidspace.org.uk/python/license.shtml">BSD License</a></td>
  35. </tr>
  36. <tr class="field"><th class="docinfo-name">Support:</th><td class="field-body"><a class="reference external" href="http://lists.sourceforge.net/lists/listinfo/configobj-develop">Mailing List</a></td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. <div class="contents topic" id="validate-manual">
  41. <p class="topic-title first">Validate Manual</p>
  42. <ul class="auto-toc simple">
  43. <li><a class="reference internal" href="#introduction" id="id1">1   Introduction</a></li>
  44. <li><a class="reference internal" href="#downloading" id="id2">2   Downloading</a><ul class="auto-toc">
  45. <li><a class="reference internal" href="#files" id="id3">2.1   Files</a></li>
  46. <li><a class="reference internal" href="#documentation" id="id4">2.2   Documentation</a></li>
  47. </ul>
  48. </li>
  49. <li><a class="reference internal" href="#the-standard-functions" id="id5">3   The standard functions</a></li>
  50. <li><a class="reference internal" href="#using-validator" id="id6">4   Using Validator</a><ul class="auto-toc">
  51. <li><a class="reference internal" href="#instantiate" id="id7">4.1   Instantiate</a></li>
  52. <li><a class="reference internal" href="#adding-functions" id="id8">4.2   Adding functions</a></li>
  53. <li><a class="reference internal" href="#writing-the-check" id="id9">4.3   Writing the check</a></li>
  54. <li><a class="reference internal" href="#the-check-method" id="id10">4.4   The check method</a><ul class="auto-toc">
  55. <li><a class="reference internal" href="#default-values" id="id11">4.4.1   Default Values</a></li>
  56. <li><a class="reference internal" href="#list-values" id="id12">4.4.2   List Values</a></li>
  57. </ul>
  58. </li>
  59. <li><a class="reference internal" href="#get-default-value" id="id13">4.5   get_default_value</a></li>
  60. </ul>
  61. </li>
  62. <li><a class="reference internal" href="#validator-exceptions" id="id14">5   Validator Exceptions</a></li>
  63. <li><a class="reference internal" href="#writing-check-functions" id="id15">6   Writing check functions</a><ul class="auto-toc">
  64. <li><a class="reference internal" href="#example" id="id16">6.1   Example</a></li>
  65. </ul>
  66. </li>
  67. <li><a class="reference internal" href="#known-issues" id="id17">7   Known Issues</a></li>
  68. <li><a class="reference internal" href="#todo" id="id18">8   TODO</a></li>
  69. <li><a class="reference internal" href="#issues" id="id19">9   ISSUES</a></li>
  70. <li><a class="reference internal" href="#changelog" id="id20">10   CHANGELOG</a><ul class="auto-toc">
  71. <li><a class="reference internal" href="#version-1-0-1" id="id21">10.1   2009/10/25 - Version 1.0.1</a></li>
  72. <li><a class="reference internal" href="#version-1-0-0" id="id22">10.2   2009/04/13 - Version 1.0.0</a></li>
  73. <li><a class="reference internal" href="#version-0-3-2" id="id23">10.3   2008/02/24 - Version 0.3.2</a></li>
  74. <li><a class="reference internal" href="#version-0-3-1" id="id24">10.4   2008/02/05 - Version 0.3.1</a></li>
  75. <li><a class="reference internal" href="#version-0-3-0" id="id25">10.5   2008/02/05 - Version 0.3.0</a></li>
  76. <li><a class="reference internal" href="#version-0-2-3" id="id26">10.6   2007/02/04      Version 0.2.3</a></li>
  77. <li><a class="reference internal" href="#version-0-2-3-alpha1" id="id27">10.7   2006/12/17      Version 0.2.3-alpha1</a></li>
  78. <li><a class="reference internal" href="#version-0-2-2" id="id28">10.8   2006/04/29      Version 0.2.2</a></li>
  79. <li><a class="reference internal" href="#version-0-2-1" id="id29">10.9   2005/12/16      Version 0.2.1</a></li>
  80. <li><a class="reference internal" href="#version-0-2-0" id="id30">10.10   2005/08/18      Version 0.2.0</a></li>
  81. <li><a class="reference internal" href="#version-0-1-0" id="id31">10.11   2005/02/01      Version 0.1.0</a></li>
  82. </ul>
  83. </li>
  84. </ul>
  85. </div>
  86. <div class="section" id="introduction">
  87. <h1><a class="toc-backref" href="#id1">1   Introduction</a></h1>
  88. <p>Validation is used to check that supplied values conform to a specification.</p>
  89. <p>The value can be supplied as a string, e.g. from a config file. In this case
  90. the check will also <em>convert</em> the value to the required type. This allows you
  91. to add validation as a transparent layer to access data stored as strings. The
  92. validation checks that the data is correct <em>and</em> converts it to the expected
  93. type.</p>
  94. <p>Checks are also strings, and are easy to write. One generic system can be used
  95. to validate information from different sources via a single consistent
  96. mechanism.</p>
  97. <p>Checks look like function calls, and map to function calls. They can include
  98. parameters and keyword arguments. These arguments are passed to the relevant
  99. function by the <tt class="docutils literal">Validator</tt> instance, along with the value being checked.</p>
  100. <p>The syntax for checks also allows for specifying a default value. This default
  101. value can be <tt class="docutils literal">None</tt>, no matter what the type of the check. This can be used
  102. to indicate that a value was missing, and so holds no useful value.</p>
  103. <p>Functions either return a new value, or raise an exception. See <a class="reference internal" href="#validator-exceptions">Validator
  104. Exceptions</a> for the low down on the exception classes that <tt class="docutils literal">validate.py</tt>
  105. defines.</p>
  106. <p>Some standard functions are provided, for basic data types; these come built
  107. into every validator. Additional checks are easy to write: they can be provided
  108. when the <tt class="docutils literal">Validator</tt> is instantiated, or added afterwards.</p>
  109. <p>Validate was primarily written to support <a class="reference external" href="http://www.voidspace.org.uk/python/configobj.html">ConfigObj</a>, but is designed to be
  110. applicable to many other situations.</p>
  111. <p>For support and bug reports please use the ConfigObj <a class="reference external" href="http://lists.sourceforge.net/lists/listinfo/configobj-develop">Mailing List</a>.</p>
  112. </div>
  113. <div class="section" id="downloading">
  114. <h1><a class="toc-backref" href="#id2">2   Downloading</a></h1>
  115. <p>The current version is <strong>1.0.1</strong>, dated 9th January 2010.</p>
  116. <p>You can get obtain validate in the following ways :</p>
  117. <div class="section" id="files">
  118. <h2><a class="toc-backref" href="#id3">2.1   Files</a></h2>
  119. <ul>
  120. <li><p class="first"><a class="reference external" href="http://www.voidspace.org.uk/cgi-bin/voidspace/download/validate.py">validate.py</a> from Voidspace</p>
  121. </li>
  122. <li><p class="first">configobj.zip from Voidspace - See the homepage of <a class="reference external" href="http://www.voidspace.org.uk/python/configobj.html">ConfigObj</a> for the latest
  123. version and download links.</p>
  124. <blockquote>
  125. <p>This contains validate.py and <a class="reference external" href="http://www.voidspace.org.uk/python/validate.html">this document</a>. (As well as <a class="reference external" href="http://www.voidspace.org.uk/python/configobj.html">ConfigObj</a> and
  126. the ConfigObj documentation).</p>
  127. </blockquote>
  128. </li>
  129. <li><p class="first">The latest development version can be obtained from the <a class="reference external" href="http://code.google.com/p/configobj/">Subversion Repository</a>.</p>
  130. </li>
  131. </ul>
  132. </div>
  133. <div class="section" id="documentation">
  134. <h2><a class="toc-backref" href="#id4">2.2   Documentation</a></h2>
  135. <p><em>configobj.zip</em> contains <a class="reference external" href="http://www.voidspace.org.uk/python/validate.html">this document</a>.</p>
  136. <ul class="simple">
  137. <li>You can view <a class="reference external" href="http://www.voidspace.org.uk/python/validate.html">this document</a> online as the <a class="reference external" href="http://www.voidspace.org.uk/python/validate.html">Validate Homepage</a>.</li>
  138. </ul>
  139. </div>
  140. </div>
  141. <div class="section" id="the-standard-functions">
  142. <h1><a class="toc-backref" href="#id5">3   The standard functions</a></h1>
  143. <p>The standard functions come built-in to every <tt class="docutils literal">Validator</tt> instance. They work
  144. with the following basic data types :</p>
  145. <ul class="simple">
  146. <li>integer</li>
  147. <li>float</li>
  148. <li>boolean</li>
  149. <li>string</li>
  150. <li>ip_addr</li>
  151. </ul>
  152. <p>plus lists of these datatypes.</p>
  153. <p>Adding additional checks is done through coding simple functions.</p>
  154. <p>The full set of standard checks are :</p>
  155. <table class="docutils field-list" frame="void" rules="none">
  156. <col class="field-name" />
  157. <col class="field-body" />
  158. <tbody valign="top">
  159. <tr class="field"><th class="field-name">'integer':</th><td class="field-body"><p class="first">matches integer values (including negative). Takes optional 'min'
  160. and 'max' arguments:</p>
  161. <pre class="literal-block">
  162. integer()
  163. integer(3, 9)    # any value from 3 to 9
  164. integer(min=0) # any positive value
  165. integer(max=9)
  166. </pre>
  167. </td>
  168. </tr>
  169. <tr class="field"><th class="field-name">'float':</th><td class="field-body"><p class="first">matches float values
  170. Has the same parameters as the integer check.</p>
  171. </td>
  172. </tr>
  173. <tr class="field"><th class="field-name">'boolean':</th><td class="field-body"><dl class="first docutils">
  174. <dt>matches boolean values: <tt class="docutils literal">True</tt> or <tt class="docutils literal">False</tt>.</dt>
  175. <dd><p class="first">Acceptable string values for True are:</p>
  176. <pre class="last literal-block">
  177. true, on, yes, 1
  178. </pre>
  179. </dd>
  180. </dl>
  181. <p>Acceptable string values for False are:</p>
  182. <pre class="literal-block">
  183. false, off, no, 0
  184. </pre>
  185. <p>Any other value raises an error.</p>
  186. </td>
  187. </tr>
  188. <tr class="field"><th class="field-name">'string':</th><td class="field-body"><p class="first">matches any string. Takes optional keyword args 'min' and 'max' to
  189. specify min and max length of string.</p>
  190. </td>
  191. </tr>
  192. <tr class="field"><th class="field-name">'ip_addr':</th><td class="field-body"><p class="first">matches an Internet Protocol address, v.4, represented by a
  193. dotted-quad string, i.e. '1.2.3.4'.</p>
  194. </td>
  195. </tr>
  196. <tr class="field"><th class="field-name">'list':</th><td class="field-body"><p class="first">matches any list. Takes optional keyword args 'min', and 'max' to
  197. specify min and max sizes of the list. The list checks always
  198. return a list.</p>
  199. </td>
  200. </tr>
  201. <tr class="field"><th class="field-name">force_list:</th><td class="field-body"><p class="first">matches any list, but if a single value is passed in will
  202. coerce it into a list containing that value. Useful for
  203. configobj if the user forgot the trailing comma to turn
  204. a single value into a list.</p>
  205. </td>
  206. </tr>
  207. <tr class="field"><th class="field-name">'tuple':</th><td class="field-body"><p class="first">matches any list. This check returns a tuple rather than a list.</p>
  208. </td>
  209. </tr>
  210. <tr class="field"><th class="field-name">'int_list':</th><td class="field-body"><p class="first">Matches a list of integers. Takes the same arguments as list.</p>
  211. </td>
  212. </tr>
  213. <tr class="field"><th class="field-name">'float_list':</th><td class="field-body"><p class="first">Matches a list of floats. Takes the same arguments as list.</p>
  214. </td>
  215. </tr>
  216. <tr class="field"><th class="field-name">'bool_list':</th><td class="field-body"><p class="first">Matches a list of boolean values. Takes the same arguments as
  217. list.</p>
  218. </td>
  219. </tr>
  220. <tr class="field"><th class="field-name">'string_list':</th><td class="field-body"><p class="first">Matches a list of strings. Takes the same arguments as list.</p>
  221. </td>
  222. </tr>
  223. <tr class="field"><th class="field-name">'ip_addr_list':</th><td class="field-body"><p class="first">Matches a list of IP addresses. Takes the same arguments as
  224. list.</p>
  225. </td>
  226. </tr>
  227. <tr class="field"><th class="field-name">'mixed_list':</th><td class="field-body"><p class="first">Matches a list with different types in specific positions.
  228. List size must match the number of arguments.</p>
  229. <p>Each position can be one of:</p>
  230. <pre class="literal-block">
  231. int, str, boolean, float, ip_addr
  232. </pre>
  233. <p>So to specify a list with two strings followed by two integers,
  234. you write the check as:</p>
  235. <pre class="literal-block">
  236. mixed_list(str, str, int, int)
  237. </pre>
  238. </td>
  239. </tr>
  240. <tr class="field"><th class="field-name">'pass':</th><td class="field-body"><p class="first">matches everything: it never fails and the value is unchanged. It is
  241. also the default if no check is specified.</p>
  242. </td>
  243. </tr>
  244. <tr class="field"><th class="field-name">'option':</th><td class="field-body"><p class="first">matches any from a list of options.
  245. You specify this test with:</p>
  246. <pre class="last literal-block">
  247. option('option 1', 'option 2', 'option 3')
  248. </pre>
  249. </td>
  250. </tr>
  251. </tbody>
  252. </table>
  253. <p>The following code will work without you having to specifically add the
  254. functions yourself.</p>
  255. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">validate</span> <span class="kn">import</span> <span class="n">Validator</span>
  256. <span class="c">#</span>
  257. <span class="n">vtor</span> <span class="o">=</span> <span class="n">Validator</span><span class="p">()</span>
  258. <span class="n">newval1</span> <span class="o">=</span> <span class="n">vtor</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="s">'integer'</span><span class="p">,</span> <span class="n">value1</span><span class="p">)</span>
  259. <span class="n">newval2</span> <span class="o">=</span> <span class="n">vtor</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="s">'boolean'</span><span class="p">,</span> <span class="n">value2</span><span class="p">)</span>
  260. <span class="c"># etc ...</span>
  261. </pre></div>
  262. <div class="note">
  263. <p class="first admonition-title">Note</p>
  264. <p class="last">Of course, if these checks fail they raise exceptions. So you should wrap
  265. them in <tt class="docutils literal"><span class="pre">try...except</span></tt> blocks. Better still,  use ConfigObj for a higher
  266. level interface.</p>
  267. </div>
  268. </div>
  269. <div class="section" id="using-validator">
  270. <h1><a class="toc-backref" href="#id6">4   Using Validator</a></h1>
  271. <p>Using <tt class="docutils literal">Validator</tt> is very easy. It has one public attribute and one public
  272. method.</p>
  273. <p>Shown below are the different steps in using <tt class="docutils literal">Validator</tt>.</p>
  274. <p>The only additional thing you need to know, is about <a class="reference internal" href="#writing-check-functions">Writing check
  275. functions</a>.</p>
  276. <div class="section" id="instantiate">
  277. <h2><a class="toc-backref" href="#id7">4.1   Instantiate</a></h2>
  278. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">validate</span> <span class="kn">import</span> <span class="n">Validator</span>
  279. <span class="n">vtor</span> <span class="o">=</span> <span class="n">Validator</span><span class="p">()</span>
  280. </pre></div>
  281. <p>or even :</p>
  282. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">validate</span> <span class="kn">import</span> <span class="n">Validator</span>
  283. <span class="c">#</span>
  284. <span class="n">fdict</span> <span class="o">=</span> <span class="p">{</span>
  285.     <span class="s">'check_name1'</span><span class="p">:</span> <span class="n">function1</span><span class="p">,</span>
  286.     <span class="s">'check_name2'</span><span class="p">:</span> <span class="n">function2</span><span class="p">,</span>
  287.     <span class="s">'check_name3'</span><span class="p">:</span> <span class="n">function3</span><span class="p">,</span>
  288. <span class="p">}</span>
  289. <span class="c">#</span>
  290. <span class="n">vtor</span> <span class="o">=</span> <span class="n">Validator</span><span class="p">(</span><span class="n">fdict</span><span class="p">)</span>
  291. </pre></div>
  292. <p>The second method adds a set of your functions as soon as your validator is
  293. created. They are stored in the <tt class="docutils literal">vtor.functions</tt> dictionary. The 'key' you
  294. give them in this dictionary is the name you use in your checks (not the
  295. original function name).</p>
  296. <p>Dictionary keys/functions you pass in can override the built-in ones if you
  297. want.</p>
  298. </div>
  299. <div class="section" id="adding-functions">
  300. <h2><a class="toc-backref" href="#id8">4.2   Adding functions</a></h2>
  301. <p>The code shown above, for adding functions on instantiation, has exactly the
  302. same effect as the following code :</p>
  303. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">validate</span> <span class="kn">import</span> <span class="n">Validator</span>
  304. <span class="c">#</span>
  305. <span class="n">vtor</span> <span class="o">=</span> <span class="n">Validator</span><span class="p">()</span>
  306. <span class="n">vtor</span><span class="o">.</span><span class="n">functions</span><span class="p">[</span><span class="s">'check_name1'</span><span class="p">]</span> <span class="o">=</span> <span class="n">function1</span>
  307. <span class="n">vtor</span><span class="o">.</span><span class="n">functions</span><span class="p">[</span><span class="s">'check_name2'</span><span class="p">]</span> <span class="o">=</span> <span class="n">function2</span>
  308. <span class="n">vtor</span><span class="o">.</span><span class="n">functions</span><span class="p">[</span><span class="s">'check_name3'</span><span class="p">]</span> <span class="o">=</span> <span class="n">function3</span>
  309. </pre></div>
  310. <p><tt class="docutils literal">vtor.functions</tt> is just a dictionary that maps names to functions, so we
  311. could also have called <tt class="docutils literal">vtor.functions.update(fdict)</tt>.</p>
  312. </div>
  313. <div class="section" id="writing-the-check">
  314. <h2><a class="toc-backref" href="#id9">4.3   Writing the check</a></h2>
  315. <p>As we've heard, the checks map to the names in the <tt class="docutils literal">functions</tt> dictionary.
  316. You've got a full list of <a class="reference internal" href="#the-standard-functions">The standard functions</a> and the arguments they
  317. take.</p>
  318. <p>If you're using <tt class="docutils literal">Validator</tt> from ConfigObj, then your checks will look like:</p>
  319. <pre class="literal-block">
  320. keyword = int_list(max=6)
  321. </pre>
  322. <p>but the check part will be identical .</p>
  323. </div>
  324. <div class="section" id="the-check-method">
  325. <h2><a class="toc-backref" href="#id10">4.4   The check method</a></h2>
  326. <p>If you're not using <tt class="docutils literal">Validator</tt> from ConfigObj, then you'll need to call the
  327. <tt class="docutils literal">check</tt> method yourself.</p>
  328. <p>If the check fails then it will raise an exception, so you'll want to trap
  329. that. Here's the basic example :</p>
  330. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">validate</span> <span class="kn">import</span> <span class="n">Validator</span><span class="p">,</span> <span class="n">ValidateError</span>
  331. <span class="c">#</span>
  332. <span class="n">vtor</span> <span class="o">=</span> <span class="n">Validator</span><span class="p">()</span>
  333. <span class="n">check</span> <span class="o">=</span> <span class="s">"integer(0, 9)"</span>
  334. <span class="n">value</span> <span class="o">=</span> <span class="mi">3</span>
  335. <span class="k">try</span><span class="p">:</span>
  336.     <span class="n">newvalue</span> <span class="o">=</span> <span class="n">vtor</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="n">check</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
  337. <span class="k">except</span> <span class="n">ValidateError</span><span class="p">:</span>
  338.     <span class="k">print</span> <span class="s">'Check Failed.'</span>
  339. <span class="k">else</span><span class="p">:</span>
  340.     <span class="k">print</span> <span class="s">'Check passed.'</span>
  341. </pre></div>
  342. <div class="caution">
  343. <p class="first admonition-title">Caution!</p>
  344. <p class="last">Although the value can be a string, if it represents a list it should
  345. already have been turned into a list of strings.</p>
  346. </div>
  347. <div class="section" id="default-values">
  348. <h3><a class="toc-backref" href="#id11">4.4.1   Default Values</a></h3>
  349. <p>Some values may not be available, and you may want to be able to specify a
  350. default as part of the check.</p>
  351. <p>You do this by passing the keyword <tt class="docutils literal">missing=True</tt> to the <tt class="docutils literal">check</tt> method, as
  352. well as a <tt class="docutils literal">default=value</tt> in the check. (Constructing these checks is done
  353. automatically by ConfigObj: you only need to know about the <tt class="docutils literal">default=value</tt>
  354. part) :</p>
  355. <div class="highlight"><pre><span class="n">check1</span> <span class="o">=</span> <span class="s">'integer(default=50)'</span>
  356. <span class="n">check2</span> <span class="o">=</span> <span class="s">'option("val 1", "val 2", "val 3", default="val 1")'</span>
  357.  
  358. <span class="k">assert</span> <span class="n">vtor</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="n">check1</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="n">missing</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="o">==</span> <span class="mi">50</span>
  359. <span class="k">assert</span> <span class="n">vtor</span><span class="o">.</span><span class="n">check</span><span class="p">(</span><span class="n">check2</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="n">missing</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="o">==</span> <span class="s">"val 1"</span>
  360. </pre></div>
  361. <p>If you pass in <tt class="docutils literal">missing=True</tt> to the check method, then the actual value is
  362. ignored. If no default is specified in the check, a <tt class="docutils literal">ValidateMissingValue</tt>
  363. exception is raised. If a default is specified then that is passed to the
  364. check instead.</p>
  365. <p>If the check has <tt class="docutils literal">default=None</tt> (case sensitive) then <tt class="docutils literal">vtor.check</tt> will
  366. <em>always</em> return <tt class="docutils literal">None</tt> (the object). This makes it easy to tell your program
  367. that this check contains no useful value when missing, i.e. the value is
  368. optional, and may be omitted without harm.</p>
  369. <div class="note">
  370. <p class="first admonition-title">Note</p>
  371. <p class="last">As of version 0.3.0, if you specify <tt class="docutils literal"><span class="pre">default='None'</span></tt> (note the quote marks
  372. around <tt class="docutils literal">None</tt>) then it will be interpreted as the string <tt class="docutils literal">'None'</tt>.</p>
  373. </div>
  374. </div>
  375. <div class="section" id="list-values">
  376. <h3><a class="toc-backref" href="#id12">4.4.2   List Values</a></h3>
  377. <p>It's possible that you would like your default value to be a list. It's even
  378. possible that you will write your own check functions - and would like to pass
  379. them keyword arguments as lists from within the check.</p>
  380. <p>To avoid confusing syntax with commas and quotes you use a list constructor to
  381. specify that keyword arguments are lists. This includes the <tt class="docutils literal">default</tt> value.
  382. This makes checks look something like:</p>
  383. <pre class="literal-block">
  384. checkname(default=list('val1', 'val2', 'val3'))
  385. </pre>
  386. </div>
  387. </div>
  388. <div class="section" id="get-default-value">
  389. <h2><a class="toc-backref" href="#id13">4.5   get_default_value</a></h2>
  390. <p><tt class="docutils literal">Validator</tt> instances have a <tt class="docutils literal">get_default_value</tt> method. It takes a <tt class="docutils literal">check</tt> string
  391. (the same string you would pass to the <tt class="docutils literal">check</tt> method) and returns the default value,
  392. converted to the right type. If the check doesn't define a default value then this method
  393. raises a <tt class="docutils literal">KeyError</tt>.</p>
  394. <p>If the <tt class="docutils literal">check</tt> has been seen before then it will have been parsed and cached already,
  395. so this method is not expensive to call (however the conversion is done each time).</p>
  396. </div>
  397. </div>
  398. <div class="section" id="validator-exceptions">
  399. <h1><a class="toc-backref" href="#id14">5   Validator Exceptions</a></h1>
  400. <div class="note">
  401. <p class="first admonition-title">Note</p>
  402. <p class="last">If you only use Validator through ConfigObj, it traps these Exceptions for
  403. you. You will still need to know about them for writing your own check
  404. functions.</p>
  405. </div>
  406. <p><tt class="docutils literal">vtor.check</tt> indicates that the check has failed by raising an exception.
  407. The appropriate error should be raised in the check function.</p>
  408. <p>The base error class is <tt class="docutils literal">ValidateError</tt>. All errors (except for <tt class="docutils literal">VdtParamError</tt>)
  409. raised are sub-classes of this.</p>
  410. <p>If an unrecognised check is specified then <tt class="docutils literal">VdtUnknownCheckError</tt> is
  411. raised.</p>
  412. <p>There are also <tt class="docutils literal">VdtTypeError</tt> and <tt class="docutils literal">VdtValueError</tt>.</p>
  413. <p>If incorrect parameters are passed to a check function then it will (or should)
  414. raise <tt class="docutils literal">VdtParamError</tt>. As this indicates <em>programmer</em> error, rather than an error
  415. in the value, it is a subclass of <tt class="docutils literal">SyntaxError</tt> instead of <tt class="docutils literal">ValidateError</tt>.</p>
  416. <div class="note">
  417. <p class="first admonition-title">Note</p>
  418. <p class="last">This means it <em>won't</em> be caught by ConfigObj - but propagated instead.</p>
  419. </div>
  420. <p>If the value supplied is the wrong type, then the check should raise
  421. <tt class="docutils literal">VdtTypeError</tt>. e.g. the check requires the value to be an integer (or
  422. representation of an integer) and something else was supplied.</p>
  423. <p>If the value supplied is the right type, but an unacceptable value, then the
  424. check should raise <tt class="docutils literal">VdtValueError</tt>. e.g. the check requires the value to
  425. be an integer (or representation of an integer) less than ten and a higher
  426. value was supplied.</p>
  427. <p>Both <tt class="docutils literal">VdtTypeError</tt> and <tt class="docutils literal">VdtValueError</tt> are initialised with the
  428. incorrect value. In other words you raise them like this :</p>
  429. <div class="highlight"><pre><span class="k">raise</span> <span class="n">VdtTypeError</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
  430. <span class="c">#</span>
  431. <span class="k">raise</span> <span class="n">VdtValueError</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
  432. </pre></div>
  433. <p><tt class="docutils literal">VdtValueError</tt> has the following subclasses, which should be raised if
  434. they are more appropriate.</p>
  435. <ul class="simple">
  436. <li><tt class="docutils literal">VdtValueTooSmallError</tt></li>
  437. <li><tt class="docutils literal">VdtValueTooBigError</tt></li>
  438. <li><tt class="docutils literal">VdtValueTooShortError</tt></li>
  439. <li><tt class="docutils literal">VdtValueTooLongError</tt></li>
  440. </ul>
  441. </div>
  442. <div class="section" id="writing-check-functions">
  443. <h1><a class="toc-backref" href="#id15">6   Writing check functions</a></h1>
  444. <p>Writing check functions is easy.</p>
  445. <p>The check function will receive the value as its first argument, followed by
  446. any other parameters and keyword arguments.</p>
  447. <p>If the check fails, it should raise a <tt class="docutils literal">VdtTypeError</tt> or a
  448. <tt class="docutils literal">VdtValueError</tt> (or an appropriate subclass).</p>
  449. <p>All parameters and keyword arguments are <em>always</em> passed as strings. (Parsed
  450. from the check string).</p>
  451. <p>The value might be a string (or list of strings) and need
  452. converting to the right type - alternatively it might already be a list of
  453. integers. Our function needs to be able to handle either.</p>
  454. <p>If the check passes then it should return the value (possibly converted to the
  455. right type).</p>
  456. <p>And that's it !</p>
  457. <div class="section" id="example">
  458. <h2><a class="toc-backref" href="#id16">6.1   Example</a></h2>
  459. <p>Here is an example function that requires a list of integers. Each integer
  460. must be between 0 and 99.</p>
  461. <p>It takes a single argument specifying the length of the list. (Which allows us
  462. to use the same check in more than one place). If the length can't be converted
  463. to an integer then we need to raise <tt class="docutils literal">VdtParamError</tt>.</p>
  464. <p>Next we check that the value is a list. Anything else should raise a
  465. <tt class="docutils literal">VdtTypeError</tt>. The list should also have 'length' entries. If the list
  466. has more or less entries then we will need to raise a
  467. <tt class="docutils literal">VdtValueTooShortError</tt> or a <tt class="docutils literal">VdtValueTooLongError</tt>.</p>
  468. <p>Then we need to check every entry in the list. Each entry should be an integer
  469. between 0 and 99, or a string representation of an integer between 0 and 99.
  470. Any other type is a <tt class="docutils literal">VdtTypeError</tt>, any other value is a
  471. <tt class="docutils literal">VdtValueError</tt> (either too big, or too small).</p>
  472. <div class="highlight"><pre><span class="k">def</span> <span class="nf">special_list</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">length</span><span class="p">):</span>
  473.     <span class="sd">"""</span>
  474. <span class="sd">    Check that the supplied value is a list of integers,</span>
  475. <span class="sd">    with 'length' entries, and each entry between 0 and 99.</span>
  476. <span class="sd">    """</span>
  477.     <span class="c"># length is supplied as a string</span>
  478.     <span class="c"># we need to convert it to an integer</span>
  479.     <span class="k">try</span><span class="p">:</span>
  480.         <span class="n">length</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">length</span><span class="p">)</span>
  481.     <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
  482.         <span class="k">raise</span> <span class="n">VdtParamError</span><span class="p">(</span><span class="s">'length'</span><span class="p">,</span> <span class="n">length</span><span class="p">)</span>
  483.     <span class="c">#</span>
  484.     <span class="c"># Check the supplied value is a list</span>
  485.     <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
  486.         <span class="k">raise</span> <span class="n">VdtTypeError</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
  487.     <span class="c">#</span>
  488.     <span class="c"># check the length of the list is correct</span>
  489.     <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="o">></span> <span class="n">length</span><span class="p">:</span>
  490.         <span class="k">raise</span> <span class="n">VdtValueTooLongError</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
  491.     <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="o"><</span> <span class="n">length</span><span class="p">:</span>
  492.         <span class="k">raise</span> <span class="n">VdtValueTooShortError</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
  493.     <span class="c">#</span>
  494.     <span class="c"># Next, check every member in the list</span>
  495.     <span class="c"># converting strings as necessary</span>
  496.     <span class="n">out</span> <span class="o">=</span> <span class="p">[]</span>
  497.     <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
  498.         <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">entry</span><span class="p">,</span> <span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="nb">unicode</span><span class="p">,</span> <span class="nb">int</span><span class="p">)):</span>
  499.             <span class="c"># a value in the list</span>
  500.             <span class="c"># is neither an integer nor a string</span>
  501.             <span class="k">raise</span> <span class="n">VdtTypeError</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
  502.         <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">entry</span><span class="p">,</span> <span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="nb">unicode</span><span class="p">)):</span>
  503.             <span class="k">if</span> <span class="ow">not</span> <span class="n">entry</span><span class="o">.</span><span class="n">isdigit</span><span class="p">():</span>
  504.                 <span class="k">raise</span> <span class="n">VdtTypeError</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
  505.             <span class="k">else</span><span class="p">:</span>
  506.                 <span class="n">entry</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span>
  507.         <span class="k">if</span> <span class="n">entry</span> <span class="o"><</span> <span class="mi">0</span><span class="p">:</span>
  508.             <span class="k">raise</span> <span class="n">VdtValueTooSmallError</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
  509.         <span class="k">elif</span> <span class="n">entry</span> <span class="o">></span> <span class="mi">99</span><span class="p">:</span>
  510.             <span class="k">raise</span> <span class="n">VdtValueTooBigError</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
  511.         <span class="n">out</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span>
  512.     <span class="c">#</span>
  513.     <span class="c"># if we got this far, all is well</span>
  514.     <span class="c"># return the new list</span>
  515.     <span class="k">return</span> <span class="n">out</span>
  516. </pre></div>
  517. <p>If you are only using validate from ConfigObj then the error type (<em>TooBig</em>,
  518. <em>TooSmall</em>, etc) is lost - so you may only want to raise <tt class="docutils literal">VdtValueError</tt>.</p>
  519. <div class="caution">
  520. <p class="first admonition-title">Caution!</p>
  521. <p>If your function raises an exception that isn't a subclass of
  522. <tt class="docutils literal">ValidateError</tt>, then ConfigObj won't trap it. This means validation will
  523. fail.</p>
  524. <p class="last">This is why our function starts by checking the type of the value. If we
  525. are passed the wrong type (e.g. an integer rather than a list) we get a
  526. <tt class="docutils literal">VdtTypeError</tt> rather than bombing out when we try to iterate over
  527. the value.</p>
  528. </div>
  529. <p>If you are using validate in another circumstance you may want to create your
  530. own subclasses of <tt class="docutils literal">ValidateError</tt> which convey more specific information.</p>
  531. </div>
  532. </div>
  533. <div class="section" id="known-issues">
  534. <h1><a class="toc-backref" href="#id17">7   Known Issues</a></h1>
  535. <p>The following parses and then blows up. The resulting error message
  536. is confusing:</p>
  537. <blockquote>
  538. <tt class="docutils literal">checkname(default=list(1, 2, 3, 4)</tt></blockquote>
  539. <p>This is because it parses as: <tt class="docutils literal"><span class="pre">checkname(default="list(1",</span> 2, 3, 4)</tt>.
  540. That isn't actually unreasonable, but the error message won't help you
  541. work out what has happened.</p>
  542. </div>
  543. <div class="section" id="todo">
  544. <h1><a class="toc-backref" href="#id18">8   TODO</a></h1>
  545. <ul class="simple">
  546. <li>A regex check function ?</li>
  547. <li>A timestamp check function ? (Using the <tt class="docutils literal">parse</tt> function from <tt class="docutils literal">DateUtil</tt> perhaps).</li>
  548. </ul>
  549. </div>
  550. <div class="section" id="issues">
  551. <h1><a class="toc-backref" href="#id19">9   ISSUES</a></h1>
  552. <div class="note">
  553. <p class="first admonition-title">Note</p>
  554. <p class="last">Please file any bug reports to <a class="reference external" href="mailto:fuzzyman%40voidspace.org.uk">Michael Foord</a> or the ConfigObj
  555. <a class="reference external" href="http://lists.sourceforge.net/lists/listinfo/configobj-develop">Mailing List</a>.</p>
  556. </div>
  557. <p>If we could pull tuples out of arguments, it would be easier
  558. to specify arguments for 'mixed_lists'.</p>
  559. </div>
  560. <div class="section" id="changelog">
  561. <h1><a class="toc-backref" href="#id20">10   CHANGELOG</a></h1>
  562. <div class="section" id="version-1-0-1">
  563. <h2><a class="toc-backref" href="#id21">10.1   2009/10/25 - Version 1.0.1</a></h2>
  564. <ul class="simple">
  565. <li>BUGFIX: Fixed compatibility with Python 2.3.</li>
  566. </ul>
  567. </div>
  568. <div class="section" id="version-1-0-0">
  569. <h2><a class="toc-backref" href="#id22">10.2   2009/04/13 - Version 1.0.0</a></h2>
  570. <ul class="simple">
  571. <li>BUGFIX: can now handle multiline strings.</li>
  572. <li>Addition of 'force_list' validation option.</li>
  573. </ul>
  574. <p>As the API is stable and there are no known bugs or outstanding feature requests I am marking this 1.0.</p>
  575. </div>
  576. <div class="section" id="version-0-3-2">
  577. <h2><a class="toc-backref" href="#id23">10.3   2008/02/24 - Version 0.3.2</a></h2>
  578. <p>BUGFIX: Handling of None as default value fixed.</p>
  579. </div>
  580. <div class="section" id="version-0-3-1">
  581. <h2><a class="toc-backref" href="#id24">10.4   2008/02/05 - Version 0.3.1</a></h2>
  582. <p>BUGFIX: Unicode checks no longer broken.</p>
  583. </div>
  584. <div class="section" id="version-0-3-0">
  585. <h2><a class="toc-backref" href="#id25">10.5   2008/02/05 - Version 0.3.0</a></h2>
  586. <p>Improved performance with a parse cache.</p>
  587. <p>New <tt class="docutils literal">get_default_value</tt> method. Given a check it returns the default
  588. value (converted to the correct type) or raises a <tt class="docutils literal">KeyError</tt> if the
  589. check doesn't specify a default.</p>
  590. <p>Added 'tuple' check and corresponding 'is_tuple' function (which always returns a tuple).</p>
  591. <p>BUGFIX: A quoted 'None' as a default value is no longer treated as None,
  592. but as the string 'None'.</p>
  593. <p>BUGFIX: We weren't unquoting keyword arguments of length two, so an
  594. empty string didn't work as a default.</p>
  595. <p>BUGFIX: Strings no longer pass the 'is_list' check. Additionally, the
  596. list checks always return lists.</p>
  597. <p>A couple of documentation bug fixes.</p>
  598. <p>Removed CHANGELOG from module.</p>
  599. </div>
  600. <div class="section" id="version-0-2-3">
  601. <h2><a class="toc-backref" href="#id26">10.6   2007/02/04      Version 0.2.3</a></h2>
  602. <p>Release of 0.2.3</p>
  603. </div>
  604. <div class="section" id="version-0-2-3-alpha1">
  605. <h2><a class="toc-backref" href="#id27">10.7   2006/12/17      Version 0.2.3-alpha1</a></h2>
  606. <p>By Nicola Larosa</p>
  607. <p>Fixed validate doc to talk of <tt class="docutils literal">boolean</tt> instead of <tt class="docutils literal">bool</tt>; changed the
  608. <tt class="docutils literal">is_bool</tt> function to <tt class="docutils literal">is_boolean</tt> (Sourceforge bug #1531525).</p>
  609. </div>
  610. <div class="section" id="version-0-2-2">
  611. <h2><a class="toc-backref" href="#id28">10.8   2006/04/29      Version 0.2.2</a></h2>
  612. <p>Addressed bug where a string would pass the <tt class="docutils literal">is_list</tt> test. (Thanks to
  613. Konrad Wojas.)</p>
  614. </div>
  615. <div class="section" id="version-0-2-1">
  616. <h2><a class="toc-backref" href="#id29">10.9   2005/12/16      Version 0.2.1</a></h2>
  617. <p>Fixed bug so we can handle keyword argument values with commas.</p>
  618. <p>We now use a list constructor for passing list values to keyword arguments
  619. (including <tt class="docutils literal">default</tt>):</p>
  620. <pre class="literal-block">
  621. default=list("val", "val", "val")
  622. </pre>
  623. <p>Added the <tt class="docutils literal">_test</tt> test.</p>
  624. <p>Moved a function call outside a try...except block.</p>
  625. </div>
  626. <div class="section" id="version-0-2-0">
  627. <h2><a class="toc-backref" href="#id30">10.10   2005/08/18      Version 0.2.0</a></h2>
  628. <p>Updated by <a class="reference external" href="mailto:fuzzyman%40voidspace.org.uk">Michael Foord</a> and <a class="reference external" href="mailto:nico%40teknico.net">Nicola Larosa</a></p>
  629. <p>Does type conversion as well.</p>
  630. </div>
  631. <div class="section" id="version-0-1-0">
  632. <h2><a class="toc-backref" href="#id31">10.11   2005/02/01      Version 0.1.0</a></h2>
  633. <p>Initial version developed by <a class="reference external" href="mailto:fuzzyman%40voidspace.org.uk">Michael Foord</a>
  634. and Mark Andrews.</p>
  635. </div>
  636. </div>
  637. </div>
  638. </body>
  639. </html>
  640.