[Zodb-checkins] CVS: Packages/ZConfig - matcher.py:1.1.2.28

Fred L. Drake, Jr. fred@zope.com
Tue, 31 Dec 2002 17:06:13 -0500


Update of /cvs-repository/Packages/ZConfig
In directory cvs.zope.org:/tmp/cvs-serv21657

Modified Files:
      Tag: zconfig-schema-devel-branch
	matcher.py 
Log Message:
SectionValue:  Override __setattr__() and __setitem__(), so that the
config values that are accessible by both name and position are kept
in sync.


=== Packages/ZConfig/matcher.py 1.1.2.27 => 1.1.2.28 ===
--- Packages/ZConfig/matcher.py:1.1.2.27	Thu Dec 19 16:11:22 2002
+++ Packages/ZConfig/matcher.py	Tue Dec 31 17:06:12 2002
@@ -209,13 +209,18 @@
 
 
 class SectionValue:
-    """Generic 'bag-of-values' object for a section."""
+    """Generic 'bag-of-values' object for a section.
+
+    Derived classes should always call the SectionValue constructor
+    before attempting to modify self.
+    """
 
     def __init__(self, attrnames, values, name, type):
-        self._attrnames = attrnames
-        self._values = values
-        self.__name__ = name
-        self.__type__ = type
+        d = self.__dict__
+        d['_attrnames'] = attrnames
+        d['_values'] = values
+        d['__name__'] = name
+        d['__type__'] = type
 
     def __repr__(self):
         if self.__name__:
@@ -235,9 +240,22 @@
             raise TypeError("SectionValue does not support slicing")
         return self._values[index]
 
+    def __setitem__(self, index, value):
+        if isinstance(index, types.SliceType):
+            raise TypeError("SectionValue does not support slicing")
+        self._values[index] = value
+
     def __getattr__(self, name):
         try:
             i = self._attrnames.index(name)
         except ValueError:
             raise AttributeError, name
         return self._values[i]
+
+    def __setattr__(self, name, value):
+        try:
+            i = self._attrnames.index(name)
+        except ValueError:
+            self.__dict__[name] = value
+        else:
+            self._values[i] = value