blob: fa76b22f2d1552d9e9cc3c063a7c207074453da6 [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
Fred Drake1e440291999-06-15 17:30:59 +00008This module defines the class \class{ConfigParser}.
9\indexii{.ini}{file}\indexii{configuration}{file}\index{ini file}
10\index{Windows ini file}
11The \class{ConfigParser} class implements a basic configuration file
Fred Drakebc866ce1999-01-26 15:47:59 +000012parser language which provides a structure similar to what you would
Fred Drake1e440291999-06-15 17:30:59 +000013find on Microsoft Windows INI files. You can use this to write Python
Fred Drakebc866ce1999-01-26 15:47:59 +000014programs which can be customized by end users easily.
15
16The configuration file consists of sections, lead by a
17\samp{[section]} header and followed by \samp{name: value} entries,
Fred Drake1e440291999-06-15 17:30:59 +000018with continuations in the style of \rfc{822}; \samp{name=value} is
19also accepted. The optional values can contain format strings which
20refer to other values in the same section, or values in a special
21\code{DEFAULT} section. Additional defaults can be provided upon
22initialization and retrieval. Lines beginning with \character{\#} are
23ignored and may be used to provide comments.
Fred Drakebc866ce1999-01-26 15:47:59 +000024
25For example:
26
27\begin{verbatim}
28foodir: %(dir)s/whatever
29\end{verbatim}
30
31would resolve the \samp{\%(dir)s} to the value of dir. All reference
32expansions are done late, on demand.
33
34Intrinsic defaults can be specified by passing them into the
Fred Drakeebe2a121999-01-26 21:49:05 +000035\class{ConfigParser} constructor as a dictionary. Additional defaults
36may be passed into the \method{get} method which will override all
37others.
Fred Drakebc866ce1999-01-26 15:47:59 +000038
39\begin{classdesc}{ConfigParser}{\optional{defaults}}
40Return a new instance of the \class{ConfigParser} class. When
41\var{defaults} is given, it is initialized into the dictionairy of
42intrinsic defaults. They keys must be strings, and the values must be
43appropriate for the \samp{\%()s} string interpolation. Note that
Fred Drake184e8361999-05-11 15:14:15 +000044\var{__name__} is always an intrinsic default; its value is the
Fred Drakebc866ce1999-01-26 15:47:59 +000045section name.
46\end{classdesc}
47
48\begin{excdesc}{NoSectionError}
49Exception raised when a specified section is not found.
50\end{excdesc}
51
52\begin{excdesc}{DuplicateSectionError}
53Exception raised when mutliple sections with the same name are found.
54\end{excdesc}
55
56\begin{excdesc}{NoOptionError}
57Exception raised when a specified option is not found in the specified
58section.
59\end{excdesc}
60
61\begin{excdesc}{InterpolationError}
62Exception raised when problems occur performing string interpolation.
63\end{excdesc}
64
65\begin{excdesc}{MissingSectionHeaderError}
66Exception raised when attempting to parse a file which has no section
67headers.
68\end{excdesc}
69
70\begin{excdesc}{ParsingError}
71Exception raised when errors occur attempting to parse a file.
72\end{excdesc}
73
Fred Drakeebe2a121999-01-26 21:49:05 +000074
Fred Drake184e8361999-05-11 15:14:15 +000075\begin{seealso}
76 \seemodule{shlex}{Support for a creating \UNIX{} shell-like
77 minilanguages which can be used as an alternate format
78 for application configuration files.}
79\end{seealso}
80
Fred Drakebc866ce1999-01-26 15:47:59 +000081\subsection{ConfigParser Objects \label{ConfigParser-objects}}
82
83\class{ConfigParser} instances have the following methods:
84
85\begin{methoddesc}{defaults}{}
86Return a dictionairy containing the instance-wide defaults.
87\end{methoddesc}
88
89\begin{methoddesc}{sections}{}
90Return a list of the sections available.
91\end{methoddesc}
92
93\begin{methoddesc}{has_section}{section}
94Indicates whether the named section is present in the
95configuration. The \code{DEFAULT} section is not acknowledged.
96\end{methoddesc}
97
98\begin{methoddesc}{options}{section}
99Returns a list of options available in the specified \var{section}.
100\end{methoddesc}
101
102\begin{methoddesc}{read}{filenames}
103Read and parse a list of filenames.
104\end{methoddesc}
105
Fred Drakeebe2a121999-01-26 21:49:05 +0000106\begin{methoddesc}{get}{section, option\optional{, raw\optional{, vars}}}
Fred Drakebc866ce1999-01-26 15:47:59 +0000107Get an \var{option} value for the provided \var{section}. All the
Fred Drake4747f7f1999-04-21 16:41:40 +0000108\character{\%} interpolations are expanded in the return values, based on
Fred Drakeebe2a121999-01-26 21:49:05 +0000109the defaults passed into the constructor, as well as the options
110\var{vars} provided, unless the \var{raw} argument is true.
Fred Drakebc866ce1999-01-26 15:47:59 +0000111\end{methoddesc}
112
113\begin{methoddesc}{getint}{section, option}
114A convenience method which coerces the \var{option} in the specified
115\var{section} to an integer.
116\end{methoddesc}
117
118\begin{methoddesc}{getfloat}{section, option}
119A convenience method which coerces the \var{option} in the specified
120\var{section} to a floating point number.
121\end{methoddesc}
122
123\begin{methoddesc}{getboolean}{section, option}
124A convenience method which coerces the \var{option} in the specified
125\var{section} to a boolean value. Note that the only accepted values
126for the option are \code{0} and \code{1}, any others will raise
127\exception{ValueError}.
128\end{methoddesc}