blob: 138619357034a980f98426582d6919c109710c69 [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.}
Fred Drake38e5d272000-04-03 20:13:55 +00006\moduleauthor{Ken Manheimer}{klm@digicool.com}
7\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
Eric S. Raymond56b31842002-12-27 20:05:36 +000016find on Microsoft Windows INI files. (Beware: this library does \emph{not}
17interpret or write the value-type prefixes used in the Windows
18Registry extended version of INI syntax.) You can use this to write Python
Fred Drakebc866ce1999-01-26 15:47:59 +000019programs which can be customized by end users easily.
20
Eric S. Raymond56b31842002-12-27 20:05:36 +000021The configuration file consists of sections, led by a
Fred Drakebc866ce1999-01-26 15:47:59 +000022\samp{[section]} header and followed by \samp{name: value} entries,
Fred Drake1e440291999-06-15 17:30:59 +000023with continuations in the style of \rfc{822}; \samp{name=value} is
Fred Drake38e5d272000-04-03 20:13:55 +000024also accepted. Note that leading whitespace is removed from values.
25The optional values can contain format strings which refer to other
26values in the same section, or values in a special
Fred Drake97d5f052002-10-25 20:20:58 +000027\code{DEFAULT} section. Additional defaults can be provided on
Fred Drake38e5d272000-04-03 20:13:55 +000028initialization and retrieval. Lines beginning with \character{\#} or
29\character{;} are ignored and may be used to provide comments.
Fred Drakebc866ce1999-01-26 15:47:59 +000030
31For example:
32
33\begin{verbatim}
Fred Drake97d5f052002-10-25 20:20:58 +000034[My Section]
Fred Drakebc866ce1999-01-26 15:47:59 +000035foodir: %(dir)s/whatever
Fred Drake38e5d272000-04-03 20:13:55 +000036dir=frob
Fred Drakebc866ce1999-01-26 15:47:59 +000037\end{verbatim}
38
Fred Drake38e5d272000-04-03 20:13:55 +000039would resolve the \samp{\%(dir)s} to the value of
40\samp{dir} (\samp{frob} in this case). All reference expansions are
41done on demand.
Fred Drakebc866ce1999-01-26 15:47:59 +000042
Fred Drake38e5d272000-04-03 20:13:55 +000043Default values can be specified by passing them into the
44\class{ConfigParser} constructor as a dictionary. Additional defaults
45may be passed into the \method{get()} method which will override all
Fred Drakeebe2a121999-01-26 21:49:05 +000046others.
Fred Drakebc866ce1999-01-26 15:47:59 +000047
Fred Drake97d5f052002-10-25 20:20:58 +000048\begin{classdesc}{RawConfigParser}{\optional{defaults}}
49The basic configuration object. When \var{defaults} is given, it is
50initialized into the dictionary of intrinsic defaults. This class
51does not support the magical interpolation behavior.
52\versionadded{2.3}
53\end{classdesc}
54
Fred Drakebc866ce1999-01-26 15:47:59 +000055\begin{classdesc}{ConfigParser}{\optional{defaults}}
Fred Drake97d5f052002-10-25 20:20:58 +000056Derived class of \class{RawConfigParser} that implements the magical
57interpolation feature and adds optional arguments the \method{get()}
58and \method{items()} methods. The values in \var{defaults} must be
Fred Drakebc866ce1999-01-26 15:47:59 +000059appropriate for the \samp{\%()s} string interpolation. Note that
Fred Drake33dde922000-09-27 22:48:44 +000060\var{__name__} is an intrinsic default; its value is the section name,
61and will override any value provided in \var{defaults}.
Fred Drakebc866ce1999-01-26 15:47:59 +000062\end{classdesc}
63
Fred Drake0eebd5c2002-10-25 21:52:00 +000064\begin{classdesc}{SafeConfigParser}{\optional{defaults}}
65Derived class of \class{ConfigParser} that implements a more-sane
66variant of the magical interpolation feature. This implementation is
67more predictable as well.
68% XXX Need to explain what's safer/more predictable about it.
69New applications should prefer this version if they don't need to be
70compatible with older versions of Python.
71\versionadded{2.3}
72\end{classdesc}
73
Fred Drakebc866ce1999-01-26 15:47:59 +000074\begin{excdesc}{NoSectionError}
75Exception raised when a specified section is not found.
76\end{excdesc}
77
78\begin{excdesc}{DuplicateSectionError}
Thomas Woutersf8316632000-07-16 19:01:10 +000079Exception raised when multiple sections with the same name are found,
Fred Drake38e5d272000-04-03 20:13:55 +000080or if \method{add_section()} is called with the name of a section that
81is already present.
Fred Drakebc866ce1999-01-26 15:47:59 +000082\end{excdesc}
83
84\begin{excdesc}{NoOptionError}
85Exception raised when a specified option is not found in the specified
86section.
87\end{excdesc}
88
89\begin{excdesc}{InterpolationError}
90Exception raised when problems occur performing string interpolation.
91\end{excdesc}
92
Fred Drake33dde922000-09-27 22:48:44 +000093\begin{excdesc}{InterpolationDepthError}
94Exception raised when string interpolation cannot be completed because
95the number of iterations exceeds \constant{MAX_INTERPOLATION_DEPTH}.
96\end{excdesc}
97
Fred Drakebc866ce1999-01-26 15:47:59 +000098\begin{excdesc}{MissingSectionHeaderError}
99Exception raised when attempting to parse a file which has no section
100headers.
101\end{excdesc}
102
103\begin{excdesc}{ParsingError}
104Exception raised when errors occur attempting to parse a file.
105\end{excdesc}
106
Fred Drake33dde922000-09-27 22:48:44 +0000107\begin{datadesc}{MAX_INTERPOLATION_DEPTH}
108The maximum depth for recursive interpolation for \method{get()} when
Fred Drake97d5f052002-10-25 20:20:58 +0000109the \var{raw} parameter is false. This is relevant only for the
110\class{ConfigParser} class.
Fred Drake33dde922000-09-27 22:48:44 +0000111\end{datadesc}
112
Fred Drakeebe2a121999-01-26 21:49:05 +0000113
Fred Drake184e8361999-05-11 15:14:15 +0000114\begin{seealso}
115 \seemodule{shlex}{Support for a creating \UNIX{} shell-like
116 minilanguages which can be used as an alternate format
117 for application configuration files.}
118\end{seealso}
119
Fred Drake5b0705d2001-02-19 22:37:24 +0000120
Fred Drake97d5f052002-10-25 20:20:58 +0000121\subsection{RawConfigParser Objects \label{RawConfigParser-objects}}
Fred Drakebc866ce1999-01-26 15:47:59 +0000122
Fred Drake97d5f052002-10-25 20:20:58 +0000123\class{RawConfigParser} instances have the following methods:
Fred Drakebc866ce1999-01-26 15:47:59 +0000124
125\begin{methoddesc}{defaults}{}
Fred Drake7cb42cd2000-05-23 02:28:26 +0000126Return a dictionary containing the instance-wide defaults.
Fred Drakebc866ce1999-01-26 15:47:59 +0000127\end{methoddesc}
128
129\begin{methoddesc}{sections}{}
Fred Drake38e5d272000-04-03 20:13:55 +0000130Return a list of the sections available; \code{DEFAULT} is not
131included in the list.
132\end{methoddesc}
133
134\begin{methoddesc}{add_section}{section}
135Add a section named \var{section} to the instance. If a section by
136the given name already exists, \exception{DuplicateSectionError} is
137raised.
Fred Drakebc866ce1999-01-26 15:47:59 +0000138\end{methoddesc}
139
140\begin{methoddesc}{has_section}{section}
141Indicates whether the named section is present in the
142configuration. The \code{DEFAULT} section is not acknowledged.
143\end{methoddesc}
144
145\begin{methoddesc}{options}{section}
146Returns a list of options available in the specified \var{section}.
147\end{methoddesc}
148
Eric S. Raymond417c4892000-07-10 18:11:00 +0000149\begin{methoddesc}{has_option}{section, option}
150If the given section exists, and contains the given option. return 1;
Fred Drake3c10c682001-09-28 16:57:16 +0000151otherwise return 0.
152\versionadded{1.6}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000153\end{methoddesc}
154
Fred Drakebc866ce1999-01-26 15:47:59 +0000155\begin{methoddesc}{read}{filenames}
Fred Draked85f0592000-05-09 15:06:32 +0000156Read and parse a list of filenames. If \var{filenames} is a string or
157Unicode string, it is treated as a single filename.
Fred Drake8b7bb7a2001-12-07 21:35:57 +0000158If a file named in \var{filenames} cannot be opened, that file will be
159ignored. This is designed so that you can specify a list of potential
160configuration file locations (for example, the current directory, the
161user's home directory, and some system-wide directory), and all
162existing configuration files in the list will be read. If none of the
163named files exist, the \class{ConfigParser} instance will contain an
164empty dataset. An application which requires initial values to be
165loaded from a file should load the required file or files using
166\method{readfp()} before calling \method{read()} for any optional
167files:
168
169\begin{verbatim}
170import ConfigParser, os
171
172config = ConfigParser.ConfigParser()
173config.readfp(open('defaults.cfg'))
174config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')])
175\end{verbatim}
Fred Draked85f0592000-05-09 15:06:32 +0000176\end{methoddesc}
177
178\begin{methoddesc}{readfp}{fp\optional{, filename}}
179Read and parse configuration data from the file or file-like object in
180\var{fp} (only the \method{readline()} method is used). If
181\var{filename} is omitted and \var{fp} has a \member{name} attribute,
182that is used for \var{filename}; the default is \samp{<???>}.
Fred Drakebc866ce1999-01-26 15:47:59 +0000183\end{methoddesc}
184
Fred Drake97d5f052002-10-25 20:20:58 +0000185\begin{methoddesc}{get}{section, option}
186Get an \var{option} value for the named \var{section}.
Fred Drakebc866ce1999-01-26 15:47:59 +0000187\end{methoddesc}
188
189\begin{methoddesc}{getint}{section, option}
190A convenience method which coerces the \var{option} in the specified
191\var{section} to an integer.
192\end{methoddesc}
193
194\begin{methoddesc}{getfloat}{section, option}
195A convenience method which coerces the \var{option} in the specified
196\var{section} to a floating point number.
197\end{methoddesc}
198
199\begin{methoddesc}{getboolean}{section, option}
200A convenience method which coerces the \var{option} in the specified
Fred Drakeb35f0ce2001-10-08 16:03:20 +0000201\var{section} to a Boolean value. Note that the accepted values
202for the option are \code{1}, \code{yes}, \code{true}, and \code{on},
203which cause this method to return true, and \code{0}, \code{no},
Fred Drake6959a2f2001-10-09 14:58:24 +0000204\code{false}, and \code{off}, which cause it to return false. These
205values are checked in a case-insensitive manner. Any other value will
206cause it to raise \exception{ValueError}.
Fred Drakebc866ce1999-01-26 15:47:59 +0000207\end{methoddesc}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000208
Fred Drake97d5f052002-10-25 20:20:58 +0000209\begin{methoddesc}{items}{section}
210Return a list of \code{(\var{name}, \var{value})} pairs for each
211option in the given \var{section}.
Fred Drake2ca041f2002-09-27 15:49:56 +0000212\end{methoddesc}
213
Eric S. Raymond417c4892000-07-10 18:11:00 +0000214\begin{methoddesc}{set}{section, option, value}
215If the given section exists, set the given option to the specified value;
Fred Drake3c10c682001-09-28 16:57:16 +0000216otherwise raise \exception{NoSectionError}.
217\versionadded{1.6}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000218\end{methoddesc}
219
Eric S. Raymondf868de62000-07-14 15:00:02 +0000220\begin{methoddesc}{write}{fileobject}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000221Write a representation of the configuration to the specified file
222object. This representation can be parsed by a future \method{read()}
Fred Drake3c10c682001-09-28 16:57:16 +0000223call.
224\versionadded{1.6}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000225\end{methoddesc}
Eric S. Raymondf868de62000-07-14 15:00:02 +0000226
227\begin{methoddesc}{remove_option}{section, option}
228Remove the specified \var{option} from the specified \var{section}.
229If the section does not exist, raise \exception{NoSectionError}.
230If the option existed to be removed, return 1; otherwise return 0.
Fred Drake3c10c682001-09-28 16:57:16 +0000231\versionadded{1.6}
Eric S. Raymondf868de62000-07-14 15:00:02 +0000232\end{methoddesc}
233
234\begin{methoddesc}{remove_section}{section}
235Remove the specified \var{section} from the configuration.
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000236If the section in fact existed, return \code{True}.
237Otherwise return \code{False}.
Eric S. Raymondf868de62000-07-14 15:00:02 +0000238\end{methoddesc}
239
Fred Drake5b0705d2001-02-19 22:37:24 +0000240\begin{methoddesc}{optionxform}{option}
241Transforms the option name \var{option} as found in an input file or
242as passed in by client code to the form that should be used in the
243internal structures. The default implementation returns a lower-case
244version of \var{option}; subclasses may override this or client code
245can set an attribute of this name on instances to affect this
246behavior. Setting this to \function{str()}, for example, would make
247option names case sensitive.
248\end{methoddesc}
Fred Drake97d5f052002-10-25 20:20:58 +0000249
250
251\subsection{ConfigParser Objects \label{ConfigParser-objects}}
252
253The \class{ConfigParser} class extends some methods of the
254\class{RawConfigParser} interface, adding some optional arguments.
255
256\begin{methoddesc}{get}{section, option\optional{, raw\optional{, vars}}}
257Get an \var{option} value for the named \var{section}. All the
258\character{\%} interpolations are expanded in the return values, based
259on the defaults passed into the constructor, as well as the options
260\var{vars} provided, unless the \var{raw} argument is true.
261\end{methoddesc}
262
263\begin{methoddesc}{items}{section\optional{, raw\optional{, vars}}}
264Create a generator which will return a tuple \code{(name, value)} for
265each option in the given \var{section}. Optional arguments have the
266same meaning as for the \code{get()} method.
267\versionadded{2.3}
268\end{methoddesc}