summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorŁukasz Langa <lukasz@langa.pl>2010-11-21 13:56:42 (GMT)
committerŁukasz Langa <lukasz@langa.pl>2010-11-21 13:56:42 (GMT)
commita73dc9d5e8ba2320a5aebf8c24ab665ccdfed5f8 (patch)
treea3ac90c85ad5c002f030a052f1f3b3ad34bb2994
parent5c86339bd09959366fcb0d4c31798a4aac128083 (diff)
downloadcpython-a73dc9d5e8ba2320a5aebf8c24ab665ccdfed5f8.zip
cpython-a73dc9d5e8ba2320a5aebf8c24ab665ccdfed5f8.tar.gz
cpython-a73dc9d5e8ba2320a5aebf8c24ab665ccdfed5f8.tar.bz2
configparser: read-only attributes to get the section name and parser from a SectionProxy instance
-rw-r--r--Lib/configparser.py40
-rw-r--r--Lib/test/test_cfgparser.py11
2 files changed, 34 insertions, 17 deletions
diff --git a/Lib/configparser.py b/Lib/configparser.py
index 94072c5..f9bb32c 100644
--- a/Lib/configparser.py
+++ b/Lib/configparser.py
@@ -1102,42 +1102,52 @@ class SafeConfigParser(ConfigParser):
class SectionProxy(MutableMapping):
"""A proxy for a single section from a parser."""
- def __init__(self, parser, section_name):
- """Creates a view on a section named `section_name` in `parser`."""
+ def __init__(self, parser, name):
+ """Creates a view on a section of the specified `name` in `parser`."""
self._parser = parser
- self._section = section_name
+ self._name = name
self.getint = functools.partial(self._parser.getint,
- self._section)
+ self._name)
self.getfloat = functools.partial(self._parser.getfloat,
- self._section)
+ self._name)
self.getboolean = functools.partial(self._parser.getboolean,
- self._section)
+ self._name)
def __repr__(self):
- return '<Section: {}>'.format(self._section)
+ return '<Section: {}>'.format(self._name)
def __getitem__(self, key):
- if not self._parser.has_option(self._section, key):
+ if not self._parser.has_option(self._name, key):
raise KeyError(key)
- return self._parser.get(self._section, key)
+ return self._parser.get(self._name, key)
def __setitem__(self, key, value):
self._parser._validate_value_type(value)
- return self._parser.set(self._section, key, value)
+ return self._parser.set(self._name, key, value)
def __delitem__(self, key):
- if not self._parser.has_option(self._section, key):
+ if not self._parser.has_option(self._name, key):
raise KeyError(key)
- return self._parser.remove_option(self._section, key)
+ return self._parser.remove_option(self._name, key)
def __contains__(self, key):
- return self._parser.has_option(self._section, key)
+ return self._parser.has_option(self._name, key)
def __len__(self):
# XXX weak performance
- return len(self._parser.options(self._section))
+ return len(self._parser.options(self._name))
def __iter__(self):
# XXX weak performance
# XXX does not break when underlying container state changed
- return self._parser.options(self._section).__iter__()
+ return self._parser.options(self._name).__iter__()
+
+ @property
+ def parser(self):
+ # The parser object of the proxy is read-only.
+ return self._parser
+
+ @property
+ def name(self):
+ # The name of the section on a proxy is read-only.
+ return self._name
diff --git a/Lib/test/test_cfgparser.py b/Lib/test/test_cfgparser.py
index 4faf90c..fc8b2ad 100644
--- a/Lib/test/test_cfgparser.py
+++ b/Lib/test/test_cfgparser.py
@@ -135,8 +135,15 @@ class BasicTestCase(CfgParserTestCaseClass):
# mapping access
eq(cf['Foo Bar']['foo'], 'bar1')
eq(cf['Spacey Bar']['foo'], 'bar2')
- eq(cf['Spacey Bar From The Beginning']['foo'], 'bar3')
- eq(cf['Spacey Bar From The Beginning']['baz'], 'qwe')
+ section = cf['Spacey Bar From The Beginning']
+ eq(section.name, 'Spacey Bar From The Beginning')
+ self.assertIs(section.parser, cf)
+ with self.assertRaises(AttributeError):
+ section.name = 'Name is read-only'
+ with self.assertRaises(AttributeError):
+ section.parser = 'Parser is read-only'
+ eq(section['foo'], 'bar3')
+ eq(section['baz'], 'qwe')
eq(cf['Commented Bar']['foo'], 'bar4')
eq(cf['Commented Bar']['baz'], 'qwe')
eq(cf['Spaces']['key with spaces'], 'value')