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