Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 1 | \section{\module{ConfigParser} --- |
Fred Drake | 4747f7f | 1999-04-21 16:41:40 +0000 | [diff] [blame] | 2 | Configuration file parser} |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 3 | |
| 4 | \declaremodule{standard}{ConfigParser} |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 5 | \modulesynopsis{Configuration file parser.} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 6 | \moduleauthor{Ken Manheimer}{klm@digicool.com} |
| 7 | \moduleauthor{Barry Warsaw}{bwarsaw@python.org} |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 8 | \moduleauthor{Eric S. Raymond}{esr@thyrsus.com} |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 9 | \sectionauthor{Christopher G. Petrilli}{petrilli@amber.org} |
| 10 | |
Fred Drake | 1e44029 | 1999-06-15 17:30:59 +0000 | [diff] [blame] | 11 | This module defines the class \class{ConfigParser}. |
| 12 | \indexii{.ini}{file}\indexii{configuration}{file}\index{ini file} |
| 13 | \index{Windows ini file} |
| 14 | The \class{ConfigParser} class implements a basic configuration file |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 15 | parser language which provides a structure similar to what you would |
Fred Drake | 1e44029 | 1999-06-15 17:30:59 +0000 | [diff] [blame] | 16 | find on Microsoft Windows INI files. You can use this to write Python |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 17 | programs which can be customized by end users easily. |
| 18 | |
| 19 | The configuration file consists of sections, lead by a |
| 20 | \samp{[section]} header and followed by \samp{name: value} entries, |
Fred Drake | 1e44029 | 1999-06-15 17:30:59 +0000 | [diff] [blame] | 21 | with continuations in the style of \rfc{822}; \samp{name=value} is |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 22 | also accepted. Note that leading whitespace is removed from values. |
| 23 | The optional values can contain format strings which refer to other |
| 24 | values in the same section, or values in a special |
Fred Drake | 97d5f05 | 2002-10-25 20:20:58 +0000 | [diff] [blame] | 25 | \code{DEFAULT} section. Additional defaults can be provided on |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 26 | initialization and retrieval. Lines beginning with \character{\#} or |
| 27 | \character{;} are ignored and may be used to provide comments. |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 28 | |
| 29 | For example: |
| 30 | |
| 31 | \begin{verbatim} |
Fred Drake | 97d5f05 | 2002-10-25 20:20:58 +0000 | [diff] [blame] | 32 | [My Section] |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 33 | foodir: %(dir)s/whatever |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 34 | dir=frob |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 35 | \end{verbatim} |
| 36 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 37 | would resolve the \samp{\%(dir)s} to the value of |
| 38 | \samp{dir} (\samp{frob} in this case). All reference expansions are |
| 39 | done on demand. |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 40 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 41 | Default values can be specified by passing them into the |
| 42 | \class{ConfigParser} constructor as a dictionary. Additional defaults |
| 43 | may be passed into the \method{get()} method which will override all |
Fred Drake | ebe2a12 | 1999-01-26 21:49:05 +0000 | [diff] [blame] | 44 | others. |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 45 | |
Fred Drake | 97d5f05 | 2002-10-25 20:20:58 +0000 | [diff] [blame] | 46 | \begin{classdesc}{RawConfigParser}{\optional{defaults}} |
| 47 | The basic configuration object. When \var{defaults} is given, it is |
| 48 | initialized into the dictionary of intrinsic defaults. This class |
| 49 | does not support the magical interpolation behavior. |
| 50 | \versionadded{2.3} |
| 51 | \end{classdesc} |
| 52 | |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 53 | \begin{classdesc}{ConfigParser}{\optional{defaults}} |
Fred Drake | 97d5f05 | 2002-10-25 20:20:58 +0000 | [diff] [blame] | 54 | Derived class of \class{RawConfigParser} that implements the magical |
| 55 | interpolation feature and adds optional arguments the \method{get()} |
| 56 | and \method{items()} methods. The values in \var{defaults} must be |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 57 | appropriate for the \samp{\%()s} string interpolation. Note that |
Fred Drake | 33dde92 | 2000-09-27 22:48:44 +0000 | [diff] [blame] | 58 | \var{__name__} is an intrinsic default; its value is the section name, |
| 59 | and will override any value provided in \var{defaults}. |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 60 | \end{classdesc} |
| 61 | |
Fred Drake | 0eebd5c | 2002-10-25 21:52:00 +0000 | [diff] [blame^] | 62 | \begin{classdesc}{SafeConfigParser}{\optional{defaults}} |
| 63 | Derived class of \class{ConfigParser} that implements a more-sane |
| 64 | variant of the magical interpolation feature. This implementation is |
| 65 | more predictable as well. |
| 66 | % XXX Need to explain what's safer/more predictable about it. |
| 67 | New applications should prefer this version if they don't need to be |
| 68 | compatible with older versions of Python. |
| 69 | \versionadded{2.3} |
| 70 | \end{classdesc} |
| 71 | |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 72 | \begin{excdesc}{NoSectionError} |
| 73 | Exception raised when a specified section is not found. |
| 74 | \end{excdesc} |
| 75 | |
| 76 | \begin{excdesc}{DuplicateSectionError} |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 77 | Exception raised when multiple sections with the same name are found, |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 78 | or if \method{add_section()} is called with the name of a section that |
| 79 | is already present. |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 80 | \end{excdesc} |
| 81 | |
| 82 | \begin{excdesc}{NoOptionError} |
| 83 | Exception raised when a specified option is not found in the specified |
| 84 | section. |
| 85 | \end{excdesc} |
| 86 | |
| 87 | \begin{excdesc}{InterpolationError} |
| 88 | Exception raised when problems occur performing string interpolation. |
| 89 | \end{excdesc} |
| 90 | |
Fred Drake | 33dde92 | 2000-09-27 22:48:44 +0000 | [diff] [blame] | 91 | \begin{excdesc}{InterpolationDepthError} |
| 92 | Exception raised when string interpolation cannot be completed because |
| 93 | the number of iterations exceeds \constant{MAX_INTERPOLATION_DEPTH}. |
| 94 | \end{excdesc} |
| 95 | |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 96 | \begin{excdesc}{MissingSectionHeaderError} |
| 97 | Exception raised when attempting to parse a file which has no section |
| 98 | headers. |
| 99 | \end{excdesc} |
| 100 | |
| 101 | \begin{excdesc}{ParsingError} |
| 102 | Exception raised when errors occur attempting to parse a file. |
| 103 | \end{excdesc} |
| 104 | |
Fred Drake | 33dde92 | 2000-09-27 22:48:44 +0000 | [diff] [blame] | 105 | \begin{datadesc}{MAX_INTERPOLATION_DEPTH} |
| 106 | The maximum depth for recursive interpolation for \method{get()} when |
Fred Drake | 97d5f05 | 2002-10-25 20:20:58 +0000 | [diff] [blame] | 107 | the \var{raw} parameter is false. This is relevant only for the |
| 108 | \class{ConfigParser} class. |
Fred Drake | 33dde92 | 2000-09-27 22:48:44 +0000 | [diff] [blame] | 109 | \end{datadesc} |
| 110 | |
Fred Drake | ebe2a12 | 1999-01-26 21:49:05 +0000 | [diff] [blame] | 111 | |
Fred Drake | 184e836 | 1999-05-11 15:14:15 +0000 | [diff] [blame] | 112 | \begin{seealso} |
| 113 | \seemodule{shlex}{Support for a creating \UNIX{} shell-like |
| 114 | minilanguages which can be used as an alternate format |
| 115 | for application configuration files.} |
| 116 | \end{seealso} |
| 117 | |
Fred Drake | 5b0705d | 2001-02-19 22:37:24 +0000 | [diff] [blame] | 118 | |
Fred Drake | 97d5f05 | 2002-10-25 20:20:58 +0000 | [diff] [blame] | 119 | \subsection{RawConfigParser Objects \label{RawConfigParser-objects}} |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 120 | |
Fred Drake | 97d5f05 | 2002-10-25 20:20:58 +0000 | [diff] [blame] | 121 | \class{RawConfigParser} instances have the following methods: |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 122 | |
| 123 | \begin{methoddesc}{defaults}{} |
Fred Drake | 7cb42cd | 2000-05-23 02:28:26 +0000 | [diff] [blame] | 124 | Return a dictionary containing the instance-wide defaults. |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 125 | \end{methoddesc} |
| 126 | |
| 127 | \begin{methoddesc}{sections}{} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 128 | Return a list of the sections available; \code{DEFAULT} is not |
| 129 | included in the list. |
| 130 | \end{methoddesc} |
| 131 | |
| 132 | \begin{methoddesc}{add_section}{section} |
| 133 | Add a section named \var{section} to the instance. If a section by |
| 134 | the given name already exists, \exception{DuplicateSectionError} is |
| 135 | raised. |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 136 | \end{methoddesc} |
| 137 | |
| 138 | \begin{methoddesc}{has_section}{section} |
| 139 | Indicates whether the named section is present in the |
| 140 | configuration. The \code{DEFAULT} section is not acknowledged. |
| 141 | \end{methoddesc} |
| 142 | |
| 143 | \begin{methoddesc}{options}{section} |
| 144 | Returns a list of options available in the specified \var{section}. |
| 145 | \end{methoddesc} |
| 146 | |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 147 | \begin{methoddesc}{has_option}{section, option} |
| 148 | If the given section exists, and contains the given option. return 1; |
Fred Drake | 3c10c68 | 2001-09-28 16:57:16 +0000 | [diff] [blame] | 149 | otherwise return 0. |
| 150 | \versionadded{1.6} |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 151 | \end{methoddesc} |
| 152 | |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 153 | \begin{methoddesc}{read}{filenames} |
Fred Drake | d85f059 | 2000-05-09 15:06:32 +0000 | [diff] [blame] | 154 | Read and parse a list of filenames. If \var{filenames} is a string or |
| 155 | Unicode string, it is treated as a single filename. |
Fred Drake | 8b7bb7a | 2001-12-07 21:35:57 +0000 | [diff] [blame] | 156 | If a file named in \var{filenames} cannot be opened, that file will be |
| 157 | ignored. This is designed so that you can specify a list of potential |
| 158 | configuration file locations (for example, the current directory, the |
| 159 | user's home directory, and some system-wide directory), and all |
| 160 | existing configuration files in the list will be read. If none of the |
| 161 | named files exist, the \class{ConfigParser} instance will contain an |
| 162 | empty dataset. An application which requires initial values to be |
| 163 | loaded from a file should load the required file or files using |
| 164 | \method{readfp()} before calling \method{read()} for any optional |
| 165 | files: |
| 166 | |
| 167 | \begin{verbatim} |
| 168 | import ConfigParser, os |
| 169 | |
| 170 | config = ConfigParser.ConfigParser() |
| 171 | config.readfp(open('defaults.cfg')) |
| 172 | config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')]) |
| 173 | \end{verbatim} |
Fred Drake | d85f059 | 2000-05-09 15:06:32 +0000 | [diff] [blame] | 174 | \end{methoddesc} |
| 175 | |
| 176 | \begin{methoddesc}{readfp}{fp\optional{, filename}} |
| 177 | Read and parse configuration data from the file or file-like object in |
| 178 | \var{fp} (only the \method{readline()} method is used). If |
| 179 | \var{filename} is omitted and \var{fp} has a \member{name} attribute, |
| 180 | that is used for \var{filename}; the default is \samp{<???>}. |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 181 | \end{methoddesc} |
| 182 | |
Fred Drake | 97d5f05 | 2002-10-25 20:20:58 +0000 | [diff] [blame] | 183 | \begin{methoddesc}{get}{section, option} |
| 184 | Get an \var{option} value for the named \var{section}. |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 185 | \end{methoddesc} |
| 186 | |
| 187 | \begin{methoddesc}{getint}{section, option} |
| 188 | A convenience method which coerces the \var{option} in the specified |
| 189 | \var{section} to an integer. |
| 190 | \end{methoddesc} |
| 191 | |
| 192 | \begin{methoddesc}{getfloat}{section, option} |
| 193 | A convenience method which coerces the \var{option} in the specified |
| 194 | \var{section} to a floating point number. |
| 195 | \end{methoddesc} |
| 196 | |
| 197 | \begin{methoddesc}{getboolean}{section, option} |
| 198 | A convenience method which coerces the \var{option} in the specified |
Fred Drake | b35f0ce | 2001-10-08 16:03:20 +0000 | [diff] [blame] | 199 | \var{section} to a Boolean value. Note that the accepted values |
| 200 | for the option are \code{1}, \code{yes}, \code{true}, and \code{on}, |
| 201 | which cause this method to return true, and \code{0}, \code{no}, |
Fred Drake | 6959a2f | 2001-10-09 14:58:24 +0000 | [diff] [blame] | 202 | \code{false}, and \code{off}, which cause it to return false. These |
| 203 | values are checked in a case-insensitive manner. Any other value will |
| 204 | cause it to raise \exception{ValueError}. |
Fred Drake | bc866ce | 1999-01-26 15:47:59 +0000 | [diff] [blame] | 205 | \end{methoddesc} |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 206 | |
Fred Drake | 97d5f05 | 2002-10-25 20:20:58 +0000 | [diff] [blame] | 207 | \begin{methoddesc}{items}{section} |
| 208 | Return a list of \code{(\var{name}, \var{value})} pairs for each |
| 209 | option in the given \var{section}. |
Fred Drake | 2ca041f | 2002-09-27 15:49:56 +0000 | [diff] [blame] | 210 | \end{methoddesc} |
| 211 | |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 212 | \begin{methoddesc}{set}{section, option, value} |
| 213 | If the given section exists, set the given option to the specified value; |
Fred Drake | 3c10c68 | 2001-09-28 16:57:16 +0000 | [diff] [blame] | 214 | otherwise raise \exception{NoSectionError}. |
| 215 | \versionadded{1.6} |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 216 | \end{methoddesc} |
| 217 | |
Eric S. Raymond | f868de6 | 2000-07-14 15:00:02 +0000 | [diff] [blame] | 218 | \begin{methoddesc}{write}{fileobject} |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 219 | Write a representation of the configuration to the specified file |
| 220 | object. This representation can be parsed by a future \method{read()} |
Fred Drake | 3c10c68 | 2001-09-28 16:57:16 +0000 | [diff] [blame] | 221 | call. |
| 222 | \versionadded{1.6} |
Eric S. Raymond | 417c489 | 2000-07-10 18:11:00 +0000 | [diff] [blame] | 223 | \end{methoddesc} |
Eric S. Raymond | f868de6 | 2000-07-14 15:00:02 +0000 | [diff] [blame] | 224 | |
| 225 | \begin{methoddesc}{remove_option}{section, option} |
| 226 | Remove the specified \var{option} from the specified \var{section}. |
| 227 | If the section does not exist, raise \exception{NoSectionError}. |
| 228 | If the option existed to be removed, return 1; otherwise return 0. |
Fred Drake | 3c10c68 | 2001-09-28 16:57:16 +0000 | [diff] [blame] | 229 | \versionadded{1.6} |
Eric S. Raymond | f868de6 | 2000-07-14 15:00:02 +0000 | [diff] [blame] | 230 | \end{methoddesc} |
| 231 | |
| 232 | \begin{methoddesc}{remove_section}{section} |
| 233 | Remove the specified \var{section} from the configuration. |
Neal Norwitz | d3dab2b | 2002-04-05 02:21:09 +0000 | [diff] [blame] | 234 | If the section in fact existed, return \code{True}. |
| 235 | Otherwise return \code{False}. |
Eric S. Raymond | f868de6 | 2000-07-14 15:00:02 +0000 | [diff] [blame] | 236 | \end{methoddesc} |
| 237 | |
Fred Drake | 5b0705d | 2001-02-19 22:37:24 +0000 | [diff] [blame] | 238 | \begin{methoddesc}{optionxform}{option} |
| 239 | Transforms the option name \var{option} as found in an input file or |
| 240 | as passed in by client code to the form that should be used in the |
| 241 | internal structures. The default implementation returns a lower-case |
| 242 | version of \var{option}; subclasses may override this or client code |
| 243 | can set an attribute of this name on instances to affect this |
| 244 | behavior. Setting this to \function{str()}, for example, would make |
| 245 | option names case sensitive. |
| 246 | \end{methoddesc} |
Fred Drake | 97d5f05 | 2002-10-25 20:20:58 +0000 | [diff] [blame] | 247 | |
| 248 | |
| 249 | \subsection{ConfigParser Objects \label{ConfigParser-objects}} |
| 250 | |
| 251 | The \class{ConfigParser} class extends some methods of the |
| 252 | \class{RawConfigParser} interface, adding some optional arguments. |
| 253 | |
| 254 | \begin{methoddesc}{get}{section, option\optional{, raw\optional{, vars}}} |
| 255 | Get an \var{option} value for the named \var{section}. All the |
| 256 | \character{\%} interpolations are expanded in the return values, based |
| 257 | on the defaults passed into the constructor, as well as the options |
| 258 | \var{vars} provided, unless the \var{raw} argument is true. |
| 259 | \end{methoddesc} |
| 260 | |
| 261 | \begin{methoddesc}{items}{section\optional{, raw\optional{, vars}}} |
| 262 | Create a generator which will return a tuple \code{(name, value)} for |
| 263 | each option in the given \var{section}. Optional arguments have the |
| 264 | same meaning as for the \code{get()} method. |
| 265 | \versionadded{2.3} |
| 266 | \end{methoddesc} |