Commit cc645b9a authored by Fred Drake's avatar Fred Drake

add consistent support for the vars and default arguments on all

configuration parser classes
(http://bugs.python.org/issue9421)
parent c934f32e
......@@ -135,7 +135,7 @@ keys within each section.
*empty_lines_in_values* were added.
.. class:: SafeConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), 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
magical interpolation feature. This implementation is more predictable as it
......@@ -155,7 +155,7 @@ keys within each section.
*empty_lines_in_values* were added.
.. class:: ConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), strict=False, empty_lines_in_values=True)
.. class:: ConfigParser(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:`RawConfigParser` that implements the magical
interpolation feature and adds optional arguments to the :meth:`get` and
......@@ -366,39 +366,44 @@ RawConfigParser Objects
Load configuration from a dictionary. Keys are section names, values are
dictionaries with keys and values that should be present in the section. If
the used dictionary type preserves order, sections and their keys will be
added in order.
added in order. Values are automatically converted to strings.
Optional argument *source* specifies a context-specific name of the
dictionary passed. If not given, ``<dict>`` is used.
.. versionadded:: 3.2
.. method:: RawConfigParser.get(section, option, [vars, default])
.. method:: RawConfigParser.get(section, option)
Get an *option* value for the named *section*.
Get an *option* value for the named *section*. If *vars* is provided, it
must be a dictionary. The *option* is looked up in *vars* (if provided),
*section*, and in *DEFAULTSECT* in that order. If the key is not found and
*default* is provided, it is used as a fallback value. ``None`` can be
provided as a *default* value.
.. method:: RawConfigParser.getint(section, option)
.. method:: RawConfigParser.getint(section, option, [vars, default])
A convenience method which coerces the *option* in the specified *section* to an
integer.
A convenience method which coerces the *option* in the specified *section* to
an integer. See :meth:`get` for explanation of *vars* and *default*.
.. method:: RawConfigParser.getfloat(section, option)
.. method:: RawConfigParser.getfloat(section, option, [vars, default])
A convenience method which coerces the *option* in the specified *section* to a
floating point number.
A convenience method which coerces the *option* in the specified *section* to
a floating point number. See :meth:`get` for explanation of *vars* and
*default*.
.. method:: RawConfigParser.getboolean(section, option)
.. method:: RawConfigParser.getboolean(section, option, [vars, default])
A convenience method which coerces the *option* in the specified *section* to a
Boolean value. Note that the accepted values for the option are ``"1"``,
``"yes"``, ``"true"``, and ``"on"``, which cause this method to return ``True``,
and ``"0"``, ``"no"``, ``"false"``, and ``"off"``, which cause it to return
``False``. These string values are checked in a case-insensitive manner. Any
other value will cause it to raise :exc:`ValueError`.
A convenience method which coerces the *option* in the specified *section*
to a Boolean value. Note that the accepted values for the option are
``"1"``, ``"yes"``, ``"true"``, and ``"on"``, which cause this method to
return ``True``, and ``"0"``, ``"no"``, ``"false"``, and ``"off"``, which
cause it to return ``False``. These string values are checked in
a case-insensitive manner. Any other value will cause it to raise
:exc:`ValueError`. See :meth:`get` for explanation of *vars* and *default*.
.. method:: RawConfigParser.items(section)
......@@ -475,17 +480,44 @@ can, consider using :class:`SafeConfigParser` which adds validation and escaping
for the interpolation.
.. method:: ConfigParser.get(section, option, raw=False, vars=None)
.. method:: ConfigParser.get(section, option, raw=False, [vars, default])
Get an *option* value for the named *section*. If *vars* is provided, it
must be a dictionary. The *option* is looked up in *vars* (if provided),
*section*, and in *defaults* in that order.
*section*, and in *DEFAULTSECT* in that order. If the key is not found and
*default* is provided, it is used as a fallback value. ``None`` can be
provided as a *default* value.
All the ``'%'`` interpolations are expanded in the return values, unless the
*raw* argument is true. Values for interpolation keys are looked up in the
same manner as the option.
.. method:: ConfigParser.getint(section, option, raw=False, [vars, default])
A convenience method which coerces the *option* in the specified *section* to
an integer. See :meth:`get` for explanation of *raw*, *vars* and *default*.
.. method:: ConfigParser.getfloat(section, option, raw=False, [vars, default])
A convenience method which coerces the *option* in the specified *section* to
a floating point number. See :meth:`get` for explanation of *raw*, *vars*
and *default*.
.. method:: ConfigParser.getboolean(section, option, raw=False, [vars, default])
A convenience method which coerces the *option* in the specified *section*
to a Boolean value. Note that the accepted values for the option are
``"1"``, ``"yes"``, ``"true"``, and ``"on"``, which cause this method to
return ``True``, and ``"0"``, ``"no"``, ``"false"``, and ``"off"``, which
cause it to return ``False``. These string values are checked in
a case-insensitive manner. Any other value will cause it to raise
:exc:`ValueError`. See :meth:`get` for explanation of *raw*, *vars* and
*default*.
.. method:: ConfigParser.items(section, raw=False, vars=None)
Return a list of ``(name, value)`` pairs for each option in the given
......
This diff is collapsed.
......@@ -62,9 +62,10 @@ class BasicTestCase(CfgParserTestCaseClass):
'Spaces',
'Spacey Bar',
'Spacey Bar From The Beginning',
'Types',
]
if self.allow_no_value:
E.append(r'NoValue')
E.append('NoValue')
E.sort()
eq = self.assertEqual
eq(L, E)
......@@ -80,9 +81,43 @@ class BasicTestCase(CfgParserTestCaseClass):
eq(cf.get('Commented Bar', 'baz'), 'qwe')
eq(cf.get('Spaces', 'key with spaces'), 'value')
eq(cf.get('Spaces', 'another with spaces'), 'splat!')
eq(cf.getint('Types', 'int'), 42)
eq(cf.get('Types', 'int'), "42")
self.assertAlmostEqual(cf.getfloat('Types', 'float'), 0.44)
eq(cf.get('Types', 'float'), "0.44")
eq(cf.getboolean('Types', 'boolean'), False)
if self.allow_no_value:
eq(cf.get('NoValue', 'option-without-value'), None)
# test vars= and default=
eq(cf.get('Foo Bar', 'foo', default='baz'), 'bar')
eq(cf.get('Foo Bar', 'foo', vars={'foo': 'baz'}), 'baz')
with self.assertRaises(configparser.NoSectionError):
cf.get('No Such Foo Bar', 'foo')
with self.assertRaises(configparser.NoOptionError):
cf.get('Foo Bar', 'no-such-foo')
eq(cf.get('No Such Foo Bar', 'foo', default='baz'), 'baz')
eq(cf.get('Foo Bar', 'no-such-foo', default='baz'), 'baz')
eq(cf.get('Spacey Bar', 'foo', default=None), 'bar')
eq(cf.get('No Such Spacey Bar', 'foo', default=None), None)
eq(cf.getint('Types', 'int', default=18), 42)
eq(cf.getint('Types', 'no-such-int', default=18), 18)
eq(cf.getint('Types', 'no-such-int', default="18"), "18") # sic!
self.assertAlmostEqual(cf.getfloat('Types', 'float',
default=0.0), 0.44)
self.assertAlmostEqual(cf.getfloat('Types', 'no-such-float',
default=0.0), 0.0)
eq(cf.getfloat('Types', 'no-such-float', default="0.0"), "0.0") # sic!
eq(cf.getboolean('Types', 'boolean', default=True), False)
eq(cf.getboolean('Types', 'no-such-boolean', default="yes"),
"yes") # sic!
eq(cf.getboolean('Types', 'no-such-boolean', default=True), True)
eq(cf.getboolean('No Such Types', 'boolean', default=True), True)
if self.allow_no_value:
eq(cf.get('NoValue', 'option-without-value', default=False), None)
eq(cf.get('NoValue', 'no-such-option-without-value',
default=False), False)
self.assertNotIn('__name__', cf.options("Foo Bar"),
'__name__ "option" should not be exposed by the API!')
......@@ -127,6 +162,10 @@ foo[de]{0[0]}Deutsch
[Spaces]
key with spaces {0[1]} value
another with spaces {0[0]} splat!
[Types]
int {0[1]} 42
float {0[0]} 0.44
boolean {0[0]} NO
""".format(self.delimiters, self.comment_prefixes)
if self.allow_no_value:
config_string += (
......@@ -194,7 +233,12 @@ another with spaces {0[0]} splat!
"Spaces": {
"key with spaces": "value",
"another with spaces": "splat!",
}
},
"Types": {
"int": 42,
"float": 0.44,
"boolean": False,
},
}
if self.allow_no_value:
config.update({
......@@ -732,8 +776,11 @@ class SafeConfigParserTestCaseTrickyFile(CfgParserTestCaseClass):
'no values here',
'tricky interpolation',
'more interpolation'])
#self.assertEqual(cf.getint('DEFAULT', 'go', vars={'interpolate': '-1'}),
# -1)
self.assertEqual(cf.getint('DEFAULT', 'go',
vars={'interpolate': '-1'}), -1)
with self.assertRaises(ValueError):
# no interpolation will happen
cf.getint('DEFAULT', 'go', raw=True, vars={'interpolate': '-1'})
self.assertEqual(len(cf.get('strange', 'other').split('\n')), 4)
self.assertEqual(len(cf.get('corruption', 'value').split('\n')), 10)
longname = 'yeah, sections can be indented as well'
......@@ -808,7 +855,7 @@ class SortedTestCase(RawConfigParserTestCase):
class CompatibleTestCase(CfgParserTestCaseClass):
config_class = configparser.RawConfigParser
comment_prefixes = configparser.RawConfigParser._COMPATIBLE
comment_prefixes = configparser._COMPATIBLE
def test_comment_handling(self):
config_string = textwrap.dedent("""\
......
......@@ -158,6 +158,9 @@ Library
- Issue #9753: Fixed socket.dup, which did not always work correctly
on Windows.
- Issue #9421: Made the get<type> methods consistently accept the vars
and default arguments on all parser classes.
- Issue #7005: Fixed output of None values for RawConfigParser.write and
ConfigParser.write.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment