[Python-checkins] r86639 - in python/branches/py3k/Lib: configparser.py test/test_cfgparser.py

lukasz.langa python-checkins at python.org
Sun Nov 21 14:56:42 CET 2010


Author: lukasz.langa
Date: Sun Nov 21 14:56:42 2010
New Revision: 86639

Log:
configparser: read-only attributes to get the section name and parser from a SectionProxy instance



Modified:
   python/branches/py3k/Lib/configparser.py
   python/branches/py3k/Lib/test/test_cfgparser.py

Modified: python/branches/py3k/Lib/configparser.py
==============================================================================
--- python/branches/py3k/Lib/configparser.py	(original)
+++ python/branches/py3k/Lib/configparser.py	Sun Nov 21 14:56:42 2010
@@ -1102,42 +1102,52 @@
 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

Modified: python/branches/py3k/Lib/test/test_cfgparser.py
==============================================================================
--- python/branches/py3k/Lib/test/test_cfgparser.py	(original)
+++ python/branches/py3k/Lib/test/test_cfgparser.py	Sun Nov 21 14:56:42 2010
@@ -135,8 +135,15 @@
         # 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')


More information about the Python-checkins mailing list