blob: 50af056bdd71b7ade88fb69394261ee11bc0e28d [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.}
Fred Drake38e5d272000-04-03 20:13:55 +00006\moduleauthor{Ken Manheimer}{klm@digicool.com}
7\moduleauthor{Barry Warsaw}{bwarsaw@python.org}
Fred Drakebc866ce1999-01-26 15:47:59 +00008\sectionauthor{Christopher G. Petrilli}{petrilli@amber.org}
9
Fred Drake1e440291999-06-15 17:30:59 +000010This module defines the class \class{ConfigParser}.
11\indexii{.ini}{file}\indexii{configuration}{file}\index{ini file}
12\index{Windows ini file}
13The \class{ConfigParser} class implements a basic configuration file
Fred Drakebc866ce1999-01-26 15:47:59 +000014parser language which provides a structure similar to what you would
Fred Drake1e440291999-06-15 17:30:59 +000015find on Microsoft Windows INI files. You can use this to write Python
Fred Drakebc866ce1999-01-26 15:47:59 +000016programs which can be customized by end users easily.
17
18The configuration file consists of sections, lead by a
19\samp{[section]} header and followed by \samp{name: value} entries,
Fred Drake1e440291999-06-15 17:30:59 +000020with continuations in the style of \rfc{822}; \samp{name=value} is
Fred Drake38e5d272000-04-03 20:13:55 +000021also accepted. Note that leading whitespace is removed from values.
22The optional values can contain format strings which refer to other
23values in the same section, or values in a special
Fred Drake1e440291999-06-15 17:30:59 +000024\code{DEFAULT} section. Additional defaults can be provided upon
Fred Drake38e5d272000-04-03 20:13:55 +000025initialization and retrieval. Lines beginning with \character{\#} or
26\character{;} are ignored and may be used to provide comments.
Fred Drakebc866ce1999-01-26 15:47:59 +000027
28For example:
29
30\begin{verbatim}
31foodir: %(dir)s/whatever
Fred Drake38e5d272000-04-03 20:13:55 +000032dir=frob
Fred Drakebc866ce1999-01-26 15:47:59 +000033\end{verbatim}
34
Fred Drake38e5d272000-04-03 20:13:55 +000035would resolve the \samp{\%(dir)s} to the value of
36\samp{dir} (\samp{frob} in this case). All reference expansions are
37done on demand.
Fred Drakebc866ce1999-01-26 15:47:59 +000038
Fred Drake38e5d272000-04-03 20:13:55 +000039Default values can be specified by passing them into the
40\class{ConfigParser} constructor as a dictionary. Additional defaults
41may be passed into the \method{get()} method which will override all
Fred Drakeebe2a121999-01-26 21:49:05 +000042others.
Fred Drakebc866ce1999-01-26 15:47:59 +000043
44\begin{classdesc}{ConfigParser}{\optional{defaults}}
45Return a new instance of the \class{ConfigParser} class. When
46\var{defaults} is given, it is initialized into the dictionairy of
47intrinsic defaults. They keys must be strings, and the values must be
48appropriate for the \samp{\%()s} string interpolation. Note that
Fred Drake184e8361999-05-11 15:14:15 +000049\var{__name__} is always an intrinsic default; its value is the
Fred Drakebc866ce1999-01-26 15:47:59 +000050section name.
51\end{classdesc}
52
53\begin{excdesc}{NoSectionError}
54Exception raised when a specified section is not found.
55\end{excdesc}
56
57\begin{excdesc}{DuplicateSectionError}
Fred Drake38e5d272000-04-03 20:13:55 +000058Exception raised when mutliple sections with the same name are found,
59or if \method{add_section()} is called with the name of a section that
60is already present.
Fred Drakebc866ce1999-01-26 15:47:59 +000061\end{excdesc}
62
63\begin{excdesc}{NoOptionError}
64Exception raised when a specified option is not found in the specified
65section.
66\end{excdesc}
67
68\begin{excdesc}{InterpolationError}
69Exception raised when problems occur performing string interpolation.
70\end{excdesc}
71
72\begin{excdesc}{MissingSectionHeaderError}
73Exception raised when attempting to parse a file which has no section
74headers.
75\end{excdesc}
76
77\begin{excdesc}{ParsingError}
78Exception raised when errors occur attempting to parse a file.
79\end{excdesc}
80
Fred Drakeebe2a121999-01-26 21:49:05 +000081
Fred Drake184e8361999-05-11 15:14:15 +000082\begin{seealso}
83 \seemodule{shlex}{Support for a creating \UNIX{} shell-like
84 minilanguages which can be used as an alternate format
85 for application configuration files.}
86\end{seealso}
87
Fred Drakebc866ce1999-01-26 15:47:59 +000088\subsection{ConfigParser Objects \label{ConfigParser-objects}}
89
90\class{ConfigParser} instances have the following methods:
91
92\begin{methoddesc}{defaults}{}
93Return a dictionairy containing the instance-wide defaults.
94\end{methoddesc}
95
96\begin{methoddesc}{sections}{}
Fred Drake38e5d272000-04-03 20:13:55 +000097Return a list of the sections available; \code{DEFAULT} is not
98included in the list.
99\end{methoddesc}
100
101\begin{methoddesc}{add_section}{section}
102Add a section named \var{section} to the instance. If a section by
103the given name already exists, \exception{DuplicateSectionError} is
104raised.
Fred Drakebc866ce1999-01-26 15:47:59 +0000105\end{methoddesc}
106
107\begin{methoddesc}{has_section}{section}
108Indicates whether the named section is present in the
109configuration. The \code{DEFAULT} section is not acknowledged.
110\end{methoddesc}
111
112\begin{methoddesc}{options}{section}
113Returns a list of options available in the specified \var{section}.
114\end{methoddesc}
115
116\begin{methoddesc}{read}{filenames}
117Read and parse a list of filenames.
118\end{methoddesc}
119
Fred Drakeebe2a121999-01-26 21:49:05 +0000120\begin{methoddesc}{get}{section, option\optional{, raw\optional{, vars}}}
Fred Drakebc866ce1999-01-26 15:47:59 +0000121Get an \var{option} value for the provided \var{section}. All the
Fred Drake4747f7f1999-04-21 16:41:40 +0000122\character{\%} interpolations are expanded in the return values, based on
Fred Drakeebe2a121999-01-26 21:49:05 +0000123the defaults passed into the constructor, as well as the options
124\var{vars} provided, unless the \var{raw} argument is true.
Fred Drakebc866ce1999-01-26 15:47:59 +0000125\end{methoddesc}
126
127\begin{methoddesc}{getint}{section, option}
128A convenience method which coerces the \var{option} in the specified
129\var{section} to an integer.
130\end{methoddesc}
131
132\begin{methoddesc}{getfloat}{section, option}
133A convenience method which coerces the \var{option} in the specified
134\var{section} to a floating point number.
135\end{methoddesc}
136
137\begin{methoddesc}{getboolean}{section, option}
138A convenience method which coerces the \var{option} in the specified
139\var{section} to a boolean value. Note that the only accepted values
Fred Drake38e5d272000-04-03 20:13:55 +0000140for the option are \samp{0} and \samp{1}, any others will raise
Fred Drakebc866ce1999-01-26 15:47:59 +0000141\exception{ValueError}.
142\end{methoddesc}