configobj.html 191 KB

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