Files for 2.1b1 distribution.
[python/dscho.git] / Doc / lib / libcfgparser.tex
blob65c27ce203e97eea47eb02d707481cecdd0eaeee
1 \section{\module{ConfigParser} ---
2 Configuration file parser}
4 \declaremodule{standard}{ConfigParser}
5 \modulesynopsis{Configuration file parser.}
6 \moduleauthor{Ken Manheimer}{klm@digicool.com}
7 \moduleauthor{Barry Warsaw}{bwarsaw@python.org}
8 \moduleauthor{Eric S. Raymond}{esr@thyrsus.com}
9 \sectionauthor{Christopher G. Petrilli}{petrilli@amber.org}
11 This module defines the class \class{ConfigParser}.
12 \indexii{.ini}{file}\indexii{configuration}{file}\index{ini file}
13 \index{Windows ini file}
14 The \class{ConfigParser} class implements a basic configuration file
15 parser language which provides a structure similar to what you would
16 find on Microsoft Windows INI files. You can use this to write Python
17 programs which can be customized by end users easily.
19 The configuration file consists of sections, lead by a
20 \samp{[section]} header and followed by \samp{name: value} entries,
21 with continuations in the style of \rfc{822}; \samp{name=value} is
22 also accepted. Note that leading whitespace is removed from values.
23 The optional values can contain format strings which refer to other
24 values in the same section, or values in a special
25 \code{DEFAULT} section. Additional defaults can be provided upon
26 initialization and retrieval. Lines beginning with \character{\#} or
27 \character{;} are ignored and may be used to provide comments.
29 For example:
31 \begin{verbatim}
32 foodir: %(dir)s/whatever
33 dir=frob
34 \end{verbatim}
36 would resolve the \samp{\%(dir)s} to the value of
37 \samp{dir} (\samp{frob} in this case). All reference expansions are
38 done on demand.
40 Default values can be specified by passing them into the
41 \class{ConfigParser} constructor as a dictionary. Additional defaults
42 may be passed into the \method{get()} method which will override all
43 others.
45 \begin{classdesc}{ConfigParser}{\optional{defaults}}
46 Return a new instance of the \class{ConfigParser} class. When
47 \var{defaults} is given, it is initialized into the dictionary of
48 intrinsic defaults. The keys must be strings, and the values must be
49 appropriate for the \samp{\%()s} string interpolation. Note that
50 \var{__name__} is an intrinsic default; its value is the section name,
51 and will override any value provided in \var{defaults}.
52 \end{classdesc}
54 \begin{excdesc}{NoSectionError}
55 Exception raised when a specified section is not found.
56 \end{excdesc}
58 \begin{excdesc}{DuplicateSectionError}
59 Exception raised when multiple sections with the same name are found,
60 or if \method{add_section()} is called with the name of a section that
61 is already present.
62 \end{excdesc}
64 \begin{excdesc}{NoOptionError}
65 Exception raised when a specified option is not found in the specified
66 section.
67 \end{excdesc}
69 \begin{excdesc}{InterpolationError}
70 Exception raised when problems occur performing string interpolation.
71 \end{excdesc}
73 \begin{excdesc}{InterpolationDepthError}
74 Exception raised when string interpolation cannot be completed because
75 the number of iterations exceeds \constant{MAX_INTERPOLATION_DEPTH}.
76 \end{excdesc}
78 \begin{excdesc}{MissingSectionHeaderError}
79 Exception raised when attempting to parse a file which has no section
80 headers.
81 \end{excdesc}
83 \begin{excdesc}{ParsingError}
84 Exception raised when errors occur attempting to parse a file.
85 \end{excdesc}
87 \begin{datadesc}{MAX_INTERPOLATION_DEPTH}
88 The maximum depth for recursive interpolation for \method{get()} when
89 the \var{raw} parameter is false. Setting this does not change the
90 allowed recursion depth.
91 \end{datadesc}
94 \begin{seealso}
95 \seemodule{shlex}{Support for a creating \UNIX{} shell-like
96 minilanguages which can be used as an alternate format
97 for application configuration files.}
98 \end{seealso}
101 \subsection{ConfigParser Objects \label{ConfigParser-objects}}
103 \class{ConfigParser} instances have the following methods:
105 \begin{methoddesc}{defaults}{}
106 Return a dictionary containing the instance-wide defaults.
107 \end{methoddesc}
109 \begin{methoddesc}{sections}{}
110 Return a list of the sections available; \code{DEFAULT} is not
111 included in the list.
112 \end{methoddesc}
114 \begin{methoddesc}{add_section}{section}
115 Add a section named \var{section} to the instance. If a section by
116 the given name already exists, \exception{DuplicateSectionError} is
117 raised.
118 \end{methoddesc}
120 \begin{methoddesc}{has_section}{section}
121 Indicates whether the named section is present in the
122 configuration. The \code{DEFAULT} section is not acknowledged.
123 \end{methoddesc}
125 \begin{methoddesc}{options}{section}
126 Returns a list of options available in the specified \var{section}.
127 \end{methoddesc}
129 \begin{methoddesc}{has_option}{section, option}
130 If the given section exists, and contains the given option. return 1;
131 otherwise return 0. (New in 1.6)
132 \end{methoddesc}
134 \begin{methoddesc}{read}{filenames}
135 Read and parse a list of filenames. If \var{filenames} is a string or
136 Unicode string, it is treated as a single filename.
137 \end{methoddesc}
139 \begin{methoddesc}{readfp}{fp\optional{, filename}}
140 Read and parse configuration data from the file or file-like object in
141 \var{fp} (only the \method{readline()} method is used). If
142 \var{filename} is omitted and \var{fp} has a \member{name} attribute,
143 that is used for \var{filename}; the default is \samp{<???>}.
144 \end{methoddesc}
146 \begin{methoddesc}{get}{section, option\optional{, raw\optional{, vars}}}
147 Get an \var{option} value for the provided \var{section}. All the
148 \character{\%} interpolations are expanded in the return values, based on
149 the defaults passed into the constructor, as well as the options
150 \var{vars} provided, unless the \var{raw} argument is true.
151 \end{methoddesc}
153 \begin{methoddesc}{getint}{section, option}
154 A convenience method which coerces the \var{option} in the specified
155 \var{section} to an integer.
156 \end{methoddesc}
158 \begin{methoddesc}{getfloat}{section, option}
159 A convenience method which coerces the \var{option} in the specified
160 \var{section} to a floating point number.
161 \end{methoddesc}
163 \begin{methoddesc}{getboolean}{section, option}
164 A convenience method which coerces the \var{option} in the specified
165 \var{section} to a boolean value. Note that the only accepted values
166 for the option are \samp{0} and \samp{1}, any others will raise
167 \exception{ValueError}.
168 \end{methoddesc}
170 \begin{methoddesc}{set}{section, option, value}
171 If the given section exists, set the given option to the specified value;
172 otherwise raise \exception{NoSectionError}. (New in 1.6)
173 \end{methoddesc}
175 \begin{methoddesc}{write}{fileobject}
176 Write a representation of the configuration to the specified file
177 object. This representation can be parsed by a future \method{read()}
178 call. (New in 1.6)
179 \end{methoddesc}
181 \begin{methoddesc}{remove_option}{section, option}
182 Remove the specified \var{option} from the specified \var{section}.
183 If the section does not exist, raise \exception{NoSectionError}.
184 If the option existed to be removed, return 1; otherwise return 0.
185 (New in 1.6)
186 \end{methoddesc}
188 \begin{methoddesc}{remove_section}{section}
189 Remove the specified \var{section} from the configuration.
190 If the section in fact existed, return 1. Otherwise return 0.
191 \end{methoddesc}
193 \begin{methoddesc}{optionxform}{option}
194 Transforms the option name \var{option} as found in an input file or
195 as passed in by client code to the form that should be used in the
196 internal structures. The default implementation returns a lower-case
197 version of \var{option}; subclasses may override this or client code
198 can set an attribute of this name on instances to affect this
199 behavior. Setting this to \function{str()}, for example, would make
200 option names case sensitive.
201 \end{methoddesc}