blob: 805767d1fe6db7e4cae1371e39ca4c6747512d2b [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 Drake1e440291999-06-15 17:30:59 +000025\code{DEFAULT} section. Additional defaults can be provided upon
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}
32foodir: %(dir)s/whatever
Fred Drake38e5d272000-04-03 20:13:55 +000033dir=frob
Fred Drakebc866ce1999-01-26 15:47:59 +000034\end{verbatim}
35
Fred Drake38e5d272000-04-03 20:13:55 +000036would resolve the \samp{\%(dir)s} to the value of
37\samp{dir} (\samp{frob} in this case). All reference expansions are
38done on demand.
Fred Drakebc866ce1999-01-26 15:47:59 +000039
Fred Drake38e5d272000-04-03 20:13:55 +000040Default values can be specified by passing them into the
41\class{ConfigParser} constructor as a dictionary. Additional defaults
42may be passed into the \method{get()} method which will override all
Fred Drakeebe2a121999-01-26 21:49:05 +000043others.
Fred Drakebc866ce1999-01-26 15:47:59 +000044
45\begin{classdesc}{ConfigParser}{\optional{defaults}}
46Return a new instance of the \class{ConfigParser} class. When
Fred Drake7cb42cd2000-05-23 02:28:26 +000047\var{defaults} is given, it is initialized into the dictionary of
Fred Drakebc866ce1999-01-26 15:47:59 +000048intrinsic defaults. They keys must be strings, and the values must be
49appropriate for the \samp{\%()s} string interpolation. Note that
Fred Drake184e8361999-05-11 15:14:15 +000050\var{__name__} is always an intrinsic default; its value is the
Fred Drakebc866ce1999-01-26 15:47:59 +000051section name.
52\end{classdesc}
53
54\begin{excdesc}{NoSectionError}
55Exception raised when a specified section is not found.
56\end{excdesc}
57
58\begin{excdesc}{DuplicateSectionError}
Fred Drake38e5d272000-04-03 20:13:55 +000059Exception raised when mutliple sections with the same name are found,
60or if \method{add_section()} is called with the name of a section that
61is already present.
Fred Drakebc866ce1999-01-26 15:47:59 +000062\end{excdesc}
63
64\begin{excdesc}{NoOptionError}
65Exception raised when a specified option is not found in the specified
66section.
67\end{excdesc}
68
69\begin{excdesc}{InterpolationError}
70Exception raised when problems occur performing string interpolation.
71\end{excdesc}
72
73\begin{excdesc}{MissingSectionHeaderError}
74Exception raised when attempting to parse a file which has no section
75headers.
76\end{excdesc}
77
78\begin{excdesc}{ParsingError}
79Exception raised when errors occur attempting to parse a file.
80\end{excdesc}
81
Fred Drakeebe2a121999-01-26 21:49:05 +000082
Fred Drake184e8361999-05-11 15:14:15 +000083\begin{seealso}
84 \seemodule{shlex}{Support for a creating \UNIX{} shell-like
85 minilanguages which can be used as an alternate format
86 for application configuration files.}
87\end{seealso}
88
Fred Drakebc866ce1999-01-26 15:47:59 +000089\subsection{ConfigParser Objects \label{ConfigParser-objects}}
90
91\class{ConfigParser} instances have the following methods:
92
93\begin{methoddesc}{defaults}{}
Fred Drake7cb42cd2000-05-23 02:28:26 +000094Return a dictionary containing the instance-wide defaults.
Fred Drakebc866ce1999-01-26 15:47:59 +000095\end{methoddesc}
96
97\begin{methoddesc}{sections}{}
Fred Drake38e5d272000-04-03 20:13:55 +000098Return a list of the sections available; \code{DEFAULT} is not
99included in the list.
100\end{methoddesc}
101
102\begin{methoddesc}{add_section}{section}
103Add a section named \var{section} to the instance. If a section by
104the given name already exists, \exception{DuplicateSectionError} is
105raised.
Fred Drakebc866ce1999-01-26 15:47:59 +0000106\end{methoddesc}
107
108\begin{methoddesc}{has_section}{section}
109Indicates whether the named section is present in the
110configuration. The \code{DEFAULT} section is not acknowledged.
111\end{methoddesc}
112
113\begin{methoddesc}{options}{section}
114Returns a list of options available in the specified \var{section}.
115\end{methoddesc}
116
Eric S. Raymond417c4892000-07-10 18:11:00 +0000117\begin{methoddesc}{has_option}{section, option}
118If the given section exists, and contains the given option. return 1;
119otherwise return 0. (New in 1.6)
120\end{methoddesc}
121
Fred Drakebc866ce1999-01-26 15:47:59 +0000122\begin{methoddesc}{read}{filenames}
Fred Draked85f0592000-05-09 15:06:32 +0000123Read and parse a list of filenames. If \var{filenames} is a string or
124Unicode string, it is treated as a single filename.
125\end{methoddesc}
126
127\begin{methoddesc}{readfp}{fp\optional{, filename}}
128Read and parse configuration data from the file or file-like object in
129\var{fp} (only the \method{readline()} method is used). If
130\var{filename} is omitted and \var{fp} has a \member{name} attribute,
131that is used for \var{filename}; the default is \samp{<???>}.
Fred Drakebc866ce1999-01-26 15:47:59 +0000132\end{methoddesc}
133
Fred Drakeebe2a121999-01-26 21:49:05 +0000134\begin{methoddesc}{get}{section, option\optional{, raw\optional{, vars}}}
Fred Drakebc866ce1999-01-26 15:47:59 +0000135Get an \var{option} value for the provided \var{section}. All the
Fred Drake4747f7f1999-04-21 16:41:40 +0000136\character{\%} interpolations are expanded in the return values, based on
Fred Drakeebe2a121999-01-26 21:49:05 +0000137the defaults passed into the constructor, as well as the options
138\var{vars} provided, unless the \var{raw} argument is true.
Fred Drakebc866ce1999-01-26 15:47:59 +0000139\end{methoddesc}
140
141\begin{methoddesc}{getint}{section, option}
142A convenience method which coerces the \var{option} in the specified
143\var{section} to an integer.
144\end{methoddesc}
145
146\begin{methoddesc}{getfloat}{section, option}
147A convenience method which coerces the \var{option} in the specified
148\var{section} to a floating point number.
149\end{methoddesc}
150
151\begin{methoddesc}{getboolean}{section, option}
152A convenience method which coerces the \var{option} in the specified
153\var{section} to a boolean value. Note that the only accepted values
Fred Drake38e5d272000-04-03 20:13:55 +0000154for the option are \samp{0} and \samp{1}, any others will raise
Fred Drakebc866ce1999-01-26 15:47:59 +0000155\exception{ValueError}.
156\end{methoddesc}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000157
158\begin{methoddesc}{set}{section, option, value}
159If the given section exists, set the given option to the specified value;
160otherwise raise \exception{NoSectionError}. (New in 1.6)
161\end{methoddesc}
162
Eric S. Raymondf868de62000-07-14 15:00:02 +0000163\begin{methoddesc}{write}{fileobject}
Eric S. Raymond417c4892000-07-10 18:11:00 +0000164Write a representation of the configuration to the specified file
165object. This representation can be parsed by a future \method{read()}
166call. (New in 1.6)
167\end{methoddesc}
Eric S. Raymondf868de62000-07-14 15:00:02 +0000168
169\begin{methoddesc}{remove_option}{section, option}
170Remove the specified \var{option} from the specified \var{section}.
171If the section does not exist, raise \exception{NoSectionError}.
172If the option existed to be removed, return 1; otherwise return 0.
173(New in 1.6)
174\end{methoddesc}
175
176\begin{methoddesc}{remove_section}{section}
177Remove the specified \var{section} from the configuration.
178If the section in fact existed, return 1. Otherwise return 0.
179\end{methoddesc}
180