[Zope3-checkins] CVS: Packages/ZConfig - matcher.py:1.1.2.15

Fred L. Drake, Jr. fred@zope.com
Thu, 12 Dec 2002 15:12:55 -0500


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

Modified Files:
      Tag: zconfig-schema-devel-branch
	matcher.py 
Log Message:
Refactor the SectionMatcher and SchemaMatcher to derive from a common
base class.  Add a check that a section name is not re-used within the
same lexical scope.


=== Packages/ZConfig/matcher.py 1.1.2.14 => 1.1.2.15 ===
--- Packages/ZConfig/matcher.py:1.1.2.14	Thu Dec 12 14:10:46 2002
+++ Packages/ZConfig/matcher.py	Thu Dec 12 15:12:54 2002
@@ -18,20 +18,20 @@
 import ZConfig
 
 
-class SectionMatcher:
-    def __init__(self, info, name):
-        if name is not None:
-            self.name = name
-        elif info.allowUnnamed():
-            self.name = None
-        else:
-            raise ZConfig.ConfigurationError(
-                `info.name` + " sections may not be unnamed")
+class BaseMatcher:
+    def __init__(self, info, type):
         self.info = info
-        self.type = info.sectiontype
-        self._values = [None] * len(self.type)
+        self.type = type
+        self._values = [None] * len(type)
+        self._sectionnames = {}
 
     def addSection(self, type, name, sectvalue):
+        if name:
+            if self._sectionnames.has_key(name):
+                raise ZConfig.ConfigurationError(
+                    "section names must not be re-used within the"
+                    " same container:" + `name`)
+            self._sectionnames[name] = name
         i = self.type.getsectionindex(type, name)
         ci = self.type.getsectioninfo(type, name)
         v = self._values[i]
@@ -134,16 +134,26 @@
         return v
 
 
-class SchemaMatcher(SectionMatcher):
+class SectionMatcher(BaseMatcher):
+    def __init__(self, info, name):
+        if name is not None:
+            self.name = name
+        elif info.allowUnnamed():
+            self.name = None
+        else:
+            raise ZConfig.ConfigurationError(
+                `info.name` + " sections may not be unnamed")
+        BaseMatcher.__init__(self, info, info.sectiontype)
+
+
+class SchemaMatcher(BaseMatcher):
     def __init__(self, info):
-        self.info = info
-        self.type = info
-        self._values = [None] * len(info)
+        BaseMatcher.__init__(self, info, info)
 
     def finish(self):
         # Since there's no outer container to call datatype.convert()
         # for the schema, we convert on the way out.
-        v = SectionMatcher.finish(self)
+        v = BaseMatcher.finish(self)
         return self.type.datatype.convert(v)