Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 1 | :mod:`configparser` --- Configuration file parser |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2 | ================================================= |
| 3 | |
Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 4 | .. module:: configparser |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 5 | :synopsis: Configuration file parser. |
Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 6 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | .. moduleauthor:: Ken Manheimer <klm@zope.com> |
| 8 | .. moduleauthor:: Barry Warsaw <bwarsaw@python.org> |
| 9 | .. moduleauthor:: Eric S. Raymond <esr@thyrsus.com> |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 10 | .. moduleauthor:: Łukasz Langa <lukasz@langa.pl> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | .. sectionauthor:: Christopher G. Petrilli <petrilli@amber.org> |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 12 | .. sectionauthor:: Łukasz Langa <lukasz@langa.pl> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | .. index:: |
| 15 | pair: .ini; file |
| 16 | pair: configuration; file |
| 17 | single: ini file |
| 18 | single: Windows ini file |
| 19 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 20 | This module provides the :class:`ConfigParser` class which implements a basic |
| 21 | configuration language which provides a structure similar to what's found in |
| 22 | Microsoft Windows INI files. You can use this to write Python programs which |
| 23 | can be customized by end users easily. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | |
Georg Brandl | e720c0a | 2009-04-27 16:20:50 +0000 | [diff] [blame] | 25 | .. note:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
Georg Brandl | e720c0a | 2009-04-27 16:20:50 +0000 | [diff] [blame] | 27 | This library does *not* interpret or write the value-type prefixes used in |
| 28 | the Windows Registry extended version of INI syntax. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 30 | .. seealso:: |
| 31 | |
| 32 | Module :mod:`shlex` |
| 33 | Support for a creating Unix shell-like mini-languages which can be used |
| 34 | as an alternate format for application configuration files. |
| 35 | |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 36 | Module :mod:`json` |
| 37 | The json module implements a subset of JavaScript syntax which can also |
| 38 | be used for this purpose. |
| 39 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 40 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 41 | Quick Start |
| 42 | ----------- |
| 43 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 44 | Let's take a very basic configuration file that looks like this: |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 45 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 46 | .. code-block:: ini |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 47 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 48 | [DEFAULT] |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 49 | ServerAliveInterval = 45 |
| 50 | Compression = yes |
| 51 | CompressionLevel = 9 |
| 52 | ForwardX11 = yes |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 53 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 54 | [bitbucket.org] |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 55 | User = hg |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 56 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 57 | [topsecret.server.com] |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 58 | Port = 50022 |
| 59 | ForwardX11 = no |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 60 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 61 | The structure of INI files is described `in the following section |
| 62 | <#supported-ini-file-structure>`_. Essentially, the file |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 63 | consists of sections, each of which contains keys with values. |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 64 | :mod:`configparser` classes can read and write such files. Let's start by |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 65 | creating the above configuration file programatically. |
| 66 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 67 | .. doctest:: |
| 68 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 69 | >>> import configparser |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 70 | >>> config = configparser.ConfigParser() |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 71 | >>> config['DEFAULT'] = {'ServerAliveInterval': '45', |
| 72 | ... 'Compression': 'yes', |
| 73 | ... 'CompressionLevel': '9'} |
| 74 | >>> config['bitbucket.org'] = {} |
| 75 | >>> config['bitbucket.org']['User'] = 'hg' |
| 76 | >>> config['topsecret.server.com'] = {} |
| 77 | >>> topsecret = config['topsecret.server.com'] |
| 78 | >>> topsecret['Port'] = '50022' # mutates the parser |
| 79 | >>> topsecret['ForwardX11'] = 'no' # same here |
| 80 | >>> config['DEFAULT']['ForwardX11'] = 'yes' |
| 81 | >>> with open('example.ini', 'w') as configfile: |
| 82 | ... config.write(configfile) |
| 83 | ... |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 84 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 85 | As you can see, we can treat a config parser much like a dictionary. |
| 86 | There are differences, `outlined later <#mapping-protocol-access>`_, but |
| 87 | the behavior is very close to what you would expect from a dictionary. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 88 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 89 | Now that we have created and saved a configuration file, let's read it |
| 90 | back and explore the data it holds. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 91 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 92 | .. doctest:: |
| 93 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 94 | >>> import configparser |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 95 | >>> config = configparser.ConfigParser() |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 96 | >>> config.sections() |
| 97 | [] |
| 98 | >>> config.read('example.ini') |
| 99 | ['example.ini'] |
| 100 | >>> config.sections() |
| 101 | ['bitbucket.org', 'topsecret.server.com'] |
| 102 | >>> 'bitbucket.org' in config |
| 103 | True |
| 104 | >>> 'bytebong.com' in config |
| 105 | False |
| 106 | >>> config['bitbucket.org']['User'] |
| 107 | 'hg' |
| 108 | >>> config['DEFAULT']['Compression'] |
| 109 | 'yes' |
| 110 | >>> topsecret = config['topsecret.server.com'] |
| 111 | >>> topsecret['ForwardX11'] |
| 112 | 'no' |
| 113 | >>> topsecret['Port'] |
| 114 | '50022' |
| 115 | >>> for key in config['bitbucket.org']: print(key) |
| 116 | ... |
| 117 | user |
| 118 | compressionlevel |
| 119 | serveraliveinterval |
| 120 | compression |
| 121 | forwardx11 |
| 122 | >>> config['bitbucket.org']['ForwardX11'] |
| 123 | 'yes' |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 124 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 125 | As we can see above, the API is pretty straightforward. The only bit of magic |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 126 | involves the ``DEFAULT`` section which provides default values for all other |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 127 | sections [1]_. Note also that keys in sections are |
| 128 | case-insensitive and stored in lowercase [1]_. |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 129 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 130 | |
| 131 | Supported Datatypes |
| 132 | ------------------- |
| 133 | |
| 134 | Config parsers do not guess datatypes of values in configuration files, always |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 135 | storing them internally as strings. This means that if you need other |
| 136 | datatypes, you should convert on your own: |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 137 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 138 | .. doctest:: |
| 139 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 140 | >>> int(topsecret['Port']) |
| 141 | 50022 |
| 142 | >>> float(topsecret['CompressionLevel']) |
| 143 | 9.0 |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 144 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 145 | Extracting Boolean values is not that simple, though. Passing the value |
| 146 | to ``bool()`` would do no good since ``bool('False')`` is still |
| 147 | ``True``. This is why config parsers also provide :meth:`getboolean`. |
| 148 | This method is case-insensitive and recognizes Boolean values from |
| 149 | ``'yes'``/``'no'``, ``'on'``/``'off'`` and ``'1'``/``'0'`` [1]_. |
| 150 | For example: |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 151 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 152 | .. doctest:: |
| 153 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 154 | >>> topsecret.getboolean('ForwardX11') |
| 155 | False |
| 156 | >>> config['bitbucket.org'].getboolean('ForwardX11') |
| 157 | True |
| 158 | >>> config.getboolean('bitbucket.org', 'Compression') |
| 159 | True |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 160 | |
| 161 | Apart from :meth:`getboolean`, config parsers also provide equivalent |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 162 | :meth:`getint` and :meth:`getfloat` methods, but these are far less |
| 163 | useful since conversion using :func:`int` and :func:`float` is |
| 164 | sufficient for these types. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 165 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 166 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 167 | Fallback Values |
| 168 | --------------- |
| 169 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 170 | As with a dictionary, you can use a section's :meth:`get` method to |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 171 | provide fallback values: |
| 172 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 173 | .. doctest:: |
| 174 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 175 | >>> topsecret.get('Port') |
| 176 | '50022' |
| 177 | >>> topsecret.get('CompressionLevel') |
| 178 | '9' |
| 179 | >>> topsecret.get('Cipher') |
| 180 | >>> topsecret.get('Cipher', '3des-cbc') |
| 181 | '3des-cbc' |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 182 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 183 | Please note that default values have precedence over fallback values. |
| 184 | For instance, in our example the ``'CompressionLevel'`` key was |
| 185 | specified only in the ``'DEFAULT'`` section. If we try to get it from |
| 186 | the section ``'topsecret.server.com'``, we will always get the default, |
| 187 | even if we specify a fallback: |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 188 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 189 | .. doctest:: |
| 190 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 191 | >>> topsecret.get('CompressionLevel', '3') |
| 192 | '9' |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 193 | |
| 194 | One more thing to be aware of is that the parser-level :meth:`get` method |
| 195 | provides a custom, more complex interface, maintained for backwards |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 196 | compatibility. When using this method, a fallback value can be provided via |
| 197 | the ``fallback`` keyword-only argument: |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 198 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 199 | .. doctest:: |
| 200 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 201 | >>> config.get('bitbucket.org', 'monster', |
| 202 | ... fallback='No such things as monsters') |
| 203 | 'No such things as monsters' |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 204 | |
| 205 | The same ``fallback`` argument can be used with the :meth:`getint`, |
| 206 | :meth:`getfloat` and :meth:`getboolean` methods, for example: |
| 207 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 208 | .. doctest:: |
| 209 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 210 | >>> 'BatchMode' in topsecret |
| 211 | False |
| 212 | >>> topsecret.getboolean('BatchMode', fallback=True) |
| 213 | True |
| 214 | >>> config['DEFAULT']['BatchMode'] = 'no' |
| 215 | >>> topsecret.getboolean('BatchMode', fallback=True) |
| 216 | False |
| 217 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 218 | |
| 219 | Supported INI File Structure |
| 220 | ---------------------------- |
| 221 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 222 | A configuration file consists of sections, each led by a ``[section]`` header, |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 223 | followed by key/value entries separated by a specific string (``=`` or ``:`` by |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 224 | default [1]_). By default, section names are case sensitive but keys are not |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 225 | [1]_. Leading and trailing whitespace is removed from keys and values. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 226 | Values can be omitted, in which case the key/value delimiter may also be left |
| 227 | out. Values can also span multiple lines, as long as they are indented deeper |
| 228 | than the first line of the value. Depending on the parser's mode, blank lines |
| 229 | may be treated as parts of multiline values or ignored. |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 230 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 231 | Configuration files may include comments, prefixed by specific |
| 232 | characters (``#`` and ``;`` by default [1]_). Comments may appear on |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 233 | their own on an otherwise empty line, possibly indented. [1]_ |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 234 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 235 | For example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 236 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 237 | .. code-block:: ini |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 238 | |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 239 | [Simple Values] |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 240 | key=value |
| 241 | spaces in keys=allowed |
| 242 | spaces in values=allowed as well |
| 243 | spaces around the delimiter = obviously |
| 244 | you can also use : to delimit keys from values |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 245 | |
| 246 | [All Values Are Strings] |
| 247 | values like this: 1000000 |
| 248 | or this: 3.14159265359 |
| 249 | are they treated as numbers? : no |
| 250 | integers, floats and booleans are held as: strings |
| 251 | can use the API to get converted values directly: true |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 252 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 253 | [Multiline Values] |
| 254 | chorus: I'm a lumberjack, and I'm okay |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 255 | I sleep all night and I work all day |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 256 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 257 | [No Values] |
| 258 | key_without_value |
| 259 | empty string value here = |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 260 | |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 261 | [You can use comments] |
| 262 | # like this |
| 263 | ; or this |
| 264 | |
| 265 | # By default only in an empty line. |
| 266 | # Inline comments can be harmful because they prevent users |
| 267 | # from using the delimiting characters as parts of values. |
| 268 | # That being said, this can be customized. |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 269 | |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 270 | [Sections Can Be Indented] |
| 271 | can_values_be_as_well = True |
| 272 | does_that_mean_anything_special = False |
| 273 | purpose = formatting for readability |
| 274 | multiline_values = are |
| 275 | handled just fine as |
| 276 | long as they are indented |
| 277 | deeper than the first line |
| 278 | of a value |
| 279 | # Did I mention we can indent comments, too? |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 280 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 281 | |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 282 | Interpolation of values |
| 283 | ----------------------- |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 284 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 285 | On top of the core functionality, :class:`ConfigParser` supports |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 286 | interpolation. This means values can be preprocessed before returning them |
| 287 | from ``get()`` calls. |
| 288 | |
| 289 | .. class:: BasicInterpolation() |
| 290 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 291 | The default implementation used by :class:`ConfigParser`. It enables |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 292 | values to contain format strings which refer to other values in the same |
| 293 | section, or values in the special default section [1]_. Additional default |
| 294 | values can be provided on initialization. |
| 295 | |
| 296 | For example: |
| 297 | |
| 298 | .. code-block:: ini |
| 299 | |
| 300 | [Paths] |
| 301 | home_dir: /Users |
| 302 | my_dir: %(home_dir)s/lumberjack |
| 303 | my_pictures: %(my_dir)s/Pictures |
| 304 | |
| 305 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 306 | In the example above, :class:`ConfigParser` with *interpolation* set to |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 307 | ``BasicInterpolation()`` would resolve ``%(home_dir)s`` to the value of |
| 308 | ``home_dir`` (``/Users`` in this case). ``%(my_dir)s`` in effect would |
| 309 | resolve to ``/Users/lumberjack``. All interpolations are done on demand so |
| 310 | keys used in the chain of references do not have to be specified in any |
| 311 | specific order in the configuration file. |
| 312 | |
| 313 | With ``interpolation`` set to ``None``, the parser would simply return |
| 314 | ``%(my_dir)s/Pictures`` as the value of ``my_pictures`` and |
| 315 | ``%(home_dir)s/lumberjack`` as the value of ``my_dir``. |
| 316 | |
| 317 | .. class:: ExtendedInterpolation() |
| 318 | |
| 319 | An alternative handler for interpolation which implements a more advanced |
| 320 | syntax, used for instance in ``zc.buildout``. Extended interpolation is |
| 321 | using ``${section:option}`` to denote a value from a foreign section. |
| 322 | Interpolation can span multiple levels. For convenience, if the ``section:`` |
| 323 | part is omitted, interpolation defaults to the current section (and possibly |
| 324 | the default values from the special section). |
| 325 | |
| 326 | For example, the configuration specified above with basic interpolation, |
| 327 | would look like this with extended interpolation: |
| 328 | |
| 329 | .. code-block:: ini |
| 330 | |
| 331 | [Paths] |
| 332 | home_dir: /Users |
| 333 | my_dir: ${home_dir}/lumberjack |
| 334 | my_pictures: ${my_dir}/Pictures |
| 335 | |
| 336 | Values from other sections can be fetched as well: |
| 337 | |
| 338 | .. code-block:: ini |
| 339 | |
| 340 | [Common] |
| 341 | home_dir: /Users |
| 342 | library_dir: /Library |
| 343 | system_dir: /System |
| 344 | macports_dir: /opt/local |
| 345 | |
| 346 | [Frameworks] |
| 347 | Python: 3.2 |
| 348 | path: ${Common:system_dir}/Library/Frameworks/ |
| 349 | |
| 350 | [Arthur] |
| 351 | nickname: Two Sheds |
| 352 | last_name: Jackson |
| 353 | my_dir: ${Common:home_dir}/twosheds |
| 354 | my_pictures: ${my_dir}/Pictures |
| 355 | python_dir: ${Frameworks:path}/Python/Versions/${Frameworks:Python} |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 356 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 357 | Mapping Protocol Access |
| 358 | ----------------------- |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 359 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 360 | .. versionadded:: 3.2 |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 361 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 362 | Mapping protocol access is a generic name for functionality that enables using |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 363 | custom objects as if they were dictionaries. In case of :mod:`configparser`, |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 364 | the mapping interface implementation is using the |
| 365 | ``parser['section']['option']`` notation. |
| 366 | |
| 367 | ``parser['section']`` in particular returns a proxy for the section's data in |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 368 | the parser. This means that the values are not copied but they are taken from |
| 369 | the original parser on demand. What's even more important is that when values |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 370 | are changed on a section proxy, they are actually mutated in the original |
| 371 | parser. |
| 372 | |
| 373 | :mod:`configparser` objects behave as close to actual dictionaries as possible. |
| 374 | The mapping interface is complete and adheres to the ``MutableMapping`` ABC. |
| 375 | However, there are a few differences that should be taken into account: |
| 376 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 377 | * By default, all keys in sections are accessible in a case-insensitive manner |
| 378 | [1]_. E.g. ``for option in parser["section"]`` yields only ``optionxform``'ed |
| 379 | option key names. This means lowercased keys by default. At the same time, |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 380 | for a section that holds the key ``'a'``, both expressions return ``True``:: |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 381 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 382 | "a" in parser["section"] |
| 383 | "A" in parser["section"] |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 384 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 385 | * All sections include ``DEFAULTSECT`` values as well which means that |
| 386 | ``.clear()`` on a section may not leave the section visibly empty. This is |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 387 | because default values cannot be deleted from the section (because technically |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 388 | they are not there). If they are overriden in the section, deleting causes |
| 389 | the default value to be visible again. Trying to delete a default value |
| 390 | causes a ``KeyError``. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 391 | |
Éric Araujo | ff2a4ba | 2010-11-30 17:20:31 +0000 | [diff] [blame] | 392 | * Trying to delete the ``DEFAULTSECT`` raises ``ValueError``. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 393 | |
Łukasz Langa | 71b37a5 | 2010-12-17 21:56:32 +0000 | [diff] [blame] | 394 | * ``parser.get(section, option, **kwargs)`` - the second argument is **not** |
| 395 | a fallback value. Note however that the section-level ``get()`` methods are |
| 396 | compatible both with the mapping protocol and the classic configparser API. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 397 | |
Łukasz Langa | 71b37a5 | 2010-12-17 21:56:32 +0000 | [diff] [blame] | 398 | * ``parser.items()`` is compatible with the mapping protocol (returns a list of |
| 399 | *section_name*, *section_proxy* pairs including the DEFAULTSECT). However, |
| 400 | this method can also be invoked with arguments: ``parser.items(section, raw, |
| 401 | vars)``. The latter call returns a list of *option*, *value* pairs for |
| 402 | a specified ``section``, with all interpolations expanded (unless |
| 403 | ``raw=True`` is provided). |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 404 | |
| 405 | The mapping protocol is implemented on top of the existing legacy API so that |
Łukasz Langa | 71b37a5 | 2010-12-17 21:56:32 +0000 | [diff] [blame] | 406 | subclasses overriding the original interface still should have mappings working |
| 407 | as expected. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 408 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 409 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 410 | Customizing Parser Behaviour |
| 411 | ---------------------------- |
| 412 | |
| 413 | There are nearly as many INI format variants as there are applications using it. |
| 414 | :mod:`configparser` goes a long way to provide support for the largest sensible |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 415 | set of INI styles available. The default functionality is mainly dictated by |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 416 | historical background and it's very likely that you will want to customize some |
| 417 | of the features. |
| 418 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 419 | The most common way to change the way a specific config parser works is to use |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 420 | the :meth:`__init__` options: |
| 421 | |
| 422 | * *defaults*, default value: ``None`` |
| 423 | |
| 424 | This option accepts a dictionary of key-value pairs which will be initially |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 425 | put in the ``DEFAULT`` section. This makes for an elegant way to support |
| 426 | concise configuration files that don't specify values which are the same as |
| 427 | the documented default. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 428 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 429 | Hint: if you want to specify default values for a specific section, use |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 430 | :meth:`read_dict` before you read the actual file. |
| 431 | |
| 432 | * *dict_type*, default value: :class:`collections.OrderedDict` |
| 433 | |
| 434 | This option has a major impact on how the mapping protocol will behave and how |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 435 | the written configuration files look. With the default ordered |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 436 | dictionary, every section is stored in the order they were added to the |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 437 | parser. Same goes for options within sections. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 438 | |
| 439 | An alternative dictionary type can be used for example to sort sections and |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 440 | options on write-back. You can also use a regular dictionary for performance |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 441 | reasons. |
| 442 | |
| 443 | Please note: there are ways to add a set of key-value pairs in a single |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 444 | operation. When you use a regular dictionary in those operations, the order |
| 445 | of the keys may be random. For example: |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 446 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 447 | .. doctest:: |
| 448 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 449 | >>> parser = configparser.ConfigParser() |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 450 | >>> parser.read_dict({'section1': {'key1': 'value1', |
| 451 | ... 'key2': 'value2', |
| 452 | ... 'key3': 'value3'}, |
| 453 | ... 'section2': {'keyA': 'valueA', |
| 454 | ... 'keyB': 'valueB', |
| 455 | ... 'keyC': 'valueC'}, |
| 456 | ... 'section3': {'foo': 'x', |
| 457 | ... 'bar': 'y', |
| 458 | ... 'baz': 'z'} |
| 459 | ... }) |
| 460 | >>> parser.sections() |
| 461 | ['section3', 'section2', 'section1'] |
| 462 | >>> [option for option in parser['section3']] |
| 463 | ['baz', 'foo', 'bar'] |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 464 | |
| 465 | In these operations you need to use an ordered dictionary as well: |
| 466 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 467 | .. doctest:: |
| 468 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 469 | >>> from collections import OrderedDict |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 470 | >>> parser = configparser.ConfigParser() |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 471 | >>> parser.read_dict( |
| 472 | ... OrderedDict(( |
| 473 | ... ('s1', |
| 474 | ... OrderedDict(( |
| 475 | ... ('1', '2'), |
| 476 | ... ('3', '4'), |
| 477 | ... ('5', '6'), |
| 478 | ... )) |
| 479 | ... ), |
| 480 | ... ('s2', |
| 481 | ... OrderedDict(( |
| 482 | ... ('a', 'b'), |
| 483 | ... ('c', 'd'), |
| 484 | ... ('e', 'f'), |
| 485 | ... )) |
| 486 | ... ), |
| 487 | ... )) |
| 488 | ... ) |
| 489 | >>> parser.sections() |
| 490 | ['s1', 's2'] |
| 491 | >>> [option for option in parser['s1']] |
| 492 | ['1', '3', '5'] |
| 493 | >>> [option for option in parser['s2'].values()] |
| 494 | ['b', 'd', 'f'] |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 495 | |
| 496 | * *allow_no_value*, default value: ``False`` |
| 497 | |
| 498 | Some configuration files are known to include settings without values, but |
| 499 | which otherwise conform to the syntax supported by :mod:`configparser`. The |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 500 | *allow_no_value* parameter to the constructor can be used to |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 501 | indicate that such values should be accepted: |
| 502 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 503 | .. doctest:: |
| 504 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 505 | >>> import configparser |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 506 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 507 | >>> sample_config = """ |
| 508 | ... [mysqld] |
| 509 | ... user = mysql |
| 510 | ... pid-file = /var/run/mysqld/mysqld.pid |
| 511 | ... skip-external-locking |
| 512 | ... old_passwords = 1 |
| 513 | ... skip-bdb |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 514 | ... # we don't need ACID today |
| 515 | ... skip-innodb |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 516 | ... """ |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 517 | >>> config = configparser.ConfigParser(allow_no_value=True) |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 518 | >>> config.read_string(sample_config) |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 519 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 520 | >>> # Settings with values are treated as before: |
| 521 | >>> config["mysqld"]["user"] |
| 522 | 'mysql' |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 523 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 524 | >>> # Settings without values provide None: |
| 525 | >>> config["mysqld"]["skip-bdb"] |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 526 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 527 | >>> # Settings which aren't specified still raise an error: |
| 528 | >>> config["mysqld"]["does-not-exist"] |
| 529 | Traceback (most recent call last): |
| 530 | ... |
| 531 | KeyError: 'does-not-exist' |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 532 | |
| 533 | * *delimiters*, default value: ``('=', ':')`` |
| 534 | |
| 535 | Delimiters are substrings that delimit keys from values within a section. The |
| 536 | first occurence of a delimiting substring on a line is considered a delimiter. |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 537 | This means values (but not keys) can contain the delimiters. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 538 | |
| 539 | See also the *space_around_delimiters* argument to |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 540 | :meth:`ConfigParser.write`. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 541 | |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 542 | * *comment_prefixes*, default value: ``('#', ';')`` |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 543 | |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 544 | * *inline_comment_prefixes*, default value: ``None`` |
| 545 | |
| 546 | Comment prefixes are strings that indicate the start of a valid comment within |
| 547 | a config file. *comment_prefixes* are used only on otherwise empty lines |
| 548 | (optionally indented) whereas *inline_comment_prefixes* can be used after |
| 549 | every valid value (e.g. section names, options and empty lines as well). By |
| 550 | default inline comments are disabled and ``'#'`` and ``';'`` are used as |
| 551 | prefixes for whole line comments. |
| 552 | |
| 553 | .. versionchanged:: 3.2 |
| 554 | In previous versions of :mod:`configparser` behaviour matched |
| 555 | ``comment_prefixes=('#',';')`` and ``inline_comment_prefixes=(';',)``. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 556 | |
| 557 | Please note that config parsers don't support escaping of comment prefixes so |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 558 | using *inline_comment_prefixes* may prevent users from specifying option |
| 559 | values with characters used as comment prefixes. When in doubt, avoid setting |
| 560 | *inline_comment_prefixes*. In any circumstances, the only way of storing |
| 561 | comment prefix characters at the beginning of a line in multiline values is to |
| 562 | interpolate the prefix, for example:: |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 563 | |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 564 | >>> from configparser import ConfigParser, ExtendedInterpolation |
| 565 | >>> parser = ConfigParser(interpolation=ExtendedInterpolation()) |
| 566 | >>> # the default BasicInterpolation could be used as well |
| 567 | >>> parser.read_string(""" |
| 568 | ... [DEFAULT] |
| 569 | ... hash = # |
| 570 | ... |
| 571 | ... [hashes] |
| 572 | ... shebang = |
| 573 | ... ${hash}!/usr/bin/env python |
| 574 | ... ${hash} -*- coding: utf-8 -*- |
| 575 | ... |
| 576 | ... extensions = |
| 577 | ... enabled_extension |
| 578 | ... another_extension |
| 579 | ... #disabled_by_comment |
| 580 | ... yet_another_extension |
| 581 | ... |
| 582 | ... interpolation not necessary = if # is not at line start |
| 583 | ... even in multiline values = line #1 |
| 584 | ... line #2 |
| 585 | ... line #3 |
| 586 | ... """) |
| 587 | >>> print(parser['hashes']['shebang']) |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 588 | |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 589 | #!/usr/bin/env python |
| 590 | # -*- coding: utf-8 -*- |
| 591 | >>> print(parser['hashes']['extensions']) |
| 592 | |
| 593 | enabled_extension |
| 594 | another_extension |
| 595 | yet_another_extension |
| 596 | >>> print(parser['hashes']['interpolation not necessary']) |
| 597 | if # is not at line start |
| 598 | >>> print(parser['hashes']['even in multiline values']) |
| 599 | line #1 |
| 600 | line #2 |
| 601 | line #3 |
| 602 | |
| 603 | * *strict*, default value: ``True`` |
| 604 | |
| 605 | When set to ``True``, the parser will not allow for any section or option |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 606 | duplicates while reading from a single source (using :meth:`read_file`, |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 607 | :meth:`read_string` or :meth:`read_dict`). It is recommended to use strict |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 608 | parsers in new applications. |
| 609 | |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 610 | .. versionchanged:: 3.2 |
| 611 | In previous versions of :mod:`configparser` behaviour matched |
| 612 | ``strict=False``. |
| 613 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 614 | * *empty_lines_in_values*, default value: ``True`` |
| 615 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 616 | In config parsers, values can span multiple lines as long as they are |
| 617 | indented more than the key that holds them. By default parsers also let |
| 618 | empty lines to be parts of values. At the same time, keys can be arbitrarily |
| 619 | indented themselves to improve readability. In consequence, when |
| 620 | configuration files get big and complex, it is easy for the user to lose |
| 621 | track of the file structure. Take for instance: |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 622 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 623 | .. code-block:: ini |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 624 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 625 | [Section] |
| 626 | key = multiline |
| 627 | value with a gotcha |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 628 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 629 | this = is still a part of the multiline value of 'key' |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 630 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 631 | This can be especially problematic for the user to see if she's using a |
| 632 | proportional font to edit the file. That is why when your application does |
| 633 | not need values with empty lines, you should consider disallowing them. This |
| 634 | will make empty lines split keys every time. In the example above, it would |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 635 | produce two keys, ``key`` and ``this``. |
| 636 | |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 637 | * *default_section*, default value: ``configparser.DEFAULTSECT`` (that is: |
| 638 | ``"DEFAULT"``) |
| 639 | |
| 640 | The convention of allowing a special section of default values for other |
| 641 | sections or interpolation purposes is a powerful concept of this library, |
| 642 | letting users create complex declarative configurations. This section is |
| 643 | normally called ``"DEFAULT"`` but this can be customized to point to any |
| 644 | other valid section name. Some typical values include: ``"general"`` or |
| 645 | ``"common"``. The name provided is used for recognizing default sections when |
| 646 | reading from any source and is used when writing configuration back to |
| 647 | a file. Its current value can be retrieved using the |
| 648 | ``parser_instance.default_section`` attribute and may be modified at runtime |
| 649 | (i.e. to convert files from one format to another). |
| 650 | |
| 651 | * *interpolation*, default value: ``configparser.BasicInterpolation`` |
| 652 | |
| 653 | Interpolation behaviour may be customized by providing a custom handler |
| 654 | through the *interpolation* argument. ``None`` can be used to turn off |
| 655 | interpolation completely, ``ExtendedInterpolation()`` provides a more |
| 656 | advanced variant inspired by ``zc.buildout``. More on the subject in the |
| 657 | `dedicated documentation section <#interpolation-of-values>`_. |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 658 | :class:`RawConfigParser` has a default value of ``None``. |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 659 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 660 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 661 | More advanced customization may be achieved by overriding default values of |
| 662 | these parser attributes. The defaults are defined on the classes, so they |
| 663 | may be overriden by subclasses or by attribute assignment. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 664 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 665 | .. attribute:: BOOLEAN_STATES |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 666 | |
| 667 | By default when using :meth:`getboolean`, config parsers consider the |
| 668 | following values ``True``: ``'1'``, ``'yes'``, ``'true'``, ``'on'`` and the |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 669 | following values ``False``: ``'0'``, ``'no'``, ``'false'``, ``'off'``. You |
| 670 | can override this by specifying a custom dictionary of strings and their |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 671 | Boolean outcomes. For example: |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 672 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 673 | .. doctest:: |
| 674 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 675 | >>> custom = configparser.ConfigParser() |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 676 | >>> custom['section1'] = {'funky': 'nope'} |
| 677 | >>> custom['section1'].getboolean('funky') |
| 678 | Traceback (most recent call last): |
| 679 | ... |
| 680 | ValueError: Not a boolean: nope |
| 681 | >>> custom.BOOLEAN_STATES = {'sure': True, 'nope': False} |
| 682 | >>> custom['section1'].getboolean('funky') |
| 683 | False |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 684 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 685 | Other typical Boolean pairs include ``accept``/``reject`` or |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 686 | ``enabled``/``disabled``. |
| 687 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 688 | .. method:: optionxform(option) |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 689 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 690 | This method transforms option names on every read, get, or set |
| 691 | operation. The default converts the name to lowercase. This also |
| 692 | means that when a configuration file gets written, all keys will be |
| 693 | lowercase. Override this method if that's unsuitable. |
| 694 | For example: |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 695 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 696 | .. doctest:: |
| 697 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 698 | >>> config = """ |
| 699 | ... [Section1] |
| 700 | ... Key = Value |
| 701 | ... |
| 702 | ... [Section2] |
| 703 | ... AnotherKey = Value |
| 704 | ... """ |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 705 | >>> typical = configparser.ConfigParser() |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 706 | >>> typical.read_string(config) |
| 707 | >>> list(typical['Section1'].keys()) |
| 708 | ['key'] |
| 709 | >>> list(typical['Section2'].keys()) |
| 710 | ['anotherkey'] |
| 711 | >>> custom = configparser.RawConfigParser() |
| 712 | >>> custom.optionxform = lambda option: option |
| 713 | >>> custom.read_string(config) |
| 714 | >>> list(custom['Section1'].keys()) |
| 715 | ['Key'] |
| 716 | >>> list(custom['Section2'].keys()) |
| 717 | ['AnotherKey'] |
| 718 | |
Łukasz Langa | 66c908e | 2011-01-28 11:57:30 +0000 | [diff] [blame] | 719 | .. attribute:: SECTCRE |
| 720 | |
| 721 | A compiled regular expression used to parse section headers. The default |
| 722 | matches ``[section]`` to the name ``"section"``. Whitespace is considered part |
| 723 | of the section name, thus ``[ larch ]`` will be read as a section of name |
| 724 | ``" larch "``. Override this attribute if that's unsuitable. For example: |
| 725 | |
| 726 | .. doctest:: |
| 727 | |
| 728 | >>> config = """ |
| 729 | ... [Section 1] |
| 730 | ... option = value |
| 731 | ... |
| 732 | ... [ Section 2 ] |
| 733 | ... another = val |
| 734 | ... """ |
| 735 | >>> typical = ConfigParser() |
| 736 | >>> typical.read_string(config) |
| 737 | >>> typical.sections() |
| 738 | ['Section 1', ' Section 2 '] |
| 739 | >>> custom = ConfigParser() |
| 740 | >>> custom.SECTCRE = re.compile(r"\[ *(?P<header>[^]]+?) *\]") |
| 741 | >>> custom.read_string(config) |
| 742 | >>> custom.sections() |
| 743 | ['Section 1', 'Section 2'] |
| 744 | |
| 745 | .. note:: |
| 746 | |
| 747 | While ConfigParser objects also use an ``OPTCRE`` attribute for recognizing |
| 748 | option lines, it's not recommended to override it because that would |
| 749 | interfere with constructor options *allow_no_value* and *delimiters*. |
| 750 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 751 | |
| 752 | Legacy API Examples |
| 753 | ------------------- |
| 754 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 755 | Mainly because of backwards compatibility concerns, :mod:`configparser` |
| 756 | provides also a legacy API with explicit ``get``/``set`` methods. While there |
| 757 | are valid use cases for the methods outlined below, mapping protocol access is |
| 758 | preferred for new projects. The legacy API is at times more advanced, |
| 759 | low-level and downright counterintuitive. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 760 | |
| 761 | An example of writing to a configuration file:: |
| 762 | |
| 763 | import configparser |
| 764 | |
| 765 | config = configparser.RawConfigParser() |
| 766 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 767 | # Please note that using RawConfigParser's set functions, you can assign |
| 768 | # non-string values to keys internally, but will receive an error when |
| 769 | # attempting to write to a file or when you get it in non-raw mode. Setting |
| 770 | # values using the mapping protocol or ConfigParser's set() does not allow |
| 771 | # such assignments to take place. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 772 | config.add_section('Section1') |
| 773 | config.set('Section1', 'int', '15') |
| 774 | config.set('Section1', 'bool', 'true') |
| 775 | config.set('Section1', 'float', '3.1415') |
| 776 | config.set('Section1', 'baz', 'fun') |
| 777 | config.set('Section1', 'bar', 'Python') |
| 778 | config.set('Section1', 'foo', '%(bar)s is %(baz)s!') |
| 779 | |
| 780 | # Writing our configuration file to 'example.cfg' |
| 781 | with open('example.cfg', 'w') as configfile: |
| 782 | config.write(configfile) |
| 783 | |
| 784 | An example of reading the configuration file again:: |
| 785 | |
| 786 | import configparser |
| 787 | |
| 788 | config = configparser.RawConfigParser() |
| 789 | config.read('example.cfg') |
| 790 | |
| 791 | # getfloat() raises an exception if the value is not a float |
| 792 | # getint() and getboolean() also do this for their respective types |
| 793 | float = config.getfloat('Section1', 'float') |
| 794 | int = config.getint('Section1', 'int') |
| 795 | print(float + int) |
| 796 | |
| 797 | # Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'. |
| 798 | # This is because we are using a RawConfigParser(). |
| 799 | if config.getboolean('Section1', 'bool'): |
| 800 | print(config.get('Section1', 'foo')) |
| 801 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 802 | To get interpolation, use :class:`ConfigParser`:: |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 803 | |
| 804 | import configparser |
| 805 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 806 | cfg = configparser.ConfigParser() |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 807 | cfg.read('example.cfg') |
| 808 | |
| 809 | # Set the optional `raw` argument of get() to True if you wish to disable |
| 810 | # interpolation in a single get operation. |
| 811 | print(cfg.get('Section1', 'foo', raw=False)) # -> "Python is fun!" |
| 812 | print(cfg.get('Section1', 'foo', raw=True)) # -> "%(bar)s is %(baz)s!" |
| 813 | |
| 814 | # The optional `vars` argument is a dict with members that will take |
| 815 | # precedence in interpolation. |
| 816 | print(cfg.get('Section1', 'foo', vars={'bar': 'Documentation', |
| 817 | 'baz': 'evil'})) |
| 818 | |
| 819 | # The optional `fallback` argument can be used to provide a fallback value |
| 820 | print(cfg.get('Section1', 'foo')) |
| 821 | # -> "Python is fun!" |
| 822 | |
| 823 | print(cfg.get('Section1', 'foo', fallback='Monty is not.')) |
| 824 | # -> "Python is fun!" |
| 825 | |
| 826 | print(cfg.get('Section1', 'monster', fallback='No such things as monsters.')) |
| 827 | # -> "No such things as monsters." |
| 828 | |
| 829 | # A bare print(cfg.get('Section1', 'monster')) would raise NoOptionError |
| 830 | # but we can also use: |
| 831 | |
| 832 | print(cfg.get('Section1', 'monster', fallback=None)) |
| 833 | # -> None |
| 834 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 835 | Default values are available in both types of ConfigParsers. They are used in |
| 836 | interpolation if an option used is not defined elsewhere. :: |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 837 | |
| 838 | import configparser |
| 839 | |
| 840 | # New instance with 'bar' and 'baz' defaulting to 'Life' and 'hard' each |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 841 | config = configparser.ConfigParser({'bar': 'Life', 'baz': 'hard'}) |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 842 | config.read('example.cfg') |
| 843 | |
| 844 | print(config.get('Section1', 'foo')) # -> "Python is fun!" |
| 845 | config.remove_option('Section1', 'bar') |
| 846 | config.remove_option('Section1', 'baz') |
| 847 | print(config.get('Section1', 'foo')) # -> "Life is hard!" |
| 848 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 849 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 850 | .. _configparser-objects: |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 851 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 852 | ConfigParser Objects |
| 853 | -------------------- |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 854 | |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 855 | .. class:: ConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=configparser.DEFAULTSECT, interpolation=BasicInterpolation()) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 856 | |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 857 | The main configuration parser. When *defaults* is given, it is initialized |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 858 | into the dictionary of intrinsic defaults. When *dict_type* is given, it |
| 859 | will be used to create the dictionary objects for the list of sections, for |
| 860 | the options within a section, and for the default values. |
| 861 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 862 | When *delimiters* is given, it is used as the set of substrings that |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 863 | divide keys from values. When *comment_prefixes* is given, it will be used |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 864 | as the set of substrings that prefix comments in otherwise empty lines. |
| 865 | Comments can be indented. When *inline_comment_prefixes* is given, it will be |
| 866 | used as the set of substrings that prefix comments in non-empty lines. |
| 867 | |
Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 868 | line and inline comments. For backwards compatibility, the default value for |
| 869 | *comment_prefixes* is a special value that indicates that ``;`` and ``#`` can |
| 870 | start whole line comments while only ``;`` can start inline comments. |
| 871 | |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 872 | When *strict* is ``True`` (the default), the parser won't allow for |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 873 | any section or option duplicates while reading from a single source (file, |
| 874 | string or dictionary), raising :exc:`DuplicateSectionError` or |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 875 | :exc:`DuplicateOptionError`. When *empty_lines_in_values* is ``False`` |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 876 | (default: ``True``), each empty line marks the end of an option. Otherwise, |
| 877 | internal empty lines of a multiline option are kept as part of the value. |
| 878 | When *allow_no_value* is ``True`` (default: ``False``), options without |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 879 | values are accepted; the value held for these is ``None`` and they are |
| 880 | serialized without the trailing delimiter. |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 881 | |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 882 | When *default_section* is given, it specifies the name for the special |
| 883 | section holding default values for other sections and interpolation purposes |
| 884 | (normally named ``"DEFAULT"``). This value can be retrieved and changed on |
| 885 | runtime using the ``default_section`` instance attribute. |
| 886 | |
| 887 | Interpolation behaviour may be customized by providing a custom handler |
| 888 | through the *interpolation* argument. ``None`` can be used to turn off |
| 889 | interpolation completely, ``ExtendedInterpolation()`` provides a more |
| 890 | advanced variant inspired by ``zc.buildout``. More on the subject in the |
| 891 | `dedicated documentation section <#interpolation-of-values>`_. |
| 892 | |
| 893 | All option names used in interpolation will be passed through the |
| 894 | :meth:`optionxform` method just like any other option name reference. For |
| 895 | example, using the default implementation of :meth:`optionxform` (which |
| 896 | converts option names to lower case), the values ``foo %(bar)s`` and ``foo |
| 897 | %(BAR)s`` are equivalent. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 898 | |
Raymond Hettinger | 231b7f1 | 2009-03-03 00:23:19 +0000 | [diff] [blame] | 899 | .. versionchanged:: 3.1 |
Raymond Hettinger | 0663a1e | 2009-03-02 23:06:00 +0000 | [diff] [blame] | 900 | The default *dict_type* is :class:`collections.OrderedDict`. |
| 901 | |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 902 | .. versionchanged:: 3.2 |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 903 | *allow_no_value*, *delimiters*, *comment_prefixes*, *strict*, |
| 904 | *empty_lines_in_values*, *default_section* and *interpolation* were |
| 905 | added. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 906 | |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 907 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 908 | .. method:: defaults() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 909 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 910 | Return a dictionary containing the instance-wide defaults. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 911 | |
| 912 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 913 | .. method:: sections() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 914 | |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 915 | Return a list of the sections available; the *default section* is not |
| 916 | included in the list. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 917 | |
| 918 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 919 | .. method:: add_section(section) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 920 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 921 | Add a section named *section* to the instance. If a section by the given |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 922 | name already exists, :exc:`DuplicateSectionError` is raised. If the |
Łukasz Langa | 2cf9ddb | 2010-12-04 12:46:01 +0000 | [diff] [blame] | 923 | *default section* name is passed, :exc:`ValueError` is raised. The name |
| 924 | of the section must be a string; if not, :exc:`TypeError` is raised. |
| 925 | |
| 926 | .. versionchanged:: 3.2 |
| 927 | Non-string section names raise :exc:`TypeError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 928 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 929 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 930 | .. method:: has_section(section) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 931 | |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 932 | Indicates whether the named *section* is present in the configuration. |
| 933 | The *default section* is not acknowledged. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 934 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 935 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 936 | .. method:: options(section) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 937 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 938 | Return a list of options available in the specified *section*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 939 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 940 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 941 | .. method:: has_option(section, option) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 942 | |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 943 | If the given *section* exists, and contains the given *option*, return |
Łukasz Langa | 71b37a5 | 2010-12-17 21:56:32 +0000 | [diff] [blame] | 944 | :const:`True`; otherwise return :const:`False`. If the specified |
| 945 | *section* is :const:`None` or an empty string, DEFAULT is assumed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 946 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 947 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 948 | .. method:: read(filenames, encoding=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 949 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 950 | Attempt to read and parse a list of filenames, returning a list of |
| 951 | filenames which were successfully parsed. If *filenames* is a string, it |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 952 | is treated as a single filename. If a file named in *filenames* cannot |
| 953 | be opened, that file will be ignored. This is designed so that you can |
| 954 | specify a list of potential configuration file locations (for example, |
| 955 | the current directory, the user's home directory, and some system-wide |
| 956 | directory), and all existing configuration files in the list will be |
| 957 | read. If none of the named files exist, the :class:`ConfigParser` |
| 958 | instance will contain an empty dataset. An application which requires |
| 959 | initial values to be loaded from a file should load the required file or |
| 960 | files using :meth:`read_file` before calling :meth:`read` for any |
| 961 | optional files:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 962 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 963 | import configparser, os |
Georg Brandl | 8dcaa73 | 2010-07-29 12:17:40 +0000 | [diff] [blame] | 964 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 965 | config = configparser.ConfigParser() |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 966 | config.read_file(open('defaults.cfg')) |
| 967 | config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')], |
| 968 | encoding='cp1250') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 969 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 970 | .. versionadded:: 3.2 |
| 971 | The *encoding* parameter. Previously, all files were read using the |
| 972 | default encoding for :func:`open`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 973 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 974 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 975 | .. method:: read_file(f, source=None) |
Georg Brandl | 73753d3 | 2009-09-22 13:53:14 +0000 | [diff] [blame] | 976 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 977 | Read and parse configuration data from the file or file-like object in |
| 978 | *f* (only the :meth:`readline` method is used). The file-like object |
| 979 | must operate in text mode. Specifically, it must return strings from |
| 980 | :meth:`readline`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 981 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 982 | Optional argument *source* specifies the name of the file being read. If |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 983 | not given and *f* has a :attr:`name` attribute, that is used for |
| 984 | *source*; the default is ``'<???>'``. |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 985 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 986 | .. versionadded:: 3.2 |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 987 | Replaces :meth:`readfp`. |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 988 | |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 989 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 990 | .. method:: read_string(string, source='<string>') |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 991 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 992 | Parse configuration data from a string. |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 993 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 994 | Optional argument *source* specifies a context-specific name of the |
| 995 | string passed. If not given, ``'<string>'`` is used. This should |
| 996 | commonly be a filesystem path or a URL. |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 997 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 998 | .. versionadded:: 3.2 |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 999 | |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 1000 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1001 | .. method:: read_dict(dictionary, source='<dict>') |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 1002 | |
Łukasz Langa | 71b37a5 | 2010-12-17 21:56:32 +0000 | [diff] [blame] | 1003 | Load configuration from any object that provides a dict-like ``items()`` |
| 1004 | method. Keys are section names, values are dictionaries with keys and |
| 1005 | values that should be present in the section. If the used dictionary |
| 1006 | type preserves order, sections and their keys will be added in order. |
| 1007 | Values are automatically converted to strings. |
Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 1008 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1009 | Optional argument *source* specifies a context-specific name of the |
| 1010 | dictionary passed. If not given, ``<dict>`` is used. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1011 | |
Łukasz Langa | 71b37a5 | 2010-12-17 21:56:32 +0000 | [diff] [blame] | 1012 | This method can be used to copy state between parsers. |
| 1013 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1014 | .. versionadded:: 3.2 |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 1015 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1016 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1017 | .. method:: get(section, option, raw=False, [vars, fallback]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1018 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1019 | Get an *option* value for the named *section*. If *vars* is provided, it |
| 1020 | must be a dictionary. The *option* is looked up in *vars* (if provided), |
| 1021 | *section*, and in *DEFAULTSECT* in that order. If the key is not found |
| 1022 | and *fallback* is provided, it is used as a fallback value. ``None`` can |
| 1023 | be provided as a *fallback* value. |
Georg Brandl | 470a123 | 2010-07-29 14:17:12 +0000 | [diff] [blame] | 1024 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1025 | All the ``'%'`` interpolations are expanded in the return values, unless |
| 1026 | the *raw* argument is true. Values for interpolation keys are looked up |
| 1027 | in the same manner as the option. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1028 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1029 | .. versionchanged:: 3.2 |
| 1030 | Arguments *raw*, *vars* and *fallback* are keyword only to protect |
| 1031 | users from trying to use the third argument as the *fallback* fallback |
| 1032 | (especially when using the mapping protocol). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1033 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1034 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1035 | .. method:: getint(section, option, raw=False, [vars, fallback]) |
Fred Drake | cc645b9 | 2010-09-04 04:35:34 +0000 | [diff] [blame] | 1036 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1037 | A convenience method which coerces the *option* in the specified *section* |
| 1038 | to an integer. See :meth:`get` for explanation of *raw*, *vars* and |
| 1039 | *fallback*. |
Fred Drake | cc645b9 | 2010-09-04 04:35:34 +0000 | [diff] [blame] | 1040 | |
| 1041 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1042 | .. method:: getfloat(section, option, raw=False, [vars, fallback]) |
Fred Drake | cc645b9 | 2010-09-04 04:35:34 +0000 | [diff] [blame] | 1043 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1044 | A convenience method which coerces the *option* in the specified *section* |
| 1045 | to a floating point number. See :meth:`get` for explanation of *raw*, |
| 1046 | *vars* and *fallback*. |
Fred Drake | cc645b9 | 2010-09-04 04:35:34 +0000 | [diff] [blame] | 1047 | |
| 1048 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1049 | .. method:: getboolean(section, option, raw=False, [vars, fallback]) |
Fred Drake | cc645b9 | 2010-09-04 04:35:34 +0000 | [diff] [blame] | 1050 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1051 | A convenience method which coerces the *option* in the specified *section* |
| 1052 | to a Boolean value. Note that the accepted values for the option are |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 1053 | ``'1'``, ``'yes'``, ``'true'``, and ``'on'``, which cause this method to |
| 1054 | return ``True``, and ``'0'``, ``'no'``, ``'false'``, and ``'off'``, which |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1055 | cause it to return ``False``. These string values are checked in a |
| 1056 | case-insensitive manner. Any other value will cause it to raise |
| 1057 | :exc:`ValueError`. See :meth:`get` for explanation of *raw*, *vars* and |
| 1058 | *fallback*. |
Fred Drake | cc645b9 | 2010-09-04 04:35:34 +0000 | [diff] [blame] | 1059 | |
| 1060 | |
Łukasz Langa | 71b37a5 | 2010-12-17 21:56:32 +0000 | [diff] [blame] | 1061 | .. method:: items([section], raw=False, vars=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1062 | |
Łukasz Langa | 71b37a5 | 2010-12-17 21:56:32 +0000 | [diff] [blame] | 1063 | When *section* is not given, return a list of *section_name*, |
| 1064 | *section_proxy* pairs, including DEFAULTSECT. |
| 1065 | |
| 1066 | Otherwise, return a list of *name*, *value* pairs for the options in the |
| 1067 | given *section*. Optional arguments have the same meaning as for the |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1068 | :meth:`get` method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1069 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1070 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1071 | .. method:: set(section, option, value) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1072 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1073 | If the given section exists, set the given option to the specified value; |
Łukasz Langa | 2cf9ddb | 2010-12-04 12:46:01 +0000 | [diff] [blame] | 1074 | otherwise raise :exc:`NoSectionError`. *option* and *value* must be |
| 1075 | strings; if not, :exc:`TypeError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1076 | |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1077 | |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 1078 | .. method:: write(fileobject, space_around_delimiters=True) |
| 1079 | |
| 1080 | Write a representation of the configuration to the specified :term:`file |
| 1081 | object`, which must be opened in text mode (accepting strings). This |
| 1082 | representation can be parsed by a future :meth:`read` call. If |
| 1083 | *space_around_delimiters* is true, delimiters between |
| 1084 | keys and values are surrounded by spaces. |
| 1085 | |
| 1086 | |
| 1087 | .. method:: remove_option(section, option) |
| 1088 | |
| 1089 | Remove the specified *option* from the specified *section*. If the |
| 1090 | section does not exist, raise :exc:`NoSectionError`. If the option |
| 1091 | existed to be removed, return :const:`True`; otherwise return |
| 1092 | :const:`False`. |
| 1093 | |
| 1094 | |
| 1095 | .. method:: remove_section(section) |
| 1096 | |
| 1097 | Remove the specified *section* from the configuration. If the section in |
| 1098 | fact existed, return ``True``. Otherwise return ``False``. |
| 1099 | |
| 1100 | |
| 1101 | .. method:: optionxform(option) |
| 1102 | |
| 1103 | Transforms the option name *option* as found in an input file or as passed |
| 1104 | in by client code to the form that should be used in the internal |
| 1105 | structures. The default implementation returns a lower-case version of |
| 1106 | *option*; subclasses may override this or client code can set an attribute |
| 1107 | of this name on instances to affect this behavior. |
| 1108 | |
| 1109 | You don't need to subclass the parser to use this method, you can also |
| 1110 | set it on an instance, to a function that takes a string argument and |
| 1111 | returns a string. Setting it to ``str``, for example, would make option |
| 1112 | names case sensitive:: |
| 1113 | |
| 1114 | cfgparser = ConfigParser() |
| 1115 | cfgparser.optionxform = str |
| 1116 | |
| 1117 | Note that when reading configuration files, whitespace around the option |
| 1118 | names is stripped before :meth:`optionxform` is called. |
| 1119 | |
| 1120 | |
| 1121 | .. method:: readfp(fp, filename=None) |
| 1122 | |
| 1123 | .. deprecated:: 3.2 |
| 1124 | Use :meth:`read_file` instead. |
| 1125 | |
| 1126 | |
| 1127 | .. data:: MAX_INTERPOLATION_DEPTH |
| 1128 | |
| 1129 | The maximum depth for recursive interpolation for :meth:`get` when the *raw* |
| 1130 | parameter is false. This is relevant only when the default *interpolation* |
| 1131 | is used. |
| 1132 | |
| 1133 | |
| 1134 | .. _rawconfigparser-objects: |
| 1135 | |
| 1136 | RawConfigParser Objects |
| 1137 | ----------------------- |
| 1138 | |
Łukasz Langa | b25a791 | 2010-12-17 01:32:29 +0000 | [diff] [blame] | 1139 | .. class:: RawConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=configaparser.DEFAULTSECT, interpolation=None) |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 1140 | |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 1141 | Legacy variant of the :class:`ConfigParser` with interpolation disabled |
Łukasz Langa | 2cf9ddb | 2010-12-04 12:46:01 +0000 | [diff] [blame] | 1142 | by default and unsafe ``add_section`` and ``set`` methods. |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 1143 | |
| 1144 | .. note:: |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 1145 | Consider using :class:`ConfigParser` instead which checks types of |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 1146 | the values to be stored internally. If you don't want interpolation, you |
Łukasz Langa | 7f64c8a | 2010-12-16 01:16:22 +0000 | [diff] [blame] | 1147 | can use ``ConfigParser(interpolation=None)``. |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 1148 | |
| 1149 | |
Łukasz Langa | 2cf9ddb | 2010-12-04 12:46:01 +0000 | [diff] [blame] | 1150 | .. method:: add_section(section) |
| 1151 | |
| 1152 | Add a section named *section* to the instance. If a section by the given |
| 1153 | name already exists, :exc:`DuplicateSectionError` is raised. If the |
| 1154 | *default section* name is passed, :exc:`ValueError` is raised. |
| 1155 | |
| 1156 | Type of *section* is not checked which lets users create non-string named |
| 1157 | sections. This behaviour is unsupported and may cause internal errors. |
| 1158 | |
| 1159 | |
Łukasz Langa | b6a6f5f | 2010-12-03 16:28:00 +0000 | [diff] [blame] | 1160 | .. method:: set(section, option, value) |
| 1161 | |
| 1162 | If the given section exists, set the given option to the specified value; |
| 1163 | otherwise raise :exc:`NoSectionError`. While it is possible to use |
| 1164 | :class:`RawConfigParser` (or :class:`ConfigParser` with *raw* parameters |
| 1165 | set to true) for *internal* storage of non-string values, full |
| 1166 | functionality (including interpolation and output to files) can only be |
| 1167 | achieved using string values. |
| 1168 | |
| 1169 | This method lets users assign non-string values to keys internally. This |
| 1170 | behaviour is unsupported and will cause errors when attempting to write |
| 1171 | to a file or get it in non-raw mode. **Use the mapping protocol API** |
| 1172 | which does not allow such assignments to take place. |
| 1173 | |
| 1174 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1175 | Exceptions |
| 1176 | ---------- |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1177 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1178 | .. exception:: Error |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1179 | |
Fred Drake | 5a7c11f | 2010-11-13 05:24:17 +0000 | [diff] [blame] | 1180 | Base class for all other :mod:`configparser` exceptions. |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1181 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 1182 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1183 | .. exception:: NoSectionError |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 1184 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1185 | Exception raised when a specified section is not found. |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1186 | |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1187 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1188 | .. exception:: DuplicateSectionError |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1189 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1190 | Exception raised if :meth:`add_section` is called with the name of a section |
| 1191 | that is already present or in strict parsers when a section if found more |
| 1192 | than once in a single input file, string or dictionary. |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1193 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1194 | .. versionadded:: 3.2 |
| 1195 | Optional ``source`` and ``lineno`` attributes and arguments to |
| 1196 | :meth:`__init__` were added. |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1197 | |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1198 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1199 | .. exception:: DuplicateOptionError |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1200 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1201 | Exception raised by strict parsers if a single option appears twice during |
| 1202 | reading from a single file, string or dictionary. This catches misspellings |
| 1203 | and case sensitivity-related errors, e.g. a dictionary may have two keys |
| 1204 | representing the same case-insensitive configuration key. |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1205 | |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1206 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1207 | .. exception:: NoOptionError |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1208 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1209 | Exception raised when a specified option is not found in the specified |
| 1210 | section. |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1211 | |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1212 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1213 | .. exception:: InterpolationError |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1214 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1215 | Base class for exceptions raised when problems occur performing string |
| 1216 | interpolation. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 1217 | |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1218 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1219 | .. exception:: InterpolationDepthError |
Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 1220 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1221 | Exception raised when string interpolation cannot be completed because the |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1222 | number of iterations exceeds :const:`MAX_INTERPOLATION_DEPTH`. Subclass of |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1223 | :exc:`InterpolationError`. |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 1224 | |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 1225 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1226 | .. exception:: InterpolationMissingOptionError |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 1227 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1228 | Exception raised when an option referenced from a value does not exist. |
| 1229 | Subclass of :exc:`InterpolationError`. |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 1230 | |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 1231 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1232 | .. exception:: InterpolationSyntaxError |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 1233 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1234 | Exception raised when the source text into which substitutions are made does |
| 1235 | not conform to the required syntax. Subclass of :exc:`InterpolationError`. |
Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 1236 | |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1237 | |
| 1238 | .. exception:: MissingSectionHeaderError |
| 1239 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1240 | Exception raised when attempting to parse a file which has no section |
| 1241 | headers. |
Łukasz Langa | 26d513c | 2010-11-10 18:57:39 +0000 | [diff] [blame] | 1242 | |
| 1243 | |
| 1244 | .. exception:: ParsingError |
| 1245 | |
| 1246 | Exception raised when errors occur attempting to parse a file. |
| 1247 | |
| 1248 | .. versionchanged:: 3.2 |
| 1249 | The ``filename`` attribute and :meth:`__init__` argument were renamed to |
| 1250 | ``source`` for consistency. |
| 1251 | |
Georg Brandl | bb27c12 | 2010-11-11 07:26:40 +0000 | [diff] [blame] | 1252 | |
| 1253 | .. rubric:: Footnotes |
| 1254 | |
| 1255 | .. [1] Config parsers allow for heavy customization. If you are interested in |
| 1256 | changing the behaviour outlined by the footnote reference, consult the |
| 1257 | `Customizing Parser Behaviour`_ section. |