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 / configobj.html next >
Encoding:
Extensible Markup Language  |  2010-03-01  |  181.6 KB  |  2,672 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>Reading and Writing Config Files</title>
  8. <meta name="authors" content="Michael Foord  Nicola Larosa" />
  9. <meta name="date" content="2010/02/27" />
  10. <meta content="ConfigObj - a Python module for easy reading and writing of config files." name="description" />
  11. <meta content="python, script, module, config, configuration, data, persistence, developer, configparser" name="keywords" />
  12. <link rel="stylesheet" href="docutils.css" type="text/css" />
  13. </head>
  14. <body>
  15. <div class="document" id="reading-and-writing-config-files">
  16. <h1 class="title">Reading and Writing Config Files</h1>
  17. <h2 class="subtitle" id="configobj-4-introduction-and-reference">ConfigObj 4 Introduction and Reference</h2>
  18. <table class="docinfo" frame="void" rules="none">
  19. <col class="docinfo-name" />
  20. <col class="docinfo-content" />
  21. <tbody valign="top">
  22. <tr><th class="docinfo-name">Authors:</th>
  23. <td>Michael Foord
  24. <br />Nicola Larosa</td></tr>
  25. <tr><th class="docinfo-name">Version:</th>
  26. <td>ConfigObj 4.7.2</td></tr>
  27. <tr><th class="docinfo-name">Date:</th>
  28. <td>2010/02/27</td></tr>
  29. <tr class="field"><th class="docinfo-name">Homepage:</th><td class="field-body"><a class="reference external" href="http://www.voidspace.org.uk/python/configobj.html">ConfigObj Homepage</a></td>
  30. </tr>
  31. <tr class="field"><th class="docinfo-name">PyPI Entry:</th><td class="field-body"><a class="reference external" href="http://pypi.python.org/pypi/configobj/">ConfigObj on PyPI</a></td>
  32. </tr>
  33. <tr class="field"><th class="docinfo-name">Development:</th><td class="field-body"><a class="reference external" href="http://code.google.com/p/configobj/">Google Code Homepage</a></td>
  34. </tr>
  35. <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>
  36. </tr>
  37. <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>
  38. </tr>
  39. </tbody>
  40. </table>
  41. <div class="contents topic" id="configobj-manual">
  42. <p class="topic-title first">ConfigObj Manual</p>
  43. <ul class="auto-toc simple">
  44. <li><a class="reference internal" href="#introduction" id="id27">1   Introduction</a></li>
  45. <li><a class="reference internal" href="#downloading" id="id28">2   Downloading</a><ul class="auto-toc">
  46. <li><a class="reference internal" href="#installing" id="id29">2.1   Installing</a></li>
  47. <li><a class="reference internal" href="#documentation" id="id30">2.2   Documentation</a></li>
  48. <li><a class="reference internal" href="#development-version" id="id31">2.3   Development Version</a></li>
  49. </ul>
  50. </li>
  51. <li><a class="reference internal" href="#configobj-in-the-real-world" id="id32">3   ConfigObj in the Real World</a></li>
  52. <li><a class="reference internal" href="#getting-started" id="id33">4   Getting Started</a><ul class="auto-toc">
  53. <li><a class="reference internal" href="#reading-a-config-file" id="id34">4.1   Reading a Config File</a></li>
  54. <li><a class="reference internal" href="#writing-a-config-file" id="id35">4.2   Writing a Config File</a></li>
  55. <li><a class="reference internal" href="#config-files" id="id36">4.3   Config Files</a></li>
  56. </ul>
  57. </li>
  58. <li><a class="reference internal" href="#configobj-specifications" id="id37">5   ConfigObj specifications</a><ul class="auto-toc">
  59. <li><a class="reference internal" href="#methods" id="id38">5.1   Methods</a><ul class="auto-toc">
  60. <li><a class="reference internal" href="#write" id="id39">5.1.1   write</a></li>
  61. <li><a class="reference internal" href="#validate" id="id40">5.1.2   validate</a><ul class="auto-toc">
  62. <li><a class="reference internal" href="#return-value" id="id41">5.1.2.1   Return Value</a></li>
  63. <li><a class="reference internal" href="#mentioning-default-values" id="id42">5.1.2.2   Mentioning Default Values</a></li>
  64. <li><a class="reference internal" href="#mentioning-repeated-sections-and-values" id="id43">5.1.2.3   Mentioning Repeated Sections and Values</a></li>
  65. <li><a class="reference internal" href="#mentioning-simpleval" id="id44">5.1.2.4   Mentioning SimpleVal</a></li>
  66. <li><a class="reference internal" href="#mentioning-copy-mode" id="id45">5.1.2.5   Mentioning copy Mode</a></li>
  67. </ul>
  68. </li>
  69. <li><a class="reference internal" href="#reload" id="id46">5.1.3   reload</a></li>
  70. <li><a class="reference internal" href="#reset" id="id47">5.1.4   reset</a></li>
  71. </ul>
  72. </li>
  73. <li><a class="reference internal" href="#attributes" id="id48">5.2   Attributes</a><ul class="auto-toc">
  74. <li><a class="reference internal" href="#interpolation" id="id49">5.2.1   interpolation</a></li>
  75. <li><a class="reference internal" href="#stringify" id="id50">5.2.2   stringify</a></li>
  76. <li><a class="reference internal" href="#bom" id="id51">5.2.3   BOM</a></li>
  77. <li><a class="reference internal" href="#initial-comment" id="id52">5.2.4   initial_comment</a></li>
  78. <li><a class="reference internal" href="#final-comment" id="id53">5.2.5   final_comment</a></li>
  79. <li><a class="reference internal" href="#list-values" id="id54">5.2.6   list_values</a></li>
  80. <li><a class="reference internal" href="#encoding" id="id55">5.2.7   encoding</a></li>
  81. <li><a class="reference internal" href="#default-encoding" id="id56">5.2.8   default_encoding</a></li>
  82. <li><a class="reference internal" href="#unrepr" id="id57">5.2.9   unrepr</a></li>
  83. <li><a class="reference internal" href="#write-empty-values" id="id58">5.2.10   write_empty_values</a></li>
  84. <li><a class="reference internal" href="#newlines" id="id59">5.2.11   newlines</a></li>
  85. </ul>
  86. </li>
  87. </ul>
  88. </li>
  89. <li><a class="reference internal" href="#the-config-file-format" id="id60">6   The Config File Format</a></li>
  90. <li><a class="reference internal" href="#sections" id="id61">7   Sections</a><ul class="auto-toc">
  91. <li><a class="reference internal" href="#section-attributes" id="id62">7.1   Section Attributes</a></li>
  92. <li><a class="reference internal" href="#section-methods" id="id63">7.2   Section Methods</a></li>
  93. <li><a class="reference internal" href="#walking-a-section" id="id64">7.3   Walking a Section</a></li>
  94. <li><a class="reference internal" href="#examples" id="id65">7.4   Examples</a></li>
  95. </ul>
  96. </li>
  97. <li><a class="reference internal" href="#exceptions" id="id66">8   Exceptions</a></li>
  98. <li><a class="reference internal" href="#validation" id="id67">9   Validation</a><ul class="auto-toc">
  99. <li><a class="reference internal" href="#configspec" id="id68">9.1   configspec</a></li>
  100. <li><a class="reference internal" href="#type-conversion" id="id69">9.2   Type Conversion</a></li>
  101. <li><a class="reference internal" href="#default-values" id="id70">9.3   Default Values</a><ul class="auto-toc">
  102. <li><a class="reference internal" href="#id13" id="id71">9.3.1   List Values</a></li>
  103. </ul>
  104. </li>
  105. <li><a class="reference internal" href="#repeated-sections" id="id72">9.4   Repeated Sections</a></li>
  106. <li><a class="reference internal" href="#repeated-values" id="id73">9.5   Repeated Values</a></li>
  107. <li><a class="reference internal" href="#copy-mode" id="id74">9.6   Copy Mode</a></li>
  108. <li><a class="reference internal" href="#validation-and-interpolation" id="id75">9.7   Validation and Interpolation</a></li>
  109. <li><a class="reference internal" href="#extra-values" id="id76">9.8   Extra Values</a></li>
  110. <li><a class="reference internal" href="#simpleval" id="id77">9.9   SimpleVal</a></li>
  111. </ul>
  112. </li>
  113. <li><a class="reference internal" href="#empty-values" id="id78">10   Empty values</a></li>
  114. <li><a class="reference internal" href="#unrepr-mode" id="id79">11   unrepr mode</a></li>
  115. <li><a class="reference internal" href="#string-interpolation" id="id80">12   String Interpolation</a><ul class="auto-toc">
  116. <li><a class="reference internal" href="#string-interpolation-and-list-values" id="id81">12.1   String Interpolation and List Values</a></li>
  117. </ul>
  118. </li>
  119. <li><a class="reference internal" href="#comments" id="id82">13   Comments</a></li>
  120. <li><a class="reference internal" href="#flatten-errors" id="id83">14   flatten_errors</a><ul class="auto-toc">
  121. <li><a class="reference internal" href="#example-usage" id="id84">14.1   Example Usage</a></li>
  122. </ul>
  123. </li>
  124. <li><a class="reference internal" href="#get-extra-values" id="id85">15   get_extra_values</a><ul class="auto-toc">
  125. <li><a class="reference internal" href="#id14" id="id86">15.1   Example Usage</a></li>
  126. </ul>
  127. </li>
  128. <li><a class="reference internal" href="#credits" id="id87">16   CREDITS</a></li>
  129. <li><a class="reference internal" href="#license" id="id88">17   LICENSE</a></li>
  130. <li><a class="reference internal" href="#todo" id="id89">18   TODO</a></li>
  131. <li><a class="reference internal" href="#issues" id="id90">19   ISSUES</a></li>
  132. <li><a class="reference internal" href="#changelog" id="id91">20   CHANGELOG</a><ul class="auto-toc">
  133. <li><a class="reference internal" href="#version-4-7-2" id="id92">20.1   2010/02/27 - Version 4.7.2</a></li>
  134. <li><a class="reference internal" href="#version-4-7-1" id="id93">20.2   2010/02/06 - Version 4.7.1</a></li>
  135. <li><a class="reference internal" href="#version-4-7-0" id="id94">20.3   2010/01/09 - Version 4.7.0</a></li>
  136. <li><a class="reference internal" href="#version-4-6-0" id="id95">20.4   2009/04/13 - Version 4.6.0</a></li>
  137. <li><a class="reference internal" href="#version-4-5-3" id="id96">20.5   2008/06/27 - Version 4.5.3</a></li>
  138. <li><a class="reference internal" href="#version-4-5-2" id="id97">20.6   2008/02/05 - Version 4.5.2</a></li>
  139. <li><a class="reference internal" href="#version-4-5-1" id="id98">20.7   2008/02/05 - Version 4.5.1</a></li>
  140. <li><a class="reference internal" href="#version-4-5-0" id="id99">20.8   2008/02/05 - Version 4.5.0</a></li>
  141. <li><a class="reference internal" href="#version-4-4-0" id="id100">20.9   2007/02/04 - Version 4.4.0</a></li>
  142. <li><a class="reference internal" href="#version-4-3-3-alpha4" id="id101">20.10   2006/12/17 - Version 4.3.3-alpha4</a></li>
  143. <li><a class="reference internal" href="#version-4-3-3-alpha3" id="id102">20.11   2006/12/17 - Version 4.3.3-alpha3</a></li>
  144. <li><a class="reference internal" href="#version-4-3-3-alpha2" id="id103">20.12   2006/12/09 - Version 4.3.3-alpha2</a></li>
  145. <li><a class="reference internal" href="#version-4-3-3-alpha1" id="id104">20.13   2006/12/09 - Version 4.3.3-alpha1</a></li>
  146. <li><a class="reference internal" href="#version-4-3-2" id="id105">20.14   2006/06/04 - Version 4.3.2</a></li>
  147. <li><a class="reference internal" href="#version-4-3-1" id="id106">20.15   2006/04/29 - Version 4.3.1</a></li>
  148. <li><a class="reference internal" href="#version-4-3-0" id="id107">20.16   2006/03/24 - Version 4.3.0</a></li>
  149. <li><a class="reference internal" href="#version-4-2-0" id="id108">20.17   2006/02/16 - Version 4.2.0</a></li>
  150. <li><a class="reference internal" href="#version-4-1-0" id="id109">20.18   2005/12/14 - Version 4.1.0</a></li>
  151. <li><a class="reference internal" href="#version-4-0-2" id="id110">20.19   2005/12/02 - Version 4.0.2</a></li>
  152. <li><a class="reference internal" href="#version-4-0-1" id="id111">20.20   2005/11/05 - Version 4.0.1</a></li>
  153. <li><a class="reference internal" href="#version-4-0-0" id="id112">20.21   2005/10/17 - Version 4.0.0</a></li>
  154. <li><a class="reference internal" href="#version-4-0-0-beta-5" id="id113">20.22   2005/09/09 - Version 4.0.0 beta 5</a></li>
  155. <li><a class="reference internal" href="#version-4-0-0-beta-4" id="id114">20.23   2005/09/07 - Version 4.0.0 beta 4</a></li>
  156. <li><a class="reference internal" href="#version-4-0-0-beta-3" id="id115">20.24   2005/08/28 - Version 4.0.0 beta 3</a></li>
  157. <li><a class="reference internal" href="#version-4-0-0-beta-2" id="id116">20.25   2005/08/25 - Version 4.0.0 beta 2</a></li>
  158. <li><a class="reference internal" href="#version-4-0-0-beta-1" id="id117">20.26   2005/08/21 - Version 4.0.0 beta 1</a></li>
  159. <li><a class="reference internal" href="#version-3-0-0" id="id118">20.27   2004/05/24 - Version 3.0.0</a></li>
  160. <li><a class="reference internal" href="#version-2-0-0-beta" id="id119">20.28   2004/03/14 - Version 2.0.0 beta</a></li>
  161. <li><a class="reference internal" href="#version-1-0-5" id="id120">20.29   2004/01/29 - Version 1.0.5</a></li>
  162. <li><a class="reference internal" href="#origins" id="id121">20.30   Origins</a></li>
  163. </ul>
  164. </li>
  165. <li><a class="reference internal" href="#footnotes" id="id122">21   Footnotes</a></li>
  166. </ul>
  167. </div>
  168. <div class="note">
  169. <p class="first admonition-title">Note</p>
  170. <p>The best introduction to working with ConfigObj, including the powerful configuration validation system,
  171. is the article:</p>
  172. <ul class="last simple">
  173. <li><a class="reference external" href="http://www.voidspace.org.uk/python/articles/configobj.shtml">An Introduction to ConfigObj</a></li>
  174. </ul>
  175. </div>
  176. <div class="section" id="introduction">
  177. <h1><a class="toc-backref" href="#id27">1   Introduction</a></h1>
  178. <p><strong>ConfigObj</strong> is a simple but powerful config file reader and writer: an <em>ini
  179. file round tripper</em>. Its main feature is that it is very easy to use, with a
  180. straightforward programmer's interface and a simple syntax for config files.
  181. It has lots of other features though :</p>
  182. <ul>
  183. <li><p class="first">Nested sections (subsections), to any level</p>
  184. </li>
  185. <li><p class="first">List values</p>
  186. </li>
  187. <li><p class="first">Multiple line values</p>
  188. </li>
  189. <li><p class="first">String interpolation (substitution)</p>
  190. </li>
  191. <li><p class="first">Integrated with a powerful validation system</p>
  192. <blockquote>
  193. <ul class="simple">
  194. <li>including automatic type checking/conversion</li>
  195. <li>repeated sections</li>
  196. <li>and allowing default values</li>
  197. </ul>
  198. </blockquote>
  199. </li>
  200. <li><p class="first">When writing out config files, ConfigObj preserves all comments and the order of members and sections</p>
  201. </li>
  202. <li><p class="first">Many useful methods and options for working with configuration files (like the 'reload' method)</p>
  203. </li>
  204. <li><p class="first">Full Unicode support</p>
  205. </li>
  206. </ul>
  207. <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> or the issue tracker on the
  208. <a class="reference external" href="http://code.google.com/p/configobj/">Google Code Homepage</a>.</p>
  209. </div>
  210. <div class="section" id="downloading">
  211. <h1><a class="toc-backref" href="#id28">2   Downloading</a></h1>
  212. <p>The current version is <strong>4.7.2</strong>, dated 27th February 2010. ConfigObj 4 is
  213. stable and mature. We still expect to pick up a few bugs along the way though <a class="footnote-reference" href="#id15" id="id1">[1]</a>.</p>
  214. <p>You can get ConfigObj in the following ways :</p>
  215. <ul>
  216. <li><p class="first"><a class="reference external" href="http://www.voidspace.org.uk/downloads/configobj.py">configobj.py</a> from Voidspace</p>
  217. <blockquote>
  218. <p>ConfigObj has no external dependencies. This file is sufficient to access
  219. all the functionality except <a class="reference internal" href="#validation">Validation</a>.</p>
  220. </blockquote>
  221. </li>
  222. <li><p class="first"><a class="reference external" href="http://www.voidspace.org.uk/downloads/configobj-4.7.2.zip">configobj.zip</a> from Voidspace</p>
  223. <blockquote>
  224. <p>This also contains <a class="reference external" href="http://www.voidspace.org.uk/downloads/validate.py">validate.py</a>  and <a class="reference external" href="http://www.voidspace.org.uk/python/configobj.html">this document</a>.</p>
  225. </blockquote>
  226. </li>
  227. <li><p class="first"><a class="reference external" href="http://www.voidspace.org.uk/downloads/validate.py">validate.py</a> from Voidspace</p>
  228. </li>
  229. </ul>
  230. <div class="section" id="installing">
  231. <h2><a class="toc-backref" href="#id29">2.1   Installing</a></h2>
  232. <p>ConfigObj has a source distribution <a class="reference external" href="http://pypi.python.org/pypi/configobj/">on PyPI</a>. If you unzip
  233. the archive you can install it with:</p>
  234. <pre class="literal-block">
  235. setup.py install
  236. </pre>
  237. <p>Alternatively, you can install with easy install or pip:</p>
  238. <pre class="literal-block">
  239. easy_install configobj
  240. </pre>
  241. </div>
  242. <div class="section" id="documentation">
  243. <h2><a class="toc-backref" href="#id30">2.2   Documentation</a></h2>
  244. <p><em>configobj.zip</em> also contains <a class="reference external" href="http://www.voidspace.org.uk/python/configobj.html">this document</a>.</p>
  245. <ul class="simple">
  246. <li>You can view <a class="reference external" href="http://www.voidspace.org.uk/python/configobj.html">this document</a> online at the <a class="reference external" href="http://www.voidspace.org.uk/python/configobj.html">ConfigObj Homepage</a>.</li>
  247. </ul>
  248. </div>
  249. <div class="section" id="development-version">
  250. <h2><a class="toc-backref" href="#id31">2.3   Development Version</a></h2>
  251. <p>It is sometimes possible to get the latest <em>development version</em> of ConfigObj
  252. from the Subversion Repository maintained on the <a class="reference external" href="http://code.google.com/p/configobj/">Google Code Homepage</a>.</p>
  253. </div>
  254. </div>
  255. <div class="section" id="configobj-in-the-real-world">
  256. <h1><a class="toc-backref" href="#id32">3   ConfigObj in the Real World</a></h1>
  257. <p><strong>ConfigObj</strong> is widely used. Projects using it include:</p>
  258. <ul>
  259. <li><p class="first"><a class="reference external" href="http://bazaar-ng.org">Bazaar</a>.</p>
  260. <blockquote>
  261. <p>Bazaar is a Python distributed {acro;VCS;Version Control System}.
  262. ConfigObj is used to read <tt class="docutils literal">bazaar.conf</tt> and <tt class="docutils literal">branches.conf</tt>.</p>
  263. </blockquote>
  264. </li>
  265. <li><p class="first"><a class="reference external" href="http://chandler.osafoundation.org/">Chandler</a></p>
  266. <blockquote>
  267. <p>A Python and <a class="reference external" href="http://www.wxpython.org">wxPython</a>
  268. Personal Information Manager, being developed by the
  269. <a class="reference external" href="http://www.osafoundation.org/">OSAFoundation</a>.</p>
  270. </blockquote>
  271. </li>
  272. <li><p class="first"><a class="reference external" href="http://matplotlib.sourceforge.net/">matplotlib</a></p>
  273. <blockquote>
  274. <p>A 2D plotting library.</p>
  275. </blockquote>
  276. </li>
  277. <li><p class="first"><a class="reference external" href="http://ipython.scipy.org/moin/">IPython</a></p>
  278. <blockquote>
  279. <p>IPython is an enhanced interactive Python shell. IPython uses ConfigObj in a module called 'TConfig' that combines it with enthought <a class="reference external" href="http://code.enthought.com/traits/">Traits</a>: <a class="reference external" href="http://ipython.scipy.org/ipython/ipython/browser/ipython/branches/saw/sandbox/tconfig">tconfig</a>.</p>
  280. </blockquote>
  281. </li>
  282. <li><p class="first"><a class="reference external" href="http://elisa.fluendo.com/">Elisa - the Fluendo Mediacenter</a></p>
  283. <blockquote>
  284. <p>Elisa is an open source cross-platform media center solution designed to be simple for people not particularly familiar with computers.</p>
  285. </blockquote>
  286. </li>
  287. </ul>
  288. </div>
  289. <div class="section" id="getting-started">
  290. <h1><a class="toc-backref" href="#id33">4   Getting Started</a></h1>
  291. <p>The outstanding feature of using ConfigObj is simplicity. Most functions can be
  292. performed with single line commands.</p>
  293. <div class="section" id="reading-a-config-file">
  294. <h2><a class="toc-backref" href="#id34">4.1   Reading a Config File</a></h2>
  295. <p>The normal way to read a config file, is to give ConfigObj the filename :</p>
  296. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">configobj</span> <span class="kn">import</span> <span class="n">ConfigObj</span>
  297. <span class="n">config</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
  298. </pre></div>
  299. <p>You can also pass the config file in as a list of lines, or a <tt class="docutils literal">StringIO</tt>
  300. instance, so it doesn't matter where your config data comes from.</p>
  301. <p>You can then access members of your config file as a dictionary. Subsections
  302. will also be dictionaries.</p>
  303. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">configobj</span> <span class="kn">import</span> <span class="n">ConfigObj</span>
  304. <span class="n">config</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
  305. <span class="c">#</span>
  306. <span class="n">value1</span> <span class="o">=</span> <span class="n">config</span><span class="p">[</span><span class="s">'keyword1'</span><span class="p">]</span>
  307. <span class="n">value2</span> <span class="o">=</span> <span class="n">config</span><span class="p">[</span><span class="s">'keyword2'</span><span class="p">]</span>
  308. <span class="c">#</span>
  309. <span class="n">section1</span> <span class="o">=</span> <span class="n">config</span><span class="p">[</span><span class="s">'section1'</span><span class="p">]</span>
  310. <span class="n">value3</span> <span class="o">=</span> <span class="n">section1</span><span class="p">[</span><span class="s">'keyword3'</span><span class="p">]</span>
  311. <span class="n">value4</span> <span class="o">=</span> <span class="n">section1</span><span class="p">[</span><span class="s">'keyword4'</span><span class="p">]</span>
  312. <span class="c">#</span>
  313. <span class="c"># you could also write</span>
  314. <span class="n">value3</span> <span class="o">=</span> <span class="n">config</span><span class="p">[</span><span class="s">'section1'</span><span class="p">][</span><span class="s">'keyword3'</span><span class="p">]</span>
  315. <span class="n">value4</span> <span class="o">=</span> <span class="n">config</span><span class="p">[</span><span class="s">'section1'</span><span class="p">][</span><span class="s">'keyword4'</span><span class="p">]</span>
  316. </pre></div>
  317. </div>
  318. <div class="section" id="writing-a-config-file">
  319. <h2><a class="toc-backref" href="#id35">4.2   Writing a Config File</a></h2>
  320. <p>Creating a new config file is just as easy as reading one. You can specify a
  321. filename when you create the ConfigObj, or do it later <a class="footnote-reference" href="#id16" id="id2">[2]</a>.</p>
  322. <p>If you <em>don't</em> set a filename, then the <tt class="docutils literal">write</tt> method will return a list of
  323. lines instead of writing to file. See the <a class="reference internal" href="#write">write</a> method for more details.</p>
  324. <p>Here we show creating an empty ConfigObj, setting a filename and some values,
  325. and then writing to file :</p>
  326. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">configobj</span> <span class="kn">import</span> <span class="n">ConfigObj</span>
  327. <span class="n">config</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">()</span>
  328. <span class="n">config</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="n">filename</span>
  329. <span class="c">#</span>
  330. <span class="n">config</span><span class="p">[</span><span class="s">'keyword1'</span><span class="p">]</span> <span class="o">=</span> <span class="n">value1</span>
  331. <span class="n">config</span><span class="p">[</span><span class="s">'keyword2'</span><span class="p">]</span> <span class="o">=</span> <span class="n">value2</span>
  332. <span class="c">#</span>
  333. <span class="n">config</span><span class="p">[</span><span class="s">'section1'</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
  334. <span class="n">config</span><span class="p">[</span><span class="s">'section1'</span><span class="p">][</span><span class="s">'keyword3'</span><span class="p">]</span> <span class="o">=</span> <span class="n">value3</span>
  335. <span class="n">config</span><span class="p">[</span><span class="s">'section1'</span><span class="p">][</span><span class="s">'keyword4'</span><span class="p">]</span> <span class="o">=</span> <span class="n">value4</span>
  336. <span class="c">#</span>
  337. <span class="n">section2</span> <span class="o">=</span> <span class="p">{</span>
  338.     <span class="s">'keyword5'</span><span class="p">:</span> <span class="n">value5</span><span class="p">,</span>
  339.     <span class="s">'keyword6'</span><span class="p">:</span> <span class="n">value6</span><span class="p">,</span>
  340.     <span class="s">'sub-section'</span><span class="p">:</span> <span class="p">{</span>
  341.         <span class="s">'keyword7'</span><span class="p">:</span> <span class="n">value7</span>
  342.         <span class="p">}</span>
  343. <span class="p">}</span>
  344. <span class="n">config</span><span class="p">[</span><span class="s">'section2'</span><span class="p">]</span> <span class="o">=</span> <span class="n">section2</span>
  345. <span class="c">#</span>
  346. <span class="n">config</span><span class="p">[</span><span class="s">'section3'</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
  347. <span class="n">config</span><span class="p">[</span><span class="s">'section3'</span><span class="p">][</span><span class="s">'keyword 8'</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">value8</span><span class="p">,</span> <span class="n">value9</span><span class="p">,</span> <span class="n">value10</span><span class="p">]</span>
  348. <span class="n">config</span><span class="p">[</span><span class="s">'section3'</span><span class="p">][</span><span class="s">'keyword 9'</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">value11</span><span class="p">,</span> <span class="n">value12</span><span class="p">,</span> <span class="n">value13</span><span class="p">]</span>
  349. <span class="c">#</span>
  350. <span class="n">config</span><span class="o">.</span><span class="n">write</span><span class="p">()</span>
  351. </pre></div>
  352. <div class="caution">
  353. <p class="first admonition-title">Caution!</p>
  354. <p>Keywords and section names can only be strings <a class="footnote-reference" href="#id17" id="id3">[3]</a>. Attempting to set
  355. anything else will raise a <tt class="docutils literal">ValueError</tt>.</p>
  356. <p class="last">See <a class="reference internal" href="#string-interpolation-and-list-values">String Interpolation and List Values</a> for an important note on
  357. using lists in combination with <a class="reference internal" href="#string-interpolation">String Interpolation</a>.</p>
  358. </div>
  359. </div>
  360. <div class="section" id="config-files">
  361. <h2><a class="toc-backref" href="#id36">4.3   Config Files</a></h2>
  362. <p>The config files that ConfigObj will read and write are based on the 'INI'
  363. format. This means it will read and write files created for <tt class="docutils literal">ConfigParser</tt>
  364. <a class="footnote-reference" href="#id18" id="id4">[4]</a>.</p>
  365. <p>Keywords and values are separated by an <tt class="docutils literal">'='</tt>, and section markers are
  366. between square brackets. Keywords, values, and section names can be surrounded
  367. by single or double quotes. Indentation is not significant, but can be
  368. preserved.</p>
  369. <p>Subsections are indicated by repeating the square brackets in the section
  370. marker. You nest levels by using more brackets.</p>
  371. <p>You can have list values by separating items with a comma, and values spanning
  372. multiple lines by using triple quotes (single or double).</p>
  373. <p>For full details on all these see <a class="reference internal" href="#the-config-file-format">the config file format</a>. Here's an example
  374. to illustrate:</p>
  375. <pre class="literal-block">
  376. # This is the 'initial_comment'
  377. # Which may be several lines
  378. keyword1 = value1
  379. 'keyword 2' = 'value 2'
  380.  
  381. [ "section 1" ]
  382. # This comment goes with keyword 3
  383. keyword 3 = value 3
  384. 'keyword 4' = value4, value 5, 'value 6'
  385.  
  386.     [[ sub-section ]]    # an inline comment
  387.     # sub-section is inside "section 1"
  388.     'keyword 5' = 'value 7'
  389.     'keyword 6' = '''A multiline value,
  390. that spans more than one line :-)
  391. The line breaks are included in the value.'''
  392.  
  393.         [[[ sub-sub-section ]]]
  394.         # sub-sub-section is *in* 'sub-section'
  395.         # which is in 'section 1'
  396.         'keyword 7' = 'value 8'
  397.  
  398. [section 2]    # an inline comment
  399. keyword8 = "value 9"
  400. keyword9 = value10     # an inline comment
  401. # The 'final_comment'
  402. # Which also may be several lines
  403. </pre>
  404. </div>
  405. </div>
  406. <div class="section" id="configobj-specifications">
  407. <h1><a class="toc-backref" href="#id37">5   ConfigObj specifications</a></h1>
  408. <div class="highlight"><pre><span class="n">config</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">infile</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">options</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">configspec</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
  409.                    <span class="n">interpolation</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">raise_errors</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">list_values</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
  410.                    <span class="n">create_empty</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">file_error</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">stringify</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
  411.                    <span class="n">indent_type</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">default_encoding</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">unrepr</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
  412.                    <span class="n">write_empty_values</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">_inspec</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
  413. </pre></div>
  414. <p>Many of the keyword arguments are available as attributes after the config file has been
  415. parsed.</p>
  416. <div class="note">
  417. <p class="first admonition-title">Note</p>
  418. <p>New in ConfigObj 4.7.0: Instantiating ConfigObj with
  419. an <tt class="docutils literal">options</tt> dictionary is now deprecated. To modify code that used to
  420. do this simply unpack the dictionary in the constructor call:</p>
  421. <div class="last"><div class="highlight"><pre><span class="n">config</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">)</span>
  422. </pre></div>
  423. </div></div>
  424. <p>ConfigObj takes the following arguments (with the default values shown) :</p>
  425. <ul>
  426. <li><p class="first">infile: <tt class="docutils literal">None</tt></p>
  427. <blockquote>
  428. <p>You don't need to specify an infile. If you omit it, an empty ConfigObj will be
  429. created. <tt class="docutils literal">infile</tt> <em>can</em> be :</p>
  430. <ul class="simple">
  431. <li>Nothing. In which case the <tt class="docutils literal">filename</tt> attribute of your ConfigObj will be
  432. <tt class="docutils literal">None</tt>. You can set a filename at any time.</li>
  433. <li>A filename. What happens if the file doesn't already exist is determined by
  434. the options <tt class="docutils literal">file_error</tt> and <tt class="docutils literal">create_empty</tt>. The filename will be
  435. preserved as the <tt class="docutils literal">filename</tt> attribute. This can be changed at any time.</li>
  436. <li>A list of lines. Any trailing newlines will be removed from the lines. The
  437. <tt class="docutils literal">filename</tt> attribute of your ConfigObj will be <tt class="docutils literal">None</tt>.</li>
  438. <li>A <tt class="docutils literal">StringIO</tt> instance or file object, or any object with a <tt class="docutils literal">read</tt> method.
  439. The <tt class="docutils literal">filename</tt> attribute of your ConfigObj will be <tt class="docutils literal">None</tt> <a class="footnote-reference" href="#id19" id="id5">[5]</a>.</li>
  440. <li>A dictionary. You can initialise a ConfigObj from a dictionary <a class="footnote-reference" href="#id20" id="id6">[6]</a>. The
  441. <tt class="docutils literal">filename</tt> attribute of your ConfigObj will be <tt class="docutils literal">None</tt>. All keys must be
  442. strings. In this case, the order of values and sections is arbitrary.</li>
  443. </ul>
  444. </blockquote>
  445. </li>
  446. <li><p class="first">'raise_errors': <tt class="docutils literal">False</tt></p>
  447. <blockquote>
  448. <p>When parsing, it is possible that the config file will be badly formed. The
  449. default is to parse the whole file and raise a single error at the end. You
  450. can set <tt class="docutils literal">raise_errors = True</tt> to have errors raised immediately. See the
  451. <a class="reference internal" href="#exceptions">exceptions</a> section for more details.</p>
  452. <p>Altering this value after initial parsing has no effect.</p>
  453. </blockquote>
  454. </li>
  455. <li><p class="first">'list_values': <tt class="docutils literal">True</tt></p>
  456. <blockquote>
  457. <p>If <tt class="docutils literal">True</tt> (the default) then list values are possible. If <tt class="docutils literal">False</tt>, the
  458. values are not parsed for lists.</p>
  459. <p>If <tt class="docutils literal">list_values = False</tt> then single line values are not quoted or
  460. unquoted when reading and writing.</p>
  461. <p>Changing this value affects whether single line values will be quoted or
  462. not when writing.</p>
  463. </blockquote>
  464. </li>
  465. <li><p class="first">'create_empty': <tt class="docutils literal">False</tt></p>
  466. <blockquote>
  467. <p>If this value is <tt class="docutils literal">True</tt> and the file specified by <tt class="docutils literal">infile</tt> doesn't
  468. exist, ConfigObj will create an empty file. This can be a useful test that
  469. the filename makes sense: an impossible filename will cause an error.</p>
  470. <p>Altering this value after initial parsing has no effect.</p>
  471. </blockquote>
  472. </li>
  473. <li><p class="first">'file_error': <tt class="docutils literal">False</tt></p>
  474. <blockquote>
  475. <p>If this value is <tt class="docutils literal">True</tt> and the file specified by <tt class="docutils literal">infile</tt> doesn't
  476. exist, ConfigObj will raise an <tt class="docutils literal">IOError</tt>.</p>
  477. <p>Altering this value after initial parsing has no effect.</p>
  478. </blockquote>
  479. </li>
  480. <li><p class="first">'interpolation': <tt class="docutils literal">True</tt></p>
  481. <blockquote>
  482. <p>Whether string interpolation is switched on or not. It is on (<tt class="docutils literal">True</tt>) by
  483. default.</p>
  484. <p>You can set this attribute to change whether string interpolation is done
  485. when values are fetched. See the <a class="reference internal" href="#string-interpolation">String Interpolation</a> section for more details.</p>
  486. <p>New in ConfigObj 4.7.0: Interpolation will also be done in list values.</p>
  487. </blockquote>
  488. </li>
  489. <li><p class="first">'configspec': <tt class="docutils literal">None</tt></p>
  490. <blockquote>
  491. <p>If you want to use the validation system, you supply a configspec. This is
  492. effectively a type of config file that specifies a check for each member.
  493. This check can be used to do type conversion as well as check that the
  494. value is within your required parameters.</p>
  495. <p>You provide a configspec in the same way as you do the initial file: a
  496. filename, or list of lines, etc. See the <a class="reference internal" href="#validation">validation</a> section for full
  497. details on how to use the system.</p>
  498. <p>When parsed, every section has a <tt class="docutils literal">configspec</tt> with a dictionary of
  499. configspec checks for <em>that section</em>.</p>
  500. </blockquote>
  501. </li>
  502. <li><p class="first">'stringify': <tt class="docutils literal">True</tt></p>
  503. <blockquote>
  504. <p>If you use the validation scheme, it can do type checking <em>and</em> conversion
  505. for you. This means you may want to set members to integers, or other
  506. non-string values.</p>
  507. <p>If 'stringify' is set to <tt class="docutils literal">True</tt> (default) then non-string values will
  508. be converted to strings when you write the config file. The <a class="reference internal" href="#validation">validation</a>
  509. process converts values from strings to the required type.</p>
  510. <p>If 'stringify' is set to <tt class="docutils literal">False</tt>, attempting to set a member to a
  511. non-string value <a class="footnote-reference" href="#id21" id="id7">[7]</a> will raise a <tt class="docutils literal">TypeError</tt> (no type conversion is
  512. done by validation).</p>
  513. </blockquote>
  514. </li>
  515. <li><p class="first">'indent_type': <tt class="docutils literal">'    '</tt></p>
  516. <blockquote>
  517. <p>Indentation is not significant; it can however be present in the input and
  518. output config. Any combination of tabs and spaces may be used: the string
  519. will be repeated for each level of indentation. Typical values are: <tt class="docutils literal">''</tt>
  520. (no indentation), <tt class="docutils literal">'    '</tt> (indentation with four spaces, the default),
  521. <tt class="docutils literal">'\t'</tt> (indentation with one tab).</p>
  522. <p>If this option is not specified, and the ConfigObj is initialised with a
  523. dictionary, the indentation used in the output is the default one, that is,
  524. four spaces.</p>
  525. <p>If this option is not specified, and the ConfigObj is initialised with a
  526. list of lines or a file, the indentation used in the first indented line is
  527. selected and used in all output lines. If no input line is indented, no
  528. output line will be either.</p>
  529. <p>If this option <em>is</em> specified, the option value is used in the output
  530. config, overriding the type of indentation in the input config (if any).</p>
  531. </blockquote>
  532. </li>
  533. <li><p class="first">'encoding': <tt class="docutils literal">None</tt></p>
  534. <blockquote>
  535. <p>By default <strong>ConfigObj</strong> does not decode the file/strings you pass it into
  536. Unicode <a class="footnote-reference" href="#id22" id="id8">[8]</a>. If you want your config file as Unicode (keys and members)
  537. you need to provide an encoding to decode the file with. This encoding will
  538. also be used to encode the config file when writing.</p>
  539. <p>You can change the encoding attribute at any time.</p>
  540. <p>Any characters in your strings that can't be encoded with the specified
  541. encoding will raise a <tt class="docutils literal">UnicodeEncodeError</tt>.</p>
  542. <div class="note">
  543. <p class="first admonition-title">Note</p>
  544. <p><tt class="docutils literal">UTF16</tt> encoded files will automatically be detected and decoded,
  545. even if <tt class="docutils literal">encoding</tt> is <tt class="docutils literal">None</tt>.</p>
  546. <p class="last">This is because it is a 16-bit encoding, and ConfigObj will mangle it
  547. (split characters on byte boundaries) if it parses it without decoding.</p>
  548. </div>
  549. </blockquote>
  550. </li>
  551. <li><p class="first">'default_encoding': <tt class="docutils literal">None</tt></p>
  552. <blockquote>
  553. <p>When using the <tt class="docutils literal">write</tt> method, <strong>ConfigObj</strong> uses the <tt class="docutils literal">encoding</tt>
  554. attribute to encode the Unicode strings. If any members (or keys) have
  555. been set as byte strings instead of Unicode, these must first be decoded
  556. to Unicode before outputting in the specified encoding.</p>
  557. <p><tt class="docutils literal">default_encoding</tt>, if specified, is the encoding used to decode byte
  558. strings in the <strong>ConfigObj</strong> before writing. If this is <tt class="docutils literal">None</tt>, then
  559. the Python default encoding (<tt class="docutils literal">sys.defaultencoding</tt> - usually ASCII) is
  560. used.</p>
  561. <p>For most Western European users, a value of <tt class="docutils literal"><span class="pre">latin-1</span></tt> is sensible.</p>
  562. <p><tt class="docutils literal">default_encoding</tt> is <em>only</em> used if an <tt class="docutils literal">encoding</tt> is specified.</p>
  563. <p>Any characters in byte-strings that can't be decoded using the
  564. <tt class="docutils literal">default_encoding</tt> will raise a <tt class="docutils literal">UnicodeDecodeError</tt>.</p>
  565. </blockquote>
  566. </li>
  567. <li><p class="first">'unrepr': <tt class="docutils literal">False</tt></p>
  568. <blockquote>
  569. <p>The <tt class="docutils literal">unrepr</tt> option reads and writes files in a different mode. This
  570. allows you to store and retrieve the basic Python data-types using config
  571. files.</p>
  572. <p>This uses Python syntax for lists and quoting. See <a class="reference internal" href="#unrepr-mode">unrepr mode</a> for the
  573. full details.</p>
  574. </blockquote>
  575. </li>
  576. <li><p class="first">'write_empty_values': <tt class="docutils literal">False</tt></p>
  577. <blockquote>
  578. <p>If <tt class="docutils literal">write_empty_values</tt> is <tt class="docutils literal">True</tt>, empty strings are written as
  579. empty values. See <a class="reference internal" href="#empty-values">Empty Values</a> for more details.</p>
  580. </blockquote>
  581. </li>
  582. <li><p class="first">'_inspec': <tt class="docutils literal">False</tt></p>
  583. <blockquote>
  584. <p>Used internally by ConfigObj when parsing configspec files. If you are
  585. creating a ConfigObj instance from a configspec file you must pass True
  586. for this argument as well as <tt class="docutils literal">list_values=False</tt>.</p>
  587. </blockquote>
  588. </li>
  589. </ul>
  590. <div class="section" id="methods">
  591. <h2><a class="toc-backref" href="#id38">5.1   Methods</a></h2>
  592. <p>The ConfigObj is a subclass of an object called <tt class="docutils literal">Section</tt>, which is itself a
  593. subclass of <tt class="docutils literal">dict</tt>, the builtin dictionary type. This means it also has
  594. <strong>all</strong> the normal dictionary methods.</p>
  595. <p>In addition, the following <a class="reference internal" href="#section-methods">Section Methods</a> may be useful :</p>
  596. <ul class="simple">
  597. <li>'restore_default'</li>
  598. <li>'restore_defaults'</li>
  599. <li>'walk'</li>
  600. <li>'merge'</li>
  601. <li>'dict'</li>
  602. <li>'as_bool'</li>
  603. <li>'as_float'</li>
  604. <li>'as_int'</li>
  605. <li>'as_list'</li>
  606. </ul>
  607. <p>Read about <a class="reference internal" href="#sections">Sections</a> for details of all the methods.</p>
  608. <div class="hint">
  609. <p class="first admonition-title">Hint</p>
  610. <p>The <em>merge</em> method of sections is a recursive update.</p>
  611. <p>You can use this to merge sections, or even whole ConfigObjs, into each
  612. other.</p>
  613. <p class="last">You would typically use this to create a default ConfigObj and then merge
  614. in user settings. This way users only need to specify values that are
  615. different from the default. You can use configspecs and validation to
  616. achieve the same thing of course.</p>
  617. </div>
  618. <p>The public methods available on ConfigObj are :</p>
  619. <ul class="simple">
  620. <li>'write'</li>
  621. <li>'validate'</li>
  622. <li>'reset'</li>
  623. <li>'reload'</li>
  624. </ul>
  625. <div class="section" id="write">
  626. <h3><a class="toc-backref" href="#id39">5.1.1   write</a></h3>
  627. <div class="highlight"><pre><span class="n">write</span><span class="p">(</span><span class="n">file_object</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
  628. </pre></div>
  629. <p>This method writes the current ConfigObj and takes a single, optional argument
  630. <a class="footnote-reference" href="#id23" id="id9">[9]</a>.</p>
  631. <p>If you pass in a file like object to the <tt class="docutils literal">write</tt> method, the config file will
  632. be written to this. (The only method of this object that is used is its
  633. <tt class="docutils literal">write</tt> method, so a <tt class="docutils literal">StringIO</tt> instance, or any other file like object
  634. will work.)</p>
  635. <p>Otherwise, the behaviour of this method depends on the <tt class="docutils literal">filename</tt> attribute
  636. of the ConfigObj.</p>
  637. <dl class="docutils">
  638. <dt><tt class="docutils literal">filename</tt></dt>
  639. <dd>ConfigObj will write the configuration to the file specified.</dd>
  640. <dt><tt class="docutils literal">None</tt></dt>
  641. <dd><tt class="docutils literal">write</tt> returns a list of lines. (Not <tt class="docutils literal">'\n'</tt> terminated)</dd>
  642. </dl>
  643. <p>First the 'initial_comment' is written, then the config file, followed by the
  644. 'final_comment'. Comment lines and inline comments are written with each
  645. key/value.</p>
  646. </div>
  647. <div class="section" id="validate">
  648. <h3><a class="toc-backref" href="#id40">5.1.2   validate</a></h3>
  649. <div class="highlight"><pre><span class="n">validate</span><span class="p">(</span><span class="n">validator</span><span class="p">,</span> <span class="n">preserve_errors</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
  650. </pre></div>
  651. <div class="highlight"><pre><span class="c"># filename is the config file</span>
  652. <span class="c"># filename2 is the configspec</span>
  653. <span class="c"># (which could also be hardcoded into your program)</span>
  654. <span class="n">config</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">configspec</span><span class="o">=</span><span class="n">filename2</span><span class="p">)</span>
  655. <span class="c">#</span>
  656. <span class="kn">from</span> <span class="nn">validate</span> <span class="kn">import</span> <span class="n">Validator</span>
  657. <span class="n">val</span> <span class="o">=</span> <span class="n">Validator</span><span class="p">()</span>
  658. <span class="n">test</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
  659. <span class="k">if</span> <span class="n">test</span> <span class="o">==</span> <span class="bp">True</span><span class="p">:</span>
  660.     <span class="k">print</span> <span class="s">'Succeeded.'</span>
  661. </pre></div>
  662. <p>The validate method uses the <a class="reference external" href="http://www.voidspace.org.uk/python/validate.html">validate</a> module to do the
  663. validation.</p>
  664. <p>This method validates the ConfigObj against the configspec. By doing type
  665. conversion as well it can abstract away the config file altogether and present
  666. the config <em>data</em> to your application (in the types it expects it to be).</p>
  667. <p>If the <tt class="docutils literal">configspec</tt> attribute of the ConfigObj is <tt class="docutils literal">None</tt>, it raises a
  668. <tt class="docutils literal">ValueError</tt>.</p>
  669. <p>If the <a class="reference internal" href="#stringify">stringify</a> attribute is set, this process will convert values to the
  670. type defined in the configspec.</p>
  671. <p>The validate method uses checks specified in the configspec and defined in the
  672. <tt class="docutils literal">Validator</tt> object. It is very easy to extend.</p>
  673. <p>The configspec looks like the config file, but instead of the value, you
  674. specify the check (and any default value). See the <a class="reference internal" href="#validation">validation</a> section for
  675. details.</p>
  676. <div class="hint">
  677. <p class="first admonition-title">Hint</p>
  678. <p>The system of configspecs can seem confusing at first, but is actually
  679. quite simple and powerful. The best guide to them is this article on
  680. ConfigObj:</p>
  681. <ul class="last simple">
  682. <li><a class="reference external" href="http://www.voidspace.org.uk/python/articles/configobj.shtml">An Introduction to ConfigObj</a></li>
  683. </ul>
  684. </div>
  685. <p>The <tt class="docutils literal">copy</tt> parameter fills in missing values from the configspec (default
  686. values), <em>without</em> marking the values as defaults. It also causes comments to
  687. be copied from the configspec into the config file. This allows you to use a
  688. configspec to create default config files. (Normally default values aren't
  689. written out by the <tt class="docutils literal">write</tt> method.)</p>
  690. <p>As of ConfigObj 4.3.0 you can also pass in a ConfigObj instance as your
  691. configspec. This is especially useful if you need to specify the encoding of
  692. your configspec file. When you read your configspec file, you <em>must</em> specify
  693. <tt class="docutils literal">list_values=False</tt>. If you need to support hashes inside the configspec
  694. values then you must also pass in <tt class="docutils literal">_inspec=True</tt>. This is because configspec
  695. files actually use a different syntax to config files and inline comment support
  696. must be switched off to correctly read configspec files with hashes in the values.</p>
  697. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">configobj</span> <span class="kn">import</span> <span class="n">ConfigObj</span>
  698. <span class="n">configspec</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">configspecfilename</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s">'UTF8'</span><span class="p">,</span>
  699.                        <span class="n">list_values</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">_inspec</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
  700. <span class="n">config</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">configspec</span><span class="o">=</span><span class="n">configspec</span><span class="p">)</span>
  701. </pre></div>
  702. <div class="section" id="return-value">
  703. <h4><a class="toc-backref" href="#id41">5.1.2.1   Return Value</a></h4>
  704. <p>By default, the validate method either returns <tt class="docutils literal">True</tt> (everything passed)
  705. or a dictionary of <tt class="docutils literal">True</tt> / <tt class="docutils literal">False</tt> representing pass/fail. The dictionary
  706. follows the structure of the ConfigObj.</p>
  707. <p>If a whole section passes then it is replaced with the value <tt class="docutils literal">True</tt>. If a
  708. whole section fails, then it is replaced with the value <tt class="docutils literal">False</tt>.</p>
  709. <p>If a value is missing, and there is no default in the check, then the check
  710. automatically fails.</p>
  711. <p>The <tt class="docutils literal">validate</tt> method takes an optional keyword argument <tt class="docutils literal">preserve_errors</tt>.
  712. If you set this to <tt class="docutils literal">True</tt>, instead of getting <tt class="docutils literal">False</tt> for failed checks you
  713. get the actual error object from the <strong>validate</strong> module. This usually contains
  714. useful information about why the check failed.</p>
  715. <p>See the <a class="reference internal" href="#flatten-errors">flatten_errors</a> function for how to turn your results dictionary into
  716. a useful list of error messages.</p>
  717. <p>Even if <tt class="docutils literal">preserve_errors</tt> is <tt class="docutils literal">True</tt>, missing keys or sections will still be
  718. represented by a <tt class="docutils literal">False</tt> in the results dictionary.</p>
  719. </div>
  720. <div class="section" id="mentioning-default-values">
  721. <h4><a class="toc-backref" href="#id42">5.1.2.2   Mentioning Default Values</a></h4>
  722. <p>In the check in your configspec, you can specify a default to be used - by
  723. using the <tt class="docutils literal">default</tt> keyword. E.g.</p>
  724. <pre class="literal-block">
  725. key1 = integer(0, 30, default=15)
  726. key2 = integer(default=15)
  727. key3 = boolean(default=True)
  728. key4 = option('Hello', 'Goodbye', 'Not Today', default='Not Today')
  729. </pre>
  730. <p>If the configspec check supplies a default and the value is missing in the
  731. config, then the default will be set in your ConfigObj. (It is still passed to
  732. the <tt class="docutils literal">Validator</tt> so that type conversion can be done: this means the default
  733. value must still pass the check.)</p>
  734. <p>ConfigObj keeps a record of which values come from defaults, using the
  735. <tt class="docutils literal">defaults</tt> attribute of <a class="reference internal" href="#sections">sections</a>. Any key in this list isn't written out by
  736. the <tt class="docutils literal">write</tt> method. If a key is set from outside (even to the same value)
  737. then it is removed from the <tt class="docutils literal">defaults</tt> list.</p>
  738. <!-- note:
  739.  
  740. Even if all the keys in a section are in the defaults list, the section
  741. marker is still written out. -->
  742. <p>There is additionally a special case default value of <tt class="docutils literal">None</tt>. If you set the
  743. default value to <tt class="docutils literal">None</tt> and the value is missing, the value will always be
  744. set to <tt class="docutils literal">None</tt>. As the other checks don't return <tt class="docutils literal">None</tt> (unless you
  745. implement your own that do), you can tell that this value came from a default
  746. value (and was missing from the config file). It allows an easy way of
  747. implementing optional values. Simply check (and ignore) members that are set
  748. to <tt class="docutils literal">None</tt>.</p>
  749. <div class="note">
  750. <p class="first admonition-title">Note</p>
  751. <p class="last">If <a class="reference internal" href="#stringify">stringify</a> is <tt class="docutils literal">False</tt> then <tt class="docutils literal">default=None</tt> returns <tt class="docutils literal">''</tt> instead of
  752. <tt class="docutils literal">None</tt>. This is because setting a value to a non-string raises an error
  753. if stringify is unset.</p>
  754. </div>
  755. <p>The default value can be a list. See <a class="reference internal" href="#id13">List Values</a> for the way to do this.</p>
  756. <p>Writing invalid default values is a <em>guaranteed</em> way of confusing your users.
  757. Default values <strong>must</strong> pass the check.</p>
  758. </div>
  759. <div class="section" id="mentioning-repeated-sections-and-values">
  760. <h4><a class="toc-backref" href="#id43">5.1.2.3   Mentioning Repeated Sections and Values</a></h4>
  761. <p>In the configspec it is possible to cause <em>every</em> sub-section in a section to
  762. be validated using the same configspec. You do this with a section in the
  763. configspec  called <tt class="docutils literal">__many__</tt>. Every sub-section in that section has the
  764. <tt class="docutils literal">__many__</tt> configspec applied to it (without you having to explicitly name
  765. them in advance).</p>
  766. <p>Your <tt class="docutils literal">__many__</tt> section can have nested subsections, which can also include
  767. <tt class="docutils literal">__many__</tt> type sections.</p>
  768. <p>You can also specify that all values should be validated using the same configspec,
  769. by having a member with the name <tt class="docutils literal">__many__</tt>. If you want to use repeated values
  770. along with repeated sections then you can call one of them <tt class="docutils literal">___many___</tt> (triple
  771. underscores).</p>
  772. <p>Sections with repeated sections or values can also have specifically named sub-sections
  773. or values. The <tt class="docutils literal">__many__</tt> configspec will only be used to validate entries that don't
  774. have an explicit configspec.</p>
  775. <p>See <a class="reference internal" href="#repeated-sections">Repeated Sections</a> for examples.</p>
  776. </div>
  777. <div class="section" id="mentioning-simpleval">
  778. <h4><a class="toc-backref" href="#id44">5.1.2.4   Mentioning SimpleVal</a></h4>
  779. <p>If you just want to check if all members are present, then you can use the
  780. <tt class="docutils literal">SimpleVal</tt> object that comes with ConfigObj. It only fails members if they
  781. are missing.</p>
  782. <p>Write a configspec that has all the members you want to check for, but set
  783. every section to <tt class="docutils literal">''</tt>.</p>
  784. <div class="highlight"><pre><span class="n">val</span> <span class="o">=</span> <span class="n">SimpleVal</span><span class="p">()</span>
  785. <span class="n">test</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
  786. <span class="k">if</span> <span class="n">test</span> <span class="ow">is</span> <span class="bp">True</span><span class="p">:</span>
  787.     <span class="k">print</span> <span class="s">'Succeeded.'</span>
  788. </pre></div>
  789. </div>
  790. <div class="section" id="mentioning-copy-mode">
  791. <h4><a class="toc-backref" href="#id45">5.1.2.5   Mentioning copy Mode</a></h4>
  792. <p>As discussed in <a class="reference internal" href="#mentioning-default-values">Mentioning Default Values</a>, you can use a configspec to
  793. supply default values. These are marked in the ConfigObj instance as defaults,
  794. and <em>not</em> written out by the <tt class="docutils literal">write</tt> mode. This means that your users only
  795. need to supply values that are different from the defaults.</p>
  796. <p>This can be inconvenient if you <em>do</em> want to write out the default values,
  797. for example to write out a default config file.</p>
  798. <p>If you set <tt class="docutils literal">copy=True</tt> when you call validate, then no values are marked as
  799. defaults. In addition, all comments from the configspec are copied into
  800. your ConfigObj instance. You can then call <tt class="docutils literal">write</tt> to create your config
  801. file.</p>
  802. <p>There is a limitation with this. In order to allow <a class="reference internal" href="#string-interpolation">String Interpolation</a> to work
  803. within configspecs, <tt class="docutils literal">DEFAULT</tt> sections are not processed by
  804. validation; even in copy mode.</p>
  805. </div>
  806. </div>
  807. <div class="section" id="reload">
  808. <h3><a class="toc-backref" href="#id46">5.1.3   reload</a></h3>
  809. <p>If a ConfigObj instance was loaded from the filesystem, then this method will reload it. It
  810. will also reuse any configspec you supplied at instantiation (including reloading it from
  811. the filesystem if you passed it in as a filename).</p>
  812. <p>If the ConfigObj does not have a filename attribute pointing to a file, then a <tt class="docutils literal">ReloadError</tt>
  813. will be raised.</p>
  814. </div>
  815. <div class="section" id="reset">
  816. <h3><a class="toc-backref" href="#id47">5.1.4   reset</a></h3>
  817. <p>This method takes no arguments and doesn't return anything. It restores a ConfigObj
  818. instance to a freshly created state.</p>
  819. </div>
  820. </div>
  821. <div class="section" id="attributes">
  822. <h2><a class="toc-backref" href="#id48">5.2   Attributes</a></h2>
  823. <p>A ConfigObj has the following attributes :</p>
  824. <ul class="simple">
  825. <li>indent_type</li>
  826. <li>interpolation</li>
  827. <li>stringify</li>
  828. <li>BOM</li>
  829. <li>initial_comment</li>
  830. <li>final_comment</li>
  831. <li>list_values</li>
  832. <li>encoding</li>
  833. <li>default_encoding</li>
  834. <li>unrepr</li>
  835. <li>write_empty_values</li>
  836. <li>newlines</li>
  837. </ul>
  838. <div class="note">
  839. <p class="first admonition-title">Note</p>
  840. <p class="last">This doesn't include <em>comments</em>, <em>inline_comments</em>, <em>defaults</em>, or
  841. <em>configspec</em>. These are actually attributes of <a class="reference internal" href="#sections">Sections</a>.</p>
  842. </div>
  843. <p>It also has the following attributes as a result of parsing. They correspond to
  844. options when the ConfigObj was created, but changing them has no effect.</p>
  845. <ul class="simple">
  846. <li>raise_errors</li>
  847. <li>create_empty</li>
  848. <li>file_error</li>
  849. </ul>
  850. <div class="section" id="interpolation">
  851. <h3><a class="toc-backref" href="#id49">5.2.1   interpolation</a></h3>
  852. <p>ConfigObj can perform string interpolation in a <em>similar</em> way to
  853. <tt class="docutils literal">ConfigParser</tt>. See the <a class="reference internal" href="#string-interpolation">String Interpolation</a> section for full details.</p>
  854. <p>If <tt class="docutils literal">interpolation</tt> is set to <tt class="docutils literal">False</tt>, then interpolation is <em>not</em> done when
  855. you fetch values.</p>
  856. </div>
  857. <div class="section" id="stringify">
  858. <h3><a class="toc-backref" href="#id50">5.2.2   stringify</a></h3>
  859. <p>If this attribute is set (<tt class="docutils literal">True</tt>) then the <a class="reference internal" href="#validate">validate</a> method changes the
  860. values in the ConfigObj. These are turned back into strings when <a class="reference internal" href="#write">write</a> is
  861. called.</p>
  862. <p>If stringify is unset (<tt class="docutils literal">False</tt>) then attempting to set a value to a non
  863. string (or a list of strings) will raise a <tt class="docutils literal">TypeError</tt>.</p>
  864. </div>
  865. <div class="section" id="bom">
  866. <h3><a class="toc-backref" href="#id51">5.2.3   BOM</a></h3>
  867. <p>If the initial config file <em>started</em> with the UTF8 Unicode signature (known
  868. slightly incorrectly as the BOM - Byte Order Mark), or the UTF16 BOM, then
  869. this attribute is set to <tt class="docutils literal">True</tt>. Otherwise it is <tt class="docutils literal">False</tt>.</p>
  870. <p>If it is set to <tt class="docutils literal">True</tt> when <tt class="docutils literal">write</tt> is called then, if <tt class="docutils literal">encoding</tt> is set
  871. to <tt class="docutils literal">None</tt> <em>or</em> to <tt class="docutils literal">utf_8</tt> (and variants) a UTF BOM will be written.</p>
  872. <p>For UTF16 encodings, a BOM is <em>always</em> written.</p>
  873. </div>
  874. <div class="section" id="initial-comment">
  875. <h3><a class="toc-backref" href="#id52">5.2.4   initial_comment</a></h3>
  876. <p>This is a list of lines. If the ConfigObj is created from an existing file, it
  877. will contain any lines of comments before the start of the members.</p>
  878. <p>If you create a new ConfigObj, this will be an empty list.</p>
  879. <p>The write method puts these lines before it starts writing out the members.</p>
  880. </div>
  881. <div class="section" id="final-comment">
  882. <h3><a class="toc-backref" href="#id53">5.2.5   final_comment</a></h3>
  883. <p>This is a list of lines. If the ConfigObj is created from an existing file, it
  884. will contain any lines of comments after the last member.</p>
  885. <p>If you create a new ConfigObj, this will be an empty list.</p>
  886. <p>The <tt class="docutils literal">write</tt> method puts these lines after it finishes writing out the
  887. members.</p>
  888. </div>
  889. <div class="section" id="list-values">
  890. <h3><a class="toc-backref" href="#id54">5.2.6   list_values</a></h3>
  891. <p>This attribute is <tt class="docutils literal">True</tt> or <tt class="docutils literal">False</tt>. If set to <tt class="docutils literal">False</tt> then values are
  892. not parsed for list values. In addition single line values are not unquoted.</p>
  893. <p>This allows you to do your own parsing of values. It exists primarily to
  894. support the reading of the <a class="reference internal" href="#configspec">configspec</a> - but has other use cases.</p>
  895. <p>For example you could use the <tt class="docutils literal">LineParser</tt> from the
  896. <a class="reference external" href="http://www.voidspace.org.uk/python/listquote.html#lineparser">listquote module</a>
  897. to read values for nested lists.</p>
  898. <p>Single line values aren't quoted when writing - but multiline values are
  899. handled as normal.</p>
  900. <div class="caution">
  901. <p class="first admonition-title">Caution!</p>
  902. <p class="last">Because values aren't quoted, leading or trailing whitespace can be lost. This behaviour was changed in version 4.0.1. Prior to this, single line values might have been quoted; even with <tt class="docutils literal">list_values=False</tt>. This means that files written by earlier versions of ConfigObj <em>could</em> now be incompatible and need the quotes removing by hand.</p>
  903. </div>
  904. </div>
  905. <div class="section" id="encoding">
  906. <h3><a class="toc-backref" href="#id55">5.2.7   encoding</a></h3>
  907. <p>This is the encoding used to encode the output, when you call <tt class="docutils literal">write</tt>. It
  908. must be a valid encoding <a class="reference external" href="http://docs.python.org/lib/standard-encodings.html">recognised by Python</a>.</p>
  909. <p>If this value is <tt class="docutils literal">None</tt> then no encoding is done when <tt class="docutils literal">write</tt> is called.</p>
  910. </div>
  911. <div class="section" id="default-encoding">
  912. <h3><a class="toc-backref" href="#id56">5.2.8   default_encoding</a></h3>
  913. <p>If encoding is set, any byte-strings in your ConfigObj instance (keys or
  914. members) will first be decoded to Unicode using the encoding specified by the
  915. <tt class="docutils literal">default_encoding</tt> attribute. This ensures that the output is in the encoding
  916. specified.</p>
  917. <p>If this value is <tt class="docutils literal">None</tt> then <tt class="docutils literal">sys.defaultencoding</tt> is used instead.</p>
  918. </div>
  919. <div class="section" id="unrepr">
  920. <h3><a class="toc-backref" href="#id57">5.2.9   unrepr</a></h3>
  921. <p>Another boolean value. If this is set, then <tt class="docutils literal">repr(value)</tt> is used to write
  922. values. This writes values in a slightly different way to the normal ConfigObj
  923. file syntax.</p>
  924. <p>This preserves basic Python data-types when read back in. See <a class="reference internal" href="#unrepr-mode">unrepr mode</a>
  925. for more details.</p>
  926. </div>
  927. <div class="section" id="write-empty-values">
  928. <h3><a class="toc-backref" href="#id58">5.2.10   write_empty_values</a></h3>
  929. <p>Also boolean. If set, values that are an empty string (<tt class="docutils literal">''</tt>) are written as
  930. empty values. See <a class="reference internal" href="#empty-values">Empty Values</a> for more details.</p>
  931. </div>
  932. <div class="section" id="newlines">
  933. <h3><a class="toc-backref" href="#id59">5.2.11   newlines</a></h3>
  934. <p>When a config file is read, ConfigObj records the type of newline separators in the
  935. file and uses this separator when writing. It defaults to <tt class="docutils literal">None</tt>, and ConfigObj
  936. uses the system default (<tt class="docutils literal">os.sep</tt>) if write is called without newlines having
  937. been set.</p>
  938. </div>
  939. </div>
  940. </div>
  941. <div class="section" id="the-config-file-format">
  942. <h1><a class="toc-backref" href="#id60">6   The Config File Format</a></h1>
  943. <p>You saw an example config file in the <a class="reference internal" href="#config-files">Config Files</a> section. Here is a fuller
  944. specification of the config files used and created by ConfigObj.</p>
  945. <p>The basic pattern for keywords is:</p>
  946. <pre class="literal-block">
  947. # comment line
  948. # comment line
  949. keyword = value # inline comment
  950. </pre>
  951. <p>Both keyword and value can optionally be surrounded in quotes. The equals sign
  952. is the only valid divider.</p>
  953. <p>Values can have comments on the lines above them, and an inline comment after
  954. them. This, of course, is optional. See the <a class="reference internal" href="#comments">comments</a> section for details.</p>
  955. <p>If a keyword or value starts or ends with whitespace, or contains a quote mark
  956. or comma, then it should be surrounded by quotes. Quotes are not necessary if
  957. whitespace is surrounded by non-whitespace.</p>
  958. <p>Values can also be lists. Lists are comma separated. You indicate a single
  959. member list by a trailing comma. An empty list is shown by a single comma:</p>
  960. <pre class="literal-block">
  961. keyword1 = value1, value2, value3
  962. keyword2 = value1, # a single member list
  963. keyword3 = , # an empty list
  964. </pre>
  965. <p>Values that contain line breaks (multi-line values) can be surrounded by triple
  966. quotes. These can also be used if a value contains both types of quotes. List
  967. members cannot be surrounded by triple quotes:</p>
  968. <pre class="literal-block">
  969. keyword1 = ''' A multi line value
  970. on several
  971. lines'''     # with a comment
  972. keyword2 = '''I won't be "afraid".'''
  973. #
  974. keyword3 = """ A multi line value
  975. on several
  976. lines"""     # with a comment
  977. keyword4 = """I won't be "afraid"."""
  978. </pre>
  979. <div class="warning">
  980. <p class="first admonition-title">Warning</p>
  981. <p class="last">There is no way of safely quoting values that contain both types of triple
  982. quotes.</p>
  983. </div>
  984. <p>A line that starts with a '#', possibly preceded by whitespace, is a comment.</p>
  985. <p>New sections are indicated by a section marker line. That is the section name
  986. in square brackets. Whitespace around the section name is ignored. The name can
  987. be quoted with single or double quotes. The marker can have comments before it
  988. and an inline comment after it:</p>
  989. <pre class="literal-block">
  990. # The First Section
  991. [ section name 1 ] # first section
  992. keyword1 = value1
  993.  
  994. # The Second Section
  995. [ "section name 2" ] # second section
  996. keyword2 = value2
  997. </pre>
  998. <p>Any subsections (sections that are <em>inside</em> the current section) are
  999. designated by repeating the square brackets before and after the section name.
  1000. The number of square brackets represents the nesting level of the sub-section.
  1001. Square brackets may be separated by whitespace; such whitespace, however, will
  1002. not be present in the output config written by the <tt class="docutils literal">write</tt> method.</p>
  1003. <p>Indentation is not significant, but can be preserved. See the description of
  1004. the <tt class="docutils literal">indent_type</tt> option, in the <a class="reference internal" href="#configobj-specifications">ConfigObj specifications</a> chapter, for the
  1005. details.</p>
  1006. <p>A <em>NestingError</em> will be raised if the number of the opening and the closing
  1007. brackets in a section marker is not the same, or if a sub-section's nesting
  1008. level is greater than the nesting level of it parent plus one.</p>
  1009. <p>In the outer section, single values can only appear before any sub-section.
  1010. Otherwise they will belong to the sub-section immediately before them:</p>
  1011. <pre class="literal-block">
  1012. # initial comment
  1013. keyword1 = value1
  1014. keyword2 = value2
  1015.  
  1016. [section 1]
  1017. keyword1 = value1
  1018. keyword2 = value2
  1019.  
  1020.     [[sub-section]]
  1021.     # this is in section 1
  1022.     keyword1 = value1
  1023.     keyword2 = value2
  1024.  
  1025.         [[[nested section]]]
  1026.         # this is in sub section
  1027.         keyword1 = value1
  1028.         keyword2 = value2
  1029.  
  1030.     [[sub-section2]]
  1031.     # this is in section 1 again
  1032.     keyword1 = value1
  1033.     keyword2 = value2
  1034.  
  1035. [[sub-section3]]
  1036. # this is also in section 1, indentation is misleading here
  1037. keyword1 = value1
  1038. keyword2 = value2
  1039.  
  1040. # final comment
  1041. </pre>
  1042. <p>When parsed, the above config file produces the following data structure:</p>
  1043. <div class="highlight"><pre><span class="n">ConfigObj</span><span class="p">({</span>
  1044.     <span class="s">'keyword1'</span><span class="p">:</span> <span class="s">'value1'</span><span class="p">,</span>
  1045.     <span class="s">'keyword2'</span><span class="p">:</span> <span class="s">'value2'</span><span class="p">,</span>
  1046.     <span class="s">'section 1'</span><span class="p">:</span> <span class="p">{</span>
  1047.         <span class="s">'keyword1'</span><span class="p">:</span> <span class="s">'value1'</span><span class="p">,</span>
  1048.         <span class="s">'keyword2'</span><span class="p">:</span> <span class="s">'value2'</span><span class="p">,</span>
  1049.         <span class="s">'sub-section'</span><span class="p">:</span> <span class="p">{</span>
  1050.             <span class="s">'keyword1'</span><span class="p">:</span> <span class="s">'value1'</span><span class="p">,</span>
  1051.             <span class="s">'keyword2'</span><span class="p">:</span> <span class="s">'value2'</span><span class="p">,</span>
  1052.             <span class="s">'nested section'</span><span class="p">:</span> <span class="p">{</span>
  1053.                 <span class="s">'keyword1'</span><span class="p">:</span> <span class="s">'value1'</span><span class="p">,</span>
  1054.                 <span class="s">'keyword2'</span><span class="p">:</span> <span class="s">'value2'</span><span class="p">,</span>
  1055.             <span class="p">},</span>
  1056.         <span class="p">},</span>
  1057.         <span class="s">'sub-section2'</span><span class="p">:</span> <span class="p">{</span>
  1058.             <span class="s">'keyword1'</span><span class="p">:</span> <span class="s">'value1'</span><span class="p">,</span>
  1059.             <span class="s">'keyword2'</span><span class="p">:</span> <span class="s">'value2'</span><span class="p">,</span>
  1060.         <span class="p">},</span>
  1061.         <span class="s">'sub-section3'</span><span class="p">:</span> <span class="p">{</span>
  1062.             <span class="s">'keyword1'</span><span class="p">:</span> <span class="s">'value1'</span><span class="p">,</span>
  1063.             <span class="s">'keyword2'</span><span class="p">:</span> <span class="s">'value2'</span><span class="p">,</span>
  1064.         <span class="p">},</span>
  1065.     <span class="p">},</span>
  1066. <span class="p">})</span>
  1067. </pre></div>
  1068. <p>Sections are ordered: note how the structure of the resulting ConfigObj is in
  1069. the same order as the original file.</p>
  1070. <div class="note">
  1071. <p class="first admonition-title">Note</p>
  1072. <p>In ConfigObj 4.3.0 <em>empty values</em> became valid syntax. They are read as the
  1073. empty string. There is also an option/attribute (<tt class="docutils literal">write_empty_values</tt>) to
  1074. allow the writing of these.</p>
  1075. <p>This is mainly to support 'legacy' config files, written from other
  1076. applications. This is documented under <a class="reference internal" href="#empty-values">Empty Values</a>.</p>
  1077. <p class="last"><a class="reference internal" href="#unrepr-mode">unrepr mode</a> introduces <em>another</em> syntax variation, used for storing
  1078. basic Python datatypes in config files.</p>
  1079. </div>
  1080. </div>
  1081. <div class="section" id="sections">
  1082. <h1><a class="toc-backref" href="#id61">7   Sections</a></h1>
  1083. <p>Every section in a ConfigObj has certain properties. The ConfigObj itself also
  1084. has these properties, because it too is a section (sometimes called the <em>root
  1085. section</em>).</p>
  1086. <p><tt class="docutils literal">Section</tt> is a subclass of the standard new-class dictionary, therefore it
  1087. has <strong>all</strong> the methods of a normal dictionary. This means you can <tt class="docutils literal">update</tt>
  1088. and <tt class="docutils literal">clear</tt> sections.</p>
  1089. <div class="note">
  1090. <p class="first admonition-title">Note</p>
  1091. <p>You create a new section by assigning a member to be a dictionary.</p>
  1092. <p>The new <tt class="docutils literal">Section</tt> is created <em>from</em> the dictionary, but isn't the same
  1093. thing as the dictionary. (So references to the dictionary you use to create
  1094. the section <em>aren't</em> references to the new section).</p>
  1095. <p>Note the following.</p>
  1096. <div class="highlight"><pre><span class="n">config</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">()</span>
  1097. <span class="n">vals</span> <span class="o">=</span> <span class="p">{</span><span class="s">'key1'</span><span class="p">:</span> <span class="s">'value 1'</span><span class="p">,</span>
  1098.         <span class="s">'key2'</span><span class="p">:</span> <span class="s">'value 2'</span>
  1099.        <span class="p">}</span>
  1100. <span class="n">config</span><span class="p">[</span><span class="s">'vals'</span><span class="p">]</span> <span class="o">=</span> <span class="n">vals</span>
  1101. <span class="n">config</span><span class="p">[</span><span class="s">'vals'</span><span class="p">]</span> <span class="o">==</span> <span class="n">vals</span>
  1102. <span class="bp">True</span>
  1103. <span class="n">config</span><span class="p">[</span><span class="s">'vals'</span><span class="p">]</span> <span class="ow">is</span> <span class="n">vals</span>
  1104. <span class="bp">False</span>
  1105. </pre></div>
  1106. <p class="last">If you now change <tt class="docutils literal">vals</tt>, the changes won't be reflected in <tt class="docutils literal"><span class="pre">config['vals']</span></tt>.</p>
  1107. </div>
  1108. <p>A section is ordered, following its <tt class="docutils literal">scalars</tt> and <tt class="docutils literal">sections</tt>
  1109. attributes documented below. This means that the following dictionary
  1110. attributes return their results in order.</p>
  1111. <ul>
  1112. <li><p class="first">'__iter__'</p>
  1113. <blockquote>
  1114. <p>More commonly known as <tt class="docutils literal">for member in section:</tt>.</p>
  1115. </blockquote>
  1116. </li>
  1117. <li><p class="first">'__repr__' and '__str__'</p>
  1118. <blockquote>
  1119. <p>Any time you print or display the ConfigObj.</p>
  1120. </blockquote>
  1121. </li>
  1122. <li><p class="first">'items'</p>
  1123. </li>
  1124. <li><p class="first">'iteritems'</p>
  1125. </li>
  1126. <li><p class="first">'iterkeys'</p>
  1127. </li>
  1128. <li><p class="first">'itervalues'</p>
  1129. </li>
  1130. <li><p class="first">'keys'</p>
  1131. </li>
  1132. <li><p class="first">'popitem'</p>
  1133. </li>
  1134. <li><p class="first">'values'</p>
  1135. </li>
  1136. </ul>
  1137. <div class="section" id="section-attributes">
  1138. <h2><a class="toc-backref" href="#id62">7.1   Section Attributes</a></h2>
  1139. <ul>
  1140. <li><p class="first">main</p>
  1141. <blockquote>
  1142. <p>A reference to the main ConfigObj.</p>
  1143. </blockquote>
  1144. </li>
  1145. <li><p class="first">parent</p>
  1146. <blockquote>
  1147. <p>A reference to the 'parent' section, the section that this section is a
  1148. member of.</p>
  1149. <p>On the ConfigObj this attribute is a reference to itself. You can use this
  1150. to walk up the sections, stopping when <tt class="docutils literal">section.parent is section</tt>.</p>
  1151. </blockquote>
  1152. </li>
  1153. <li><p class="first">depth</p>
  1154. <blockquote>
  1155. <p>The nesting level of the current section.</p>
  1156. <p>If you create a new ConfigObj and add sections, 1 will be added to the
  1157. depth level between sections.</p>
  1158. </blockquote>
  1159. </li>
  1160. <li><p class="first">defaults</p>
  1161. <blockquote>
  1162. <p>This attribute is a list of scalars that came from default values. Values
  1163. that came from defaults aren't written out by the <tt class="docutils literal">write</tt> method.
  1164. Setting any of these values in the section removes them from the defaults
  1165. list.</p>
  1166. </blockquote>
  1167. </li>
  1168. <li><p class="first">default_values</p>
  1169. <blockquote>
  1170. <p>This attribute is a dictionary mapping keys to the default values for the
  1171. keys. By default it is an empty dictionary and is populated when you
  1172. validate the ConfigObj.</p>
  1173. </blockquote>
  1174. </li>
  1175. <li><p class="first">scalars, sections</p>
  1176. <blockquote>
  1177. <p>These attributes are normal lists, representing the order that members,
  1178. single values and subsections appear in the section. The order will either
  1179. be the order of the original config file, <em>or</em> the order that you added
  1180. members.</p>
  1181. <p>The order of members in this lists is the order that <tt class="docutils literal">write</tt> creates in
  1182. the config file. The <tt class="docutils literal">scalars</tt> list is output before the <tt class="docutils literal">sections</tt>
  1183. list.</p>
  1184. <p>Adding or removing members also alters these lists. You can manipulate the
  1185. lists directly to alter the order of members.</p>
  1186. <div class="warning">
  1187. <p class="first admonition-title">Warning</p>
  1188. <p class="last">If you alter the <tt class="docutils literal">scalars</tt>, <tt class="docutils literal">sections</tt>, or <tt class="docutils literal">defaults</tt> attributes
  1189. so that they no longer reflect the contents of the section, you will
  1190. break your ConfigObj.</p>
  1191. </div>
  1192. <p>See also the <tt class="docutils literal">rename</tt> method.</p>
  1193. </blockquote>
  1194. </li>
  1195. <li><p class="first">comments</p>
  1196. <blockquote>
  1197. <p>This is a dictionary of comments associated with each member. Each entry is
  1198. a list of lines. These lines are written out before the member.</p>
  1199. </blockquote>
  1200. </li>
  1201. <li><p class="first">inline_comments</p>
  1202. <blockquote>
  1203. <p>This is <em>another</em> dictionary of comments associated with each member. Each
  1204. entry is a string that is put inline with the member.</p>
  1205. </blockquote>
  1206. </li>
  1207. <li><p class="first">configspec</p>
  1208. <blockquote>
  1209. <p>The configspec attribute is a dictionary mapping scalars to <em>checks</em>. A
  1210. check defines the expected type and possibly the allowed values for a
  1211. member.</p>
  1212. <p>The configspec has the same format as a config file, but instead of values
  1213. it has a specification for the value (which may include a default value).
  1214. The <a class="reference internal" href="#validate">validate</a> method uses it to check the config file makes sense. If a
  1215. configspec is passed in when the ConfigObj is created, then it is parsed
  1216. and broken up to become the <tt class="docutils literal">configspec</tt> attribute of each section.</p>
  1217. <p>If you didn't pass in a configspec, this attribute will be <tt class="docutils literal">None</tt> on the
  1218. root section (the main ConfigObj).</p>
  1219. <p>You can set the configspec attribute directly on a section.</p>
  1220. <p>See the <a class="reference internal" href="#validation">validation</a> section for full details of how to write configspecs.</p>
  1221. </blockquote>
  1222. </li>
  1223. <li><p class="first">extra_values</p>
  1224. <blockquote>
  1225. <p>By default an empty list. After <a class="reference internal" href="#validation">validation</a> this is populated with any members
  1226. of the section that don't appear in the configspec (i.e. they are additional
  1227. values). Rather than accessing this directly it may be more convenient to get
  1228. all the extra values in a config file using the <a class="reference internal" href="#get-extra-values">get_extra_values</a> function.</p>
  1229. <p>New in ConfigObj 4.7.0.</p>
  1230. </blockquote>
  1231. </li>
  1232. </ul>
  1233. </div>
  1234. <div class="section" id="section-methods">
  1235. <h2><a class="toc-backref" href="#id63">7.2   Section Methods</a></h2>
  1236. <ul>
  1237. <li><p class="first"><strong>dict</strong></p>
  1238. <blockquote>
  1239. <p>This method takes no arguments. It returns a deep copy of the section as a
  1240. dictionary. All subsections will also be dictionaries, and list values will
  1241. be copies, rather than references to the original <a class="footnote-reference" href="#id24" id="id10">[10]</a>.</p>
  1242. </blockquote>
  1243. </li>
  1244. <li><p class="first"><strong>rename</strong></p>
  1245. <blockquote>
  1246. <p><tt class="docutils literal">rename(oldkey, newkey)</tt></p>
  1247. <p>This method renames a key, without affecting its position in the sequence.</p>
  1248. </blockquote>
  1249. </li>
  1250. <li><p class="first"><strong>merge</strong></p>
  1251. <blockquote>
  1252. <p><tt class="docutils literal">merge(indict)</tt></p>
  1253. <p>This method is a <em>recursive update</em> method. It allows you to merge two
  1254. config files together.</p>
  1255. <p>You would typically use this to create a default ConfigObj and then merge
  1256. in user settings. This way users only need to specify values that are
  1257. different from the default.</p>
  1258. <p>For example :</p>
  1259. <div class="highlight"><pre><span class="c"># def_cfg contains your default config settings</span>
  1260. <span class="c"># user_cfg contains the user settings</span>
  1261. <span class="n">cfg</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">def_cfg</span><span class="p">)</span>
  1262. <span class="n">usr</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">user_cfg</span><span class="p">)</span>
  1263. <span class="c">#</span>
  1264. <span class="n">cfg</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">usr</span><span class="p">)</span>
  1265.  
  1266. <span class="sd">"""</span>
  1267. <span class="sd">cfg now contains a combination of the default settings and the user</span>
  1268. <span class="sd">settings.</span>
  1269.  
  1270. <span class="sd">The user settings will have overwritten any of the default ones.</span>
  1271. <span class="sd">"""</span>
  1272. </pre></div>
  1273. </blockquote>
  1274. </li>
  1275. <li><p class="first"><strong>walk</strong></p>
  1276. <blockquote>
  1277. <p>This method can be used to transform values and names. See <a class="reference internal" href="#walking-a-section">walking a
  1278. section</a> for examples and explanation.</p>
  1279. </blockquote>
  1280. </li>
  1281. <li><p class="first"><strong>as_bool</strong></p>
  1282. <blockquote>
  1283. <p><tt class="docutils literal">as_bool(key)</tt></p>
  1284. <p>Returns <tt class="docutils literal">True</tt> if the key contains a string that represents <tt class="docutils literal">True</tt>, or
  1285. is the <tt class="docutils literal">True</tt> object.</p>
  1286. <p>Returns <tt class="docutils literal">False</tt> if the key contains a string that represents <tt class="docutils literal">False</tt>,
  1287. or is the <tt class="docutils literal">False</tt> object.</p>
  1288. <p>Raises a <tt class="docutils literal">ValueError</tt> if the key contains anything else.</p>
  1289. <p>Strings that represent <tt class="docutils literal">True</tt> are (not case sensitive):</p>
  1290. <pre class="literal-block">
  1291. true, yes, on, 1
  1292. </pre>
  1293. <p>Strings that represent <tt class="docutils literal">False</tt> are:</p>
  1294. <pre class="literal-block">
  1295. false, no, off, 0
  1296. </pre>
  1297. </blockquote>
  1298. </li>
  1299. <li><p class="first"><strong>as_int</strong></p>
  1300. <blockquote>
  1301. <p><tt class="docutils literal">as_int(key)</tt></p>
  1302. <p>This returns the value contained in the specified key as an integer.</p>
  1303. <p>It raises a <tt class="docutils literal">ValueError</tt> if the conversion can't be done.</p>
  1304. </blockquote>
  1305. </li>
  1306. <li><p class="first"><strong>as_float</strong></p>
  1307. <blockquote>
  1308. <p><tt class="docutils literal">as_float(key)</tt></p>
  1309. <p>This returns the value contained in the specified key as a float.</p>
  1310. <p>It raises a <tt class="docutils literal">ValueError</tt> if the conversion can't be done.</p>
  1311. </blockquote>
  1312. </li>
  1313. <li><p class="first"><strong>as_list</strong></p>
  1314. <blockquote>
  1315. <p><tt class="docutils literal">as_list(key)</tt></p>
  1316. <p>This returns the value contained in the specified key as a list.</p>
  1317. <p>If it isn't a list it will be wrapped as a list so that you can
  1318. guarantee the returned value will be a list.</p>
  1319. </blockquote>
  1320. </li>
  1321. <li><p class="first"><strong>restore_default</strong></p>
  1322. <blockquote>
  1323. <p><tt class="docutils literal">restore_default(key)</tt></p>
  1324. <p>Restore (and return) the default value for the specified key.</p>
  1325. <p>This method will only work for a ConfigObj that was created
  1326. with a configspec and has been validated.</p>
  1327. <p>If there is no default value for this key, <tt class="docutils literal">KeyError</tt> is raised.</p>
  1328. </blockquote>
  1329. </li>
  1330. <li><p class="first"><strong>restore_defaults</strong></p>
  1331. <blockquote>
  1332. <p><tt class="docutils literal">restore_defaults()</tt></p>
  1333. <p>Recursively restore default values to all members
  1334. that have them.</p>
  1335. <p>This method will only work for a ConfigObj that was created
  1336. with a configspec and has been validated.</p>
  1337. <p>It doesn't delete or modify entries without default values.</p>
  1338. </blockquote>
  1339. </li>
  1340. </ul>
  1341. </div>
  1342. <div class="section" id="walking-a-section">
  1343. <h2><a class="toc-backref" href="#id64">7.3   Walking a Section</a></h2>
  1344. <div class="note">
  1345. <p class="first admonition-title">Note</p>
  1346. <p class="last">The walk method allows you to call a function on every member/name.</p>
  1347. </div>
  1348. <div class="highlight"><pre><span class="n">walk</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">raise_errors</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
  1349.      <span class="n">call_on_sections</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="o">**</span><span class="n">keywargs</span><span class="p">)</span>
  1350. </pre></div>
  1351. <p><tt class="docutils literal">walk</tt> is a method of the <tt class="docutils literal">Section</tt> object. This means it is also a method
  1352. of ConfigObj.</p>
  1353. <p>It walks through every member and calls a function on the keyword and value. It
  1354. walks recursively through subsections.</p>
  1355. <p>It returns a dictionary of all the computed values.</p>
  1356. <p>If the function raises an exception, the default is to propagate the error, and
  1357. stop. If <tt class="docutils literal">raise_errors=False</tt> then it sets the return value for that keyword
  1358. to <tt class="docutils literal">False</tt> instead, and continues. This is similar to the way <a class="reference internal" href="#validation">validation</a>
  1359. works.</p>
  1360. <p>Your function receives the arguments <tt class="docutils literal">(section, key)</tt>. The current value is
  1361. then <tt class="docutils literal">section[key]</tt> <a class="footnote-reference" href="#id25" id="id11">[11]</a>. Any unrecognised keyword arguments you pass to
  1362. walk, are passed on to the function.</p>
  1363. <p>Normally <tt class="docutils literal">walk</tt> just recurses into subsections. If you are transforming (or
  1364. checking) names as well as values, then you want to be able to change the names
  1365. of sections. In this case set <tt class="docutils literal">call_on_sections</tt> to <tt class="docutils literal">True</tt>. Now, on
  1366. encountering a sub-section, <em>first</em> the function is called for the <em>whole</em>
  1367. sub-section, and <em>then</em> it recurses into it's members. This means your function
  1368. must be able to handle receiving dictionaries as well as strings and lists.</p>
  1369. <p>If you are using the return value from <tt class="docutils literal">walk</tt> <em>and</em> <tt class="docutils literal">call_on_sections</tt>,
  1370. note that walk discards the return value when it calls your function.</p>
  1371. <div class="caution">
  1372. <p class="first admonition-title">Caution!</p>
  1373. <p class="last">You can use <tt class="docutils literal">walk</tt> to transform the names of members of a section
  1374. but you mustn't add or delete members.</p>
  1375. </div>
  1376. </div>
  1377. <div class="section" id="examples">
  1378. <h2><a class="toc-backref" href="#id65">7.4   Examples</a></h2>
  1379. <p>You can use this for transforming all values in your ConfigObj. For example
  1380. you might like the nested lists from ConfigObj 3. This was provided by the
  1381. <a class="reference external" href="http://www.voidspace.org.uk/python/modules.shtml#listquote">listquote</a> module. You could switch off the parsing for list values
  1382. (<tt class="docutils literal">list_values=False</tt>) and use listquote to parse every value.</p>
  1383. <p>Another thing you might want to do is use the Python escape codes in your
  1384. values. You might be <em>used</em> to using <tt class="docutils literal">\n</tt> for line feed and <tt class="docutils literal">\t</tt> for tab.
  1385. Obviously we'd need to decode strings that come from the config file (using the
  1386. escape codes). Before writing out we'll need to put the escape codes back in
  1387. encode.</p>
  1388. <p>As an example we'll write a function to use with walk, that encodes or decodes
  1389. values using the <tt class="docutils literal"><span class="pre">string-escape</span></tt> codec.</p>
  1390. <p>The function has to take each value and set the new value. As a bonus we'll
  1391. create one function that will do decode <em>or</em> encode depending on a keyword
  1392. argument.</p>
  1393. <p>We don't want to work with section names, we're only transforming values, so
  1394. we can leave <tt class="docutils literal">call_on_sections</tt> as <tt class="docutils literal">False</tt>. This means the two datatypes we
  1395. have to handle are strings and lists, we can ignore everything else. (We'll
  1396. treat tuples as lists as well).</p>
  1397. <p>We're not using the return values, so it doesn't need to return anything, just
  1398. change the values if appropriate.</p>
  1399. <div class="highlight"><pre><span class="k">def</span> <span class="nf">string_escape</span><span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">encode</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
  1400.     <span class="sd">"""</span>
  1401. <span class="sd">    A function to encode or decode using the 'string-escape' codec.</span>
  1402. <span class="sd">    To be passed to the walk method of a ConfigObj.</span>
  1403. <span class="sd">    By default it decodes.</span>
  1404. <span class="sd">    To encode, pass in the keyword argument ``encode=True``.</span>
  1405. <span class="sd">    """</span>
  1406.     <span class="n">val</span> <span class="o">=</span> <span class="n">section</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
  1407.     <span class="c"># is it a type we can work with</span>
  1408.     <span class="c"># NOTE: for platforms where Python > 2.2</span>
  1409.     <span class="c"># you can use basestring instead of (str, unicode)</span>
  1410.     <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">val</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">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
  1411.         <span class="c"># no !</span>
  1412.         <span class="k">return</span>
  1413.     <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">val</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>
  1414.         <span class="c"># it's a string !</span>
  1415.         <span class="k">if</span> <span class="ow">not</span> <span class="n">encode</span><span class="p">:</span>
  1416.             <span class="n">section</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">val</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">'string-escape'</span><span class="p">)</span>
  1417.         <span class="k">else</span><span class="p">:</span>
  1418.             <span class="n">section</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">val</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">'string-escape'</span><span class="p">)</span>
  1419.     <span class="k">else</span><span class="p">:</span>
  1420.         <span class="c"># it must be a list or tuple!</span>
  1421.         <span class="c"># we'll be lazy and create a new list</span>
  1422.         <span class="n">newval</span> <span class="o">=</span> <span class="p">[]</span>
  1423.         <span class="c"># we'll check every member of the list</span>
  1424.         <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">val</span><span class="p">:</span>
  1425.             <span class="k">if</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>
  1426.                 <span class="k">if</span> <span class="ow">not</span> <span class="n">encode</span><span class="p">:</span>
  1427.                     <span class="n">newval</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">'string-escape'</span><span class="p">))</span>
  1428.                 <span class="k">else</span><span class="p">:</span>
  1429.                    <span class="n">newval</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">'string-escape'</span><span class="p">))</span>
  1430.             <span class="k">else</span><span class="p">:</span>
  1431.                 <span class="n">newval</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span>
  1432.         <span class="c"># done !</span>
  1433.         <span class="n">section</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span>  <span class="n">newval</span>
  1434.  
  1435. <span class="c"># assume we have a ConfigObj called ``config``</span>
  1436. <span class="c">#</span>
  1437. <span class="c"># To decode</span>
  1438. <span class="n">config</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">string_escape</span><span class="p">)</span>
  1439. <span class="c">#</span>
  1440. <span class="c"># To encode.</span>
  1441. <span class="c"># Because ``walk`` doesn't recognise the ``encode`` argument</span>
  1442. <span class="c"># it passes it to our function.</span>
  1443. <span class="n">config</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">string_escape</span><span class="p">,</span> <span class="n">encode</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
  1444. </pre></div>
  1445. <p>Here's a simple example of using <tt class="docutils literal">walk</tt> to transform names and values. One
  1446. usecase of this would be to create a <em>standard</em> config file with placeholders
  1447. for section and keynames. You can then use walk to create new config files
  1448. and change values and member names :</p>
  1449. <div class="highlight"><pre><span class="c"># We use 'XXXX' as a placeholder</span>
  1450. <span class="n">config</span> <span class="o">=</span> <span class="s">'''</span>
  1451. <span class="s">XXXXkey1 = XXXXvalue1</span>
  1452. <span class="s">XXXXkey2 = XXXXvalue2</span>
  1453. <span class="s">XXXXkey3 = XXXXvalue3</span>
  1454. <span class="s">[XXXXsection1]</span>
  1455. <span class="s">XXXXkey1 = XXXXvalue1</span>
  1456. <span class="s">XXXXkey2 = XXXXvalue2</span>
  1457. <span class="s">XXXXkey3 = XXXXvalue3</span>
  1458. <span class="s">[XXXXsection2]</span>
  1459. <span class="s">XXXXkey1 = XXXXvalue1</span>
  1460. <span class="s">XXXXkey2 = XXXXvalue2</span>
  1461. <span class="s">XXXXkey3 = XXXXvalue3</span>
  1462. <span class="s">    [[XXXXsection1]]</span>
  1463. <span class="s">    XXXXkey1 = XXXXvalue1</span>
  1464. <span class="s">    XXXXkey2 = XXXXvalue2</span>
  1465. <span class="s">    XXXXkey3 = XXXXvalue3</span>
  1466. <span class="s">'''</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()</span>
  1467. <span class="n">cfg</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">config</span><span class="p">)</span>
  1468. <span class="c">#</span>
  1469. <span class="k">def</span> <span class="nf">transform</span><span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
  1470.     <span class="n">val</span> <span class="o">=</span> <span class="n">section</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
  1471.     <span class="n">newkey</span> <span class="o">=</span> <span class="n">key</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">'XXXX'</span><span class="p">,</span> <span class="s">'CLIENT1'</span><span class="p">)</span>
  1472.     <span class="n">section</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">newkey</span><span class="p">)</span>
  1473.     <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">,</span> <span class="nb">dict</span><span class="p">)):</span>
  1474.         <span class="k">pass</span>
  1475.     <span class="k">else</span><span class="p">:</span>
  1476.         <span class="n">val</span> <span class="o">=</span> <span class="n">val</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">'XXXX'</span><span class="p">,</span> <span class="s">'CLIENT1'</span><span class="p">)</span>
  1477.         <span class="n">section</span><span class="p">[</span><span class="n">newkey</span><span class="p">]</span> <span class="o">=</span> <span class="n">val</span>
  1478. <span class="c">#</span>
  1479. <span class="n">cfg</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">transform</span><span class="p">,</span> <span class="n">call_on_sections</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
  1480. <span class="k">print</span> <span class="n">cfg</span>
  1481. <span class="n">ConfigObj</span><span class="p">({</span><span class="s">'CLIENT1key1'</span><span class="p">:</span> <span class="s">'CLIENT1value1'</span><span class="p">,</span> <span class="s">'CLIENT1key2'</span><span class="p">:</span> <span class="s">'CLIENT1value2'</span><span class="p">,</span>
  1482. <span class="s">'CLIENT1key3'</span><span class="p">:</span> <span class="s">'CLIENT1value3'</span><span class="p">,</span>
  1483. <span class="s">'CLIENT1section1'</span><span class="p">:</span> <span class="p">{</span><span class="s">'CLIENT1key1'</span><span class="p">:</span> <span class="s">'CLIENT1value1'</span><span class="p">,</span>
  1484.     <span class="s">'CLIENT1key2'</span><span class="p">:</span> <span class="s">'CLIENT1value2'</span><span class="p">,</span> <span class="s">'CLIENT1key3'</span><span class="p">:</span> <span class="s">'CLIENT1value3'</span><span class="p">},</span>
  1485. <span class="s">'CLIENT1section2'</span><span class="p">:</span> <span class="p">{</span><span class="s">'CLIENT1key1'</span><span class="p">:</span> <span class="s">'CLIENT1value1'</span><span class="p">,</span>
  1486.     <span class="s">'CLIENT1key2'</span><span class="p">:</span> <span class="s">'CLIENT1value2'</span><span class="p">,</span> <span class="s">'CLIENT1key3'</span><span class="p">:</span> <span class="s">'CLIENT1value3'</span><span class="p">,</span>
  1487.     <span class="s">'CLIENT1section1'</span><span class="p">:</span> <span class="p">{</span><span class="s">'CLIENT1key1'</span><span class="p">:</span> <span class="s">'CLIENT1value1'</span><span class="p">,</span>
  1488.         <span class="s">'CLIENT1key2'</span><span class="p">:</span> <span class="s">'CLIENT1value2'</span><span class="p">,</span> <span class="s">'CLIENT1key3'</span><span class="p">:</span> <span class="s">'CLIENT1value3'</span><span class="p">}}})</span>
  1489. </pre></div>
  1490. </div>
  1491. </div>
  1492. <div class="section" id="exceptions">
  1493. <h1><a class="toc-backref" href="#id66">8   Exceptions</a></h1>
  1494. <p>There are several places where ConfigObj may raise exceptions (other than
  1495. because of bugs).</p>
  1496. <ol class="arabic">
  1497. <li><dl class="first docutils">
  1498. <dt>If a configspec filename you pass in doesn't exist, or a config file</dt>
  1499. <dd><p class="first last">filename doesn't exist <em>and</em> <tt class="docutils literal">file_error=True</tt>, an <tt class="docutils literal">IOError</tt> will be
  1500. raised.</p>
  1501. </dd>
  1502. </dl>
  1503. </li>
  1504. <li><dl class="first docutils">
  1505. <dt>If you try to set a non-string key, or a non string value when</dt>
  1506. <dd><p class="first last"><tt class="docutils literal">stringify=False</tt>, a <tt class="docutils literal">TypeError</tt> will be raised.</p>
  1507. </dd>
  1508. </dl>
  1509. </li>
  1510. <li><p class="first">A badly built config file will cause parsing errors.</p>
  1511. </li>
  1512. <li><p class="first">A parsing error can also occur when reading a configspec.</p>
  1513. </li>
  1514. <li><dl class="first docutils">
  1515. <dt>In string interpolation you can specify a value that doesn't exist, or</dt>
  1516. <dd><p class="first last">create circular references (recursion).</p>
  1517. </dd>
  1518. </dl>
  1519. </li>
  1520. </ol>
  1521. <p>Number 5 (which is actually two different types of exceptions) is documented
  1522. in <a class="reference internal" href="#string-interpolation">String Interpolation</a>.</p>
  1523. <p><em>This</em> section is about errors raised during parsing.</p>
  1524. <p>The base error class is <tt class="docutils literal">ConfigObjError</tt>. This is a subclass of
  1525. <tt class="docutils literal">SyntaxError</tt>, so you can trap for <tt class="docutils literal">SyntaxError</tt> without needing to
  1526. directly import any of the ConfigObj exceptions.</p>
  1527. <p>The following other exceptions are defined (all deriving from
  1528. <tt class="docutils literal">ConfigObjError</tt>) :</p>
  1529. <ul>
  1530. <li><p class="first"><tt class="docutils literal">NestingError</tt></p>
  1531. <blockquote>
  1532. <p>This error indicates either a mismatch in the brackets in a section marker,
  1533. or an excessive level of nesting.</p>
  1534. </blockquote>
  1535. </li>
  1536. <li><p class="first"><tt class="docutils literal">ParseError</tt></p>
  1537. <blockquote>
  1538. <p>This error indicates that a line is badly written. It is neither a valid
  1539. <tt class="docutils literal">key = value</tt> line, nor a valid section marker line, nor a comment line.</p>
  1540. </blockquote>
  1541. </li>
  1542. <li><p class="first"><tt class="docutils literal">DuplicateError</tt></p>
  1543. <blockquote>
  1544. <p>The keyword or section specified already exists.</p>
  1545. </blockquote>
  1546. </li>
  1547. <li><p class="first"><tt class="docutils literal">ConfigspecError</tt></p>
  1548. <blockquote>
  1549. <p>An error occurred whilst parsing a configspec.</p>
  1550. </blockquote>
  1551. </li>
  1552. <li><p class="first"><tt class="docutils literal">UnreprError</tt></p>
  1553. <blockquote>
  1554. <p>An error occurred when parsing a value in <a class="reference internal" href="#unrepr-mode">unrepr mode</a>.</p>
  1555. </blockquote>
  1556. </li>
  1557. <li><p class="first"><tt class="docutils literal">ReloadError</tt></p>
  1558. <blockquote>
  1559. <p><tt class="docutils literal">reload</tt> was called on a ConfigObj instance that doesn't have a valid
  1560. filename attribute.</p>
  1561. </blockquote>
  1562. </li>
  1563. </ul>
  1564. <p>When parsing a configspec, ConfigObj will stop on the first error it
  1565. encounters.  It will raise a <tt class="docutils literal">ConfigspecError</tt>. This will have an <tt class="docutils literal">error</tt>
  1566. attribute, which is the actual error that was raised.</p>
  1567. <p>Behaviour when parsing a config file depends on the option <tt class="docutils literal">raise_errors</tt>.
  1568. If ConfigObj encounters an error while parsing a config file:</p>
  1569. <blockquote>
  1570. <p>If <tt class="docutils literal">raise_errors=True</tt> then ConfigObj will raise the appropriate error
  1571. and parsing will stop.</p>
  1572. <p>If <tt class="docutils literal">raise_errors=False</tt> (the default) then parsing will continue to the
  1573. end and <em>all</em> errors will be collected.</p>
  1574. </blockquote>
  1575. <p>If <tt class="docutils literal">raise_errors</tt> is False and multiple errors are found a <tt class="docutils literal">ConfigObjError</tt>
  1576. is raised. The error raised has a <tt class="docutils literal">config</tt> attribute, which is the parts of
  1577. the ConfigObj that parsed successfully. It also has an attribute <tt class="docutils literal">errors</tt>,
  1578. which is a list of <em>all</em> the errors raised. Each entry in the list is an
  1579. instance of the appropriate error type. Each one has the following attributes
  1580. (useful for delivering a sensible error message to your user) :</p>
  1581. <ul class="simple">
  1582. <li><tt class="docutils literal">line</tt>: the original line that caused the error.</li>
  1583. <li><tt class="docutils literal">line_number</tt>: its number in the config file.</li>
  1584. <li><tt class="docutils literal">message</tt>: the error message that accompanied the error.</li>
  1585. </ul>
  1586. <p>If only one error is found, then that error is re-raised. The error still has
  1587. the <tt class="docutils literal">config</tt> and <tt class="docutils literal">errors</tt> attributes. This means that your error handling
  1588. code can be the same whether one error is raised in parsing , or several.</p>
  1589. <p>It also means that in the most common case (a single error) a useful error
  1590. message will be raised.</p>
  1591. <div class="note">
  1592. <p class="first admonition-title">Note</p>
  1593. <p class="last">One wrongly written line could break the basic structure of your config
  1594. file. This could cause every line after it to flag an error, so having a
  1595. list of all the lines that caused errors may not be as useful as it sounds.</p>
  1596. </div>
  1597. </div>
  1598. <div class="section" id="validation">
  1599. <h1><a class="toc-backref" href="#id67">9   Validation</a></h1>
  1600. <div class="hint">
  1601. <p class="first admonition-title">Hint</p>
  1602. <p>The system of configspecs can seem confusing at first, but is actually
  1603. quite simple and powerful. The best reference is my article on ConfigObj:</p>
  1604. <ul class="last simple">
  1605. <li><a class="reference external" href="http://www.voidspace.org.uk/python/articles/configobj.shtml">An Introduction to ConfigObj</a></li>
  1606. </ul>
  1607. </div>
  1608. <p>Validation is done through a combination of the <a class="reference internal" href="#configspec">configspec</a> and a <tt class="docutils literal">Validator</tt>
  1609. object. For this you need <em>validate.py</em> <a class="footnote-reference" href="#id26" id="id12">[12]</a>. See <a class="reference internal" href="#downloading">downloading</a> if you don't
  1610. have a copy.</p>
  1611. <p>Validation can perform two different operations :</p>
  1612. <ol class="arabic">
  1613. <li><dl class="first docutils">
  1614. <dt>Check that a value meets a specification. For example, check that a value</dt>
  1615. <dd><p class="first last">is an integer between one and six, or is a choice from a specific set of
  1616. options.</p>
  1617. </dd>
  1618. </dl>
  1619. </li>
  1620. <li><dl class="first docutils">
  1621. <dt>It can convert the value into the type required. For example, if one of</dt>
  1622. <dd><p class="first last">your values is a port number, validation will turn it into an integer for
  1623. you.</p>
  1624. </dd>
  1625. </dl>
  1626. </li>
  1627. </ol>
  1628. <p>So validation can act as a transparent layer between the datatypes of your
  1629. application configuration (boolean, integers, floats, etc) and the text format
  1630. of your config file.</p>
  1631. <div class="section" id="configspec">
  1632. <h2><a class="toc-backref" href="#id68">9.1   configspec</a></h2>
  1633. <p>The <tt class="docutils literal">validate</tt> method checks members against an entry in the configspec. Your
  1634. configspec therefore resembles your config file, with a check for every member.</p>
  1635. <p>In order to perform validation you need a <tt class="docutils literal">Validator</tt> object. This has
  1636. several useful built-in check functions. You can also create your own custom
  1637. functions and register them with your Validator object.</p>
  1638. <p>Each check is the name of one of these functions, including any parameters and
  1639. keyword arguments. The configspecs look like function calls, and they map to
  1640. function calls.</p>
  1641. <p>The basic datatypes that an un-extended Validator can test for are :</p>
  1642. <ul class="simple">
  1643. <li>boolean values (True and False)</li>
  1644. <li>integers (including minimum and maximum values)</li>
  1645. <li>floats (including min and max)</li>
  1646. <li>strings (including min and max length)</li>
  1647. <li>IP addresses (v4 only)</li>
  1648. </ul>
  1649. <p>It can also handle lists of these types and restrict a value to being one from
  1650. a set of options.</p>
  1651. <p>An example configspec is going to look something like:</p>
  1652. <pre class="literal-block">
  1653. port = integer(0, 100)
  1654. user = string(max=25)
  1655. mode = option('quiet', 'loud', 'silent')
  1656. </pre>
  1657. <p>You can specify default values, and also have the same configspec applied to
  1658. several sections. This is called <a class="reference internal" href="#repeated-sections">repeated sections</a>.</p>
  1659. <p>For full details on writing configspecs, please refer to the <a class="reference external" href="http://www.voidspace.org.uk/python/validate.html">validate.py
  1660. documentation</a>.</p>
  1661. <div class="important">
  1662. <p class="first admonition-title">Important</p>
  1663. <p>Your configspec is read by ConfigObj in the same way as a config file.</p>
  1664. <p>That means you can do interpolation <em>within</em> your configspec.</p>
  1665. <p>In order to allow this, checks in the 'DEFAULT' section (of the root level
  1666. of your configspec) are <em>not</em> used.</p>
  1667. <p>If you want to use a configspec <em>without</em> interpolation being done in it
  1668. you can create your configspec manually and switch off interpolation:</p>
  1669. <div class="last"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">configobj</span> <span class="kn">import</span> <span class="n">ConfigObj</span>
  1670.  
  1671. <span class="n">configspec</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">spec_filename</span><span class="p">,</span> <span class="n">interpolation</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">list_values</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
  1672.                        <span class="n">_inspec</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
  1673. <span class="n">conf</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">config_filename</span><span class="p">,</span> <span class="n">configspec</span><span class="o">=</span><span class="n">configspec</span><span class="p">)</span>
  1674. </pre></div>
  1675. </div></div>
  1676. <p>If you need to specify the encoding of your configspec, then you can pass in a
  1677. ConfigObj instance as your configspec. When you read your configspec file, you
  1678. <em>must</em> specify <tt class="docutils literal">list_values=False</tt>. If you need to support hashes in
  1679. configspec values then you must also pass in <tt class="docutils literal">_inspec=True</tt>.</p>
  1680. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">configobj</span> <span class="kn">import</span> <span class="n">ConfigObj</span>
  1681. <span class="n">configspec</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">configspecfilename</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s">'UTF8'</span><span class="p">,</span>
  1682.                        <span class="n">list_values</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">_inspec</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
  1683. <span class="n">config</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">configspec</span><span class="o">=</span><span class="n">configspec</span><span class="p">)</span>
  1684. </pre></div>
  1685. </div>
  1686. <div class="section" id="type-conversion">
  1687. <h2><a class="toc-backref" href="#id69">9.2   Type Conversion</a></h2>
  1688. <p>By default, validation does type conversion. This means that if you specify
  1689. <tt class="docutils literal">integer</tt> as the check, then calling <a class="reference internal" href="#validate">validate</a> will actually change the value
  1690. to an integer (so long as the check succeeds).</p>
  1691. <p>It also means that when you call the <a class="reference internal" href="#write">write</a> method, the value will be converted
  1692. back into a string using the <tt class="docutils literal">str</tt> function.</p>
  1693. <p>To switch this off, and leave values as strings after validation, you need to
  1694. set the <a class="reference internal" href="#stringify">stringify</a> attribute to <tt class="docutils literal">False</tt>. If this is the case, attempting to
  1695. set a value to a non-string will raise an error.</p>
  1696. </div>
  1697. <div class="section" id="default-values">
  1698. <h2><a class="toc-backref" href="#id70">9.3   Default Values</a></h2>
  1699. <p>You can set a default value in your check. If the value is missing from the
  1700. config file then this value will be used instead. This means that your user
  1701. only has to supply values that differ from the defaults.</p>
  1702. <p>If you <em>don't</em> supply a default then for a value to be missing is an error,
  1703. and this will show in the <a class="reference internal" href="#return-value">return value</a> from validate.</p>
  1704. <p>Additionally you can set the default to be <tt class="docutils literal">None</tt>. This means the value will
  1705. be set to <tt class="docutils literal">None</tt> (the object) <em>whichever check is used</em>. (It will be set to
  1706. <tt class="docutils literal">''</tt> rather than <tt class="docutils literal">None</tt> if <a class="reference internal" href="#stringify">stringify</a> is <tt class="docutils literal">False</tt>). You can use this
  1707. to easily implement optional values in your config files.</p>
  1708. <pre class="literal-block">
  1709. port = integer(0, 100, default=80)
  1710. user = string(max=25, default=0)
  1711. mode = option('quiet', 'loud', 'silent', default='loud')
  1712. nick = string(default=None)
  1713. </pre>
  1714. <div class="note">
  1715. <p class="first admonition-title">Note</p>
  1716. <p>Because the default goes through type conversion, it also has to pass the
  1717. check.</p>
  1718. <p class="last">Note that <tt class="docutils literal">default=None</tt> is case sensitive.</p>
  1719. </div>
  1720. <div class="section" id="id13">
  1721. <h3><a class="toc-backref" href="#id71">9.3.1   List Values</a></h3>
  1722. <p>It's possible that you will want to specify a list as a default value. To avoid
  1723. confusing syntax with commas and quotes you use a list constructor to specify
  1724. that keyword arguments are lists. This includes the <tt class="docutils literal">default</tt> value. This
  1725. makes checks look something like:</p>
  1726. <pre class="literal-block">
  1727. checkname(default=list('val1', 'val2', 'val3'))
  1728. </pre>
  1729. <p>This works with all keyword arguments, but is most useful for default values.</p>
  1730. </div>
  1731. </div>
  1732. <div class="section" id="repeated-sections">
  1733. <h2><a class="toc-backref" href="#id72">9.4   Repeated Sections</a></h2>
  1734. <p>Repeated sections are a way of specifying a configspec for a section that
  1735. should be applied to all unspecified subsections in the same section.</p>
  1736. <p>The easiest way of explaining this is to give an example. Suppose you have a
  1737. config file that describes a dog. That dog has various attributes, but it can
  1738. also have many fleas. You don't know in advance how many fleas there will be,
  1739. or what they will be called, but you want each flea validated against the same
  1740. configspec.</p>
  1741. <p>We can define a section called <em>fleas</em>. We want every flea in that section
  1742. (every sub-section) to have the same configspec applied to it. We do this by
  1743. defining a single section called <tt class="docutils literal">__many__</tt>.</p>
  1744. <pre class="literal-block">
  1745. [dog]
  1746. name = string(default=Rover)
  1747. age = float(0, 99, default=0)
  1748.  
  1749.     [[fleas]]
  1750.  
  1751.         [[[__many__]]]
  1752.         bloodsucker = boolean(default=True)
  1753.         children = integer(default=10000)
  1754.         size = option(small, tiny, micro, default=tiny)
  1755. </pre>
  1756. <p>Every flea on our dog will now be validated using the <tt class="docutils literal">__many__</tt> configspec.</p>
  1757. <p><tt class="docutils literal">__many__</tt> sections can have sub-sections, including their own <tt class="docutils literal">__many__</tt>
  1758. sub-sections. Defaults work in the normal way in repeated sections.</p>
  1759. </div>
  1760. <div class="section" id="repeated-values">
  1761. <h2><a class="toc-backref" href="#id73">9.5   Repeated Values</a></h2>
  1762. <p>As well as using <tt class="docutils literal">__many__</tt> to validate unspecified sections you can use it to validate values. For
  1763. example, to specify that all values in a section should be integers:</p>
  1764. <pre class="literal-block">
  1765. [section]
  1766.     __many__ = integer
  1767. </pre>
  1768. <p>If you want to use repeated values alongside repeated sections you can call one <tt class="docutils literal">__many__</tt> and the
  1769. other <tt class="docutils literal">___many___</tt> (with three underscores).</p>
  1770. </div>
  1771. <div class="section" id="copy-mode">
  1772. <h2><a class="toc-backref" href="#id74">9.6   Copy Mode</a></h2>
  1773. <p>Because you can specify default values in your configspec, you can use
  1774. ConfigObj to write out default config files for your application.</p>
  1775. <p>However, normally values supplied from a default in a configspec are <em>not</em>
  1776. written out by the <tt class="docutils literal">write</tt> method.</p>
  1777. <p>To do this, you need to specify <tt class="docutils literal">copy=True</tt> when you call validate. As well
  1778. as not marking values as default, all the comments in the configspec file
  1779. will be copied into your ConfigObj instance.</p>
  1780. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">configobj</span> <span class="kn">import</span> <span class="n">ConfigObj</span>
  1781. <span class="kn">from</span> <span class="nn">validate</span> <span class="kn">import</span> <span class="n">Validator</span>
  1782. <span class="n">vdt</span> <span class="o">=</span> <span class="n">Validator</span><span class="p">()</span>
  1783. <span class="n">config</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">configspec</span><span class="o">=</span><span class="s">'default.ini'</span><span class="p">)</span>
  1784. <span class="n">config</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="s">'new_default.ini'</span>
  1785. <span class="n">config</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">vdt</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
  1786. <span class="n">config</span><span class="o">.</span><span class="n">write</span><span class="p">()</span>
  1787. </pre></div>
  1788. <p>If you need to support hashes in the configspec values then you must create
  1789. it with <tt class="docutils literal">_inspec=True</tt>. This has the side effect of switching off the parsing
  1790. of inline comments, meaning that they won't be copied into the new config file.
  1791. (ConfigObj syntax is slightly different from configspec syntax and the parser
  1792. can't support both inline comments and hashes in configspec values.)</p>
  1793. </div>
  1794. <div class="section" id="validation-and-interpolation">
  1795. <h2><a class="toc-backref" href="#id75">9.7   Validation and Interpolation</a></h2>
  1796. <p>String interpolation and validation don't play well together. When validation
  1797. changes type it sets the value. If the value uses interpolation, then the
  1798. interpolation reference would normally be overwritten. Calling <tt class="docutils literal">write</tt> would
  1799. then use the absolute value and the interpolation reference would be lost.</p>
  1800. <p>As a compromise - if the value is unchanged by validation then it is not reset.
  1801. This means strings that pass through validation unmodified will not be
  1802. overwritten. If validation changes type - the value has to be overwritten, and
  1803. any interpolation references are lost.</p>
  1804. </div>
  1805. <div class="section" id="extra-values">
  1806. <h2><a class="toc-backref" href="#id76">9.8   Extra Values</a></h2>
  1807. <p>After validation the <tt class="docutils literal">extra_values</tt> member of every section that is listed in
  1808. the configspec will be populated with the names of members that are in the
  1809. config file but not in the configspec.</p>
  1810. <p>If you are reporting configuration errors to your user this information can be
  1811. useful, for example some missing entries may be due to misspelt entries that
  1812. appear as extra values.</p>
  1813. <p>See the <a class="reference internal" href="#get-extra-values">get_extra_values</a> function</p>
  1814. <p>New in ConfigObj 4.7.0.</p>
  1815. </div>
  1816. <div class="section" id="simpleval">
  1817. <h2><a class="toc-backref" href="#id77">9.9   SimpleVal</a></h2>
  1818. <p>You may not need a full validation process, but still want to check if all the
  1819. expected values are present.</p>
  1820. <p>Provided as part of the ConfigObj module is the <tt class="docutils literal">SimpleVal</tt> object. This has
  1821. a dummy <tt class="docutils literal">test</tt> method that always passes.</p>
  1822. <p>The only reason a test will fail is if the value is missing. The return value
  1823. from <tt class="docutils literal">validate</tt> will either be <tt class="docutils literal">True</tt>, meaning all present, or a dictionary
  1824. with <tt class="docutils literal">False</tt> for all missing values/sections.</p>
  1825. <p>To use it, you still need to pass in a valid configspec when you create the
  1826. ConfigObj, but just set all the values to <tt class="docutils literal">''</tt>. Then create an instance of
  1827. <tt class="docutils literal">SimpleVal</tt> and pass it to the <tt class="docutils literal">validate</tt> method.</p>
  1828. <p>As a trivial example if you had the following config file:</p>
  1829. <pre class="literal-block">
  1830. # config file for an application
  1831. port = 80
  1832. protocol = http
  1833. domain = voidspace
  1834. top_level_domain = org.uk
  1835. </pre>
  1836. <p>You would write the following configspec:</p>
  1837. <pre class="literal-block">
  1838. port = ''
  1839. protocol = ''
  1840. domain = ''
  1841. top_level_domain = ''
  1842. </pre>
  1843. <div class="highlight"><pre><span class="n">config</span> <span class="o">=</span> <span class="n">Configobj</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">configspec</span><span class="o">=</span><span class="n">configspec</span><span class="p">)</span>
  1844. <span class="n">val</span> <span class="o">=</span> <span class="n">SimpleVal</span><span class="p">()</span>
  1845. <span class="n">test</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
  1846. <span class="k">if</span> <span class="n">test</span> <span class="o">==</span> <span class="bp">True</span><span class="p">:</span>
  1847.     <span class="k">print</span> <span class="s">'All values present.'</span>
  1848. <span class="k">elif</span> <span class="n">test</span> <span class="o">==</span> <span class="bp">False</span><span class="p">:</span>
  1849.     <span class="k">print</span> <span class="s">'No values present!'</span>
  1850. <span class="k">else</span><span class="p">:</span>
  1851.     <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">test</span><span class="p">:</span>
  1852.         <span class="k">if</span> <span class="n">test</span><span class="p">[</span><span class="n">entry</span><span class="p">]</span> <span class="o">==</span> <span class="bp">False</span><span class="p">:</span>
  1853.             <span class="k">print</span> <span class="s">'"</span><span class="si">%s</span><span class="s">" missing.'</span> <span class="o">%</span> <span class="n">entry</span>
  1854. </pre></div>
  1855. </div>
  1856. </div>
  1857. <div class="section" id="empty-values">
  1858. <h1><a class="toc-backref" href="#id78">10   Empty values</a></h1>
  1859. <p>Many config files from other applications allow empty values. As of version
  1860. 4.3.0, ConfigObj will read these as an empty string.</p>
  1861. <p>A new option/attribute has been added (<tt class="docutils literal">write_empty_values</tt>) to allow
  1862. ConfigObj to write empty strings as empty values.</p>
  1863. <div class="highlight"><pre><span class="kn">from</span> <span class="nn">configobj</span> <span class="kn">import</span> <span class="n">ConfigObj</span>
  1864. <span class="n">cfg</span> <span class="o">=</span> <span class="s">'''</span>
  1865. <span class="s">    key =</span>
  1866. <span class="s">    key2 = # a comment</span>
  1867. <span class="s">'''</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()</span>
  1868. <span class="n">config</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">cfg</span><span class="p">)</span>
  1869. <span class="k">print</span> <span class="n">config</span>
  1870. <span class="n">ConfigObj</span><span class="p">({</span><span class="s">'key'</span><span class="p">:</span> <span class="s">''</span><span class="p">,</span> <span class="s">'key2'</span><span class="p">:</span> <span class="s">''</span><span class="p">})</span>
  1871.  
  1872. <span class="n">config</span><span class="o">.</span><span class="n">write_empty_values</span> <span class="o">=</span> <span class="bp">True</span>
  1873. <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">config</span><span class="o">.</span><span class="n">write</span><span class="p">():</span>
  1874.     <span class="k">print</span> <span class="n">line</span>
  1875.  
  1876. <span class="n">key</span> <span class="o">=</span>
  1877. <span class="n">key2</span> <span class="o">=</span>     <span class="c"># a comment</span>
  1878. </pre></div>
  1879. </div>
  1880. <div class="section" id="unrepr-mode">
  1881. <h1><a class="toc-backref" href="#id79">11   unrepr mode</a></h1>
  1882. <p>The <tt class="docutils literal">unrepr</tt> option allows you to store and retrieve the basic Python
  1883. data-types using config files. It has to use a slightly different syntax to
  1884. normal ConfigObj files. Unsurprisingly it uses Python syntax.</p>
  1885. <p>This means that lists are different (they are surrounded by square brackets),
  1886. and strings <em>must</em> be quoted.</p>
  1887. <p>The types that <tt class="docutils literal">unrepr</tt> can work with are :</p>
  1888. <blockquote>
  1889. <div class="line-block">
  1890. <div class="line">strings, lists tuples</div>
  1891. <div class="line">None, True, False</div>
  1892. <div class="line">dictionaries, integers, floats</div>
  1893. <div class="line">longs and complex numbers</div>
  1894. </div>
  1895. </blockquote>
  1896. <p>You can't store classes, types or instances.</p>
  1897. <p><tt class="docutils literal">unrepr</tt> uses <tt class="docutils literal">repr(object)</tt> to write out values, so it currently <em>doesn't</em>
  1898. check that you are writing valid objects. If you attempt to read an unsupported
  1899. value, ConfigObj will raise a <tt class="docutils literal">configobj.UnknownType</tt> exception.</p>
  1900. <p>Values that are triple quoted cased. The triple quotes are removed <em>before</em>
  1901. converting. This means that you can use triple quotes to write dictionaries
  1902. over several lines in your config files. They won't be written like this
  1903. though.</p>
  1904. <p>If you are writing config files by hand, for use with <tt class="docutils literal">unrepr</tt>, you should
  1905. be aware of the following differences from normal ConfigObj syntax :</p>
  1906. <blockquote>
  1907. <div class="line-block">
  1908. <div class="line">List : <tt class="docutils literal">['A List', 'With', 'Strings']</tt></div>
  1909. <div class="line">Strings : <tt class="docutils literal">"Must be quoted."</tt></div>
  1910. <div class="line">Backslash : <tt class="docutils literal">"The backslash must be escaped \\"</tt></div>
  1911. </div>
  1912. </blockquote>
  1913. <p>These all follow normal Python syntax.</p>
  1914. <p>In unrepr mode <em>inline comments</em> are not saved. This is because lines are
  1915. parsed using the <a class="reference external" href="http://docs.python.org/lib/compiler.html">compiler package</a>
  1916. which discards comments.</p>
  1917. </div>
  1918. <div class="section" id="string-interpolation">
  1919. <h1><a class="toc-backref" href="#id80">12   String Interpolation</a></h1>
  1920. <div class="note">
  1921. <p class="first admonition-title">Note</p>
  1922. <p class="last">String interpolation can slow down (slightly) the fetching of values
  1923. from your config object. If you aren't using interpolation and it
  1924. is performance critical then create your instance with
  1925. <tt class="docutils literal">interpolation=False</tt>.</p>
  1926. </div>
  1927. <p>ConfigObj allows string interpolation <em>similar</em> to the way <tt class="docutils literal">ConfigParser</tt>
  1928. or <tt class="docutils literal">string.Template</tt> work. The value of the <tt class="docutils literal">interpolation</tt> attribute
  1929. determines which style of interpolation you want to use. Valid values are
  1930. "ConfigParser" or "Template" (case-insensitive, so "configparser" and
  1931. "template" will also work). For backwards compatibility reasons, the value
  1932. <tt class="docutils literal">True</tt> is also a valid value for the <tt class="docutils literal">interpolation</tt> attribute, and
  1933. will select <tt class="docutils literal">ConfigParser</tt>-style interpolation. At some undetermined point
  1934. in the future, that default <em>may</em> change to <tt class="docutils literal">Template</tt>-style interpolation.</p>
  1935. <p>For <tt class="docutils literal">ConfigParser</tt>-style interpolation, you specify a value to be
  1936. substituted by including <tt class="docutils literal">%(name)s</tt> in the value.</p>
  1937. <p>For <tt class="docutils literal">Template</tt>-style interpolation, you specify a value to be substituted
  1938. by including <tt class="docutils literal">${cl}name{cr}</tt> in the value. Alternately, if 'name' is a valid
  1939. Python identifier (i.e., is composed of nothing but alphanumeric characters,
  1940. plus the underscore character), then the braces are optional and the value
  1941. can be written as <tt class="docutils literal">$name</tt>.</p>
  1942. <p>Note that <tt class="docutils literal">ConfigParser</tt>-style interpolation and <tt class="docutils literal">Template</tt>-style
  1943. interpolation are mutually exclusive; you cannot have a configuration file
  1944. that's a mix of one or the other. Pick one and stick to it. <tt class="docutils literal">Template</tt>-style
  1945. interpolation is simpler to read and write by hand, and is recommended if
  1946. you don't have a particular reason to use <tt class="docutils literal">ConfigParser</tt>-style.</p>
  1947. <p>Interpolation checks first the current section to see if <tt class="docutils literal">name</tt> is the key
  1948. to a value. ('name' is case sensitive).</p>
  1949. <p>If it doesn't find it, next it checks the 'DEFAULT' sub-section of the current
  1950. section.</p>
  1951. <p>If it still doesn't find it, it moves on to check the parent section and the
  1952. parent section's 'DEFAULT' subsection, and so on all the way up to the main
  1953. section.</p>
  1954. <p>If the value specified isn't found in any of these locations, then a
  1955. <tt class="docutils literal">MissingInterpolationOption</tt> error is raised (a subclass of
  1956. <tt class="docutils literal">ConfigObjError</tt>).</p>
  1957. <p>If it is found then the returned value is also checked for substitutions. This
  1958. allows you to make up compound values (for example directory paths) that use
  1959. more than one default value. It also means it's possible to create circular
  1960. references. If there are any circular references which would cause an infinite
  1961. interpolation loop, an <tt class="docutils literal">InterpolationLoopError</tt> is raised.</p>
  1962. <p>Both of these errors are subclasses of <tt class="docutils literal">InterpolationError</tt>, which is a
  1963. subclass of <tt class="docutils literal">ConfigObjError</tt>.</p>
  1964. <p>String interpolation and validation don't play well together. This is because
  1965. validation overwrites values - and so may erase the interpolation references.
  1966. See <a class="reference internal" href="#validation-and-interpolation">Validation and Interpolation</a>. (This can only happen if validation
  1967. has to <em>change</em> the value).</p>
  1968. <p>New in ConfigObj 4.7.0: String interpolation is now done in members of list
  1969. values.</p>
  1970. <div class="section" id="string-interpolation-and-list-values">
  1971. <h2><a class="toc-backref" href="#id81">12.1   String Interpolation and List Values</a></h2>
  1972. <p>Since version 4.7 string interpolation is done on string members of list values.
  1973. If interpolation changes any members of the list then what you get back is a
  1974. <em>copy</em> of the list rather than the original list.</p>
  1975. <p>This makes fetching list values slightly slower when interpolation is on, it
  1976. also means that if you mutate the list changes won't be reflected in the
  1977. original list:</p>
  1978. <div class="highlight"><pre><span class="o">>>></span> <span class="n">c</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">()</span>
  1979. <span class="o">>>></span> <span class="n">c</span><span class="p">[</span><span class="s">'foo'</span><span class="p">]</span> <span class="o">=</span> <span class="s">'boo'</span>
  1980. <span class="o">>>></span> <span class="n">c</span><span class="p">[</span><span class="s">'bar'</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="s">'</span><span class="si">%(foo)s</span><span class="s">'</span><span class="p">]</span>
  1981. <span class="o">>>></span> <span class="n">c</span><span class="p">[</span><span class="s">'bar'</span><span class="p">]</span>
  1982. <span class="p">[</span><span class="s">'boo'</span><span class="p">]</span>
  1983. <span class="o">>>></span> <span class="n">c</span><span class="p">[</span><span class="s">'bar'</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">'fish'</span><span class="p">)</span>
  1984. <span class="o">>>></span> <span class="n">c</span><span class="p">[</span><span class="s">'bar'</span><span class="p">]</span>
  1985. <span class="p">[</span><span class="s">'boo'</span><span class="p">]</span>
  1986. </pre></div>
  1987. <p>Instead of mutating the list you must create a new list and reassign it.</p>
  1988. </div>
  1989. </div>
  1990. <div class="section" id="comments">
  1991. <h1><a class="toc-backref" href="#id82">13   Comments</a></h1>
  1992. <p>Any line that starts with a '#', possibly preceded by whitespace, is a comment.</p>
  1993. <p>If a config file starts with comments then these are preserved as the
  1994. <a class="reference internal" href="#initial-comment">initial_comment</a>.</p>
  1995. <p>If a config file ends with comments then these are preserved as the
  1996. <a class="reference internal" href="#final-comment">final_comment</a>.</p>
  1997. <p>Every key or section marker may have lines of comments immediately above it.
  1998. These are saved as the <tt class="docutils literal">comments</tt> attribute of the section. Each member is a
  1999. list of lines.</p>
  2000. <p>You can also have a comment inline with a value. These are saved as the
  2001. <tt class="docutils literal">inline_comments</tt> attribute of the section, with one entry per member of the
  2002. section.</p>
  2003. <p>Subsections (section markers in the config file) can also have comments.</p>
  2004. <p>See <a class="reference internal" href="#section-attributes">Section Attributes</a> for more on these attributes.</p>
  2005. <p>These comments are all written back out by the <tt class="docutils literal">write</tt> method.</p>
  2006. </div>
  2007. <div class="section" id="flatten-errors">
  2008. <h1><a class="toc-backref" href="#id83">14   flatten_errors</a></h1>
  2009. <div class="highlight"><pre><span class="n">flatten_errors</span><span class="p">(</span><span class="n">cfg</span><span class="p">,</span> <span class="n">res</span><span class="p">)</span>
  2010. </pre></div>
  2011. <p><a class="reference internal" href="#validation">Validation</a> is a powerful way of checking that the values supplied by the user
  2012. make sense.</p>
  2013. <p>The <a class="reference internal" href="#validate">validate</a> method returns a results dictionary that represents pass or fail
  2014. for each value. This doesn't give you any information about <em>why</em> the check
  2015. failed.</p>
  2016. <p><tt class="docutils literal">flatten_errors</tt> is an example function that turns a results dictionary into
  2017. a flat list, that only contains values that <em>failed</em>.</p>
  2018. <p><tt class="docutils literal">cfg</tt> is the ConfigObj instance being checked, <tt class="docutils literal">res</tt> is the results
  2019. dictionary returned by <tt class="docutils literal">validate</tt>.</p>
  2020. <p>It returns a list of keys that failed. Each member of the list is a tuple:</p>
  2021. <pre class="literal-block">
  2022. ([list of sections...], key, result)
  2023. </pre>
  2024. <p>If <tt class="docutils literal">validate</tt> was called with <tt class="docutils literal">preserve_errors=False</tt> (the default)
  2025. then <tt class="docutils literal">result</tt> will always be <tt class="docutils literal">False</tt>.</p>
  2026. <p><em>list of sections</em> is a flattened list of sections that the key was found
  2027. in.</p>
  2028. <p>If the section was missing then key will be <tt class="docutils literal">None</tt>.</p>
  2029. <p>If the value (or section) was missing then <tt class="docutils literal">result</tt> will be <tt class="docutils literal">False</tt>.</p>
  2030. <p>If <tt class="docutils literal">validate</tt> was called with <tt class="docutils literal">preserve_errors=True</tt> and a value
  2031. was present, but failed the check, then <tt class="docutils literal">result</tt> will be the exception
  2032. object returned. You can use this as a string that describes the failure.</p>
  2033. <p>For example :</p>
  2034. <blockquote>
  2035. <em>The value "3" is of the wrong type</em>.</blockquote>
  2036. <div class="section" id="example-usage">
  2037. <h2><a class="toc-backref" href="#id84">14.1   Example Usage</a></h2>
  2038. <p>The output from <tt class="docutils literal">flatten_errors</tt> is a list of tuples.</p>
  2039. <p>Here is an example of how you could present this information to the user.</p>
  2040. <div class="highlight"><pre><span class="n">vtor</span> <span class="o">=</span> <span class="n">validate</span><span class="o">.</span><span class="n">Validator</span><span class="p">()</span>
  2041. <span class="c"># ini is your config file - cs is the configspec</span>
  2042. <span class="n">cfg</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">ini</span><span class="p">,</span> <span class="n">configspec</span><span class="o">=</span><span class="n">cs</span><span class="p">)</span>
  2043. <span class="n">res</span> <span class="o">=</span> <span class="n">cfg</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">vtor</span><span class="p">,</span> <span class="n">preserve_errors</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
  2044. <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">flatten_errors</span><span class="p">(</span><span class="n">cfg</span><span class="p">,</span> <span class="n">res</span><span class="p">):</span>
  2045.     <span class="c"># each entry is a tuple</span>
  2046.     <span class="n">section_list</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">error</span> <span class="o">=</span> <span class="n">entry</span>
  2047.     <span class="k">if</span> <span class="n">key</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
  2048.        <span class="n">section_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  2049.     <span class="k">else</span><span class="p">:</span>
  2050.         <span class="n">section_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">'[missing section]'</span><span class="p">)</span>
  2051.     <span class="n">section_string</span> <span class="o">=</span> <span class="s">', '</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">section_list</span><span class="p">)</span>
  2052.     <span class="k">if</span> <span class="n">error</span> <span class="o">==</span> <span class="bp">False</span><span class="p">:</span>
  2053.         <span class="n">error</span> <span class="o">=</span> <span class="s">'Missing value or section.'</span>
  2054.     <span class="k">print</span> <span class="n">section_string</span><span class="p">,</span> <span class="s">' = '</span><span class="p">,</span> <span class="n">error</span>
  2055. </pre></div>
  2056. </div>
  2057. </div>
  2058. <div class="section" id="get-extra-values">
  2059. <h1><a class="toc-backref" href="#id85">15   get_extra_values</a></h1>
  2060. <div class="highlight"><pre><span class="n">get_extra_values</span><span class="p">(</span><span class="n">conf</span><span class="p">)</span>
  2061. </pre></div>
  2062. <p>New in ConfigObj 4.7.0.</p>
  2063. <p>Find all the values and sections not in the configspec from a validated
  2064. ConfigObj.</p>
  2065. <p><tt class="docutils literal">get_extra_values</tt> returns a list of tuples where each tuple represents
  2066. either an extra section, or an extra value.</p>
  2067. <p>The tuples contain two values, a tuple representing the section the value
  2068. is in and the name of the extra values. For extra values in the top level
  2069. section the first member will be an empty tuple. For values in the 'foo'
  2070. section the first member will be <tt class="docutils literal"><span class="pre">('foo',)</span></tt>. For members in the 'bar'
  2071. subsection of the 'foo' section the first member will be <tt class="docutils literal">('foo', 'bar')</tt>.</p>
  2072. <p>Extra sections will only have one entry. Values and subsections inside
  2073. an extra section aren't listed separately.</p>
  2074. <p>NOTE: If you call <tt class="docutils literal">get_extra_values</tt> on a ConfigObj instance that hasn't
  2075. been validated it will return an empty list.</p>
  2076. <div class="section" id="id14">
  2077. <h2><a class="toc-backref" href="#id86">15.1   Example Usage</a></h2>
  2078. <p>The output from <tt class="docutils literal">get_extra_values</tt> is a list of tuples.</p>
  2079. <p>Here is an example of how you could present this information to the user.</p>
  2080. <div class="highlight"><pre><span class="n">vtor</span> <span class="o">=</span> <span class="n">validate</span><span class="o">.</span><span class="n">Validator</span><span class="p">()</span>
  2081. <span class="c"># ini is your config file - cs is the configspec</span>
  2082. <span class="n">cfg</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">ini</span><span class="p">,</span> <span class="n">configspec</span><span class="o">=</span><span class="n">cs</span><span class="p">)</span>
  2083. <span class="n">cfg</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">vtor</span><span class="p">,</span> <span class="n">preserve_errors</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
  2084.  
  2085. <span class="k">for</span> <span class="n">sections</span><span class="p">,</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">get_extra_values</span><span class="p">(</span><span class="n">cfg</span><span class="p">):</span>
  2086.  
  2087.     <span class="c"># this code gets the extra values themselves</span>
  2088.     <span class="n">the_section</span> <span class="o">=</span> <span class="n">cfg</span>
  2089.     <span class="k">for</span> <span class="n">section</span> <span class="ow">in</span> <span class="n">sections</span><span class="p">:</span>
  2090.         <span class="n">the_section</span> <span class="o">=</span> <span class="n">cfg</span><span class="p">[</span><span class="n">section</span><span class="p">]</span>
  2091.  
  2092.     <span class="c"># the_value may be a section or a value</span>
  2093.     <span class="n">the_value</span> <span class="o">=</span> <span class="n">the_section</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
  2094.  
  2095.     <span class="n">section_or_value</span> <span class="o">=</span> <span class="s">'value</span>
  2096.     <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">the_value</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
  2097.         <span class="c"># Sections are subclasses of dict</span>
  2098.         <span class="n">section_or_value</span> <span class="o">=</span> <span class="s">'section'</span>
  2099.  
  2100.     <span class="n">section_string</span> <span class="o">=</span> <span class="s">', '</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">sections</span><span class="p">)</span> <span class="ow">or</span> <span class="s">"top level"</span>
  2101.     <span class="k">print</span> <span class="s">'Extra entry in section: </span><span class="si">%s</span><span class="s">. Entry </span><span class="si">%r</span><span class="s"> is a </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">section_string</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">section_or_value</span><span class="p">)</span>
  2102. </pre></div>
  2103. </div>
  2104. </div>
  2105. <div class="section" id="credits">
  2106. <h1><a class="toc-backref" href="#id87">16   CREDITS</a></h1>
  2107. <p>ConfigObj 4 is written by (and copyright) <a class="reference external" href="http://www.voidspace.org.uk/python/weblog/index.shtml">Michael Foord</a> and
  2108. <a class="reference external" href="http://www.teknico.net">Nicola Larosa</a>.</p>
  2109. <p>Particularly thanks to Nicola Larosa for help on the config file spec, the
  2110. validation system and the doctests.</p>
  2111. <p><em>validate.py</em> was originally written by Michael Foord and Mark Andrews.</p>
  2112. <p>Thanks to many others for input, patches and bugfixes.</p>
  2113. </div>
  2114. <div class="section" id="license">
  2115. <h1><a class="toc-backref" href="#id88">17   LICENSE</a></h1>
  2116. <p>ConfigObj, and related files, are licensed under the BSD license. This is a
  2117. very unrestrictive license, but it comes with the usual disclaimer. This is
  2118. free software: test it, break it, just don't blame us if it eats your data !
  2119. Of course if it does, let us know and we'll fix the problem so it doesn't
  2120. happen to anyone else:</p>
  2121. <pre class="literal-block">
  2122. Copyright (c) 2004 - 2010, Michael Foord & Nicola Larosa
  2123. All rights reserved.
  2124.  
  2125. Redistribution and use in source and binary forms, with or without
  2126. modification, are permitted provided that the following conditions are
  2127. met:
  2128.  
  2129.  
  2130.     * Redistributions of source code must retain the above copyright
  2131.       notice, this list of conditions and the following disclaimer.
  2132.  
  2133.     * Redistributions in binary form must reproduce the above
  2134.       copyright notice, this list of conditions and the following
  2135.       disclaimer in the documentation and/or other materials provided
  2136.       with the distribution.
  2137.  
  2138.     * Neither the name of Michael Foord nor Nicola Larosa
  2139.       may be used to endorse or promote products derived from this
  2140.       software without specific prior written permission.
  2141.  
  2142. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  2143. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  2144. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  2145. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  2146. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  2147. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  2148. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  2149. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  2150. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2151. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  2152. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2153. </pre>
  2154. <p>You should also be able to find a copy of this license at : <a class="reference external" href="http://www.voidspace.org.uk/python/license.shtml">BSD License</a></p>
  2155. </div>
  2156. <div class="section" id="todo">
  2157. <h1><a class="toc-backref" href="#id89">18   TODO</a></h1>
  2158. <p>Better support for configuration from multiple files, including tracking
  2159. <em>where</em> the original file came from and writing changes to the correct
  2160. file.</p>
  2161. <p>Make <tt class="docutils literal">newline</tt> a keyword argument (as well as an attribute) ?</p>
  2162. <p><tt class="docutils literal">UTF16</tt> encoded files, when returned as a list of lines, will have the
  2163. BOM at the start of every line. Should this be removed from all but the
  2164. first line ?</p>
  2165. <p>Option to set warning type for unicode decode ? (Defaults to strict).</p>
  2166. <p>A method to optionally remove uniform indentation from multiline values.
  2167. (do as an example of using <tt class="docutils literal">walk</tt> - along with string-escape)</p>
  2168. <p>Should the results dictionary from validate be an ordered dictionary if
  2169. <a class="reference external" href="http://www.voidspace.org.uk/python/odict.html">odict</a> is available ?</p>
  2170. <p>Implement some of the sequence methods (which include slicing) from the
  2171. newer <tt class="docutils literal">odict</tt> ?</p>
  2172. <p>Preserve line numbers of values (and possibly the original text of each value).</p>
  2173. </div>
  2174. <div class="section" id="issues">
  2175. <h1><a class="toc-backref" href="#id90">19   ISSUES</a></h1>
  2176. <div class="note">
  2177. <p class="first admonition-title">Note</p>
  2178. <p class="last">Please file any bug reports to <a class="reference external" href="http://www.voidspace.org.uk/python/weblog/index.shtml">Michael Foord</a> or the <strong>ConfigObj</strong>
  2179. <a class="reference external" href="http://lists.sourceforge.net/lists/listinfo/configobj-develop">Mailing List</a>.</p>
  2180. </div>
  2181. <p>There is currently no way to specify the encoding of a configspec file.</p>
  2182. <p>As a consequence of the changes to configspec handling in version 4.6.0, when
  2183. you create a ConfigObj instance and provide a configspec, the configspec
  2184. attribute is only set on the ConfigObj instance - it isn't set on the sections until you validate. You also can't set the configspec attribute to be a dictionary. This wasn't documented but did work previously.</p>
  2185. <p>In order to fix the problem with hashes in configspecs I had to turn off the parsing of inline comments in configspecs. This will only affect you if you are using <tt class="docutils literal">copy=True</tt> when validating and expecting inline comments to be copied from the configspec into the ConfigObj instance (all other comments will be copied as usual).</p>
  2186. <p>If you <em>create</em> the configspec by passing in a ConfigObj instance (usual way is to pass in a filename or list of lines) then you should pass in <tt class="docutils literal">_inspec=True</tt> to the constructor to allow hashes in values. This is the magic that switches off inline comment parsing.</p>
  2187. <p>When using <tt class="docutils literal">copy</tt> mode for validation, it won't copy <tt class="docutils literal">DEFAULT</tt>
  2188. sections. This is so that you <em>can</em> use interpolation in configspec
  2189. files. This is probably true even if interpolation is off in the
  2190. configspec.</p>
  2191. <p>You can't have a keyword with the same name as a section (in the same
  2192. section). They are both dictionary keys - so they would overlap.</p>
  2193. <p>ConfigObj doesn't quote and unquote values if <tt class="docutils literal">list_values=False</tt>.
  2194. This means that leading or trailing whitespace in values will be lost when
  2195. writing. (Unless you manually quote).</p>
  2196. <p>Interpolation checks first the current section, then the 'DEFAULT' subsection
  2197. of the current section, before moving on to the current section's parent and
  2198. so on up the tree.</p>
  2199. <p>Does it matter that we don't support the ':' divider, which is supported
  2200. by <tt class="docutils literal">ConfigParser</tt> ?</p>
  2201. <p>String interpolation and validation don't play well together. When
  2202. validation changes type it sets the value. This will correctly fetch the
  2203. value using interpolation - but then overwrite the interpolation reference.
  2204. If the value is unchanged by validation (it's a string) - but other types
  2205. will be.</p>
  2206. </div>
  2207. <div class="section" id="changelog">
  2208. <h1><a class="toc-backref" href="#id91">20   CHANGELOG</a></h1>
  2209. <p>This is an abbreviated changelog showing the major releases up to version 4.
  2210. From version 4 it lists all releases and changes.</p>
  2211. <div class="section" id="version-4-7-2">
  2212. <h2><a class="toc-backref" href="#id92">20.1   2010/02/27 - Version 4.7.2</a></h2>
  2213. <ul class="simple">
  2214. <li>BUGFIX: Restore Python 2.3 compatibility</li>
  2215. <li>BUGFIX: Members that were lists were being returned as copies due to interpolation
  2216. introduced in 4.7. Lists are now only copies if interpolation changes a list
  2217. member.</li>
  2218. <li>BUGFIX: <tt class="docutils literal">pop</tt> now does interpolation in list values as well.</li>
  2219. <li>BUGFIX: where interpolation matches a section name rather than a value it is
  2220. ignored instead of raising an exception on fetching the item.</li>
  2221. <li>BUGFIX: values that use interpolation to reference members that don't exist can
  2222. now be repr'd.</li>
  2223. <li>BUGFIX: Fix to avoid writing '\r\r\n' on Windows when given a file opened in
  2224. text write mode ('w').</li>
  2225. </ul>
  2226. <p>See <a class="reference internal" href="#string-interpolation-and-list-values">String Interpolation and List Values</a> for information about the problem with lists and interpolation.</p>
  2227. </div>
  2228. <div class="section" id="version-4-7-1">
  2229. <h2><a class="toc-backref" href="#id93">20.2   2010/02/06 - Version 4.7.1</a></h2>
  2230. <ul class="simple">
  2231. <li>Fix bug in options deprecation warning added in 4.7.0</li>
  2232. </ul>
  2233. </div>
  2234. <div class="section" id="version-4-7-0">
  2235. <h2><a class="toc-backref" href="#id94">20.3   2010/01/09 - Version 4.7.0</a></h2>
  2236. <ul class="simple">
  2237. <li>Minimum supported version of Python is now 2.3</li>
  2238. <li>~25% performance improvement thanks to Christian Heimes</li>
  2239. <li>String interpolation now works in list value members</li>
  2240. <li>After validation any additional entries not in the configspec are listed in
  2241. the <tt class="docutils literal">extra_values</tt> section member</li>
  2242. <li>Addition of the <tt class="docutils literal">get_extra_values</tt> function for finding all extra values
  2243. in a validated ConfigObj instance</li>
  2244. <li>Deprecated the use of the <tt class="docutils literal">options</tt> dictionary in the ConfigObj constructor
  2245. and added explicit keyword arguments instead. Use **options if you want
  2246. to initialise a ConfigObj instance from a dictionary</li>
  2247. <li>Constructing a ConfigObj from an existing ConfigObj instance now preserves
  2248. the order of values and sections from the original instance in the new one</li>
  2249. <li>BUGFIX: Checks that failed validation would not populate <tt class="docutils literal">default_values</tt> and
  2250. <tt class="docutils literal">restore_default_value()</tt> wouldn't work for those entries</li>
  2251. <li>BUGFIX: clear() now clears 'defaults'</li>
  2252. <li>BUGFIX: empty values in list values were accidentally valid syntax. They now
  2253. raise a <tt class="docutils literal">ParseError</tt>. e.g. "value = 1, , 2"</li>
  2254. <li>BUGFIX: Change to the result of a call to <tt class="docutils literal">validate</tt> when <tt class="docutils literal">preserve_errors</tt>
  2255. is True. Previously sections where <em>all</em> values failed validation would
  2256. return False for the section rather than preserving the errors. False will
  2257. now only be returned for a section if it is missing</li>
  2258. <li>Distribution includes version 1.0.1 of validate.py</li>
  2259. <li>Removed __revision__ and __docformat__</li>
  2260. </ul>
  2261. </div>
  2262. <div class="section" id="version-4-6-0">
  2263. <h2><a class="toc-backref" href="#id95">20.4   2009/04/13 - Version 4.6.0</a></h2>
  2264. <ul class="simple">
  2265. <li>Pickling of ConfigObj instances now supported (thanks to Christian Heimes)</li>
  2266. <li>Hashes in confgspecs are now allowed (see note below)</li>
  2267. <li>Replaced use of hasattr (which can swallow exceptions) with getattr</li>
  2268. <li>__many__ in configspecs can refer to scalars (ordinary values) as well as sections</li>
  2269. <li>You can use ___many___ (three underscores!) where you want to use __many__ as well</li>
  2270. <li>You can now have normal sections inside configspec sections that use __many__</li>
  2271. <li>You can now create an empty ConfigObj with a configspec, programmatically set values and then validate</li>
  2272. <li>A section that was supplied as a value (or vice-versa) in the actual config file would cause an exception during validation (the config file is still broken of course, but it is now handled gracefully)</li>
  2273. <li>Added <tt class="docutils literal">as_list</tt> method</li>
  2274. <li>Removed the deprecated <tt class="docutils literal">istrue</tt>, <tt class="docutils literal">encode</tt> and <tt class="docutils literal">decode</tt> methods</li>
  2275. <li>Running test_configobj.py now also runs the doctests in the configobj module</li>
  2276. </ul>
  2277. <p>As a consequence of the changes to configspec handling, when you create a ConfigObj instance and provide
  2278. a configspec, the configspec attribute is only set on the ConfigObj instance - it isn't set on the
  2279. sections until you validate. You also can't set the configspec attribute to be a dictionary. This wasn't
  2280. documented but did work previously.</p>
  2281. <p>In order to fix the problem with hashes in configspecs I had to turn off the parsing of inline comments
  2282. in configspecs. This will only affect you if you are using <tt class="docutils literal">copy=True</tt> when validating and expecting
  2283. inline comments to be copied from the configspec into the ConfigObj instance (all other comments will be
  2284. copied as usual).</p>
  2285. <p>If you <em>create</em> the configspec by passing in a ConfigObj instance (usual way is to pass in a filename or
  2286. list of lines) then you should pass in <tt class="docutils literal">_inspec=True</tt> to the constructor to allow hashes in values.
  2287. This is the magic that switches off inline comment parsing.</p>
  2288. </div>
  2289. <div class="section" id="version-4-5-3">
  2290. <h2><a class="toc-backref" href="#id96">20.5   2008/06/27 - Version 4.5.3</a></h2>
  2291. <p>BUGFIX: fixed a problem with <tt class="docutils literal">copy=True</tt> when validating with configspecs that use
  2292. <tt class="docutils literal">__many__</tt> sections.</p>
  2293. </div>
  2294. <div class="section" id="version-4-5-2">
  2295. <h2><a class="toc-backref" href="#id97">20.6   2008/02/05 - Version 4.5.2</a></h2>
  2296. <p>Distribution updated to include version 0.3.2 of <a class="reference internal" href="#validate">validate</a>. This means that
  2297. <tt class="docutils literal">None</tt> as a default value in configspecs works.</p>
  2298. </div>
  2299. <div class="section" id="version-4-5-1">
  2300. <h2><a class="toc-backref" href="#id98">20.7   2008/02/05 - Version 4.5.1</a></h2>
  2301. <p>Distribution updated to include version 0.3.1 of <a class="reference internal" href="#validate">validate</a>. This means that
  2302. Unicode configspecs now work.</p>
  2303. </div>
  2304. <div class="section" id="version-4-5-0">
  2305. <h2><a class="toc-backref" href="#id99">20.8   2008/02/05 - Version 4.5.0</a></h2>
  2306. <p>ConfigObj will now guarantee that files will be written terminated with a
  2307. newline.</p>
  2308. <p>ConfigObj will no longer attempt to import the <tt class="docutils literal">validate</tt> module, until/unless
  2309. you call <tt class="docutils literal">ConfigObj.validate</tt> with <tt class="docutils literal">preserve_errors=True</tt>. This makes it
  2310. faster to import.</p>
  2311. <p>New methods <tt class="docutils literal">restore_default</tt> and <tt class="docutils literal">restore_defaults</tt>. <tt class="docutils literal">restore_default</tt>
  2312. resets an entry to its default value (and returns that value). <tt class="docutils literal">restore_defaults</tt>
  2313. resets all entries to their default value. It doesn't modify entries without a
  2314. default value. You must have validated a ConfigObj (which populates the
  2315. <tt class="docutils literal">default_values</tt> dictionary) before calling these methods.</p>
  2316. <p>BUGFIX: Proper quoting of keys, values and list values that contain hashes
  2317. (when writing).  When <tt class="docutils literal">list_values=False</tt>, values containing hashes are
  2318. triple quoted.</p>
  2319. <p>Added the <tt class="docutils literal">reload</tt> method. This reloads a ConfigObj from file. If the filename
  2320. attribute is not set then a <tt class="docutils literal">ReloadError</tt> (a new exception inheriting from
  2321. <tt class="docutils literal">IOError</tt>) is raised.</p>
  2322. <p>BUGFIX: Files are read in with 'rb' mode, so that native/non-native line endings work!</p>
  2323. <p>Minor efficiency improvement in <tt class="docutils literal">unrepr</tt> mode.</p>
  2324. <p>Added missing docstrings for some overidden dictionary methods.</p>
  2325. <p>Added the <tt class="docutils literal">reset</tt> method. This restores a ConfigObj to a freshly created state.</p>
  2326. <p>Removed old CHANGELOG file.</p>
  2327. </div>
  2328. <div class="section" id="version-4-4-0">
  2329. <h2><a class="toc-backref" href="#id100">20.9   2007/02/04 - Version 4.4.0</a></h2>
  2330. <p>Official release of 4.4.0</p>
  2331. </div>
  2332. <div class="section" id="version-4-3-3-alpha4">
  2333. <h2><a class="toc-backref" href="#id101">20.10   2006/12/17 - Version 4.3.3-alpha4</a></h2>
  2334. <p>By Nicola Larosa</p>
  2335. <p>Allowed arbitrary indentation in the <tt class="docutils literal">indent_type</tt> parameter, removed the
  2336. <tt class="docutils literal">NUM_INDENT_SPACES</tt> and <tt class="docutils literal">MAX_INTERPOL_DEPTH</tt> (a leftover) constants,
  2337. added indentation tests (including another docutils workaround, sigh), updated
  2338. the documentation.</p>
  2339. <p>By Michael Foord</p>
  2340. <p>Made the import of <tt class="docutils literal">compiler</tt> conditional so that <tt class="docutils literal">ConfigObj</tt> can be used
  2341. with <a class="reference external" href="http://www.codeplex.com/IronPython">IronPython</a>.</p>
  2342. </div>
  2343. <div class="section" id="version-4-3-3-alpha3">
  2344. <h2><a class="toc-backref" href="#id102">20.11   2006/12/17 - Version 4.3.3-alpha3</a></h2>
  2345. <p>By Nicola Larosa</p>
  2346. <p>Added a missing <tt class="docutils literal">self.</tt> in the _handle_comment method and a related test,
  2347. per Sourceforge bug #1523975.</p>
  2348. </div>
  2349. <div class="section" id="version-4-3-3-alpha2">
  2350. <h2><a class="toc-backref" href="#id103">20.12   2006/12/09 - Version 4.3.3-alpha2</a></h2>
  2351. <p>By Nicola Larosa</p>
  2352. <p>Changed interpolation search strategy, based on this patch by Robin Munn:
  2353. <a class="reference external" href="http://sourceforge.net/mailarchive/message.php?msg_id=17125993">http://sourceforge.net/mailarchive/message.php?msg_id=17125993</a></p>
  2354. </div>
  2355. <div class="section" id="version-4-3-3-alpha1">
  2356. <h2><a class="toc-backref" href="#id104">20.13   2006/12/09 - Version 4.3.3-alpha1</a></h2>
  2357. <p>By Nicola Larosa</p>
  2358. <p>Added Template-style interpolation, with tests, based on this patch by
  2359. Robin Munn: <a class="reference external" href="http://sourceforge.net/mailarchive/message.php?msg_id=17125991">http://sourceforge.net/mailarchive/message.php?msg_id=17125991</a>
  2360. (awful archives, bad Sourceforge, bad).</p>
  2361. </div>
  2362. <div class="section" id="version-4-3-2">
  2363. <h2><a class="toc-backref" href="#id105">20.14   2006/06/04 - Version 4.3.2</a></h2>
  2364. <p>Changed error handling, if parsing finds a single error then that error will
  2365. be re-raised. That error will still have an <tt class="docutils literal">errors</tt> and a <tt class="docutils literal">config</tt>
  2366. attribute.</p>
  2367. <p>Fixed bug where '\n' terminated files could be truncated.</p>
  2368. <p>Bugfix in <tt class="docutils literal">unrepr</tt> mode, it couldn't handle '#' in values. (Thanks to
  2369. Philippe Normand for the report.)</p>
  2370. <p>As a consequence of this fix, ConfigObj doesn't now keep inline comments in
  2371. <tt class="docutils literal">unrepr</tt> mode. This is because the parser in the <a class="reference external" href="http://docs.python.org/lib/compiler.html">compiler package</a>
  2372. doesn't keep comments.</p>
  2373. <p>Error messages are now more useful. They tell you the number of parsing errors
  2374. and the line number of the first error. (In the case of multiple errors.)</p>
  2375. <p>Line numbers in exceptions now start at 1, not 0.</p>
  2376. <p>Errors in <tt class="docutils literal">unrepr</tt> mode are now handled the same way as in the normal mode.
  2377. The errors stored will be an <tt class="docutils literal">UnreprError</tt>.</p>
  2378. </div>
  2379. <div class="section" id="version-4-3-1">
  2380. <h2><a class="toc-backref" href="#id106">20.15   2006/04/29 - Version 4.3.1</a></h2>
  2381. <p>Added <tt class="docutils literal">validate.py</tt> back into <tt class="docutils literal">configobj.zip</tt>. (Thanks to Stewart
  2382. Midwinter)</p>
  2383. <p>Updated to <a class="reference external" href="http://www.voidspace.org.uk/downloads/validate.py">validate.py</a> 0.2.2.</p>
  2384. <p>Preserve tuples when calling the <tt class="docutils literal">dict</tt> method. (Thanks to Gustavo Niemeyer.)</p>
  2385. <p>Changed <tt class="docutils literal">__repr__</tt> to return a string that contains <tt class="docutils literal">ConfigObj({ ... })</tt>.</p>
  2386. <p>Change so that an options dictionary isn't modified by passing it to ConfigObj.
  2387. (Thanks to Artarious.)</p>
  2388. <p>Added ability to handle negative integers in <tt class="docutils literal">unrepr</tt>. (Thanks to Kevin
  2389. Dangoor.)</p>
  2390. </div>
  2391. <div class="section" id="version-4-3-0">
  2392. <h2><a class="toc-backref" href="#id107">20.16   2006/03/24 - Version 4.3.0</a></h2>
  2393. <p>Moved the tests and the CHANGELOG (etc) into a separate file. This has reduced
  2394. the size of <tt class="docutils literal">configobj.py</tt> by about 40%.</p>
  2395. <p>Added the <tt class="docutils literal">unrepr</tt> mode to reading and writing config files. Thanks to Kevin
  2396. Dangoor for this suggestion.</p>
  2397. <p>Empty values are now valid syntax. They are read as an empty string <tt class="docutils literal">''</tt>.
  2398. (<tt class="docutils literal">key =</tt>, or <tt class="docutils literal">key = # comment</tt>.)</p>
  2399. <p><tt class="docutils literal">validate</tt> now honours the order of the configspec.</p>
  2400. <p>Added the <tt class="docutils literal">copy</tt> mode to validate. Thanks to Louis Cordier for this
  2401. suggestion.</p>
  2402. <p>Fixed bug where files written on windows could be given <tt class="docutils literal">'\r\r\n'</tt> line
  2403. terminators.</p>
  2404. <p>Fixed bug where last occurring comment line could be interpreted as the
  2405. final comment if the last line isn't terminated.</p>
  2406. <p>Fixed bug where nested list values would be flattened when <tt class="docutils literal">write</tt> is
  2407. called. Now sub-lists have a string representation written instead.</p>
  2408. <p>Deprecated <tt class="docutils literal">encode</tt> and <tt class="docutils literal">decode</tt> methods instead.</p>
  2409. <p>You can now pass in a ConfigObj instance as a configspec (remember to read
  2410. the configspec file using <tt class="docutils literal">list_values=False</tt>).</p>
  2411. <p>Sorted footnotes in the docs.</p>
  2412. </div>
  2413. <div class="section" id="version-4-2-0">
  2414. <h2><a class="toc-backref" href="#id108">20.17   2006/02/16 - Version 4.2.0</a></h2>
  2415. <p>Removed <tt class="docutils literal">BOM_UTF8</tt> from <tt class="docutils literal">__all__</tt>.</p>
  2416. <p>The <tt class="docutils literal">BOM</tt> attribute has become a boolean. (Defaults to <tt class="docutils literal">False</tt>.) It is
  2417. <em>only</em> <tt class="docutils literal">True</tt> for the <tt class="docutils literal">UTF16/UTF8</tt> encodings.</p>
  2418. <p>File like objects no longer need a <tt class="docutils literal">seek</tt> attribute.</p>
  2419. <p>Full unicode support added. New options/attributes <tt class="docutils literal">encoding</tt>,
  2420. <tt class="docutils literal">default_encoding</tt>.</p>
  2421. <p>ConfigObj no longer keeps a reference to file like objects. Instead the
  2422. <tt class="docutils literal">write</tt> method takes a file like object as an optional argument. (Which
  2423. will be used in preference of the <tt class="docutils literal">filename</tt> attribute if that exists as
  2424. well.)</p>
  2425. <p>utf16 files decoded to unicode.</p>
  2426. <p>If <tt class="docutils literal">BOM</tt> is <tt class="docutils literal">True</tt>, but no encoding specified, then the utf8 BOM is
  2427. written out at the start of the file. (It will normally only be <tt class="docutils literal">True</tt> if
  2428. the utf8 BOM was found when the file was read.)</p>
  2429. <p>Thanks to Aaron Bentley for help and testing on the unicode issues.</p>
  2430. <p>File paths are <em>not</em> converted to absolute paths, relative paths will
  2431. remain relative as the <tt class="docutils literal">filename</tt> attribute.</p>
  2432. <p>Fixed bug where <tt class="docutils literal">final_comment</tt> wasn't returned if <tt class="docutils literal">write</tt> is returning
  2433. a list of lines.</p>
  2434. <p>Deprecated <tt class="docutils literal">istrue</tt>, replaced it with <tt class="docutils literal">as_bool</tt>.</p>
  2435. <p>Added <tt class="docutils literal">as_int</tt> and <tt class="docutils literal">as_float</tt>.</p>
  2436. </div>
  2437. <div class="section" id="version-4-1-0">
  2438. <h2><a class="toc-backref" href="#id109">20.18   2005/12/14 - Version 4.1.0</a></h2>
  2439. <p>Added <tt class="docutils literal">merge</tt>, a recursive update.</p>
  2440. <p>Added <tt class="docutils literal">preserve_errors</tt> to <tt class="docutils literal">validate</tt> and the <tt class="docutils literal">flatten_errors</tt>
  2441. example function.</p>
  2442. <p>Thanks to Matthew Brett for suggestions and helping me iron out bugs.</p>
  2443. <p>Fixed bug where a config file is <em>all</em> comment, the comment will now be
  2444. <tt class="docutils literal">initial_comment</tt> rather than <tt class="docutils literal">final_comment</tt>.</p>
  2445. <p>Validation no longer done on the 'DEFAULT' section (only in the root level).
  2446. This allows interpolation in configspecs.</p>
  2447. <p>Also use the new list syntax in <a class="reference internal" href="#validate">validate</a> 0.2.1. (For configspecs).</p>
  2448. </div>
  2449. <div class="section" id="version-4-0-2">
  2450. <h2><a class="toc-backref" href="#id110">20.19   2005/12/02 - Version 4.0.2</a></h2>
  2451. <p>Fixed bug in <tt class="docutils literal">create_empty</tt>. Thanks to Paul Jimenez for the report.</p>
  2452. </div>
  2453. <div class="section" id="version-4-0-1">
  2454. <h2><a class="toc-backref" href="#id111">20.20   2005/11/05 - Version 4.0.1</a></h2>
  2455. <p>Fixed bug in <tt class="docutils literal">Section.walk</tt> when transforming names as well as values.</p>
  2456. <p>Added the <tt class="docutils literal">istrue</tt> method. (Fetches the boolean equivalent of a string
  2457. value).</p>
  2458. <p>Fixed <tt class="docutils literal">list_values=False</tt> - they are now only quoted/unquoted if they
  2459. are multiline values.</p>
  2460. <p>List values are written as <tt class="docutils literal">item, item</tt> rather than <tt class="docutils literal">item,item</tt>.</p>
  2461. </div>
  2462. <div class="section" id="version-4-0-0">
  2463. <h2><a class="toc-backref" href="#id112">20.21   2005/10/17 - Version 4.0.0</a></h2>
  2464. <p><strong>ConfigObj 4.0.0 Final</strong></p>
  2465. <p>Fixed bug in <tt class="docutils literal">setdefault</tt>. When creating a new section with setdefault the
  2466. reference returned would be to the dictionary passed in <em>not</em> to the new
  2467. section. Bug fixed and behaviour documented.</p>
  2468. <p>Obscure typo/bug fixed in <tt class="docutils literal">write</tt>. Wouldn't have affected anyone though.</p>
  2469. </div>
  2470. <div class="section" id="version-4-0-0-beta-5">
  2471. <h2><a class="toc-backref" href="#id113">20.22   2005/09/09 - Version 4.0.0 beta 5</a></h2>
  2472. <p>Removed <tt class="docutils literal">PositionError</tt>.</p>
  2473. <p>Allowed quotes around keys as documented.</p>
  2474. <p>Fixed bug with commas in comments. (matched as a list value)</p>
  2475. </div>
  2476. <div class="section" id="version-4-0-0-beta-4">
  2477. <h2><a class="toc-backref" href="#id114">20.23   2005/09/07 - Version 4.0.0 beta 4</a></h2>
  2478. <p>Fixed bug in <tt class="docutils literal">__delitem__</tt>. Deleting an item no longer deletes the
  2479. <tt class="docutils literal">inline_comments</tt> attribute.</p>
  2480. <p>Fixed bug in initialising ConfigObj from a ConfigObj.</p>
  2481. <p>Changed the mailing list address.</p>
  2482. </div>
  2483. <div class="section" id="version-4-0-0-beta-3">
  2484. <h2><a class="toc-backref" href="#id115">20.24   2005/08/28 - Version 4.0.0 beta 3</a></h2>
  2485. <p>Interpolation is switched off before writing out files.</p>
  2486. <p>Fixed bug in handling <tt class="docutils literal">StringIO</tt> instances. (Thanks to report from
  2487. Gustavo Niemeyer.)</p>
  2488. <p>Moved the doctests from the <tt class="docutils literal">__init__</tt> method to a separate function.
  2489. (For the sake of IDE calltips).</p>
  2490. </div>
  2491. <div class="section" id="version-4-0-0-beta-2">
  2492. <h2><a class="toc-backref" href="#id116">20.25   2005/08/25 - Version 4.0.0 beta 2</a></h2>
  2493. <p>Amendments to <em>validate.py</em>.</p>
  2494. <p>First public release.</p>
  2495. </div>
  2496. <div class="section" id="version-4-0-0-beta-1">
  2497. <h2><a class="toc-backref" href="#id117">20.26   2005/08/21 - Version 4.0.0 beta 1</a></h2>
  2498. <p>Reads nested subsections to any depth.</p>
  2499. <p>Multiline values.</p>
  2500. <p>Simplified options and methods.</p>
  2501. <p>New list syntax.</p>
  2502. <p>Faster, smaller, and better parser.</p>
  2503. <p>Validation greatly improved. Includes:</p>
  2504. <blockquote>
  2505. <ul class="simple">
  2506. <li>type conversion</li>
  2507. <li>default values</li>
  2508. <li>repeated sections</li>
  2509. </ul>
  2510. </blockquote>
  2511. <p>Improved error handling.</p>
  2512. <p>Plus lots of other improvements.</p>
  2513. </div>
  2514. <div class="section" id="version-3-0-0">
  2515. <h2><a class="toc-backref" href="#id118">20.27   2004/05/24 - Version 3.0.0</a></h2>
  2516. <p>Several incompatible changes: another major overhaul and change. (Lots of
  2517. improvements though).</p>
  2518. <p>Added support for standard config files with sections. This has an entirely
  2519. new interface: each section is a dictionary of values.</p>
  2520. <p>Changed the update method to be called writein: update clashes with a dict
  2521. method.</p>
  2522. <p>Made various attributes keyword arguments, added several.</p>
  2523. <p>Configspecs and orderlists have changed a great deal.</p>
  2524. <p>Removed support for adding dictionaries: use update instead.</p>
  2525. <p>Now subclasses a new class called caselessDict. This should add various
  2526. dictionary methods that could have caused errors before.</p>
  2527. <p>It also preserves the original casing of keywords when writing them back out.</p>
  2528. <p>Comments are also saved using a <tt class="docutils literal">caselessDict</tt>.</p>
  2529. <p>Using a non-string key will now raise a <tt class="docutils literal">TypeError</tt> rather than converting
  2530. the key.</p>
  2531. <p>Added an exceptions keyword for <em>much</em> better handling of errors.</p>
  2532. <p>Made <tt class="docutils literal">creatempty=False</tt> the default.</p>
  2533. <p>Now checks indict <em>and</em> any keyword args. Keyword args take precedence over
  2534. indict.</p>
  2535. <p><tt class="docutils literal">' ', <span class="pre">':',</span> <span class="pre">'=',</span> ','</tt> and <tt class="docutils literal">'\t'</tt> are now all valid dividers where the
  2536. keyword is unquoted.</p>
  2537. <p>ConfigObj now does no type checking against configspec when you set items.</p>
  2538. <p>delete and add methods removed (they were unnecessary).</p>
  2539. <p>Docs rewritten to include all this gumph and more; actually ConfigObj is
  2540. <em>really</em> easy to use.</p>
  2541. <p>Support for stdout was removed.</p>
  2542. <p>A few new methods added.</p>
  2543. <p>Charmap is now incorporated into ConfigObj.</p>
  2544. </div>
  2545. <div class="section" id="version-2-0-0-beta">
  2546. <h2><a class="toc-backref" href="#id119">20.28   2004/03/14 - Version 2.0.0 beta</a></h2>
  2547. <p>Re-written it to subclass dict. My first forays into inheritance and operator
  2548. overloading.</p>
  2549. <p>The config object now behaves like a dictionary.</p>
  2550. <p>I've completely broken the interface, but I don't think anyone was really
  2551. using it anyway.</p>
  2552. <p>This new version is much more 'classy'.</p>
  2553. <p>It will also read straight from/to a filename and completely parse a config
  2554. file without you <em>having</em> to supply a config spec.</p>
  2555. <p>Uses listparse, so can handle nested list items as values.</p>
  2556. <p>No longer has getval and setval methods: use normal dictionary methods, or add
  2557. and delete.</p>
  2558. </div>
  2559. <div class="section" id="version-1-0-5">
  2560. <h2><a class="toc-backref" href="#id120">20.29   2004/01/29 - Version 1.0.5</a></h2>
  2561. <p>Version 1.0.5 has a couple of bugfixes as well as a couple of useful additions
  2562. over previous versions.</p>
  2563. <p>Since 1.0.0 the buildconfig function has been moved into this distribution,
  2564. and the methods reset, verify, getval and setval have been added.</p>
  2565. <p>A couple of bugs have been fixed.</p>
  2566. </div>
  2567. <div class="section" id="origins">
  2568. <h2><a class="toc-backref" href="#id121">20.30   Origins</a></h2>
  2569. <p>ConfigObj originated in a set of functions for reading config files in the
  2570. <a class="reference external" href="http://www.voidspace.org.uk/atlantibots/">atlantibots</a> project. The original
  2571. functions were written by Rob McNeur.</p>
  2572. </div>
  2573. </div>
  2574. <hr class="docutils" />
  2575. <div class="section" id="footnotes">
  2576. <h1><a class="toc-backref" href="#id122">21   Footnotes</a></h1>
  2577. <table class="docutils footnote" frame="void" id="id15" rules="none">
  2578. <colgroup><col class="label" /><col /></colgroup>
  2579. <tbody valign="top">
  2580. <tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>And if you discover any bugs, let us know. We'll fix them quickly.</td></tr>
  2581. </tbody>
  2582. </table>
  2583. <table class="docutils footnote" frame="void" id="id16" rules="none">
  2584. <colgroup><col class="label" /><col /></colgroup>
  2585. <tbody valign="top">
  2586. <tr><td class="label"><a class="fn-backref" href="#id2">[2]</a></td><td>If you specify a filename that doesn't exist, ConfigObj will assume you
  2587. are creating a new one. See the <em>create_empty</em> and <em>file_error</em> options.</td></tr>
  2588. </tbody>
  2589. </table>
  2590. <table class="docutils footnote" frame="void" id="id17" rules="none">
  2591. <colgroup><col class="label" /><col /></colgroup>
  2592. <tbody valign="top">
  2593. <tr><td class="label"><a class="fn-backref" href="#id3">[3]</a></td><td>They can be byte strings (<em>ordinary</em> strings) or Unicode.</td></tr>
  2594. </tbody>
  2595. </table>
  2596. <table class="docutils footnote" frame="void" id="id18" rules="none">
  2597. <colgroup><col class="label" /><col /></colgroup>
  2598. <tbody valign="top">
  2599. <tr><td class="label"><a class="fn-backref" href="#id4">[4]</a></td><td>Except we don't support the RFC822 style line continuations, nor ':' as
  2600. a divider.</td></tr>
  2601. </tbody>
  2602. </table>
  2603. <table class="docutils footnote" frame="void" id="id19" rules="none">
  2604. <colgroup><col class="label" /><col /></colgroup>
  2605. <tbody valign="top">
  2606. <tr><td class="label"><a class="fn-backref" href="#id5">[5]</a></td><td>This is a change in ConfigObj 4.2.0. Note that ConfigObj doesn't call
  2607. the seek method of any file like object you pass in. You may want to call
  2608. <tt class="docutils literal">file_object.seek(0)</tt> yourself, first.</td></tr>
  2609. </tbody>
  2610. </table>
  2611. <table class="docutils footnote" frame="void" id="id20" rules="none">
  2612. <colgroup><col class="label" /><col /></colgroup>
  2613. <tbody valign="top">
  2614. <tr><td class="label"><a class="fn-backref" href="#id6">[6]</a></td><td><p class="first">A side effect of this is that it enables you to copy a ConfigObj :</p>
  2615. <div class="highlight"><pre><span class="c"># only copies members</span>
  2616. <span class="c"># not attributes/comments</span>
  2617. <span class="n">config2</span> <span class="o">=</span> <span class="n">ConfigObj</span><span class="p">(</span><span class="n">config1</span><span class="p">)</span>
  2618. </pre></div>
  2619. <p class="last">Since ConfigObj 4.7.0 the order of members and sections will be
  2620. preserved when copying a ConfigObj instance.</p>
  2621. </td></tr>
  2622. </tbody>
  2623. </table>
  2624. <table class="docutils footnote" frame="void" id="id21" rules="none">
  2625. <colgroup><col class="label" /><col /></colgroup>
  2626. <tbody valign="top">
  2627. <tr><td class="label"><a class="fn-backref" href="#id7">[7]</a></td><td>Other than lists of strings.</td></tr>
  2628. </tbody>
  2629. </table>
  2630. <table class="docutils footnote" frame="void" id="id22" rules="none">
  2631. <colgroup><col class="label" /><col /></colgroup>
  2632. <tbody valign="top">
  2633. <tr><td class="label"><a class="fn-backref" href="#id8">[8]</a></td><td>The exception is if it detects a <tt class="docutils literal">UTF16</tt> encoded file which it
  2634. must decode before parsing.</td></tr>
  2635. </tbody>
  2636. </table>
  2637. <table class="docutils footnote" frame="void" id="id23" rules="none">
  2638. <colgroup><col class="label" /><col /></colgroup>
  2639. <tbody valign="top">
  2640. <tr><td class="label"><a class="fn-backref" href="#id9">[9]</a></td><td>The method signature shows that this method takes
  2641. two arguments. The second is the section to be written. This is because the
  2642. <tt class="docutils literal">write</tt> method is called recursively.</td></tr>
  2643. </tbody>
  2644. </table>
  2645. <table class="docutils footnote" frame="void" id="id24" rules="none">
  2646. <colgroup><col class="label" /><col /></colgroup>
  2647. <tbody valign="top">
  2648. <tr><td class="label"><a class="fn-backref" href="#id10">[10]</a></td><td>The dict method doesn't actually use the deepcopy mechanism. This means
  2649. if you add nested lists (etc) to your ConfigObj, then the dictionary
  2650. returned by dict may contain some references. For all <em>normal</em> ConfigObjs
  2651. it will return a deepcopy.</td></tr>
  2652. </tbody>
  2653. </table>
  2654. <table class="docutils footnote" frame="void" id="id25" rules="none">
  2655. <colgroup><col class="label" /><col /></colgroup>
  2656. <tbody valign="top">
  2657. <tr><td class="label"><a class="fn-backref" href="#id11">[11]</a></td><td>Passing <tt class="docutils literal">(section, key)</tt> rather than <tt class="docutils literal">(value, key)</tt> allows you to
  2658. change the value by setting <tt class="docutils literal">section[key] = newval</tt>. It also gives you
  2659. access to the <em>rename</em> method of the section.</td></tr>
  2660. </tbody>
  2661. </table>
  2662. <table class="docutils footnote" frame="void" id="id26" rules="none">
  2663. <colgroup><col class="label" /><col /></colgroup>
  2664. <tbody valign="top">
  2665. <tr><td class="label"><a class="fn-backref" href="#id12">[12]</a></td><td>Minimum required version of <em>validate.py</em> 0.2.0 .</td></tr>
  2666. </tbody>
  2667. </table>
  2668. </div>
  2669. </div>
  2670. </body>
  2671. </html>
  2672.