blob: 2c08ec424ca2a556be52c860c8c2177f45a1c60e [file] [log] [blame]
Fred Drakebc866ce1999-01-26 15:47:59 +00001\section{\module{ConfigParser} ---
Fred Drake4747f7f1999-04-21 16:41:40 +00002 Configuration file parser}
Fred Drakebc866ce1999-01-26 15:47:59 +00003
4\declaremodule{standard}{ConfigParser}
Fred Drakebc866ce1999-01-26 15:47:59 +00005\modulesynopsis{Configuration file parser.}
Andrew M. Kuchlingc62af022004-01-08 15:01:08 +00006\moduleauthor{Ken Manheimer}{klm@zope.com}
Fred Drake38e5d272000-04-03 20:13:55 +00007\moduleauthor{Barry Warsaw}{bwarsaw@python.org}
Eric S. Raymond417c4892000-07-10 18:11:00 +00008\moduleauthor{Eric S. Raymond}{esr@thyrsus.com}
Fred Drakebc866ce1999-01-26 15:47:59 +00009\sectionauthor{Christopher G. Petrilli}{petrilli@amber.org}
10
Fred Drake1e440291999-06-15 17:30:59 +000011This module defines the class \class{ConfigParser}.
12\indexii{.ini}{file}\indexii{configuration}{file}\index{ini file}
13\index{Windows ini file}
14The \class{ConfigParser} class implements a basic configuration file
Fred Drakebc866ce1999-01-26 15:47:59 +000015parser language which provides a structure similar to what you would
Fred Drake81e4aa72002-12-30 23:50:19 +000016find on Microsoft Windows INI files. You can use this to write Python
Fred Drakebc866ce1999-01-26 15:47:59 +000017programs which can be customized by end users easily.
18
Fred Drake81e4aa72002-12-30 23:50:19 +000019\begin{notice}[warning]
20 This library does \emph{not} interpret or write the value-type
21 prefixes used in the Windows Registry extended version of INI syntax.
22\end{notice}
23
Eric S. Raymond56b31842002-12-27 20:05:36 +000024The configuration file consists of sections, led by a
Fred Drakebc866ce1999-01-26 15:47:59 +000025\samp{[section]} header and followed by \samp{name: value} entries,
Fred Drake1e440291999-06-15 17:30:59 +000026with continuations in the style of \rfc{822}; \samp{name=value} is
Fred Drake38e5d272000-04-03 20:13:55 +000027also accepted. Note that leading whitespace is removed from values.
28The optional values can contain format strings which refer to other
29values in the same section, or values in a special
Fred Drake97d5f052002-10-25 20:20:58 +000030\code{DEFAULT} section. Additional defaults can be provided on
Fred Drake38e5d272000-04-03 20:13:55 +000031initialization and retrieval. Lines beginning with \character{\#} or
32\character{;} are ignored and may be used to provide comments.
Fred Drakebc866ce1999-01-26 15:47:59 +000033
34For example:
35
36\begin{verbatim}
Fred Drake97d5f052002-10-25 20:20:58 +000037[My Section]
Fred Drakebc866ce1999-01-26 15:47:59 +000038foodir: %(dir)s/whatever
Fred Drake38e5d272000-04-03 20:13:55 +000039dir=frob
Fred Drakebc866ce1999-01-26 15:47:59 +000040\end{verbatim}
41
Fred Drake38e5d272000-04-03 20:13:55 +000042would resolve the \samp{\%(dir)s} to the value of
43\samp{dir} (\samp{frob} in this case). All reference expansions are
44done on demand.
Fred Drakebc866ce1999-01-26 15:47:59 +000045
Fred Drake38e5d272000-04-03 20:13:55 +000046Default values can be specified by passing them into the
47\class{ConfigParser} constructor as a dictionary. Additional defaults
48may be passed into the \method{get()} method which will override all
Fred Drakeebe2a121999-01-26 21:49:05 +000049others.
Fred Drakebc866ce1999-01-26 15:47:59 +000050
Martin v. Löwisa00bcac2006-12-03 12:01:53 +000051Sections are normally stored in a builtin dictionary. An alternative
52dictionary type can be passed to the \class{ConfigParser} constructor.
Neal Norwitz0adf0842006-12-11 01:01:06 +000053For example, if a dictionary type is passed that sorts its keys,
Martin v. Löwisa00bcac2006-12-03 12:01:53 +000054the sections will be sorted on write-back, as will be the keys within
55each section.
56
57\begin{classdesc}{RawConfigParser}{\optional{defaults\optional{, dict_type}}}
Fred Drake97d5f052002-10-25 20:20:58 +000058The basic configuration object. When \var{defaults} is given, it is
Martin v. Löwisa00bcac2006-12-03 12:01:53 +000059initialized into the dictionary of intrinsic defaults. When \var{dict_type}
60is given, it will be used to create the dictionary objects for the list
61of sections, for the options within a section, and for the default values.
62This class does not support the magical interpolation behavior.
Fred Drake97d5f052002-10-25 20:20:58 +000063\versionadded{2.3}
George Yoshida9be49052006-12-04 11:41:54 +000064\versionchanged[\var{dict_type} was added]{2.6}
Fred Drake97d5f052002-10-25 20:20:58 +000065\end{classdesc}
66
Fred Drakebc866ce1999-01-26 15:47:59 +000067\begin{classdesc}{ConfigParser}{\optional{defaults}}
Fred Drake97d5f052002-10-25 20:20:58 +000068Derived class of \class{RawConfigParser} that implements the magical
Skip Montanaro6485a872003-12-15 14:38:57 +000069interpolation feature and adds optional arguments to the \method{get()}
Fred Drake97d5f052002-10-25 20:20:58 +000070and \method{items()} methods. The values in \var{defaults} must be
Fred Drakebc866ce1999-01-26 15:47:59 +000071appropriate for the \samp{\%()s} string interpolation. Note that
Fred Drake33dde922000-09-27 22:48:44 +000072\var{__name__} is an intrinsic default; its value is the section name,
73and will override any value provided in \var{defaults}.
Fred Drakebc12b012004-05-18 02:25:51 +000074
75All option names used in interpolation will be passed through the
76\method{optionxform()} method just like any other option name
77reference. For example, using the default implementation of
78\method{optionxform()} (which converts option names to lower case),
79the values \samp{foo \%(bar)s} and \samp{foo \%(BAR)s} are
80equivalent.
Fred Drakebc866ce1999-01-26 15:47:59 +000081\end{classdesc}
82
Fred Drake0eebd5c2002-10-25 21:52:00 +000083\begin{classdesc}{SafeConfigParser}{\optional{defaults}}
84Derived class of \class{ConfigParser} that implements a more-sane
85variant of the magical interpolation feature. This implementation is
86more predictable as well.
87% XXX Need to explain what's safer/more predictable about it.
88New applications should prefer this version if they don't need to be
89compatible with older versions of Python.
90\versionadded{2.3}
91\end{classdesc}
92
Fred Drakebc866ce1999-01-26 15:47:59 +000093\begin{excdesc}{NoSectionError}
94Exception raised when a specified section is not found.
95\end{excdesc}
96
97\begin{excdesc}{DuplicateSectionError}
Fred Drakeb4c60912004-05-18 03:56:51 +000098Exception raised if \method{add_section()} is called with the name of
99a section that is already present.
Fred Drakebc866ce1999-01-26 15:47:59 +0000100\end{excdesc}
101
102\begin{excdesc}{NoOptionError}
103Exception raised when a specified option is not found in the specified
104section.
105\end{excdesc}
106
107\begin{excdesc}{InterpolationError}
Fred Drakee2c64912002-12-31 17:23:27 +0000108Base class for exceptions raised when problems occur performing string
109interpolation.
Fred Drakebc866ce1999-01-26 15:47:59 +0000110\end{excdesc}
111
Fred Drake33dde922000-09-27 22:48:44 +0000112\begin{excdesc}{InterpolationDepthError}
113Exception raised when string interpolation cannot be completed because
114the number of iterations exceeds \constant{MAX_INTERPOLATION_DEPTH}.
Fred Drakee2c64912002-12-31 17:23:27 +0000115Subclass of \exception{InterpolationError}.
116\end{excdesc}
117
118\begin{excdesc}{InterpolationMissingOptionError}
119Exception raised when an option referenced from a value does not exist.
120Subclass of \exception{InterpolationError}.
121\versionadded{2.3}
Fred Drake33dde922000-09-27 22:48:44 +0000122\end{excdesc}
123
Fred Drake81e4aa72002-12-30 23:50:19 +0000124\begin{excdesc}{InterpolationSyntaxError}
125Exception raised when the source text into which substitutions are
126made does not conform to the required syntax.
Fred Drakee2c64912002-12-31 17:23:27 +0000127Subclass of \exception{InterpolationError}.
Neal Norwitz99448e52002-12-31 12:23:10 +0000128\versionadded{2.3}
Fred Drake81e4aa72002-12-30 23:50:19 +0000129\end{excdesc}
130
Fred Drakebc866ce1999-01-26 15:47:59 +0000131\begin{excdesc}{MissingSectionHeaderError}
132Exception raised when attempting to parse a file which has no section
133headers.
134\end{excdesc}
135
136\begin{excdesc}{ParsingError}
137Exception raised when errors occur attempting to parse a file.
138\end{excdesc}
139
Fred Drake33dde922000-09-27 22:48:44 +0000140\begin{datadesc}{MAX_INTERPOLATION_DEPTH}
141The maximum depth for recursive interpolation for \method{get()} when
Fred Drake97d5f052002-10-25 20:20:58 +0000142the \var{raw} parameter is false. This is relevant only for the
143\class{ConfigParser} class.
Fred Drake33dde922000-09-27 22:48:44 +0000144\end{datadesc}
145
Fred Drakeebe2a121999-01-26 21:49:05 +0000146
Fred Drake184e8361999-05-11 15:14:15 +0000147\begin{seealso}
148 \seemodule{shlex}{Support for a creating \UNIX{} shell-like
Fred Drakee2c64912002-12-31 17:23:27 +0000149 mini-languages which can be used as an alternate
150 format for application configuration files.}
Fred Drake184e8361999-05-11 15:14:15 +0000151\end{seealso}
152
Fred Drake5b0705d2001-02-19 22:37:24 +0000153
Fred Drake97d5f052002-10-25 20:20:58 +0000154\subsection{RawConfigParser Objects \label{RawConfigParser-objects}}
Fred Drakebc866ce1999-01-26 15:47:59 +0000155
Fred Drake97d5f052002-10-25 20:20:58 +0000156\class{RawConfigParser} instances have the following methods:
Fred Drakebc866ce1999-01-26 15:47:59 +0000157
158\begin{methoddesc}{defaults}{}
Fred Drake7cb42cd2000-05-23 02:28:26 +0000159Return a dictionary containing the instance-wide defaults.
Fred Drakebc866ce1999-01-26 15:47:59 +0000160\end{methoddesc}
161
162\begin{methoddesc}{sections}{}
Fred Drake38e5d272000-04-03 20:13:55 +0000163Return a list of the sections available; \code{DEFAULT} is not
164included in the list.
165\end{methoddesc}
166
167\begin{methoddesc}{add_section}{section}
168Add a section named \var{section} to the instance. If a section by
169the given name already exists, \exception{DuplicateSectionError} is
170raised.
Fred Drakebc866ce1999-01-26 15:47:59 +0000171\end{methoddesc}
172
173\begin{methoddesc}{has_section}{section}
174Indicates whether the named section is present in the
175configuration. The \code{DEFAULT} section is not acknowledged.
176\end{methoddesc}
177
178\begin{methoddesc}{options}{section}
179Returns a list of options available in the specified \var{section}.
180\end{methoddesc}
181
Eric S. Raymond417c4892000-07-10 18:11:00 +0000182\begin{methoddesc}{has_option}{section, option}
Raymond Hettinger9b4dab42003-12-31 18:37:28 +0000183If the given section exists, and contains the given option,
184return \constant{True}; otherwise return \constant{False}.
Fred Drake3c10c682001-09-28 16:57:16 +0000185\versionadded{1.6}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000186\end{methoddesc}
187
Fred Drakebc866ce1999-01-26 15:47:59 +0000188\begin{methoddesc}{read}{filenames}
Fred Drake82903142004-05-18 04:24:02 +0000189Attempt to read and parse a list of filenames, returning a list of filenames
190which were successfully parsed. If \var{filenames} is a string or
Fred Draked85f0592000-05-09 15:06:32 +0000191Unicode string, it is treated as a single filename.
Fred Drake8b7bb7a2001-12-07 21:35:57 +0000192If a file named in \var{filenames} cannot be opened, that file will be
193ignored. This is designed so that you can specify a list of potential
194configuration file locations (for example, the current directory, the
195user's home directory, and some system-wide directory), and all
196existing configuration files in the list will be read. If none of the
197named files exist, the \class{ConfigParser} instance will contain an
198empty dataset. An application which requires initial values to be
199loaded from a file should load the required file or files using
200\method{readfp()} before calling \method{read()} for any optional
201files:
202
203\begin{verbatim}
204import ConfigParser, os
205
206config = ConfigParser.ConfigParser()
207config.readfp(open('defaults.cfg'))
208config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')])
209\end{verbatim}
Fred Drake82903142004-05-18 04:24:02 +0000210\versionchanged[Returns list of successfully parsed filenames]{2.4}
Fred Draked85f0592000-05-09 15:06:32 +0000211\end{methoddesc}
212
213\begin{methoddesc}{readfp}{fp\optional{, filename}}
214Read and parse configuration data from the file or file-like object in
215\var{fp} (only the \method{readline()} method is used). If
216\var{filename} is omitted and \var{fp} has a \member{name} attribute,
217that is used for \var{filename}; the default is \samp{<???>}.
Fred Drakebc866ce1999-01-26 15:47:59 +0000218\end{methoddesc}
219
Fred Drake97d5f052002-10-25 20:20:58 +0000220\begin{methoddesc}{get}{section, option}
221Get an \var{option} value for the named \var{section}.
Fred Drakebc866ce1999-01-26 15:47:59 +0000222\end{methoddesc}
223
224\begin{methoddesc}{getint}{section, option}
225A convenience method which coerces the \var{option} in the specified
226\var{section} to an integer.
227\end{methoddesc}
228
229\begin{methoddesc}{getfloat}{section, option}
230A convenience method which coerces the \var{option} in the specified
231\var{section} to a floating point number.
232\end{methoddesc}
233
234\begin{methoddesc}{getboolean}{section, option}
235A convenience method which coerces the \var{option} in the specified
Fred Drakeb35f0ce2001-10-08 16:03:20 +0000236\var{section} to a Boolean value. Note that the accepted values
Raymond Hettingerfb857892003-08-14 19:58:35 +0000237for the option are \code{"1"}, \code{"yes"}, \code{"true"}, and \code{"on"},
238which cause this method to return \code{True}, and \code{"0"}, \code{"no"},
239\code{"false"}, and \code{"off"}, which cause it to return \code{False}. These
240string values are checked in a case-insensitive manner. Any other value will
Fred Drake6959a2f2001-10-09 14:58:24 +0000241cause it to raise \exception{ValueError}.
Fred Drakebc866ce1999-01-26 15:47:59 +0000242\end{methoddesc}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000243
Fred Drake97d5f052002-10-25 20:20:58 +0000244\begin{methoddesc}{items}{section}
245Return a list of \code{(\var{name}, \var{value})} pairs for each
246option in the given \var{section}.
Fred Drake2ca041f2002-09-27 15:49:56 +0000247\end{methoddesc}
248
Eric S. Raymond417c4892000-07-10 18:11:00 +0000249\begin{methoddesc}{set}{section, option, value}
David Goodger1cbf2062004-10-03 15:55:09 +0000250If the given section exists, set the given option to the specified
251value; otherwise raise \exception{NoSectionError}. While it is
252possible to use \class{RawConfigParser} (or \class{ConfigParser} with
253\var{raw} parameters set to true) for \emph{internal} storage of
254non-string values, full functionality (including interpolation and
255output to files) can only be achieved using string values.
Fred Drake3c10c682001-09-28 16:57:16 +0000256\versionadded{1.6}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000257\end{methoddesc}
258
Eric S. Raymondf868de62000-07-14 15:00:02 +0000259\begin{methoddesc}{write}{fileobject}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000260Write a representation of the configuration to the specified file
261object. This representation can be parsed by a future \method{read()}
Fred Drake3c10c682001-09-28 16:57:16 +0000262call.
263\versionadded{1.6}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000264\end{methoddesc}
Eric S. Raymondf868de62000-07-14 15:00:02 +0000265
266\begin{methoddesc}{remove_option}{section, option}
267Remove the specified \var{option} from the specified \var{section}.
268If the section does not exist, raise \exception{NoSectionError}.
Raymond Hettinger9b4dab42003-12-31 18:37:28 +0000269If the option existed to be removed, return \constant{True};
270otherwise return \constant{False}.
Fred Drake3c10c682001-09-28 16:57:16 +0000271\versionadded{1.6}
Eric S. Raymondf868de62000-07-14 15:00:02 +0000272\end{methoddesc}
273
274\begin{methoddesc}{remove_section}{section}
275Remove the specified \var{section} from the configuration.
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000276If the section in fact existed, return \code{True}.
277Otherwise return \code{False}.
Eric S. Raymondf868de62000-07-14 15:00:02 +0000278\end{methoddesc}
279
Fred Drake5b0705d2001-02-19 22:37:24 +0000280\begin{methoddesc}{optionxform}{option}
281Transforms the option name \var{option} as found in an input file or
282as passed in by client code to the form that should be used in the
283internal structures. The default implementation returns a lower-case
284version of \var{option}; subclasses may override this or client code
285can set an attribute of this name on instances to affect this
286behavior. Setting this to \function{str()}, for example, would make
287option names case sensitive.
288\end{methoddesc}
Fred Drake97d5f052002-10-25 20:20:58 +0000289
290
291\subsection{ConfigParser Objects \label{ConfigParser-objects}}
292
293The \class{ConfigParser} class extends some methods of the
294\class{RawConfigParser} interface, adding some optional arguments.
295
296\begin{methoddesc}{get}{section, option\optional{, raw\optional{, vars}}}
297Get an \var{option} value for the named \var{section}. All the
298\character{\%} interpolations are expanded in the return values, based
299on the defaults passed into the constructor, as well as the options
300\var{vars} provided, unless the \var{raw} argument is true.
301\end{methoddesc}
302
303\begin{methoddesc}{items}{section\optional{, raw\optional{, vars}}}
Fred Drake86c60ed2003-10-21 16:50:55 +0000304Return a list of \code{(\var{name}, \var{value})} pairs for each
305option in the given \var{section}. Optional arguments have the
306same meaning as for the \method{get()} method.
Fred Drake97d5f052002-10-25 20:20:58 +0000307\versionadded{2.3}
308\end{methoddesc}
David Goodger1cbf2062004-10-03 15:55:09 +0000309
310
311\subsection{SafeConfigParser Objects \label{SafeConfigParser-objects}}
312
313The \class{SafeConfigParser} class implements the same extended
314interface as \class{ConfigParser}, with the following addition:
315
316\begin{methoddesc}{set}{section, option, value}
317If the given section exists, set the given option to the specified
318value; otherwise raise \exception{NoSectionError}. \var{value} must
319be a string (\class{str} or \class{unicode}); if not,
320\exception{TypeError} is raised.
321\versionadded{2.4}
322\end{methoddesc}