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