blob: 946590d83e770e3429357d1ad892fcf23667014f [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
Fred Drake1e440291999-06-15 17:30:59 +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
19The configuration file consists of sections, lead by a
20\samp{[section]} header and followed by \samp{name: value} entries,
Fred Drake1e440291999-06-15 17:30:59 +000021with continuations in the style of \rfc{822}; \samp{name=value} is
Fred Drake38e5d272000-04-03 20:13:55 +000022also accepted. Note that leading whitespace is removed from values.
23The optional values can contain format strings which refer to other
24values in the same section, or values in a special
Fred Drake97d5f052002-10-25 20:20:58 +000025\code{DEFAULT} section. Additional defaults can be provided on
Fred Drake38e5d272000-04-03 20:13:55 +000026initialization and retrieval. Lines beginning with \character{\#} or
27\character{;} are ignored and may be used to provide comments.
Fred Drakebc866ce1999-01-26 15:47:59 +000028
29For example:
30
31\begin{verbatim}
Fred Drake97d5f052002-10-25 20:20:58 +000032[My Section]
Fred Drakebc866ce1999-01-26 15:47:59 +000033foodir: %(dir)s/whatever
Fred Drake38e5d272000-04-03 20:13:55 +000034dir=frob
Fred Drakebc866ce1999-01-26 15:47:59 +000035\end{verbatim}
36
Fred Drake38e5d272000-04-03 20:13:55 +000037would resolve the \samp{\%(dir)s} to the value of
38\samp{dir} (\samp{frob} in this case). All reference expansions are
39done on demand.
Fred Drakebc866ce1999-01-26 15:47:59 +000040
Fred Drake38e5d272000-04-03 20:13:55 +000041Default values can be specified by passing them into the
42\class{ConfigParser} constructor as a dictionary. Additional defaults
43may be passed into the \method{get()} method which will override all
Fred Drakeebe2a121999-01-26 21:49:05 +000044others.
Fred Drakebc866ce1999-01-26 15:47:59 +000045
Fred Drake97d5f052002-10-25 20:20:58 +000046\begin{classdesc}{RawConfigParser}{\optional{defaults}}
47The basic configuration object. When \var{defaults} is given, it is
48initialized into the dictionary of intrinsic defaults. This class
49does not support the magical interpolation behavior.
50\versionadded{2.3}
51\end{classdesc}
52
Fred Drakebc866ce1999-01-26 15:47:59 +000053\begin{classdesc}{ConfigParser}{\optional{defaults}}
Fred Drake97d5f052002-10-25 20:20:58 +000054Derived class of \class{RawConfigParser} that implements the magical
55interpolation feature and adds optional arguments the \method{get()}
56and \method{items()} methods. The values in \var{defaults} must be
Fred Drakebc866ce1999-01-26 15:47:59 +000057appropriate for the \samp{\%()s} string interpolation. Note that
Fred Drake33dde922000-09-27 22:48:44 +000058\var{__name__} is an intrinsic default; its value is the section name,
59and will override any value provided in \var{defaults}.
Fred Drakebc866ce1999-01-26 15:47:59 +000060\end{classdesc}
61
62\begin{excdesc}{NoSectionError}
63Exception raised when a specified section is not found.
64\end{excdesc}
65
66\begin{excdesc}{DuplicateSectionError}
Thomas Woutersf8316632000-07-16 19:01:10 +000067Exception raised when multiple sections with the same name are found,
Fred Drake38e5d272000-04-03 20:13:55 +000068or if \method{add_section()} is called with the name of a section that
69is already present.
Fred Drakebc866ce1999-01-26 15:47:59 +000070\end{excdesc}
71
72\begin{excdesc}{NoOptionError}
73Exception raised when a specified option is not found in the specified
74section.
75\end{excdesc}
76
77\begin{excdesc}{InterpolationError}
78Exception raised when problems occur performing string interpolation.
79\end{excdesc}
80
Fred Drake33dde922000-09-27 22:48:44 +000081\begin{excdesc}{InterpolationDepthError}
82Exception raised when string interpolation cannot be completed because
83the number of iterations exceeds \constant{MAX_INTERPOLATION_DEPTH}.
84\end{excdesc}
85
Fred Drakebc866ce1999-01-26 15:47:59 +000086\begin{excdesc}{MissingSectionHeaderError}
87Exception raised when attempting to parse a file which has no section
88headers.
89\end{excdesc}
90
91\begin{excdesc}{ParsingError}
92Exception raised when errors occur attempting to parse a file.
93\end{excdesc}
94
Fred Drake33dde922000-09-27 22:48:44 +000095\begin{datadesc}{MAX_INTERPOLATION_DEPTH}
96The maximum depth for recursive interpolation for \method{get()} when
Fred Drake97d5f052002-10-25 20:20:58 +000097the \var{raw} parameter is false. This is relevant only for the
98\class{ConfigParser} class.
Fred Drake33dde922000-09-27 22:48:44 +000099\end{datadesc}
100
Fred Drakeebe2a121999-01-26 21:49:05 +0000101
Fred Drake184e8361999-05-11 15:14:15 +0000102\begin{seealso}
103 \seemodule{shlex}{Support for a creating \UNIX{} shell-like
104 minilanguages which can be used as an alternate format
105 for application configuration files.}
106\end{seealso}
107
Fred Drake5b0705d2001-02-19 22:37:24 +0000108
Fred Drake97d5f052002-10-25 20:20:58 +0000109\subsection{RawConfigParser Objects \label{RawConfigParser-objects}}
Fred Drakebc866ce1999-01-26 15:47:59 +0000110
Fred Drake97d5f052002-10-25 20:20:58 +0000111\class{RawConfigParser} instances have the following methods:
Fred Drakebc866ce1999-01-26 15:47:59 +0000112
113\begin{methoddesc}{defaults}{}
Fred Drake7cb42cd2000-05-23 02:28:26 +0000114Return a dictionary containing the instance-wide defaults.
Fred Drakebc866ce1999-01-26 15:47:59 +0000115\end{methoddesc}
116
117\begin{methoddesc}{sections}{}
Fred Drake38e5d272000-04-03 20:13:55 +0000118Return a list of the sections available; \code{DEFAULT} is not
119included in the list.
120\end{methoddesc}
121
122\begin{methoddesc}{add_section}{section}
123Add a section named \var{section} to the instance. If a section by
124the given name already exists, \exception{DuplicateSectionError} is
125raised.
Fred Drakebc866ce1999-01-26 15:47:59 +0000126\end{methoddesc}
127
128\begin{methoddesc}{has_section}{section}
129Indicates whether the named section is present in the
130configuration. The \code{DEFAULT} section is not acknowledged.
131\end{methoddesc}
132
133\begin{methoddesc}{options}{section}
134Returns a list of options available in the specified \var{section}.
135\end{methoddesc}
136
Eric S. Raymond417c4892000-07-10 18:11:00 +0000137\begin{methoddesc}{has_option}{section, option}
138If the given section exists, and contains the given option. return 1;
Fred Drake3c10c682001-09-28 16:57:16 +0000139otherwise return 0.
140\versionadded{1.6}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000141\end{methoddesc}
142
Fred Drakebc866ce1999-01-26 15:47:59 +0000143\begin{methoddesc}{read}{filenames}
Fred Draked85f0592000-05-09 15:06:32 +0000144Read and parse a list of filenames. If \var{filenames} is a string or
145Unicode string, it is treated as a single filename.
Fred Drake8b7bb7a2001-12-07 21:35:57 +0000146If a file named in \var{filenames} cannot be opened, that file will be
147ignored. This is designed so that you can specify a list of potential
148configuration file locations (for example, the current directory, the
149user's home directory, and some system-wide directory), and all
150existing configuration files in the list will be read. If none of the
151named files exist, the \class{ConfigParser} instance will contain an
152empty dataset. An application which requires initial values to be
153loaded from a file should load the required file or files using
154\method{readfp()} before calling \method{read()} for any optional
155files:
156
157\begin{verbatim}
158import ConfigParser, os
159
160config = ConfigParser.ConfigParser()
161config.readfp(open('defaults.cfg'))
162config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')])
163\end{verbatim}
Fred Draked85f0592000-05-09 15:06:32 +0000164\end{methoddesc}
165
166\begin{methoddesc}{readfp}{fp\optional{, filename}}
167Read and parse configuration data from the file or file-like object in
168\var{fp} (only the \method{readline()} method is used). If
169\var{filename} is omitted and \var{fp} has a \member{name} attribute,
170that is used for \var{filename}; the default is \samp{<???>}.
Fred Drakebc866ce1999-01-26 15:47:59 +0000171\end{methoddesc}
172
Fred Drake97d5f052002-10-25 20:20:58 +0000173\begin{methoddesc}{get}{section, option}
174Get an \var{option} value for the named \var{section}.
Fred Drakebc866ce1999-01-26 15:47:59 +0000175\end{methoddesc}
176
177\begin{methoddesc}{getint}{section, option}
178A convenience method which coerces the \var{option} in the specified
179\var{section} to an integer.
180\end{methoddesc}
181
182\begin{methoddesc}{getfloat}{section, option}
183A convenience method which coerces the \var{option} in the specified
184\var{section} to a floating point number.
185\end{methoddesc}
186
187\begin{methoddesc}{getboolean}{section, option}
188A convenience method which coerces the \var{option} in the specified
Fred Drakeb35f0ce2001-10-08 16:03:20 +0000189\var{section} to a Boolean value. Note that the accepted values
190for the option are \code{1}, \code{yes}, \code{true}, and \code{on},
191which cause this method to return true, and \code{0}, \code{no},
Fred Drake6959a2f2001-10-09 14:58:24 +0000192\code{false}, and \code{off}, which cause it to return false. These
193values are checked in a case-insensitive manner. Any other value will
194cause it to raise \exception{ValueError}.
Fred Drakebc866ce1999-01-26 15:47:59 +0000195\end{methoddesc}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000196
Fred Drake97d5f052002-10-25 20:20:58 +0000197\begin{methoddesc}{items}{section}
198Return a list of \code{(\var{name}, \var{value})} pairs for each
199option in the given \var{section}.
Fred Drake2ca041f2002-09-27 15:49:56 +0000200\end{methoddesc}
201
Eric S. Raymond417c4892000-07-10 18:11:00 +0000202\begin{methoddesc}{set}{section, option, value}
203If the given section exists, set the given option to the specified value;
Fred Drake3c10c682001-09-28 16:57:16 +0000204otherwise raise \exception{NoSectionError}.
205\versionadded{1.6}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000206\end{methoddesc}
207
Eric S. Raymondf868de62000-07-14 15:00:02 +0000208\begin{methoddesc}{write}{fileobject}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000209Write a representation of the configuration to the specified file
210object. This representation can be parsed by a future \method{read()}
Fred Drake3c10c682001-09-28 16:57:16 +0000211call.
212\versionadded{1.6}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000213\end{methoddesc}
Eric S. Raymondf868de62000-07-14 15:00:02 +0000214
215\begin{methoddesc}{remove_option}{section, option}
216Remove the specified \var{option} from the specified \var{section}.
217If the section does not exist, raise \exception{NoSectionError}.
218If the option existed to be removed, return 1; otherwise return 0.
Fred Drake3c10c682001-09-28 16:57:16 +0000219\versionadded{1.6}
Eric S. Raymondf868de62000-07-14 15:00:02 +0000220\end{methoddesc}
221
222\begin{methoddesc}{remove_section}{section}
223Remove the specified \var{section} from the configuration.
Neal Norwitzd3dab2b2002-04-05 02:21:09 +0000224If the section in fact existed, return \code{True}.
225Otherwise return \code{False}.
Eric S. Raymondf868de62000-07-14 15:00:02 +0000226\end{methoddesc}
227
Fred Drake5b0705d2001-02-19 22:37:24 +0000228\begin{methoddesc}{optionxform}{option}
229Transforms the option name \var{option} as found in an input file or
230as passed in by client code to the form that should be used in the
231internal structures. The default implementation returns a lower-case
232version of \var{option}; subclasses may override this or client code
233can set an attribute of this name on instances to affect this
234behavior. Setting this to \function{str()}, for example, would make
235option names case sensitive.
236\end{methoddesc}
Fred Drake97d5f052002-10-25 20:20:58 +0000237
238
239\subsection{ConfigParser Objects \label{ConfigParser-objects}}
240
241The \class{ConfigParser} class extends some methods of the
242\class{RawConfigParser} interface, adding some optional arguments.
243
244\begin{methoddesc}{get}{section, option\optional{, raw\optional{, vars}}}
245Get an \var{option} value for the named \var{section}. All the
246\character{\%} interpolations are expanded in the return values, based
247on the defaults passed into the constructor, as well as the options
248\var{vars} provided, unless the \var{raw} argument is true.
249\end{methoddesc}
250
251\begin{methoddesc}{items}{section\optional{, raw\optional{, vars}}}
252Create a generator which will return a tuple \code{(name, value)} for
253each option in the given \var{section}. Optional arguments have the
254same meaning as for the \code{get()} method.
255\versionadded{2.3}
256\end{methoddesc}