[Zodb-checkins] CVS: Packages/ZConfig - schema.py:1.8

Fred L. Drake, Jr. fred@zope.com
Tue, 7 Jan 2003 13:31:07 -0500


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

Modified Files:
	schema.py 
Log Message:
More general reorganization.


=== Packages/ZConfig/schema.py 1.7 => 1.8 ===
--- Packages/ZConfig/schema.py:1.7	Tue Jan  7 12:11:44 2003
+++ Packages/ZConfig/schema.py	Tue Jan  7 13:31:03 2003
@@ -70,10 +70,10 @@
 
     def startElement(self, name, attrs):
         attrs = dict(attrs)
-        if name == "schema":
+        if name == self._top_level:
             if self._schema is not None:
                 self.error("schema element improperly nested")
-            self.start_schema(attrs)
+            getattr(self, "start_" + name)(attrs)
         elif name in self._handled_tags:
             if self._schema is None:
                 self.error(name + " element outside of schema")
@@ -107,46 +107,9 @@
 
     def endDocument(self):
         if self._schema is None:
-            self.error("no schema found")
-
-    # schema loading logic
-
-    def characters_default(self, data):
-        self._stack[-1].adddefault(data, self._position)
-
-    def characters_description(self, data):
-        self._stack[-1].description = data
-
-    def characters_example(self, data):
-        self._stack[-1].example = data
-
-    def characters_metadefault(self, data):
-        self._stack[-1].metadefault = data
-
-    def start_import(self, attrs):
-        src = attrs.get("src", "").strip()
-        pkg = attrs.get("package", "").strip()
-        if not (src or pkg):
-            self.error("import must specify either src or package")
-        if src and pkg:
-            self.error("import may only specify one of src or package")
-        if src:
-            src = url.urljoin(self._url, src)
-            src, fragment = url.urldefrag(src)
-            if fragment:
-                self.error("import src many not include a fragment identifier")
-            urls = [src]
-        else:
-            urls = self._loader.schemaPackageURLs(pkg)
-            if not urls:
-                self.error("could not locate schema component " + `pkg`)
-        for s in urls:
-            schema = self._loader.loadURL(s)
-            for n in schema.gettypenames():
-                self._schema.addtype(schema.gettype(n))
+            self.error("no %s found" % self._top_level)
 
-    def end_import(self):
-        pass
+    # helper methods
 
     def get_position(self):
         if self._locator:
@@ -178,6 +141,9 @@
             prefix = ''
         self._prefixes.append(prefix)
 
+    def pop_prefix(self):
+        del self._prefixes[-1]
+
     def get_classname(self, name):
         if name.startswith("."):
             return self._prefixes[-1] + name
@@ -201,6 +167,102 @@
         datatype = self.get_datatype(attrs, "datatype", "null")
         return keytype, valuetype, datatype
 
+    def get_required(self, attrs):
+        if attrs.has_key("required"):
+            v = attrs["required"]
+            if v == "yes":
+                return True
+            elif v == "no":
+                return False
+            self.error("value for 'required' must be 'yes' or 'no'")
+        else:
+            return False
+
+    def get_ordinality(self, attrs):
+        min, max = 0, info.Unbounded
+        if self.get_required(attrs):
+            min = 1
+        return min, max
+
+    def get_sectiontype(self, attrs):
+        type = attrs.get("type")
+        if not type:
+            self.error("section must specify type")
+        return self._schema.gettype(type)
+
+    def get_key_info(self, attrs, element):
+        any, name, attribute = self.get_name_info(attrs, element)
+        if any == '*':
+            self.error(element + " may not specify '*' for name")
+        if not name and any != '+':
+            self.error(element + " name may not be omitted or empty")
+        datatype = self.get_datatype(attrs, "datatype", "string")
+        handler = self.get_handler(attrs)
+        return name or any, datatype, handler, attribute
+
+    def get_name_info(self, attrs, element):
+        name = attrs.get("name")
+        if not name:
+            self.error(element + " name must be specified and non-empty")
+        aname = attrs.get("attribute")
+        if aname:
+            aname = self.identifier(aname)
+            if aname.startswith("getSection"):
+                # reserved; used for SectionValue methods to get meta-info
+                self.error("attribute names may not start with 'getSection'")
+        if name in ("*", "+"):
+            if not aname:
+                self.error(
+                    "container attribute must be specified and non-empty"
+                    " when using '*' or '+' for a section name")
+            return name, None, aname
+        else:
+            # run the keytype converter to make sure this is a valid key
+            name = self._stack[-1].keytype(name)
+            if not aname:
+                aname = self.basic_key(name)
+                aname = self.identifier(aname.replace('-', '_'))
+            return None, self.basic_key(name), aname
+
+    # schema loading logic
+
+    def characters_default(self, data):
+        self._stack[-1].adddefault(data, self._position)
+
+    def characters_description(self, data):
+        self._stack[-1].description = data
+
+    def characters_example(self, data):
+        self._stack[-1].example = data
+
+    def characters_metadefault(self, data):
+        self._stack[-1].metadefault = data
+
+    def start_import(self, attrs):
+        src = attrs.get("src", "").strip()
+        pkg = attrs.get("package", "").strip()
+        if not (src or pkg):
+            self.error("import must specify either src or package")
+        if src and pkg:
+            self.error("import may only specify one of src or package")
+        if src:
+            src = url.urljoin(self._url, src)
+            src, fragment = url.urldefrag(src)
+            if fragment:
+                self.error("import src many not include a fragment identifier")
+            urls = [src]
+        else:
+            urls = self._loader.schemaPackageURLs(pkg)
+            if not urls:
+                self.error("could not locate schema component " + `pkg`)
+        for s in urls:
+            schema = self._loader.loadURL(s)
+            for n in schema.gettypenames():
+                self._schema.addtype(schema.gettype(n))
+
+    def end_import(self):
+        pass
+
     def start_sectiontype(self, attrs):
         name = attrs.get("type")
         if not name:
