blob: b203168dde771232265a4608cb8e7ff821a4f646 [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.}
6\sectionauthor{Christopher G. Petrilli}{petrilli@amber.org}
7
8This module defines the class \class{ConfigParser}. The
9\class{ConfigParser} class implements a basic configuration file
10parser language which provides a structure similar to what you would
11find on Microsoft Windows INI files. You can use this to write Python
12programs which can be customized by end users easily.
13
14The configuration file consists of sections, lead by a
15\samp{[section]} header and followed by \samp{name: value} entries,
Fred Drake00a3a651999-02-22 17:12:42 +000016with continuations in the style of \rfc{822}. The optional values
Fred Drakebc866ce1999-01-26 15:47:59 +000017can contain format strings which refer to other values in the same
18section, or values in a special \code{DEFAULT} section. Additional
Fred Drake51027c41999-02-24 22:36:44 +000019defaults can be provided upon initialization and retrieval. Lines
20beginning with \character{\#} are ignored and may be used to provide
21comments.
Fred Drakebc866ce1999-01-26 15:47:59 +000022
23For example:
24
25\begin{verbatim}
26foodir: %(dir)s/whatever
27\end{verbatim}
28
29would resolve the \samp{\%(dir)s} to the value of dir. All reference
30expansions are done late, on demand.
31
32Intrinsic defaults can be specified by passing them into the
Fred Drakeebe2a121999-01-26 21:49:05 +000033\class{ConfigParser} constructor as a dictionary. Additional defaults
34may be passed into the \method{get} method which will override all
35others.
Fred Drakebc866ce1999-01-26 15:47:59 +000036
37\begin{classdesc}{ConfigParser}{\optional{defaults}}
38Return a new instance of the \class{ConfigParser} class. When
39\var{defaults} is given, it is initialized into the dictionairy of
40intrinsic defaults. They keys must be strings, and the values must be
41appropriate for the \samp{\%()s} string interpolation. Note that
42\var{__name__} is always an intrinsic default; it's value is the
43section name.
44\end{classdesc}
45
46\begin{excdesc}{NoSectionError}
47Exception raised when a specified section is not found.
48\end{excdesc}
49
50\begin{excdesc}{DuplicateSectionError}
51Exception raised when mutliple sections with the same name are found.
52\end{excdesc}
53
54\begin{excdesc}{NoOptionError}
55Exception raised when a specified option is not found in the specified
56section.
57\end{excdesc}
58
59\begin{excdesc}{InterpolationError}
60Exception raised when problems occur performing string interpolation.
61\end{excdesc}
62
63\begin{excdesc}{MissingSectionHeaderError}
64Exception raised when attempting to parse a file which has no section
65headers.
66\end{excdesc}
67
68\begin{excdesc}{ParsingError}
69Exception raised when errors occur attempting to parse a file.
70\end{excdesc}
71
Fred Drakeebe2a121999-01-26 21:49:05 +000072
Fred Drakebc866ce1999-01-26 15:47:59 +000073\subsection{ConfigParser Objects \label{ConfigParser-objects}}
74
75\class{ConfigParser} instances have the following methods:
76
77\begin{methoddesc}{defaults}{}
78Return a dictionairy containing the instance-wide defaults.
79\end{methoddesc}
80
81\begin{methoddesc}{sections}{}
82Return a list of the sections available.
83\end{methoddesc}
84
85\begin{methoddesc}{has_section}{section}
86Indicates whether the named section is present in the
87configuration. The \code{DEFAULT} section is not acknowledged.
88\end{methoddesc}
89
90\begin{methoddesc}{options}{section}
91Returns a list of options available in the specified \var{section}.
92\end{methoddesc}
93
94\begin{methoddesc}{read}{filenames}
95Read and parse a list of filenames.
96\end{methoddesc}
97
Fred Drakeebe2a121999-01-26 21:49:05 +000098\begin{methoddesc}{get}{section, option\optional{, raw\optional{, vars}}}
Fred Drakebc866ce1999-01-26 15:47:59 +000099Get an \var{option} value for the provided \var{section}. All the
Fred Drake4747f7f1999-04-21 16:41:40 +0000100\character{\%} interpolations are expanded in the return values, based on
Fred Drakeebe2a121999-01-26 21:49:05 +0000101the defaults passed into the constructor, as well as the options
102\var{vars} provided, unless the \var{raw} argument is true.
Fred Drakebc866ce1999-01-26 15:47:59 +0000103\end{methoddesc}
104
105\begin{methoddesc}{getint}{section, option}
106A convenience method which coerces the \var{option} in the specified
107\var{section} to an integer.
108\end{methoddesc}
109
110\begin{methoddesc}{getfloat}{section, option}
111A convenience method which coerces the \var{option} in the specified
112\var{section} to a floating point number.
113\end{methoddesc}
114
115\begin{methoddesc}{getboolean}{section, option}
116A convenience method which coerces the \var{option} in the specified
117\var{section} to a boolean value. Note that the only accepted values
118for the option are \code{0} and \code{1}, any others will raise
119\exception{ValueError}.
120\end{methoddesc}