[Zodb-checkins] CVS: StandaloneZConfig/ZConfig - info.py:1.21 matcher.py:1.14 schema.py:1.32

Fred L. Drake, Jr. fred at zope.com
Tue Mar 16 15:47:55 EST 2004


Update of /cvs-repository/StandaloneZConfig/ZConfig
In directory cvs.zope.org:/tmp/cvs-serv31115

Modified Files:
	info.py matcher.py schema.py 
Log Message:
- make it possible to provide defaults for <multikey name="+"> and
  <key name="+">; keys need to be provided for each default value
- fix bug in converting an empty section that contained a
  <multikey name="+">
- document <default> element in reference manual and DTD
- more tests


=== StandaloneZConfig/ZConfig/info.py 1.20 => 1.21 ===
--- StandaloneZConfig/ZConfig/info.py:1.20	Fri Jan  2 00:35:03 2004
+++ StandaloneZConfig/ZConfig/info.py	Tue Mar 16 15:47:24 2004
@@ -13,6 +13,8 @@
 ##############################################################################
 """Objects that can describe a ZConfig schema."""
 
+import copy
+
 import ZConfig
 
 
@@ -119,16 +121,41 @@
                 "cannot finish KeyInfo more than once")
         self._finished = True
 
-    def adddefault(self, value, position):
+    def adddefault(self, value, position, key=None):
         if self._finished:
             raise ZConfig.SchemaError(
                 "cannot add default values to finished KeyInfo")
+        # Check that the name/keyed relationship is right:
+        if self.name == "+" and key is None:
+            raise ZConfig.SchemaError(
+                "default values must be keyed for name='+'")
+        elif self.name != "+" and key is not None:
+            raise ZConfig.SchemaError(
+                "unexpected key for default value")
+
         value = ValueInfo(value, position)
         if self.maxOccurs > 1:
-            if self._default is None:
+            if self.name == "+":
+                # This is a keyed value, not a simple value:
+                if self._default is None:
+                    self._default = {key: [value]}
+                else:
+                    if key in self._default:
+                        self._default[key].append(value)
+                    else:
+                        self._default[key] = [value]
+            elif self._default is None:
                 self._default = [value]
             else:
                 self._default.append(value)
+        elif self.name == "+":
+            if self._default is None:
+                self._default = {key: value}
+            else:
+                if self._default.has_key(key):
+                    raise ZConfig.SchemaError(
+                        "duplicate default value for key %s" % `key`)
+                self._default[key] = value
         elif self._default is not None:
             raise ZConfig.SchemaError(
                 "cannot set more than one default to key with maxOccurs == 1")
@@ -140,10 +167,16 @@
             raise ZConfig.SchemaError(
                 "cannot get default value of key before KeyInfo"
                 " has been completely initialized")
-        if self._default is None and self.maxOccurs > 1:
-            return []
+        if self._default is None:
+            if self.name == "+":
+                return {}
+            elif self.maxOccurs > 1:
+                return []
         else:
-            return self._default
+            # Use copy.copy() to make sure we don't allow polution of
+            # our internal data without having to worry about both the
+            # list and dictionary cases:
+            return copy.copy(self._default)
 
 
 class SectionInfo(BaseInfo):


=== StandaloneZConfig/ZConfig/matcher.py 1.13 => 1.14 ===
--- StandaloneZConfig/ZConfig/matcher.py:1.13	Wed May 28 09:17:12 2003
+++ StandaloneZConfig/ZConfig/matcher.py	Tue Mar 16 15:47:24 2004
@@ -156,7 +156,11 @@
                     v = values[attr] = default[:]
             if ci.ismulti():
                 if not v:
-                    v[:] = ci.getdefault()
+                    default = ci.getdefault()
+                    if isinstance(default, dict):
+                        v.update(default)
+                    else:
+                        v[:] = default
                 if len(v) < ci.minOccurs:
                     raise ZConfig.ConfigurationError(
                         "not enough values for %s; %d found, %d required"
@@ -204,8 +208,12 @@
                     v = None
             elif name == '+':
                 v = values[attr]
-                for key, val in v.items():
-                    v[key] = val.convert(ci.datatype)
+                if not v:
+                    for key, val in ci.getdefault().items():
+                        v[key] = val.convert(ci.datatype)
+                else:
+                    for key, val in v.items():
+                        v[key] = val.convert(ci.datatype)
             else:
                 v = values[attr]
                 if v is not None:


=== StandaloneZConfig/ZConfig/schema.py 1.31 => 1.32 ===
--- StandaloneZConfig/ZConfig/schema.py:1.31	Fri Jan  2 01:03:08 2004
+++ StandaloneZConfig/ZConfig/schema.py	Tue Mar 16 15:47:24 2004
@@ -69,7 +69,7 @@
                         "schema", "component", "extension"],
         "example": ["key", "section", "multikey", "multisection"],
         "metadefault": ["key", "section", "multikey", "multisection"],
-        "default": ["multikey"],
+        "default": ["key", "multikey"],
         "import": ["schema", "component", "extension"],
         "abstracttype": ["schema", "component", "extension"],
         "sectiontype": ["schema", "component", "extension"],
@@ -124,6 +124,7 @@
                 self.error(name + " element improperly nested")
             self._cdata = []
             self._position = None
+            self._attrs = attrs
 
     def characters(self, data):
         if self._cdata is not None:
@@ -279,7 +280,8 @@
     # schema loading logic
 
     def characters_default(self, data):
-        self._stack[-1].adddefault(data, self._position)
+        key = self._attrs.get("key")
+        self._stack[-1].adddefault(data, self._position, key)
 
     def characters_description(self, data):
         self._stack[-1].description = data
@@ -412,12 +414,15 @@
                 self.error("required key cannot have a default value")
             key.adddefault(str(attrs["default"]).strip(),
                            self.get_position())
-        key.finish()
+        if name != "+":
+            key.finish()
         self._stack[-1].addkey(key)
         self._stack.append(key)
 
     def end_key(self):
-        self._stack.pop()
+        key = self._stack.pop()
+        if key.name == "+":
+            key.finish()
 
     def start_multikey(self, attrs):
         if attrs.has_key("default"):




More information about the Zodb-checkins mailing list