mirror of
https://github.com/python/cpython.git
synced 2025-11-03 03:22:27 +00:00
fix copy-editing level nits
This commit is contained in:
parent
11c49a5213
commit
5a7c11f92f
1 changed files with 105 additions and 103 deletions
|
|
@ -18,8 +18,8 @@
|
||||||
single: Windows ini file
|
single: Windows ini file
|
||||||
|
|
||||||
This module provides the classes :class:`RawConfigParser` and
|
This module provides the classes :class:`RawConfigParser` and
|
||||||
:class:`SafeConfigParser`. They implement a basic configuration file parser
|
:class:`SafeConfigParser`. They implement a basic configuration
|
||||||
language which provides a structure similar to what you would find in Microsoft
|
language which provides a structure similar to what's found in Microsoft
|
||||||
Windows INI files. You can use this to write Python programs which can be
|
Windows INI files. You can use this to write Python programs which can be
|
||||||
customized by end users easily.
|
customized by end users easily.
|
||||||
|
|
||||||
|
|
@ -55,8 +55,8 @@ Let's take a very basic configuration file that looks like this:
|
||||||
Port = 50022
|
Port = 50022
|
||||||
ForwardX11 = no
|
ForwardX11 = no
|
||||||
|
|
||||||
The supported file structure of INI files is described `in the following section
|
The structure of INI files is described `in the following section
|
||||||
<#supported-ini-file-structure>`_, fow now all there is to know is that the file
|
<#supported-ini-file-structure>`_. Essentially, the file
|
||||||
consists of sections, each of which contains keys with values.
|
consists of sections, each of which contains keys with values.
|
||||||
:mod:`configparser` classes can read and write such files. Let's start by
|
:mod:`configparser` classes can read and write such files. Let's start by
|
||||||
creating the above configuration file programatically.
|
creating the above configuration file programatically.
|
||||||
|
|
@ -79,12 +79,12 @@ creating the above configuration file programatically.
|
||||||
... config.write(configfile)
|
... config.write(configfile)
|
||||||
...
|
...
|
||||||
|
|
||||||
As you can see, we can treat a config parser just like a dictionary. There are
|
As you can see, we can treat a config parser much like a dictionary.
|
||||||
a few differences, `outlined later on <#mapping-protocol-access>`_, but the
|
There are differences, `outlined later <#mapping-protocol-access>`_, but
|
||||||
behaviour is very close to what you would expect from a dictionary.
|
the behavior is very close to what you would expect from a dictionary.
|
||||||
|
|
||||||
Now that we have created and saved a configuration file, let's try reading it
|
Now that we have created and saved a configuration file, let's read it
|
||||||
back and exploring the data it holds.
|
back and explore the data it holds.
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
||||||
|
|
@ -119,10 +119,10 @@ back and exploring the data it holds.
|
||||||
>>> config['bitbucket.org']['ForwardX11']
|
>>> config['bitbucket.org']['ForwardX11']
|
||||||
'yes'
|
'yes'
|
||||||
|
|
||||||
As we can see above, the API is pretty straight forward. The only bit of magic
|
As we can see above, the API is pretty straightforward. The only bit of magic
|
||||||
involves the ``DEFAULT`` section which provides default values for all other
|
involves the ``DEFAULT`` section which provides default values for all other
|
||||||
sections [1]_. Another thing to note is that keys in sections are
|
sections [1]_. Note also that keys in sections are
|
||||||
case-insensitive so they're stored in lowercase [1]_.
|
case-insensitive and stored in lowercase [1]_.
|
||||||
|
|
||||||
|
|
||||||
Supported Datatypes
|
Supported Datatypes
|
||||||
|
|
@ -139,12 +139,12 @@ datatypes, you should convert on your own:
|
||||||
>>> float(topsecret['CompressionLevel'])
|
>>> float(topsecret['CompressionLevel'])
|
||||||
9.0
|
9.0
|
||||||
|
|
||||||
Converting to the boolean type is not that simple, though. Wrapping the return
|
Extracting Boolean values is not that simple, though. Passing the value
|
||||||
value around ``bool()`` would do us no good since ``bool('False')`` is still
|
to ``bool()`` would do no good since ``bool('False')`` is still
|
||||||
``True``. This is why config parsers also provide :meth:`getboolean`. This
|
``True``. This is why config parsers also provide :meth:`getboolean`.
|
||||||
handy method is also case insensitive and correctly recognizes boolean values
|
This method is case-insensitive and recognizes Boolean values from
|
||||||
from ``'yes'``/``'no'``, ``'on'``/``'off'`` and ``'1'``/``'0'`` [1]_. An
|
``'yes'``/``'no'``, ``'on'``/``'off'`` and ``'1'``/``'0'`` [1]_.
|
||||||
example of getting the boolean value:
|
For example:
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
||||||
|
|
@ -156,14 +156,15 @@ example of getting the boolean value:
|
||||||
True
|
True
|
||||||
|
|
||||||
Apart from :meth:`getboolean`, config parsers also provide equivalent
|
Apart from :meth:`getboolean`, config parsers also provide equivalent
|
||||||
:meth:`getint` and :meth:`getfloat` methods, but these are far less useful
|
:meth:`getint` and :meth:`getfloat` methods, but these are far less
|
||||||
because explicit casting is enough for these types.
|
useful since conversion using :func:`int` and :func:`float` is
|
||||||
|
sufficient for these types.
|
||||||
|
|
||||||
|
|
||||||
Fallback Values
|
Fallback Values
|
||||||
---------------
|
---------------
|
||||||
|
|
||||||
As with a regular dictionary, you can use a section's :meth:`get` method to
|
As with a dictionary, you can use a section's :meth:`get` method to
|
||||||
provide fallback values:
|
provide fallback values:
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
@ -176,11 +177,11 @@ provide fallback values:
|
||||||
>>> topsecret.get('Cipher', '3des-cbc')
|
>>> topsecret.get('Cipher', '3des-cbc')
|
||||||
'3des-cbc'
|
'3des-cbc'
|
||||||
|
|
||||||
Please note that default values have precedence over fallback values. For
|
Please note that default values have precedence over fallback values.
|
||||||
instance, in our example the ``CompressionLevel`` key was specified only in the
|
For instance, in our example the ``'CompressionLevel'`` key was
|
||||||
``DEFAULT`` section. If we try to get it from the section
|
specified only in the ``'DEFAULT'`` section. If we try to get it from
|
||||||
``topsecret.server.com``, we will always get the default, even if we specify a
|
the section ``'topsecret.server.com'``, we will always get the default,
|
||||||
fallback:
|
even if we specify a fallback:
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
||||||
|
|
@ -189,8 +190,8 @@ fallback:
|
||||||
|
|
||||||
One more thing to be aware of is that the parser-level :meth:`get` method
|
One more thing to be aware of is that the parser-level :meth:`get` method
|
||||||
provides a custom, more complex interface, maintained for backwards
|
provides a custom, more complex interface, maintained for backwards
|
||||||
compatibility. When using this method, a fallback value can be provided via the
|
compatibility. When using this method, a fallback value can be provided via
|
||||||
``fallback`` keyword-only argument:
|
the ``fallback`` keyword-only argument:
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
||||||
|
|
@ -218,18 +219,19 @@ Supported INI File Structure
|
||||||
A configuration file consists of sections, each led by a ``[section]`` header,
|
A configuration file consists of sections, each led by a ``[section]`` header,
|
||||||
followed by key/value entries separated by a specific string (``=`` or ``:`` by
|
followed by key/value entries separated by a specific string (``=`` or ``:`` by
|
||||||
default [1]_). By default, section names are case sensitive but keys are not
|
default [1]_). By default, section names are case sensitive but keys are not
|
||||||
[1]_. Leading und trailing whitespace is removed from keys and from values.
|
[1]_. Leading and trailing whitespace is removed from keys and values.
|
||||||
Values can be omitted, in which case the key/value delimiter may also be left
|
Values can be omitted, in which case the key/value delimiter may also be left
|
||||||
out. Values can also span multiple lines, as long as they are indented deeper
|
out. Values can also span multiple lines, as long as they are indented deeper
|
||||||
than the first line of the value. Depending on the parser's mode, blank lines
|
than the first line of the value. Depending on the parser's mode, blank lines
|
||||||
may be treated as parts of multiline values or ignored.
|
may be treated as parts of multiline values or ignored.
|
||||||
|
|
||||||
Configuration files may include comments, prefixed by specific characters (``#``
|
Configuration files may include comments, prefixed by specific
|
||||||
and ``;`` by default [1]_). Comments may appear on their own in an otherwise
|
characters (``#`` and ``;`` by default [1]_). Comments may appear on
|
||||||
empty line, or may be entered in lines holding values or section names. In the
|
their own on an otherwise empty line, or may be entered on lines holding
|
||||||
latter case, they need to be preceded by a whitespace character to be recognized
|
values or section names. In the latter case, they need to be preceded
|
||||||
as a comment. (For backwards compatibility, by default only ``;`` starts an
|
by a whitespace character to be recognized as a comment. For backwards
|
||||||
inline comment, while ``#`` does not [1]_.)
|
compatibility, by default only ``;`` starts an inline comment, while
|
||||||
|
``#`` does not [1]_.
|
||||||
|
|
||||||
On top of the core functionality, :class:`SafeConfigParser` supports
|
On top of the core functionality, :class:`SafeConfigParser` supports
|
||||||
interpolation. This means values can contain format strings which refer to
|
interpolation. This means values can contain format strings which refer to
|
||||||
|
|
@ -306,7 +308,7 @@ However, there are a few differences that should be taken into account:
|
||||||
* By default, all keys in sections are accessible in a case-insensitive manner
|
* By default, all keys in sections are accessible in a case-insensitive manner
|
||||||
[1]_. E.g. ``for option in parser["section"]`` yields only ``optionxform``'ed
|
[1]_. E.g. ``for option in parser["section"]`` yields only ``optionxform``'ed
|
||||||
option key names. This means lowercased keys by default. At the same time,
|
option key names. This means lowercased keys by default. At the same time,
|
||||||
for a section that holds the key ``"a"``, both expressions return ``True``::
|
for a section that holds the key ``'a'``, both expressions return ``True``::
|
||||||
|
|
||||||
"a" in parser["section"]
|
"a" in parser["section"]
|
||||||
"A" in parser["section"]
|
"A" in parser["section"]
|
||||||
|
|
@ -326,14 +328,14 @@ However, there are a few differences that should be taken into account:
|
||||||
* ``parser.get(section, option, **kwargs)`` - the second argument is **not** a
|
* ``parser.get(section, option, **kwargs)`` - the second argument is **not** a
|
||||||
fallback value
|
fallback value
|
||||||
|
|
||||||
* ``parser.items(section)`` - this returns a list of ``(option, value)`` pairs
|
* ``parser.items(section)`` - this returns a list of *option*, *value* pairs
|
||||||
for a specified ``section``
|
for a specified ``section``
|
||||||
|
|
||||||
The mapping protocol is implemented on top of the existing legacy API so that
|
The mapping protocol is implemented on top of the existing legacy API so that
|
||||||
subclassing the original interface makes the mappings work as expected as well.
|
subclassing the original interface makes the mappings work as expected as well.
|
||||||
One difference is the explicit lack of support for the ``__name__`` special key.
|
One difference is the explicit lack of support for the ``'__name__'`` special
|
||||||
This is because the existing behaviour of ``__name__`` is very inconsistent and
|
key. This is because the existing behavior of ``'__name__'`` is very
|
||||||
supporting it would only lead to problems. Details `here
|
inconsistent and supporting it would only lead to problems. Details `here
|
||||||
<http://mail.python.org/pipermail/python-dev/2010-July/102556.html>`_.
|
<http://mail.python.org/pipermail/python-dev/2010-July/102556.html>`_.
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -346,7 +348,7 @@ set of INI styles available. The default functionality is mainly dictated by
|
||||||
historical background and it's very likely that you will want to customize some
|
historical background and it's very likely that you will want to customize some
|
||||||
of the features.
|
of the features.
|
||||||
|
|
||||||
The most natural way to change the way a specific config parser works is to use
|
The most common way to change the way a specific config parser works is to use
|
||||||
the :meth:`__init__` options:
|
the :meth:`__init__` options:
|
||||||
|
|
||||||
* *defaults*, default value: ``None``
|
* *defaults*, default value: ``None``
|
||||||
|
|
@ -356,13 +358,13 @@ the :meth:`__init__` options:
|
||||||
configuration files that don't specify values which are the same as the
|
configuration files that don't specify values which are the same as the
|
||||||
documented default.
|
documented default.
|
||||||
|
|
||||||
Hint: if you want to specify default values for a specific section, use the
|
Hint: if you want to specify default values for a specific section, use
|
||||||
:meth:`read_dict` before you read the actual file.
|
:meth:`read_dict` before you read the actual file.
|
||||||
|
|
||||||
* *dict_type*, default value: :class:`collections.OrderedDict`
|
* *dict_type*, default value: :class:`collections.OrderedDict`
|
||||||
|
|
||||||
This option has a major impact on how the mapping protocol will behave and how
|
This option has a major impact on how the mapping protocol will behave and how
|
||||||
the written configuration files will look like. With the default ordered
|
the written configuration files look. With the default ordered
|
||||||
dictionary, every section is stored in the order they were added to the
|
dictionary, every section is stored in the order they were added to the
|
||||||
parser. Same goes for options within sections.
|
parser. Same goes for options within sections.
|
||||||
|
|
||||||
|
|
@ -427,7 +429,7 @@ the :meth:`__init__` options:
|
||||||
|
|
||||||
Some configuration files are known to include settings without values, but
|
Some configuration files are known to include settings without values, but
|
||||||
which otherwise conform to the syntax supported by :mod:`configparser`. The
|
which otherwise conform to the syntax supported by :mod:`configparser`. The
|
||||||
*allow_no_value* parameter to the :meth:`__init__` method can be used to
|
*allow_no_value* parameter to the constructor can be used to
|
||||||
indicate that such values should be accepted:
|
indicate that such values should be accepted:
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
@ -463,8 +465,7 @@ the :meth:`__init__` options:
|
||||||
|
|
||||||
Delimiters are substrings that delimit keys from values within a section. The
|
Delimiters are substrings that delimit keys from values within a section. The
|
||||||
first occurence of a delimiting substring on a line is considered a delimiter.
|
first occurence of a delimiting substring on a line is considered a delimiter.
|
||||||
This means values (but not keus) can contain substrings that are in the
|
This means values (but not keys) can contain the delimiters.
|
||||||
*delimiters*.
|
|
||||||
|
|
||||||
See also the *space_around_delimiters* argument to
|
See also the *space_around_delimiters* argument to
|
||||||
:meth:`RawConfigParser.write`.
|
:meth:`RawConfigParser.write`.
|
||||||
|
|
@ -472,7 +473,7 @@ the :meth:`__init__` options:
|
||||||
* *comment_prefixes*, default value: ``_COMPATIBLE`` (``'#'`` valid on empty
|
* *comment_prefixes*, default value: ``_COMPATIBLE`` (``'#'`` valid on empty
|
||||||
lines, ``';'`` valid also on non-empty lines)
|
lines, ``';'`` valid also on non-empty lines)
|
||||||
|
|
||||||
Comment prefixes are substrings that indicate the start of a valid comment
|
Comment prefixes are strings that indicate the start of a valid comment
|
||||||
within a config file. The peculiar default value allows for comments starting
|
within a config file. The peculiar default value allows for comments starting
|
||||||
with ``'#'`` or ``';'`` but only the latter can be used in a non-empty line.
|
with ``'#'`` or ``';'`` but only the latter can be used in a non-empty line.
|
||||||
This is obviously dictated by backwards compatibiliy. A more predictable
|
This is obviously dictated by backwards compatibiliy. A more predictable
|
||||||
|
|
@ -493,12 +494,12 @@ the :meth:`__init__` options:
|
||||||
|
|
||||||
* *empty_lines_in_values*, default value: ``True``
|
* *empty_lines_in_values*, default value: ``True``
|
||||||
|
|
||||||
In config parsers, values can be multiline as long as they are indented deeper
|
In config parsers, values can span multiple lines as long as they are
|
||||||
than the key that holds them. By default parsers also let empty lines to be
|
indented more than the key that holds them. By default parsers also let
|
||||||
parts of values. At the same time, keys can be arbitrarily indented
|
empty lines to be parts of values. At the same time, keys can be arbitrarily
|
||||||
themselves to improve readability. In consequence, when configuration files
|
indented themselves to improve readability. In consequence, when
|
||||||
get big and complex, it is easy for the user to lose track of the file
|
configuration files get big and complex, it is easy for the user to lose
|
||||||
structure. Take for instance:
|
track of the file structure. Take for instance:
|
||||||
|
|
||||||
.. code-block:: ini
|
.. code-block:: ini
|
||||||
|
|
||||||
|
|
@ -516,16 +517,17 @@ the :meth:`__init__` options:
|
||||||
produce two keys, ``key`` and ``this``.
|
produce two keys, ``key`` and ``this``.
|
||||||
|
|
||||||
|
|
||||||
More advanced customization may be achieved by overriding default values of the
|
More advanced customization may be achieved by overriding default values of
|
||||||
following parser members:
|
these parser attributes. The defaults are defined on the classes, so they
|
||||||
|
may be overriden by subclasses or by attribute assignment.
|
||||||
|
|
||||||
* `RawConfigParser.BOOLEAN_STATES`
|
.. attribute:: BOOLEAN_STATES
|
||||||
|
|
||||||
By default when using :meth:`getboolean`, config parsers consider the
|
By default when using :meth:`getboolean`, config parsers consider the
|
||||||
following values ``True``: ``'1'``, ``'yes'``, ``'true'``, ``'on'`` and the
|
following values ``True``: ``'1'``, ``'yes'``, ``'true'``, ``'on'`` and the
|
||||||
following values ``False``: ``'0'``, ``'no'``, ``'false'``, ``'off'``. You
|
following values ``False``: ``'0'``, ``'no'``, ``'false'``, ``'off'``. You
|
||||||
can override this by specifying a custom dictionary of strings and their
|
can override this by specifying a custom dictionary of strings and their
|
||||||
boolean outcomes. For example:
|
Boolean outcomes. For example:
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
||||||
|
|
@ -539,15 +541,16 @@ following parser members:
|
||||||
>>> custom['section1'].getboolean('funky')
|
>>> custom['section1'].getboolean('funky')
|
||||||
False
|
False
|
||||||
|
|
||||||
Other typical boolean pairs include ``accept``/``reject`` or
|
Other typical Boolean pairs include ``accept``/``reject`` or
|
||||||
``enabled``/``disabled``.
|
``enabled``/``disabled``.
|
||||||
|
|
||||||
* :meth:`RawConfigParser.optionxform`
|
.. method:: optionxform(option)
|
||||||
|
|
||||||
This is a method that transforms option names on every read or set operation.
|
This method transforms option names on every read, get, or set
|
||||||
By default it converts the name to lowercase. This also means that when a
|
operation. The default converts the name to lowercase. This also
|
||||||
configuration file gets written, all keys will be lowercase. If you find that
|
means that when a configuration file gets written, all keys will be
|
||||||
behaviour unsuitable, you can override this method. For example:
|
lowercase. Override this method if that's unsuitable.
|
||||||
|
For example:
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
||||||
|
|
@ -624,7 +627,7 @@ An example of reading the configuration file again::
|
||||||
if config.getboolean('Section1', 'bool'):
|
if config.getboolean('Section1', 'bool'):
|
||||||
print(config.get('Section1', 'foo'))
|
print(config.get('Section1', 'foo'))
|
||||||
|
|
||||||
To get interpolation, you will need to use a :class:`SafeConfigParser` or, if
|
To get interpolation, use :class:`SafeConfigParser` or, if
|
||||||
you absolutely have to, a :class:`ConfigParser`::
|
you absolutely have to, a :class:`ConfigParser`::
|
||||||
|
|
||||||
import configparser
|
import configparser
|
||||||
|
|
@ -658,9 +661,8 @@ you absolutely have to, a :class:`ConfigParser`::
|
||||||
print(cfg.get('Section1', 'monster', fallback=None))
|
print(cfg.get('Section1', 'monster', fallback=None))
|
||||||
# -> None
|
# -> None
|
||||||
|
|
||||||
|
Default values are available in all three types of ConfigParsers. They are
|
||||||
Defaults are available in all three types of ConfigParsers. They are used in
|
used in interpolation if an option used is not defined elsewhere. ::
|
||||||
interpolation if an option used is not defined elsewhere. ::
|
|
||||||
|
|
||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
|
|
@ -681,12 +683,12 @@ RawConfigParser Objects
|
||||||
|
|
||||||
.. class:: RawConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=_COMPATIBLE, strict=False, empty_lines_in_values=True)
|
.. class:: RawConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=_COMPATIBLE, strict=False, empty_lines_in_values=True)
|
||||||
|
|
||||||
The basic configuration object. When *defaults* is given, it is initialized
|
The basic configuration parser. When *defaults* is given, it is initialized
|
||||||
into the dictionary of intrinsic defaults. When *dict_type* is given, it
|
into the dictionary of intrinsic defaults. When *dict_type* is given, it
|
||||||
will be used to create the dictionary objects for the list of sections, for
|
will be used to create the dictionary objects for the list of sections, for
|
||||||
the options within a section, and for the default values.
|
the options within a section, and for the default values.
|
||||||
|
|
||||||
When *delimiters* is given, it will be used as the set of substrings that
|
When *delimiters* is given, it is used as the set of substrings that
|
||||||
divide keys from values. When *comment_prefixes* is given, it will be used
|
divide keys from values. When *comment_prefixes* is given, it will be used
|
||||||
as the set of substrings that prefix comments in a line, both for the whole
|
as the set of substrings that prefix comments in a line, both for the whole
|
||||||
line and inline comments. For backwards compatibility, the default value for
|
line and inline comments. For backwards compatibility, the default value for
|
||||||
|
|
@ -696,7 +698,7 @@ RawConfigParser Objects
|
||||||
When *strict* is ``True`` (default: ``False``), the parser won't allow for
|
When *strict* is ``True`` (default: ``False``), the parser won't allow for
|
||||||
any section or option duplicates while reading from a single source (file,
|
any section or option duplicates while reading from a single source (file,
|
||||||
string or dictionary), raising :exc:`DuplicateSectionError` or
|
string or dictionary), raising :exc:`DuplicateSectionError` or
|
||||||
:exc:`DuplicateOptionError`. When *empty_lines_in_values* is ``False``
|
:exc:`DuplicateOptionError`. When *empty_lines_in_values* is ``False``
|
||||||
(default: ``True``), each empty line marks the end of an option. Otherwise,
|
(default: ``True``), each empty line marks the end of an option. Otherwise,
|
||||||
internal empty lines of a multiline option are kept as part of the value.
|
internal empty lines of a multiline option are kept as part of the value.
|
||||||
When *allow_no_value* is ``True`` (default: ``False``), options without
|
When *allow_no_value* is ``True`` (default: ``False``), options without
|
||||||
|
|
@ -776,25 +778,26 @@ RawConfigParser Objects
|
||||||
|
|
||||||
.. method:: read_file(f, source=None)
|
.. method:: read_file(f, source=None)
|
||||||
|
|
||||||
Read and parse configuration data from the file or file-like object in *f*
|
Read and parse configuration data from the file or file-like object in
|
||||||
(only the :meth:`readline` method is used). The file-like object must
|
*f* (only the :meth:`readline` method is used). The file-like object
|
||||||
operate in text mode, i.e. return strings from :meth:`readline`.
|
must operate in text mode. Specifically, it must return strings from
|
||||||
|
:meth:`readline`.
|
||||||
|
|
||||||
Optional argument *source* specifies the name of the file being read. If
|
Optional argument *source* specifies the name of the file being read. If
|
||||||
not given and *f* has a :attr:`name` attribute, that is used for *source*;
|
not given and *f* has a :attr:`name` attribute, that is used for
|
||||||
the default is ``<???>``.
|
*source*; the default is ``'<???>'``.
|
||||||
|
|
||||||
.. versionadded:: 3.2
|
.. versionadded:: 3.2
|
||||||
Renamed from :meth:`readfp` (with the ``filename`` attribute renamed to
|
Replaces :meth:`readfp`.
|
||||||
``source`` for consistency with other ``read_*`` methods).
|
|
||||||
|
|
||||||
|
|
||||||
.. method:: read_string(string, source='<string>')
|
.. method:: read_string(string, source='<string>')
|
||||||
|
|
||||||
Parse configuration data from a given string.
|
Parse configuration data from a string.
|
||||||
|
|
||||||
Optional argument *source* specifies a context-specific name of the string
|
Optional argument *source* specifies a context-specific name of the
|
||||||
passed. If not given, ``<string>`` is used.
|
string passed. If not given, ``'<string>'`` is used. This should
|
||||||
|
commonly be a filesystem path or a URL.
|
||||||
|
|
||||||
.. versionadded:: 3.2
|
.. versionadded:: 3.2
|
||||||
|
|
||||||
|
|
@ -852,7 +855,7 @@ RawConfigParser Objects
|
||||||
|
|
||||||
.. method:: items(section)
|
.. method:: items(section)
|
||||||
|
|
||||||
Return a list of ``(name, value)`` pairs for each option in the given
|
Return a list of *name*, *value* pairs for each option in the given
|
||||||
*section*.
|
*section*.
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -878,7 +881,7 @@ RawConfigParser Objects
|
||||||
Write a representation of the configuration to the specified :term:`file
|
Write a representation of the configuration to the specified :term:`file
|
||||||
object`, which must be opened in text mode (accepting strings). This
|
object`, which must be opened in text mode (accepting strings). This
|
||||||
representation can be parsed by a future :meth:`read` call. If
|
representation can be parsed by a future :meth:`read` call. If
|
||||||
``space_around_delimiters`` is ``True`` (the default), delimiters between
|
*space_around_delimiters* is true, delimiters between
|
||||||
keys and values are surrounded by spaces.
|
keys and values are surrounded by spaces.
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -904,23 +907,22 @@ RawConfigParser Objects
|
||||||
*option*; subclasses may override this or client code can set an attribute
|
*option*; subclasses may override this or client code can set an attribute
|
||||||
of this name on instances to affect this behavior.
|
of this name on instances to affect this behavior.
|
||||||
|
|
||||||
You don't necessarily need to subclass a ConfigParser to use this method,
|
You don't need to subclass the parser to use this method, you can also
|
||||||
you can also re-set it on an instance, to a function that takes a string
|
set it on an instance, to a function that takes a string argument and
|
||||||
argument. Setting it to ``str``, for example, would make option names
|
returns a string. Setting it to ``str``, for example, would make option
|
||||||
case sensitive::
|
names case sensitive::
|
||||||
|
|
||||||
cfgparser = ConfigParser()
|
cfgparser = ConfigParser()
|
||||||
...
|
|
||||||
cfgparser.optionxform = str
|
cfgparser.optionxform = str
|
||||||
|
|
||||||
Note that when reading configuration files, whitespace around the option
|
Note that when reading configuration files, whitespace around the option
|
||||||
names are stripped before :meth:`optionxform` is called.
|
names is stripped before :meth:`optionxform` is called.
|
||||||
|
|
||||||
|
|
||||||
.. method:: readfp(fp, filename=None)
|
.. method:: readfp(fp, filename=None)
|
||||||
|
|
||||||
.. deprecated:: 3.2
|
.. deprecated:: 3.2
|
||||||
Please use :meth:`read_file` instead.
|
Use :meth:`read_file` instead.
|
||||||
|
|
||||||
|
|
||||||
.. _configparser-objects:
|
.. _configparser-objects:
|
||||||
|
|
@ -945,8 +947,8 @@ The :class:`ConfigParser` class extends some methods of the
|
||||||
need interpolation.
|
need interpolation.
|
||||||
|
|
||||||
The values in *defaults* must be appropriate for the ``%()s`` string
|
The values in *defaults* must be appropriate for the ``%()s`` string
|
||||||
interpolation. Note that *__name__* is an intrinsic default; its value is
|
interpolation. Note that ``'__name__'`` is an intrinsic default; its value
|
||||||
the section name, and will override any value provided in *defaults*.
|
is the section name, and will override any value provided in *defaults*.
|
||||||
|
|
||||||
All option names used in interpolation will be passed through the
|
All option names used in interpolation will be passed through the
|
||||||
:meth:`optionxform` method just like any other option name reference. For
|
:meth:`optionxform` method just like any other option name reference. For
|
||||||
|
|
@ -998,8 +1000,8 @@ The :class:`ConfigParser` class extends some methods of the
|
||||||
|
|
||||||
A convenience method which coerces the *option* in the specified *section*
|
A convenience method which coerces the *option* in the specified *section*
|
||||||
to a Boolean value. Note that the accepted values for the option are
|
to a Boolean value. Note that the accepted values for the option are
|
||||||
``"1"``, ``"yes"``, ``"true"``, and ``"on"``, which cause this method to
|
``'1'``, ``'yes'``, ``'true'``, and ``'on'``, which cause this method to
|
||||||
return ``True``, and ``"0"``, ``"no"``, ``"false"``, and ``"off"``, which
|
return ``True``, and ``'0'``, ``'no'``, ``'false'``, and ``'off'``, which
|
||||||
cause it to return ``False``. These string values are checked in a
|
cause it to return ``False``. These string values are checked in a
|
||||||
case-insensitive manner. Any other value will cause it to raise
|
case-insensitive manner. Any other value will cause it to raise
|
||||||
:exc:`ValueError`. See :meth:`get` for explanation of *raw*, *vars* and
|
:exc:`ValueError`. See :meth:`get` for explanation of *raw*, *vars* and
|
||||||
|
|
@ -1008,7 +1010,7 @@ The :class:`ConfigParser` class extends some methods of the
|
||||||
|
|
||||||
.. method:: items(section, raw=False, vars=None)
|
.. method:: items(section, raw=False, vars=None)
|
||||||
|
|
||||||
Return a list of ``(name, value)`` pairs for each option in the given
|
Return a list of *name*, *value* pairs for the options in the given
|
||||||
*section*. Optional arguments have the same meaning as for the
|
*section*. Optional arguments have the same meaning as for the
|
||||||
:meth:`get` method.
|
:meth:`get` method.
|
||||||
|
|
||||||
|
|
@ -1026,10 +1028,10 @@ SafeConfigParser Objects
|
||||||
|
|
||||||
.. class:: SafeConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=_COMPATIBLE, strict=False, empty_lines_in_values=True)
|
.. class:: SafeConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=_COMPATIBLE, strict=False, empty_lines_in_values=True)
|
||||||
|
|
||||||
Derived class of :class:`ConfigParser` that implements a sane variant of the
|
Derived class of :class:`ConfigParser` that implements a variant of the
|
||||||
magical interpolation feature. This implementation is more predictable as it
|
magical interpolation feature. This implementation is more predictable as
|
||||||
validates the interpolation syntax used within a configuration file. This
|
it validates the interpolation syntax used within a configuration file.
|
||||||
class also enables escaping the interpolation character (e.g. a key can have
|
This class also enables escaping the interpolation character (a key can have
|
||||||
``%`` as part of the value by specifying ``%%`` in the file).
|
``%`` as part of the value by specifying ``%%`` in the file).
|
||||||
|
|
||||||
Applications that don't require interpolation should use
|
Applications that don't require interpolation should use
|
||||||
|
|
@ -1044,14 +1046,14 @@ SafeConfigParser Objects
|
||||||
*empty_lines_in_values* were added.
|
*empty_lines_in_values* were added.
|
||||||
|
|
||||||
|
|
||||||
The :class:`SafeConfigParser` class implements the same extended interface as
|
The :class:`SafeConfigParser` class implements the same extended interface
|
||||||
:class:`ConfigParser`, with the following addition:
|
as :class:`ConfigParser`, with the following addition:
|
||||||
|
|
||||||
.. method:: set(section, option, value)
|
.. method:: set(section, option, value)
|
||||||
|
|
||||||
If the given section exists, set the given option to the specified value;
|
If the given section exists, set the given option to the specified value;
|
||||||
otherwise raise :exc:`NoSectionError`. *value* must be a string; if it is
|
otherwise raise :exc:`NoSectionError`. *value* must be a string; if not,
|
||||||
not, :exc:`TypeError` is raised.
|
:exc:`TypeError` is raised.
|
||||||
|
|
||||||
|
|
||||||
Exceptions
|
Exceptions
|
||||||
|
|
@ -1059,7 +1061,7 @@ Exceptions
|
||||||
|
|
||||||
.. exception:: Error
|
.. exception:: Error
|
||||||
|
|
||||||
Base class for all other configparser exceptions.
|
Base class for all other :mod:`configparser` exceptions.
|
||||||
|
|
||||||
|
|
||||||
.. exception:: NoSectionError
|
.. exception:: NoSectionError
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue