blob: be65f6291108b98aa66115b07b77b861d75509d9 [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 Drake51027c41999-02-24 22:36:44 +000020defaults can be provided upon initialization and retrieval. Lines
21beginning with \character{\#} are ignored and may be used to provide
22comments.
Fred Drakebc866ce1999-01-26 15:47:59 +000023
24For example:
25
26\begin{verbatim}
27foodir: %(dir)s/whatever
28\end{verbatim}
29
30would resolve the \samp{\%(dir)s} to the value of dir. All reference
31expansions are done late, on demand.
32
33Intrinsic defaults can be specified by passing them into the
Fred Drakeebe2a121999-01-26 21:49:05 +000034\class{ConfigParser} constructor as a dictionary. Additional defaults
35may be passed into the \method{get} method which will override all
36others.
Fred Drakebc866ce1999-01-26 15:47:59 +000037
38\begin{classdesc}{ConfigParser}{\optional{defaults}}
39Return a new instance of the \class{ConfigParser} class. When
40\var{defaults} is given, it is initialized into the dictionairy of
41intrinsic defaults. They keys must be strings, and the values must be
42appropriate for the \samp{\%()s} string interpolation. Note that
43\var{__name__} is always an intrinsic default; it's value is the
44section name.
45\end{classdesc}
46
47\begin{excdesc}{NoSectionError}
48Exception raised when a specified section is not found.
49\end{excdesc}
50
51\begin{excdesc}{DuplicateSectionError}
52Exception raised when mutliple sections with the same name are found.
53\end{excdesc}
54
55\begin{excdesc}{NoOptionError}
56Exception raised when a specified option is not found in the specified
57section.
58\end{excdesc}
59
60\begin{excdesc}{InterpolationError}
61Exception raised when problems occur performing string interpolation.
62\end{excdesc}
63
64\begin{excdesc}{MissingSectionHeaderError}
65Exception raised when attempting to parse a file which has no section
66headers.
67\end{excdesc}
68
69\begin{excdesc}{ParsingError}
70Exception raised when errors occur attempting to parse a file.
71\end{excdesc}
72
Fred Drakeebe2a121999-01-26 21:49:05 +000073
Fred Drakebc866ce1999-01-26 15:47:59 +000074\subsection{ConfigParser Objects \label{ConfigParser-objects}}
75
76\class{ConfigParser} instances have the following methods:
77
78\begin{methoddesc}{defaults}{}
79Return a dictionairy containing the instance-wide defaults.
80\end{methoddesc}
81
82\begin{methoddesc}{sections}{}
83Return a list of the sections available.
84\end{methoddesc}
85
86\begin{methoddesc}{has_section}{section}
87Indicates whether the named section is present in the
88configuration. The \code{DEFAULT} section is not acknowledged.
89\end{methoddesc}
90
91\begin{methoddesc}{options}{section}
92Returns a list of options available in the specified \var{section}.
93\end{methoddesc}
94
95\begin{methoddesc}{read}{filenames}
96Read and parse a list of filenames.
97\end{methoddesc}
98
Fred Drakeebe2a121999-01-26 21:49:05 +000099\begin{methoddesc}{get}{section, option\optional{, raw\optional{, vars}}}
Fred Drakebc866ce1999-01-26 15:47:59 +0000100Get an \var{option} value for the provided \var{section}. All the
101\samp{\%} interpolations are expanded in the return values, based on
Fred Drakeebe2a121999-01-26 21:49:05 +0000102the defaults passed into the constructor, as well as the options
103\var{vars} provided, unless the \var{raw} argument is true.
Fred Drakebc866ce1999-01-26 15:47:59 +0000104\end{methoddesc}
105
106\begin{methoddesc}{getint}{section, option}
107A convenience method which coerces the \var{option} in the specified
108\var{section} to an integer.
109\end{methoddesc}
110
111\begin{methoddesc}{getfloat}{section, option}
112A convenience method which coerces the \var{option} in the specified
113\var{section} to a floating point number.
114\end{methoddesc}
115
116\begin{methoddesc}{getboolean}{section, option}
117A convenience method which coerces the \var{option} in the specified
118\var{section} to a boolean value. Note that the only accepted values
119for the option are \code{0} and \code{1}, any others will raise
120\exception{ValueError}.
121\end{methoddesc}