| 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> | 
|  | 10 | .. sectionauthor:: Christopher G. Petrilli <petrilli@amber.org> | 
|  | 11 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | .. index:: | 
|  | 13 | pair: .ini; file | 
|  | 14 | pair: configuration; file | 
|  | 15 | single: ini file | 
|  | 16 | single: Windows ini file | 
|  | 17 |  | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 18 | This module provides the classes :class:`RawConfigParser` and | 
|  | 19 | :class:`SafeConfigParser`.  They implement a basic configuration file parser | 
|  | 20 | language which provides a structure similar to what you would find in Microsoft | 
|  | 21 | Windows INI files.  You can use this to write Python programs which can be | 
|  | 22 | customized by end users easily. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 |  | 
| Georg Brandl | e720c0a | 2009-04-27 16:20:50 +0000 | [diff] [blame] | 24 | .. note:: | 
| 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 | This library does *not* interpret or write the value-type prefixes used in | 
|  | 27 | the Windows Registry extended version of INI syntax. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 |  | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 29 | A configuration file consists of sections, each led by a ``[section]`` header, | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 30 | followed by key/value entries separated by a specific string (``=`` or ``:`` by | 
|  | 31 | default). By default, section names are case sensitive but keys are not. Leading | 
|  | 32 | und trailing whitespace is removed from keys and from values.  Values can be | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 33 | ommitted, in which case the key/value delimiter may also be left out.  Values | 
|  | 34 | can also span multiple lines, as long as they are indented deeper than the first | 
|  | 35 | line of the value.  Depending on the parser's mode, blank lines may be treated | 
|  | 36 | as parts of multiline values or ignored. | 
|  | 37 |  | 
|  | 38 | Configuration files may include comments, prefixed by specific characters (``#`` | 
|  | 39 | and ``;`` by default).  Comments may appear on their own in an otherwise empty | 
|  | 40 | line, or may be entered in lines holding values or spection names.  In the | 
|  | 41 | latter case, they need to be preceded by a whitespace character to be recognized | 
|  | 42 | as a comment.  (For backwards compatibility, by default only ``;`` starts an | 
|  | 43 | inline comment, while ``#`` does not.) | 
|  | 44 |  | 
|  | 45 | On top of the core functionality, :class:`SafeConfigParser` supports | 
|  | 46 | interpolation.  This means values can contain format strings which refer to | 
|  | 47 | other values in the same section, or values in a special ``DEFAULT`` section. | 
| Georg Brandl | 470a123 | 2010-07-29 14:17:12 +0000 | [diff] [blame] | 48 | Additional defaults can be provided on initialization. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 |  | 
|  | 50 | For example:: | 
|  | 51 |  | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 52 | [Paths] | 
|  | 53 | home_dir: /Users | 
|  | 54 | my_dir: %(home_dir)s/lumberjack | 
|  | 55 | my_pictures: %(my_dir)s/Pictures | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 |  | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 57 | [Multiline Values] | 
|  | 58 | chorus: I'm a lumberjack, and I'm okay | 
|  | 59 | I sleep all night and I work all day | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 |  | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 61 | [No Values] | 
|  | 62 | key_without_value | 
|  | 63 | empty string value here = | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 |  | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 65 | [You can use comments] ; after a useful line | 
|  | 66 | ; in an empty line | 
|  | 67 | after: a_value ; here's another comment | 
|  | 68 | inside: a         ;comment | 
|  | 69 | multiline ;comment | 
|  | 70 | value!    ;comment | 
|  | 71 |  | 
|  | 72 | [Sections Can Be Indented] | 
|  | 73 | can_values_be_as_well = True | 
|  | 74 | does_that_mean_anything_special = False | 
|  | 75 | purpose = formatting for readability | 
|  | 76 | multiline_values = are | 
|  | 77 | handled just fine as | 
|  | 78 | long as they are indented | 
|  | 79 | deeper than the first line | 
|  | 80 | of a value | 
|  | 81 | # Did I mention we can indent comments, too? | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 82 |  | 
|  | 83 |  | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 84 | In the example above, :class:`SafeConfigParser` would resolve ``%(home_dir)s`` | 
|  | 85 | to the value of ``home_dir`` (``/Users`` in this case).  ``%(my_dir)s`` in | 
|  | 86 | effect would resolve to ``/Users/lumberjack``.  All interpolations are done on | 
|  | 87 | demand so keys used in the chain of references do not have to be specified in | 
|  | 88 | any specific order in the configuration file. | 
|  | 89 |  | 
|  | 90 | :class:`RawConfigParser` would simply return ``%(my_dir)s/Pictures`` as the | 
|  | 91 | value of ``my_pictures`` and ``%(home_dir)s/lumberjack`` as the value of | 
|  | 92 | ``my_dir``.  Other features presented in the example are handled in the same | 
|  | 93 | manner by both parsers. | 
|  | 94 |  | 
|  | 95 | Default values can be specified by passing them as a dictionary when | 
| Georg Brandl | 470a123 | 2010-07-29 14:17:12 +0000 | [diff] [blame] | 96 | constructing the :class:`SafeConfigParser`. | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 97 |  | 
|  | 98 | Sections are normally stored in an :class:`collections.OrderedDict` which | 
|  | 99 | maintains the order of all keys.  An alternative dictionary type can be passed | 
|  | 100 | to the :meth:`__init__` method.  For example, if a dictionary type is passed | 
|  | 101 | that sorts its keys, the sections will be sorted on write-back, as will be the | 
|  | 102 | keys within each section. | 
|  | 103 |  | 
|  | 104 |  | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 105 | .. 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] | 106 |  | 
|  | 107 | The basic configuration object.  When *defaults* is given, it is initialized | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 108 | into the dictionary of intrinsic defaults.  When *dict_type* is given, it | 
|  | 109 | will be used to create the dictionary objects for the list of sections, for | 
|  | 110 | the options within a section, and for the default values. | 
|  | 111 |  | 
|  | 112 | When *delimiters* is given, it will be used as the set of substrings that | 
|  | 113 | divide keys from values.  When *comment_prefixes* is given, it will be used | 
|  | 114 | as the set of substrings that prefix comments in a line, both for the whole | 
|  | 115 | line and inline comments.  For backwards compatibility, the default value for | 
|  | 116 | *comment_prefixes* is a special value that indicates that ``;`` and ``#`` can | 
|  | 117 | start whole line comments while only ``;`` can start inline comments. | 
|  | 118 |  | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 119 | When *strict* is ``True`` (default: ``False``), the parser won't allow for | 
|  | 120 | any section or option duplicates while reading from a single source (file, | 
|  | 121 | string or dictionary), raising :exc:`DuplicateSectionError` or | 
|  | 122 | :exc:`DuplicateOptionError`. When *empty_lines_in_values* is ``False`` | 
|  | 123 | (default: ``True``), each empty line marks the end of an option.  Otherwise, | 
|  | 124 | internal empty lines of a multiline option are kept as part of the value. | 
|  | 125 | When *allow_no_value* is ``True`` (default: ``False``), options without | 
|  | 126 | values are accepted; the value presented for these is ``None``. | 
| Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 127 |  | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 128 | This class does not support the magical interpolation behavior. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 129 |  | 
| Raymond Hettinger | 231b7f1 | 2009-03-03 00:23:19 +0000 | [diff] [blame] | 130 | .. versionchanged:: 3.1 | 
| Raymond Hettinger | 0663a1e | 2009-03-02 23:06:00 +0000 | [diff] [blame] | 131 | The default *dict_type* is :class:`collections.OrderedDict`. | 
|  | 132 |  | 
| Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 133 | .. versionchanged:: 3.2 | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 134 | *allow_no_value*, *delimiters*, *comment_prefixes*, *strict* and | 
|  | 135 | *empty_lines_in_values* were added. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 136 |  | 
| Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 137 |  | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 138 | .. class:: SafeConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), strict=False, empty_lines_in_values=True) | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 139 |  | 
|  | 140 | Derived class of :class:`ConfigParser` that implements a sane variant of the | 
|  | 141 | magical interpolation feature.  This implementation is more predictable as it | 
|  | 142 | validates the interpolation syntax used within a configuration file.  This | 
|  | 143 | class also enables escaping the interpolation character (e.g. a key can have | 
|  | 144 | ``%`` as part of the value by specifying ``%%`` in the file). | 
|  | 145 |  | 
|  | 146 | Applications that don't require interpolation should use | 
|  | 147 | :class:`RawConfigParser`, otherwise :class:`SafeConfigParser` is the best | 
|  | 148 | option. | 
|  | 149 |  | 
|  | 150 | .. versionchanged:: 3.1 | 
|  | 151 | The default *dict_type* is :class:`collections.OrderedDict`. | 
|  | 152 |  | 
|  | 153 | .. versionchanged:: 3.2 | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 154 | *allow_no_value*, *delimiters*, *comment_prefixes*, *strict* and | 
|  | 155 | *empty_lines_in_values* were added. | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 156 |  | 
|  | 157 |  | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 158 | .. class:: ConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), strict=False, empty_lines_in_values=True) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 159 |  | 
|  | 160 | Derived class of :class:`RawConfigParser` that implements the magical | 
|  | 161 | interpolation feature and adds optional arguments to the :meth:`get` and | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 162 | :meth:`items` methods. | 
|  | 163 |  | 
|  | 164 | :class:`SafeConfigParser` is generally recommended over this class if you | 
|  | 165 | need interpolation. | 
|  | 166 |  | 
|  | 167 | The values in *defaults* must be appropriate for the ``%()s`` string | 
|  | 168 | interpolation.  Note that *__name__* is an intrinsic default; its value is | 
|  | 169 | the section name, and will override any value provided in *defaults*. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 170 |  | 
|  | 171 | All option names used in interpolation will be passed through the | 
|  | 172 | :meth:`optionxform` method just like any other option name reference.  For | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 173 | example, using the default implementation of :meth:`optionxform` (which | 
|  | 174 | converts option names to lower case), the values ``foo %(bar)s`` and ``foo | 
|  | 175 | %(BAR)s`` are equivalent. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 176 |  | 
| Raymond Hettinger | 231b7f1 | 2009-03-03 00:23:19 +0000 | [diff] [blame] | 177 | .. versionchanged:: 3.1 | 
| Raymond Hettinger | 0663a1e | 2009-03-02 23:06:00 +0000 | [diff] [blame] | 178 | The default *dict_type* is :class:`collections.OrderedDict`. | 
|  | 179 |  | 
| Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 180 | .. versionchanged:: 3.2 | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 181 | *allow_no_value*, *delimiters*, *comment_prefixes*, | 
|  | 182 | *strict* and *empty_lines_in_values* were added. | 
| Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 183 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 184 |  | 
| Georg Brandl | 436ccdc | 2010-07-29 14:32:22 +0000 | [diff] [blame] | 185 | .. exception:: Error | 
|  | 186 |  | 
|  | 187 | Base class for all other configparser exceptions. | 
|  | 188 |  | 
|  | 189 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 | .. exception:: NoSectionError | 
|  | 191 |  | 
|  | 192 | Exception raised when a specified section is not found. | 
|  | 193 |  | 
|  | 194 |  | 
|  | 195 | .. exception:: DuplicateSectionError | 
|  | 196 |  | 
|  | 197 | Exception raised if :meth:`add_section` is called with the name of a section | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 198 | that is already present or in strict parsers when a section if found more | 
|  | 199 | than once in a single input file, string or dictionary. | 
|  | 200 |  | 
|  | 201 | .. versionadded:: 3.2 | 
|  | 202 | Optional ``source`` and ``lineno`` attributes and arguments to | 
|  | 203 | :meth:`__init__` were added. | 
|  | 204 |  | 
|  | 205 |  | 
|  | 206 | .. exception:: DuplicateOptionError | 
|  | 207 |  | 
|  | 208 | Exception raised by strict parsers if a single option appears twice during | 
|  | 209 | reading from a single file, string or dictionary. This catches misspellings | 
|  | 210 | and case sensitivity-related errors, e.g. a dictionary may have two keys | 
|  | 211 | representing the same case-insensitive configuration key. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 212 |  | 
|  | 213 |  | 
|  | 214 | .. exception:: NoOptionError | 
|  | 215 |  | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 216 | Exception raised when a specified option is not found in the specified | 
|  | 217 | section. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 218 |  | 
|  | 219 |  | 
|  | 220 | .. exception:: InterpolationError | 
|  | 221 |  | 
|  | 222 | Base class for exceptions raised when problems occur performing string | 
|  | 223 | interpolation. | 
|  | 224 |  | 
|  | 225 |  | 
|  | 226 | .. exception:: InterpolationDepthError | 
|  | 227 |  | 
|  | 228 | Exception raised when string interpolation cannot be completed because the | 
|  | 229 | number of iterations exceeds :const:`MAX_INTERPOLATION_DEPTH`. Subclass of | 
|  | 230 | :exc:`InterpolationError`. | 
|  | 231 |  | 
|  | 232 |  | 
|  | 233 | .. exception:: InterpolationMissingOptionError | 
|  | 234 |  | 
|  | 235 | Exception raised when an option referenced from a value does not exist. Subclass | 
|  | 236 | of :exc:`InterpolationError`. | 
|  | 237 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 238 |  | 
|  | 239 | .. exception:: InterpolationSyntaxError | 
|  | 240 |  | 
|  | 241 | Exception raised when the source text into which substitutions are made does not | 
|  | 242 | conform to the required syntax. Subclass of :exc:`InterpolationError`. | 
|  | 243 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 244 |  | 
|  | 245 | .. exception:: MissingSectionHeaderError | 
|  | 246 |  | 
|  | 247 | Exception raised when attempting to parse a file which has no section headers. | 
|  | 248 |  | 
|  | 249 |  | 
|  | 250 | .. exception:: ParsingError | 
|  | 251 |  | 
|  | 252 | Exception raised when errors occur attempting to parse a file. | 
|  | 253 |  | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 254 | .. versionchanged:: 3.2 | 
|  | 255 | The ``filename`` attribute and :meth:`__init__` argument were renamed to | 
|  | 256 | ``source`` for consistency. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 257 |  | 
|  | 258 | .. data:: MAX_INTERPOLATION_DEPTH | 
|  | 259 |  | 
|  | 260 | The maximum depth for recursive interpolation for :meth:`get` when the *raw* | 
|  | 261 | parameter is false.  This is relevant only for the :class:`ConfigParser` class. | 
|  | 262 |  | 
|  | 263 |  | 
|  | 264 | .. seealso:: | 
|  | 265 |  | 
|  | 266 | Module :mod:`shlex` | 
|  | 267 | Support for a creating Unix shell-like mini-languages which can be used as an | 
|  | 268 | alternate format for application configuration files. | 
|  | 269 |  | 
|  | 270 |  | 
|  | 271 | .. _rawconfigparser-objects: | 
|  | 272 |  | 
|  | 273 | RawConfigParser Objects | 
|  | 274 | ----------------------- | 
|  | 275 |  | 
|  | 276 | :class:`RawConfigParser` instances have the following methods: | 
|  | 277 |  | 
|  | 278 |  | 
|  | 279 | .. method:: RawConfigParser.defaults() | 
|  | 280 |  | 
|  | 281 | Return a dictionary containing the instance-wide defaults. | 
|  | 282 |  | 
|  | 283 |  | 
|  | 284 | .. method:: RawConfigParser.sections() | 
|  | 285 |  | 
|  | 286 | Return a list of the sections available; ``DEFAULT`` is not included in the | 
|  | 287 | list. | 
|  | 288 |  | 
|  | 289 |  | 
|  | 290 | .. method:: RawConfigParser.add_section(section) | 
|  | 291 |  | 
|  | 292 | Add a section named *section* to the instance.  If a section by the given name | 
| Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 293 | already exists, :exc:`DuplicateSectionError` is raised. If the name | 
|  | 294 | ``DEFAULT`` (or any of it's case-insensitive variants) is passed, | 
|  | 295 | :exc:`ValueError` is raised. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 296 |  | 
|  | 297 | .. method:: RawConfigParser.has_section(section) | 
|  | 298 |  | 
|  | 299 | Indicates whether the named section is present in the configuration. The | 
|  | 300 | ``DEFAULT`` section is not acknowledged. | 
|  | 301 |  | 
|  | 302 |  | 
|  | 303 | .. method:: RawConfigParser.options(section) | 
|  | 304 |  | 
|  | 305 | Returns a list of options available in the specified *section*. | 
|  | 306 |  | 
|  | 307 |  | 
|  | 308 | .. method:: RawConfigParser.has_option(section, option) | 
|  | 309 |  | 
|  | 310 | If the given section exists, and contains the given option, return | 
|  | 311 | :const:`True`; otherwise return :const:`False`. | 
|  | 312 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 313 |  | 
| Georg Brandl | 8dcaa73 | 2010-07-29 12:17:40 +0000 | [diff] [blame] | 314 | .. method:: RawConfigParser.read(filenames, encoding=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 315 |  | 
|  | 316 | Attempt to read and parse a list of filenames, returning a list of filenames | 
| Georg Brandl | 8dcaa73 | 2010-07-29 12:17:40 +0000 | [diff] [blame] | 317 | which were successfully parsed.  If *filenames* is a string, it is treated as | 
|  | 318 | a single filename.  If a file named in *filenames* cannot be opened, that | 
|  | 319 | file will be ignored.  This is designed so that you can specify a list of | 
|  | 320 | potential configuration file locations (for example, the current directory, | 
|  | 321 | the user's home directory, and some system-wide directory), and all existing | 
|  | 322 | configuration files in the list will be read.  If none of the named files | 
|  | 323 | exist, the :class:`ConfigParser` instance will contain an empty dataset.  An | 
|  | 324 | application which requires initial values to be loaded from a file should | 
| Florent Xicluna | e3c39ae | 2010-08-15 20:21:26 +0000 | [diff] [blame] | 325 | load the required file or files using :meth:`read_file` before calling | 
| Georg Brandl | 8dcaa73 | 2010-07-29 12:17:40 +0000 | [diff] [blame] | 326 | :meth:`read` for any optional files:: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 327 |  | 
| Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 328 | import configparser, os | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 329 |  | 
| Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 330 | config = configparser.ConfigParser() | 
| Florent Xicluna | e3c39ae | 2010-08-15 20:21:26 +0000 | [diff] [blame] | 331 | config.read_file(open('defaults.cfg')) | 
| Georg Brandl | 8dcaa73 | 2010-07-29 12:17:40 +0000 | [diff] [blame] | 332 | config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')], encoding='cp1250') | 
|  | 333 |  | 
|  | 334 | .. versionadded:: 3.2 | 
|  | 335 | The *encoding* parameter.  Previously, all files were read using the | 
|  | 336 | default encoding for :func:`open`. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 337 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 338 |  | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 339 | .. method:: RawConfigParser.read_file(f, source=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 340 |  | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 341 | Read and parse configuration data from the file or file-like object in *f* | 
| Georg Brandl | 73753d3 | 2009-09-22 13:53:14 +0000 | [diff] [blame] | 342 | (only the :meth:`readline` method is used).  The file-like object must | 
|  | 343 | operate in text mode, i.e. return strings from :meth:`readline`. | 
|  | 344 |  | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 345 | Optional argument *source* specifies the name of the file being read. It not | 
|  | 346 | given and *f* has a :attr:`name` attribute, that is used for *source*; the | 
|  | 347 | default is ``<???>``. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 348 |  | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 349 | .. versionadded:: 3.2 | 
|  | 350 | Renamed from :meth:`readfp` (with the ``filename`` attribute renamed to | 
|  | 351 | ``source`` for consistency with other ``read_*`` methods). | 
|  | 352 |  | 
|  | 353 |  | 
|  | 354 | .. method:: RawConfigParser.read_string(string, source='<string>') | 
|  | 355 |  | 
|  | 356 | Parse configuration data from a given string. | 
|  | 357 |  | 
|  | 358 | Optional argument *source* specifies a context-specific name of the string | 
|  | 359 | passed. If not given, ``<string>`` is used. | 
|  | 360 |  | 
|  | 361 | .. versionadded:: 3.2 | 
|  | 362 |  | 
| Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame^] | 363 |  | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 364 | .. method:: RawConfigParser.read_dict(dictionary, source='<dict>') | 
|  | 365 |  | 
|  | 366 | Load configuration from a dictionary. Keys are section names, values are | 
|  | 367 | dictionaries with keys and values that should be present in the section. If | 
|  | 368 | the used dictionary type preserves order, sections and their keys will be | 
|  | 369 | added in order. | 
|  | 370 |  | 
|  | 371 | Optional argument *source* specifies a context-specific name of the | 
|  | 372 | dictionary passed.  If not given, ``<dict>`` is used. | 
|  | 373 |  | 
|  | 374 | .. versionadded:: 3.2 | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 375 |  | 
| Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame^] | 376 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 377 | .. method:: RawConfigParser.get(section, option) | 
|  | 378 |  | 
|  | 379 | Get an *option* value for the named *section*. | 
|  | 380 |  | 
|  | 381 |  | 
|  | 382 | .. method:: RawConfigParser.getint(section, option) | 
|  | 383 |  | 
|  | 384 | A convenience method which coerces the *option* in the specified *section* to an | 
|  | 385 | integer. | 
|  | 386 |  | 
|  | 387 |  | 
|  | 388 | .. method:: RawConfigParser.getfloat(section, option) | 
|  | 389 |  | 
|  | 390 | A convenience method which coerces the *option* in the specified *section* to a | 
|  | 391 | floating point number. | 
|  | 392 |  | 
|  | 393 |  | 
|  | 394 | .. method:: RawConfigParser.getboolean(section, option) | 
|  | 395 |  | 
|  | 396 | A convenience method which coerces the *option* in the specified *section* to a | 
|  | 397 | Boolean value.  Note that the accepted values for the option are ``"1"``, | 
|  | 398 | ``"yes"``, ``"true"``, and ``"on"``, which cause this method to return ``True``, | 
|  | 399 | and ``"0"``, ``"no"``, ``"false"``, and ``"off"``, which cause it to return | 
|  | 400 | ``False``.  These string values are checked in a case-insensitive manner.  Any | 
|  | 401 | other value will cause it to raise :exc:`ValueError`. | 
|  | 402 |  | 
|  | 403 |  | 
|  | 404 | .. method:: RawConfigParser.items(section) | 
|  | 405 |  | 
|  | 406 | Return a list of ``(name, value)`` pairs for each option in the given *section*. | 
|  | 407 |  | 
|  | 408 |  | 
|  | 409 | .. method:: RawConfigParser.set(section, option, value) | 
|  | 410 |  | 
|  | 411 | If the given section exists, set the given option to the specified value; | 
|  | 412 | otherwise raise :exc:`NoSectionError`.  While it is possible to use | 
|  | 413 | :class:`RawConfigParser` (or :class:`ConfigParser` with *raw* parameters set to | 
|  | 414 | true) for *internal* storage of non-string values, full functionality (including | 
|  | 415 | interpolation and output to files) can only be achieved using string values. | 
|  | 416 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 417 |  | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 418 | .. method:: RawConfigParser.write(fileobject, space_around_delimiters=True) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 419 |  | 
| Georg Brandl | 73753d3 | 2009-09-22 13:53:14 +0000 | [diff] [blame] | 420 | Write a representation of the configuration to the specified file object, | 
|  | 421 | which must be opened in text mode (accepting strings).  This representation | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 422 | can be parsed by a future :meth:`read` call. If ``space_around_delimiters`` | 
|  | 423 | is ``True`` (the default), delimiters between keys and values are surrounded | 
|  | 424 | by spaces. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 425 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 426 |  | 
|  | 427 | .. method:: RawConfigParser.remove_option(section, option) | 
|  | 428 |  | 
|  | 429 | Remove the specified *option* from the specified *section*. If the section does | 
|  | 430 | not exist, raise :exc:`NoSectionError`.  If the option existed to be removed, | 
|  | 431 | return :const:`True`; otherwise return :const:`False`. | 
|  | 432 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 433 |  | 
|  | 434 | .. method:: RawConfigParser.remove_section(section) | 
|  | 435 |  | 
|  | 436 | Remove the specified *section* from the configuration. If the section in fact | 
|  | 437 | existed, return ``True``. Otherwise return ``False``. | 
|  | 438 |  | 
|  | 439 |  | 
|  | 440 | .. method:: RawConfigParser.optionxform(option) | 
|  | 441 |  | 
| Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 442 | Transforms the option name *option* as found in an input file or as passed in | 
|  | 443 | by client code to the form that should be used in the internal structures. | 
|  | 444 | The default implementation returns a lower-case version of *option*; | 
|  | 445 | subclasses may override this or client code can set an attribute of this name | 
|  | 446 | on instances to affect this behavior. | 
|  | 447 |  | 
|  | 448 | You don't necessarily need to subclass a ConfigParser to use this method, you | 
|  | 449 | can also re-set it on an instance, to a function that takes a string | 
|  | 450 | argument.  Setting it to ``str``, for example, would make option names case | 
|  | 451 | sensitive:: | 
|  | 452 |  | 
|  | 453 | cfgparser = ConfigParser() | 
|  | 454 | ... | 
|  | 455 | cfgparser.optionxform = str | 
|  | 456 |  | 
|  | 457 | Note that when reading configuration files, whitespace around the | 
|  | 458 | option names are stripped before :meth:`optionxform` is called. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 459 |  | 
| Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame^] | 460 |  | 
| Fred Drake | a492362 | 2010-08-09 12:52:45 +0000 | [diff] [blame] | 461 | .. method:: RawConfigParser.readfp(fp, filename=None) | 
|  | 462 |  | 
|  | 463 | .. deprecated:: 3.2 | 
|  | 464 | Please use :meth:`read_file` instead. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 465 |  | 
| Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame^] | 466 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 467 | .. _configparser-objects: | 
|  | 468 |  | 
|  | 469 | ConfigParser Objects | 
|  | 470 | -------------------- | 
|  | 471 |  | 
|  | 472 | The :class:`ConfigParser` class extends some methods of the | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 473 | :class:`RawConfigParser` interface, adding some optional arguments. Whenever you | 
|  | 474 | can, consider using :class:`SafeConfigParser` which adds validation and escaping | 
|  | 475 | for the interpolation. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 476 |  | 
|  | 477 |  | 
| Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 478 | .. method:: ConfigParser.get(section, option, raw=False, vars=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 479 |  | 
| Georg Brandl | 470a123 | 2010-07-29 14:17:12 +0000 | [diff] [blame] | 480 | Get an *option* value for the named *section*.  If *vars* is provided, it | 
|  | 481 | must be a dictionary.  The *option* is looked up in *vars* (if provided), | 
|  | 482 | *section*, and in *defaults* in that order. | 
|  | 483 |  | 
|  | 484 | All the ``'%'`` interpolations are expanded in the return values, unless the | 
|  | 485 | *raw* argument is true.  Values for interpolation keys are looked up in the | 
|  | 486 | same manner as the option. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 487 |  | 
|  | 488 |  | 
| Georg Brandl | c2a4f4f | 2009-04-10 09:03:43 +0000 | [diff] [blame] | 489 | .. method:: ConfigParser.items(section, raw=False, vars=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 490 |  | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 491 | Return a list of ``(name, value)`` pairs for each option in the given | 
|  | 492 | *section*.  Optional arguments have the same meaning as for the :meth:`get` | 
|  | 493 | method. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 494 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 495 |  | 
|  | 496 | .. _safeconfigparser-objects: | 
|  | 497 |  | 
|  | 498 | SafeConfigParser Objects | 
|  | 499 | ------------------------ | 
|  | 500 |  | 
|  | 501 | The :class:`SafeConfigParser` class implements the same extended interface as | 
|  | 502 | :class:`ConfigParser`, with the following addition: | 
|  | 503 |  | 
|  | 504 |  | 
|  | 505 | .. method:: SafeConfigParser.set(section, option, value) | 
|  | 506 |  | 
|  | 507 | If the given section exists, set the given option to the specified value; | 
| Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 508 | otherwise raise :exc:`NoSectionError`.  *value* must be a string; if it is | 
|  | 509 | not, :exc:`TypeError` is raised. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 510 |  | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 511 |  | 
|  | 512 | Examples | 
|  | 513 | -------- | 
|  | 514 |  | 
|  | 515 | An example of writing to a configuration file:: | 
|  | 516 |  | 
| Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 517 | import configparser | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 518 |  | 
| Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 519 | config = configparser.RawConfigParser() | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 520 |  | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 521 | # When adding sections or items, add them in the reverse order of | 
|  | 522 | # how you want them to be displayed in the actual file. | 
|  | 523 | # In addition, please note that using RawConfigParser's and the raw | 
|  | 524 | # mode of ConfigParser's respective set functions, you can assign | 
|  | 525 | # non-string values to keys internally, but will receive an error | 
|  | 526 | # when attempting to write to a file or when you get it in non-raw | 
|  | 527 | # mode. SafeConfigParser does not allow such assignments to take place. | 
|  | 528 | config.add_section('Section1') | 
|  | 529 | config.set('Section1', 'int', '15') | 
|  | 530 | config.set('Section1', 'bool', 'true') | 
|  | 531 | config.set('Section1', 'float', '3.1415') | 
|  | 532 | config.set('Section1', 'baz', 'fun') | 
|  | 533 | config.set('Section1', 'bar', 'Python') | 
|  | 534 | config.set('Section1', 'foo', '%(bar)s is %(baz)s!') | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 535 |  | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 536 | # Writing our configuration file to 'example.cfg' | 
| Georg Brandl | 73753d3 | 2009-09-22 13:53:14 +0000 | [diff] [blame] | 537 | with open('example.cfg', 'w') as configfile: | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 538 | config.write(configfile) | 
|  | 539 |  | 
|  | 540 | An example of reading the configuration file again:: | 
|  | 541 |  | 
| Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 542 | import configparser | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 543 |  | 
| Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 544 | config = configparser.RawConfigParser() | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 545 | config.read('example.cfg') | 
|  | 546 |  | 
|  | 547 | # getfloat() raises an exception if the value is not a float | 
|  | 548 | # getint() and getboolean() also do this for their respective types | 
|  | 549 | float = config.getfloat('Section1', 'float') | 
|  | 550 | int = config.getint('Section1', 'int') | 
| Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 551 | print(float + int) | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 552 |  | 
|  | 553 | # Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'. | 
|  | 554 | # This is because we are using a RawConfigParser(). | 
|  | 555 | if config.getboolean('Section1', 'bool'): | 
| Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 556 | print(config.get('Section1', 'foo')) | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 557 |  | 
|  | 558 | To get interpolation, you will need to use a :class:`ConfigParser` or | 
|  | 559 | :class:`SafeConfigParser`:: | 
|  | 560 |  | 
| Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 561 | import configparser | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 562 |  | 
| Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 563 | config = configparser.ConfigParser() | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 564 | config.read('example.cfg') | 
|  | 565 |  | 
|  | 566 | # Set the third, optional argument of get to 1 if you wish to use raw mode. | 
| Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 567 | print(config.get('Section1', 'foo', 0)) # -> "Python is fun!" | 
|  | 568 | print(config.get('Section1', 'foo', 1)) # -> "%(bar)s is %(baz)s!" | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 569 |  | 
|  | 570 | # The optional fourth argument is a dict with members that will take | 
|  | 571 | # precedence in interpolation. | 
| Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 572 | print(config.get('Section1', 'foo', 0, {'bar': 'Documentation', | 
|  | 573 | 'baz': 'evil'})) | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 574 |  | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 575 | Defaults are available in all three types of ConfigParsers. They are used in | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 576 | interpolation if an option used is not defined elsewhere. :: | 
|  | 577 |  | 
| Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 578 | import configparser | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 579 |  | 
|  | 580 | # New instance with 'bar' and 'baz' defaulting to 'Life' and 'hard' each | 
| Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 581 | config = configparser.SafeConfigParser({'bar': 'Life', 'baz': 'hard'}) | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 582 | config.read('example.cfg') | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 583 |  | 
| Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 584 | print(config.get('Section1', 'foo')) # -> "Python is fun!" | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 585 | config.remove_option('Section1', 'bar') | 
|  | 586 | config.remove_option('Section1', 'baz') | 
| Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 587 | print(config.get('Section1', 'foo')) # -> "Life is hard!" | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 588 |  | 
|  | 589 | The function ``opt_move`` below can be used to move options between sections:: | 
|  | 590 |  | 
|  | 591 | def opt_move(config, section1, section2, option): | 
|  | 592 | try: | 
|  | 593 | config.set(section2, option, config.get(section1, option, 1)) | 
| Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 594 | except configparser.NoSectionError: | 
| Guido van Rossum | 2fd4f37 | 2007-11-29 18:43:05 +0000 | [diff] [blame] | 595 | # Create non-existent section | 
|  | 596 | config.add_section(section2) | 
|  | 597 | opt_move(config, section1, section2, option) | 
| Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 598 | else: | 
|  | 599 | config.remove_option(section1, option) | 
| Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 600 |  | 
|  | 601 | Some configuration files are known to include settings without values, but which | 
|  | 602 | otherwise conform to the syntax supported by :mod:`configparser`.  The | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 603 | *allow_no_value* parameter to the :meth:`__init__` method can be used to | 
|  | 604 | indicate that such values should be accepted: | 
| Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 605 |  | 
|  | 606 | .. doctest:: | 
|  | 607 |  | 
|  | 608 | >>> import configparser | 
|  | 609 | >>> import io | 
|  | 610 |  | 
|  | 611 | >>> sample_config = """ | 
|  | 612 | ... [mysqld] | 
| Georg Brandl | 96a60ae | 2010-07-28 13:13:46 +0000 | [diff] [blame] | 613 | ...   user = mysql | 
|  | 614 | ...   pid-file = /var/run/mysqld/mysqld.pid | 
|  | 615 | ...   skip-external-locking | 
|  | 616 | ...   old_passwords = 1 | 
|  | 617 | ...   skip-bdb | 
|  | 618 | ...   skip-innodb # we don't need ACID today | 
| Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 619 | ... """ | 
|  | 620 | >>> config = configparser.RawConfigParser(allow_no_value=True) | 
| Florent Xicluna | e3c39ae | 2010-08-15 20:21:26 +0000 | [diff] [blame] | 621 | >>> config.read_file(io.BytesIO(sample_config)) | 
| Fred Drake | 03c44a3 | 2010-02-19 06:08:41 +0000 | [diff] [blame] | 622 |  | 
|  | 623 | >>> # Settings with values are treated as before: | 
|  | 624 | >>> config.get("mysqld", "user") | 
|  | 625 | 'mysql' | 
|  | 626 |  | 
|  | 627 | >>> # Settings without values provide None: | 
|  | 628 | >>> config.get("mysqld", "skip-bdb") | 
|  | 629 |  | 
|  | 630 | >>> # Settings which aren't specified still raise an error: | 
|  | 631 | >>> config.get("mysqld", "does-not-exist") | 
|  | 632 | Traceback (most recent call last): | 
|  | 633 | ... | 
|  | 634 | configparser.NoOptionError: No option 'does-not-exist' in section: 'mysqld' |