Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="module-fileinput.html" /> | |
13 | <link rel="prev" href="module-itertools.html" /> | |
14 | <link rel="parent" href="misc.html" /> | |
15 | <link rel="next" href="RawConfigParser-objects.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>5.17 ConfigParser -- Configuration file parser</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="5.16.3 Recipes" | |
25 | href="itertools-recipes.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services" | |
28 | href="misc.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="5.17.1 RawConfigParser Objects" | |
31 | href="RawConfigParser-objects.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="itertools-recipes.html">5.16.3 Recipes</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="RawConfigParser-objects.html">5.17.1 RawConfigParser Objects</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION0071700000000000000000"> | |
56 | 5.17 <tt class="module">ConfigParser</tt> -- | |
57 | Configuration file parser</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-ConfigParser"></A> | |
62 | ||
63 | <P> | |
64 | This module defines the class <tt class="class">ConfigParser</tt>. | |
65 | <a id='l2h-1398' xml:id='l2h-1398'></a><a id='l2h-1399' xml:id='l2h-1399'></a><a id='l2h-1413' xml:id='l2h-1413'></a> | |
66 | The <tt class="class">ConfigParser</tt> class implements a basic configuration file | |
67 | parser language which provides a structure similar to what you would | |
68 | find on Microsoft Windows INI files. You can use this to write Python | |
69 | programs which can be customized by end users easily. | |
70 | ||
71 | <P> | |
72 | <div class="warning"><b class="label">Warning:</b> | |
73 | ||
74 | This library does <em>not</em> interpret or write the value-type | |
75 | prefixes used in the Windows Registry extended version of INI syntax. | |
76 | </div> | |
77 | ||
78 | <P> | |
79 | The configuration file consists of sections, led by a | |
80 | "<tt class="samp">[section]</tt>" header and followed by "<tt class="samp">name: value</tt>" entries, | |
81 | with continuations in the style of <a class="rfc" id='rfcref-87570' xml:id='rfcref-87570' | |
82 | href="http://www.faqs.org/rfcs/rfc822.html">RFC 822</a>; "<tt class="samp">name=value</tt>" is | |
83 | also accepted. Note that leading whitespace is removed from values. | |
84 | The optional values can contain format strings which refer to other | |
85 | values in the same section, or values in a special | |
86 | <code>DEFAULT</code> section. Additional defaults can be provided on | |
87 | initialization and retrieval. Lines beginning with "<tt class="character">#</tt>" or | |
88 | "<tt class="character">;</tt>" are ignored and may be used to provide comments. | |
89 | ||
90 | <P> | |
91 | For example: | |
92 | ||
93 | <P> | |
94 | <div class="verbatim"><pre> | |
95 | [My Section] | |
96 | foodir: %(dir)s/whatever | |
97 | dir=frob | |
98 | </pre></div> | |
99 | ||
100 | <P> | |
101 | would resolve the "<tt class="samp">%(dir)s</tt>" to the value of | |
102 | "<tt class="samp">dir</tt>" ("<tt class="samp">frob</tt>" in this case). All reference expansions are | |
103 | done on demand. | |
104 | ||
105 | <P> | |
106 | Default values can be specified by passing them into the | |
107 | <tt class="class">ConfigParser</tt> constructor as a dictionary. Additional defaults | |
108 | may be passed into the <tt class="method">get()</tt> method which will override all | |
109 | others. | |
110 | ||
111 | <P> | |
112 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
113 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-1400' xml:id='l2h-1400' class="class">RawConfigParser</tt></b>(</nobr></td> | |
114 | <td><var></var><big>[</big><var>defaults</var><big>]</big><var></var>)</td></tr></table></dt> | |
115 | <dd> | |
116 | The basic configuration object. When <var>defaults</var> is given, it is | |
117 | initialized into the dictionary of intrinsic defaults. This class | |
118 | does not support the magical interpolation behavior. | |
119 | ||
120 | <span class="versionnote">New in version 2.3.</span> | |
121 | ||
122 | </dl> | |
123 | ||
124 | <P> | |
125 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
126 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-1401' xml:id='l2h-1401' class="class">ConfigParser</tt></b>(</nobr></td> | |
127 | <td><var></var><big>[</big><var>defaults</var><big>]</big><var></var>)</td></tr></table></dt> | |
128 | <dd> | |
129 | Derived class of <tt class="class">RawConfigParser</tt> that implements the magical | |
130 | interpolation feature and adds optional arguments to the <tt class="method">get()</tt> | |
131 | and <tt class="method">items()</tt> methods. The values in <var>defaults</var> must be | |
132 | appropriate for the "<tt class="samp">%()s</tt>" string interpolation. Note that | |
133 | <var>__name__</var> is an intrinsic default; its value is the section name, | |
134 | and will override any value provided in <var>defaults</var>. | |
135 | ||
136 | <P> | |
137 | All option names used in interpolation will be passed through the | |
138 | <tt class="method">optionxform()</tt> method just like any other option name | |
139 | reference. For example, using the default implementation of | |
140 | <tt class="method">optionxform()</tt> (which converts option names to lower case), | |
141 | the values "<tt class="samp">foo %(bar)s</tt>" and "<tt class="samp">foo %(BAR)s</tt>" are | |
142 | equivalent. | |
143 | </dl> | |
144 | ||
145 | <P> | |
146 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
147 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-1402' xml:id='l2h-1402' class="class">SafeConfigParser</tt></b>(</nobr></td> | |
148 | <td><var></var><big>[</big><var>defaults</var><big>]</big><var></var>)</td></tr></table></dt> | |
149 | <dd> | |
150 | Derived class of <tt class="class">ConfigParser</tt> that implements a more-sane | |
151 | variant of the magical interpolation feature. This implementation is | |
152 | more predictable as well. | |
153 | New applications should prefer this version if they don't need to be | |
154 | compatible with older versions of Python. | |
155 | ||
156 | <span class="versionnote">New in version 2.3.</span> | |
157 | ||
158 | </dl> | |
159 | ||
160 | <P> | |
161 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-1403' xml:id='l2h-1403' class="exception">NoSectionError</tt></b></dt> | |
162 | <dd> | |
163 | Exception raised when a specified section is not found. | |
164 | </dd></dl> | |
165 | ||
166 | <P> | |
167 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-1404' xml:id='l2h-1404' class="exception">DuplicateSectionError</tt></b></dt> | |
168 | <dd> | |
169 | Exception raised if <tt class="method">add_section()</tt> is called with the name of | |
170 | a section that is already present. | |
171 | </dd></dl> | |
172 | ||
173 | <P> | |
174 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-1405' xml:id='l2h-1405' class="exception">NoOptionError</tt></b></dt> | |
175 | <dd> | |
176 | Exception raised when a specified option is not found in the specified | |
177 | section. | |
178 | </dd></dl> | |
179 | ||
180 | <P> | |
181 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-1406' xml:id='l2h-1406' class="exception">InterpolationError</tt></b></dt> | |
182 | <dd> | |
183 | Base class for exceptions raised when problems occur performing string | |
184 | interpolation. | |
185 | </dd></dl> | |
186 | ||
187 | <P> | |
188 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-1407' xml:id='l2h-1407' class="exception">InterpolationDepthError</tt></b></dt> | |
189 | <dd> | |
190 | Exception raised when string interpolation cannot be completed because | |
191 | the number of iterations exceeds <tt class="constant">MAX_INTERPOLATION_DEPTH</tt>. | |
192 | Subclass of <tt class="exception">InterpolationError</tt>. | |
193 | </dd></dl> | |
194 | ||
195 | <P> | |
196 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-1408' xml:id='l2h-1408' class="exception">InterpolationMissingOptionError</tt></b></dt> | |
197 | <dd> | |
198 | Exception raised when an option referenced from a value does not exist. | |
199 | Subclass of <tt class="exception">InterpolationError</tt>. | |
200 | ||
201 | <span class="versionnote">New in version 2.3.</span> | |
202 | ||
203 | </dd></dl> | |
204 | ||
205 | <P> | |
206 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-1409' xml:id='l2h-1409' class="exception">InterpolationSyntaxError</tt></b></dt> | |
207 | <dd> | |
208 | Exception raised when the source text into which substitutions are | |
209 | made does not conform to the required syntax. | |
210 | Subclass of <tt class="exception">InterpolationError</tt>. | |
211 | ||
212 | <span class="versionnote">New in version 2.3.</span> | |
213 | ||
214 | </dd></dl> | |
215 | ||
216 | <P> | |
217 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-1410' xml:id='l2h-1410' class="exception">MissingSectionHeaderError</tt></b></dt> | |
218 | <dd> | |
219 | Exception raised when attempting to parse a file which has no section | |
220 | headers. | |
221 | </dd></dl> | |
222 | ||
223 | <P> | |
224 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-1411' xml:id='l2h-1411' class="exception">ParsingError</tt></b></dt> | |
225 | <dd> | |
226 | Exception raised when errors occur attempting to parse a file. | |
227 | </dd></dl> | |
228 | ||
229 | <P> | |
230 | <dl><dt><b><tt id='l2h-1412' xml:id='l2h-1412'>MAX_INTERPOLATION_DEPTH</tt></b></dt> | |
231 | <dd> | |
232 | The maximum depth for recursive interpolation for <tt class="method">get()</tt> when | |
233 | the <var>raw</var> parameter is false. This is relevant only for the | |
234 | <tt class="class">ConfigParser</tt> class. | |
235 | </dd></dl> | |
236 | ||
237 | <P> | |
238 | <div class="seealso"> | |
239 | <p class="heading">See Also:</p> | |
240 | ||
241 | <dl compact="compact" class="seemodule"> | |
242 | <dt>Module <b><tt class="module"><a href="module-shlex.html">shlex</a></tt>:</b> | |
243 | <dd>Support for a creating <span class="Unix">Unix</span> shell-like | |
244 | mini-languages which can be used as an alternate | |
245 | format for application configuration files. | |
246 | </dl> | |
247 | </div> | |
248 | ||
249 | <P> | |
250 | ||
251 | <p><br /></p><hr class='online-navigation' /> | |
252 | <div class='online-navigation'> | |
253 | <!--Table of Child-Links--> | |
254 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
255 | ||
256 | <UL CLASS="ChildLinks"> | |
257 | <LI><A href="RawConfigParser-objects.html">5.17.1 RawConfigParser Objects</a> | |
258 | <LI><A href="ConfigParser-objects.html">5.17.2 ConfigParser Objects</a> | |
259 | <LI><A href="SafeConfigParser-objects.html">5.17.3 SafeConfigParser Objects</a> | |
260 | </ul> | |
261 | <!--End of Table of Child-Links--> | |
262 | </div> | |
263 | ||
264 | <DIV CLASS="navigation"> | |
265 | <div class='online-navigation'> | |
266 | <p></p><hr /> | |
267 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
268 | <tr> | |
269 | <td class='online-navigation'><a rel="prev" title="5.16.3 Recipes" | |
270 | href="itertools-recipes.html"><img src='../icons/previous.png' | |
271 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
272 | <td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services" | |
273 | href="misc.html"><img src='../icons/up.png' | |
274 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
275 | <td class='online-navigation'><a rel="next" title="5.17.1 RawConfigParser Objects" | |
276 | href="RawConfigParser-objects.html"><img src='../icons/next.png' | |
277 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
278 | <td align="center" width="100%">Python Library Reference</td> | |
279 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
280 | href="contents.html"><img src='../icons/contents.png' | |
281 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
282 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
283 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
284 | <td class='online-navigation'><a rel="index" title="Index" | |
285 | href="genindex.html"><img src='../icons/index.png' | |
286 | border='0' height='32' alt='Index' width='32' /></A></td> | |
287 | </tr></table> | |
288 | <div class='online-navigation'> | |
289 | <b class="navlabel">Previous:</b> | |
290 | <a class="sectref" rel="prev" href="itertools-recipes.html">5.16.3 Recipes</A> | |
291 | <b class="navlabel">Up:</b> | |
292 | <a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A> | |
293 | <b class="navlabel">Next:</b> | |
294 | <a class="sectref" rel="next" href="RawConfigParser-objects.html">5.17.1 RawConfigParser Objects</A> | |
295 | </div> | |
296 | </div> | |
297 | <hr /> | |
298 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
299 | </DIV> | |
300 | <!--End of Navigation Panel--> | |
301 | <ADDRESS> | |
302 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
303 | </ADDRESS> | |
304 | </BODY> | |
305 | </HTML> |