[Zope3-checkins] CVS: Packages/ZConfig - loader.py:1.1.2.23 schema.py:1.1.2.32

Fred L. Drake, Jr. fred@zope.com
Mon, 23 Dec 2002 11:22:28 -0500


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

Modified Files:
      Tag: zconfig-schema-devel-branch
	loader.py schema.py 
Log Message:
Simple type import mechanism to allow schema components to be re-used.

=== Packages/ZConfig/loader.py 1.1.2.22 => 1.1.2.23 ===
--- Packages/ZConfig/loader.py:1.1.2.22	Mon Dec 23 10:56:40 2002
+++ Packages/ZConfig/loader.py	Mon Dec 23 11:21:57 2002
@@ -116,9 +116,8 @@
         if resource.url and self._cache.has_key(resource.url):
             schema = self._cache[resource.url]
         else:
-            from ZConfig.schema import SchemaParser
-            parser = SchemaParser(self.registry)
-            schema = parser.parseResource(resource)
+            from ZConfig.schema import parseResource
+            schema = parseResource(resource, self.registry, self)
             self._cache[resource.url] = schema
         if resource.fragment:
             type = self.registry.get("basic-key")(resource.fragment)


=== Packages/ZConfig/schema.py 1.1.2.31 => 1.1.2.32 ===
--- Packages/ZConfig/schema.py:1.1.2.31	Mon Dec 23 10:56:40 2002
+++ Packages/ZConfig/schema.py	Mon Dec 23 11:21:57 2002
@@ -13,6 +13,7 @@
 ##############################################################################
 """Parser for ZConfig schemas."""
 
+import urlparse
 import xml.sax
 import xml.sax.saxutils
 
@@ -37,14 +38,21 @@
         return d
 
 
+def parseResource(resource, registry, loader):
+    parser = SchemaParser(registry, loader, resource.url)
+    xml.sax.parse(resource.file, parser)
+    return parser._schema
+
+
 class SchemaParser(xml.sax.ContentHandler):
 
     _cdata_tags = "description", "metadefault", "example", "default"
-    _handled_tags = ("schema", "sectiongroup", "sectiontype",
+    _handled_tags = ("schema", "import", "sectiongroup", "sectiontype",
                      "key", "multikey", "section", "multisection")
 
-    def __init__(self, registry):
+    def __init__(self, registry, loader, url):
         self._registry = registry
+        self._loader = loader
         self._basic_key = registry.get("basic-key")
         self._identifier = registry.get("identifier")
         self._cdata = None
@@ -53,11 +61,7 @@
         self._schema = None
         self._stack = []
         self._group = None
-
-    def parseResource(self, resource):
-        self._url = resource.url
-        xml.sax.parse(resource.file, self)
-        return self._schema
+        self._url = url
 
     # SAX 2 ContentHandler methods
 
@@ -107,6 +111,20 @@
             self.error("no schema found")
 
     # schema loading logic
+
+    def start_import(self, attrs):
+        src = attrs.get("src", "").strip()
+        if not src:
+            self.error("import src may not be omitted or empty")
+        url, fragment = urlparse.urldefrag(src)
+        if fragment:
+            self.error("import src many not include a fragment identifier")
+        schema = self._loader.loadURL(urlparse.urljoin(self._url, url))
+        for n in schema.gettypenames():
+            self._schema.addtype(schema.gettype(n))
+
+    def end_import(self):
+        pass
 
     def get_handler(self, attrs):
         v = attrs.get("handler")