@@ -236,32 +298,9 @@
         self._stack.append(sectinfo)
 
     def end_sectiontype(self):
-        del self._prefixes[-1]
+        self.pop_prefix()
         self._stack.pop()
 
-    def get_required(self, attrs):
-        if attrs.has_key("required"):
-            v = attrs["required"]
-            if v == "yes":
-                return True
-            elif v == "no":
-                return False
-            self.error("value for 'required' must be 'yes' or 'no'")
-        else:
-            return False
-
-    def get_ordinality(self, attrs):
-        min, max = 0, info.Unbounded
-        if self.get_required(attrs):
-            min = 1
-        return min, max
-
-    def get_sectiontype(self, attrs):
-        type = attrs.get("type")
-        if not type:
-            self.error("section must specify type")
-        return self._schema.gettype(type)
-
     def start_section(self, attrs):
         sectiontype = self.get_sectiontype(attrs)
         handler = self.get_handler(attrs)
@@ -306,20 +345,10 @@
         self._stack.append(self._group)
 
     def end_sectiongroup(self):
-        del self._prefixes[-1]
+        self.pop_prefix()
         self._group = None
         self._stack.pop()
 
-    def get_key_info(self, attrs, element):
-        any, name, attribute = self.get_name_info(attrs, element)
-        if any == '*':
-            self.error(element + " may not specify '*' for name")
-        if not name and any != '+':
-            self.error(element + " name may not be omitted or empty")
-        datatype = self.get_datatype(attrs, "datatype", "string")
-        handler = self.get_handler(attrs)
-        return name or any, datatype, handler, attribute
-
     def start_key(self, attrs):
         name, datatype, handler, attribute = self.get_key_info(attrs, "key")
         min = self.get_required(attrs) and 1 or 0
@@ -350,30 +379,6 @@
     def end_multikey(self):
         self._stack.pop().finish()
 
-    def get_name_info(self, attrs, element):
-        name = attrs.get("name")
-        if not name:
-            self.error(element + " name must be specified and non-empty")
-        aname = attrs.get("attribute")
-        if aname:
-            aname = self.identifier(aname)
-            if aname.startswith("getSection"):
-                # reserved; used for SectionValue methods to get meta-info
-                self.error("attribute names may not start with 'getSection'")
-        if name in ("*", "+"):
-            if not aname:
-                self.error(
-                    "container attribute must be specified and non-empty"
-                    " when using '*' or '+' for a section name")
-            return name, None, aname
-        else:
-            # run the keytype converter to make sure this is a valid key
-            name = self._stack[-1].keytype(name)
-            if not aname:
-                aname = self.basic_key(name)
-                aname = self.identifier(aname.replace('-', '_'))
-            return None, self.basic_key(name), aname
-
     # datatype conversion wrappers
 
     def basic_key(self, s):
@@ -381,18 +386,12 @@
             return self._basic_key(s)
         except ValueError, e:
             self.error(e[0])
-        except ZConfig.SchemaError, e:
-            self.initerror(e)
-            raise
 
     def identifier(self, s):
         try:
             return self._identifier(s)
         except ValueError, e:
             self.error(e[0])
-        except ZConfig.SchemaError, e:
-            self.initerror(e)
-            raise
 
     # exception setup helpers
 
@@ -411,6 +410,7 @@
 
     # needed by startElement() and endElement()
     _handled_tags = BaseParser._handled_tags + ("schema",)
+    _top_level = "schema"
 
     def start_schema(self, attrs):
         self.push_prefix(attrs)
@@ -429,5 +429,5 @@
     def end_schema(self):
         del self._stack[-1]
         assert not self._stack
-        del self._prefixes[-1]
+        self.pop_prefix()
         assert not self._prefixes