Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
0b894b40
Commit
0b894b40
authored
Sep 15, 2014
by
Łukasz Langa
Browse files
Options
Browse Files
Download
Plain Diff
Closes #18159: ConfigParser getters not available on SectionProxy
parents
884d5284
dfdd2f7e
Changes
3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
423 additions
and
88 deletions
+423
-88
Doc/library/configparser.rst
Doc/library/configparser.rst
+72
-46
Lib/configparser.py
Lib/configparser.py
+128
-42
Lib/test/test_configparser.py
Lib/test/test_configparser.py
+223
-0
No files found.
Doc/library/configparser.rst
View file @
0b894b40
...
...
@@ -144,12 +144,13 @@ datatypes, you should convert on your own:
>>> float(topsecret['CompressionLevel'])
9.0
Extracting Boolean values is not that simple, though. Passing the value
to ``bool()`` would do no good since ``bool('False')`` is still
``True``. This is why config parsers also provide :meth:`getboolean`.
This method is case-insensitive and recognizes Boolean values from
``'yes'``/``'no'``, ``'on'``/``'off'`` and ``'1'``/``'0'`` [1]_.
For example:
Since this task is so common, config parsers provide a range of handy getter
methods to handle integers, floats and booleans. The last one is the most
interesting because simply passing the value to ``bool()`` would do no good
since ``bool('False')`` is still ``True``. This is why config parsers also
provide :meth:`getboolean`. This method is case-insensitive and recognizes
Boolean values from ``'yes'``/``'no'``, ``'on'``/``'off'``,
``'true'``/``'false'`` and ``'1'``/``'0'`` [1]_. For example:
.. doctest::
...
...
@@ -161,10 +162,8 @@ For example:
True
Apart from :meth:`getboolean`, config parsers also provide equivalent
:meth:`getint` and :meth:`getfloat` methods, but these are far less
useful since conversion using :func:`int` and :func:`float` is
sufficient for these types.
:meth:`getint` and :meth:`getfloat` methods. You can register your own
converters and customize the provided ones. [1]_
Fallback Values
---------------
...
...
@@ -321,9 +320,9 @@ from ``get()`` calls.
An alternative handler for interpolation which implements a more advanced
syntax, used for instance in ``zc.buildout``. Extended interpolation is
using ``${section:option}`` to denote a value from a foreign section.
Interpolation can span multiple levels.
For convenience, if the ``section:``
part is omitted, interpolation defaults to the current section (and possibly
the default values from the special section).
Interpolation can span multiple levels.
For convenience, if the
``section:`` part is omitted, interpolation defaults to the current section
(and possibly
the default values from the special section).
For example, the configuration specified above with basic interpolation,
would look like this with extended interpolation:
...
...
@@ -541,9 +540,9 @@ the :meth:`__init__` options:
* *delimiters*, default value: ``('=', ':')``
Delimiters are substrings that delimit keys from values within a section.
The
first occurrence of a delimiting substring on a line is considered a delimiter.
This means values (but not keys) can contain the delimiters.
Delimiters are substrings that delimit keys from values within a section.
The first occurrence of a delimiting substring on a line is considered
a delimiter.
This means values (but not keys) can contain the delimiters.
See also the *space_around_delimiters* argument to
:meth:`ConfigParser.write`.
...
...
@@ -565,10 +564,10 @@ the :meth:`__init__` options:
Please note that config parsers don't support escaping of comment prefixes so
using *inline_comment_prefixes* may prevent users from specifying option
values with characters used as comment prefixes.
When in doubt, avoid setting
*inline_comment_prefixes*. In any circumstances, the only way of storing
comment prefix characters at the beginning of a line in multiline values is to
interpolate the prefix, for example::
values with characters used as comment prefixes.
When in doubt, avoid
setting *inline_comment_prefixes*. In any circumstances, the only way of
storing comment prefix characters at the beginning of a line in multiline
values is to
interpolate the prefix, for example::
>>> from configparser import ConfigParser, ExtendedInterpolation
>>> parser = ConfigParser(interpolation=ExtendedInterpolation())
...
...
@@ -651,8 +650,8 @@ the :meth:`__init__` options:
letting users create complex declarative configurations. This section is
normally called ``"DEFAULT"`` but this can be customized to point to any
other valid section name. Some typical values include: ``"general"`` or
``"common"``.
The name provided is used for recognizing default sections when
reading from any source and is used when writing configuration back to
``"common"``.
The name provided is used for recognizing default sections
when
reading from any source and is used when writing configuration back to
a file. Its current value can be retrieved using the
``parser_instance.default_section`` attribute and may be modified at runtime
(i.e. to convert files from one format to another).
...
...
@@ -666,10 +665,26 @@ the :meth:`__init__` options:
`dedicated documentation section <#interpolation-of-values>`_.
:class:`RawConfigParser` has a default value of ``None``.
* *converters*, default value: not set
Config parsers provide option value getters that perform type conversion. By
default :meth:`getint`, :meth:`getfloat`, and :meth:`getboolean` are
implemented. Should other getters be desirable, users may define them in
a subclass or pass a dictionary where each key is a name of the converter and
each value is a callable implementing said conversion. For instance, passing
``{'decimal': decimal.Decimal}`` would add :meth:`getdecimal` on both the
parser object and all section proxies. In other words, it will be possible
to write both ``parser_instance.getdecimal('section', 'key', fallback=0)``
and ``parser_instance['section'].getdecimal('key', 0)``.
If the converter needs to access the state of the parser, it can be
implemented as a method on a config parser subclass. If the name of this
method starts with ``get``, it will be available on all section proxies, in
the dict-compatible form (see the ``getdecimal()`` example above).
More advanced customization may be achieved by overriding default values of
these parser attributes. The defaults are defined on the classes, so they
may
be overridden by subclasses or by attribute assignment.
these parser attributes. The defaults are defined on the classes, so they
may
be overridden by subclasses or by attribute assignment.
.. attribute:: BOOLEAN_STATES
...
...
@@ -728,9 +743,10 @@ may be overridden by subclasses or by attribute assignment.
.. attribute:: SECTCRE
A compiled regular expression used to parse section headers. The default
matches ``[section]`` to the name ``"section"``. Whitespace is considered part
of the section name, thus ``[ larch ]`` will be read as a section of name
``" larch "``. Override this attribute if that's unsuitable. For example:
matches ``[section]`` to the name ``"section"``. Whitespace is considered
part of the section name, thus ``[ larch ]`` will be read as a section of
name ``" larch "``. Override this attribute if that's unsuitable. For
example:
.. doctest::
...
...
@@ -861,7 +877,7 @@ interpolation if an option used is not defined elsewhere. ::
ConfigParser Objects
--------------------
.. class:: ConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=configparser.DEFAULTSECT, interpolation=BasicInterpolation())
.. class:: ConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=configparser.DEFAULTSECT, interpolation=BasicInterpolation()
, converters={}
)
The main configuration parser. When *defaults* is given, it is initialized
into the dictionary of intrinsic defaults. When *dict_type* is given, it
...
...
@@ -871,8 +887,8 @@ ConfigParser Objects
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
as the set of substrings that prefix comments in otherwise empty lines.
Comments can be indented.
When *inline_comment_prefixes* is given, it will be
used as the set of substrings that prefix comments in non-empty lines.
Comments can be indented.
When *inline_comment_prefixes* is given, it will
be
used as the set of substrings that prefix comments in non-empty lines.
When *strict* is ``True`` (the default), the parser won't allow for
any section or option duplicates while reading from a single source (file,
...
...
@@ -901,6 +917,12 @@ ConfigParser Objects
converts option names to lower case), the values ``foo %(bar)s`` and ``foo
%(BAR)s`` are equivalent.
When *converters* is given, it should be a dictionary where each key
represents the name of a type converter and each value is a callable
implementing the conversion from string to the desired datatype. Every
converter gets its own corresponding :meth:`get*()` method on the parser
object and section proxies.
.. versionchanged:: 3.1
The default *dict_type* is :class:`collections.OrderedDict`.
...
...
@@ -909,6 +931,9 @@ ConfigParser Objects
*empty_lines_in_values*, *default_section* and *interpolation* were
added.
.. versionchanged:: 3.5
The *converters* argument was added.
.. method:: defaults()
...
...
@@ -1284,3 +1309,4 @@ Exceptions
.. [1] Config parsers allow for heavy customization. If you are interested in
changing the behaviour outlined by the footnote reference, consult the
`Customizing Parser Behaviour`_ section.
Lib/configparser.py
View file @
0b894b40
This diff is collapsed.
Click to expand it.
Lib/test/test_configparser.py
View file @
0b894b40
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment