[Zodb-checkins] CVS: Packages/ZConfig - info.py:1.3 schema.py:1.3
Fred L. Drake, Jr.
fred@zope.com
Mon, 6 Jan 2003 13:20:13 -0500
Update of /cvs-repository/Packages/ZConfig
In directory cvs.zope.org:/tmp/cvs-serv27185
Modified Files:
info.py schema.py
Log Message:
Make SectionType and SchemaType instances more similar, and separate the
idea of a type collection from the idea of a type group. This allows a
section type to be used as a complete schema directly.
=== Packages/ZConfig/info.py 1.2 => 1.3 ===
--- Packages/ZConfig/info.py:1.2 Fri Jan 3 16:05:51 2003
+++ Packages/ZConfig/info.py Mon Jan 6 13:19:38 2003
@@ -182,39 +182,33 @@
else:
return None
-class TypeContainer:
- def __init__(self):
- self._types = {}
- def addtype(self, typeinfo):
- n = typeinfo.name.lower()
- if self._types.has_key(n):
- raise ZConfig.SchemaError("type name cannot be redefined: "
- + `typeinfo.name`)
- self._types[n] = typeinfo
+class GroupType:
+ def __init__(self, name):
+ self._subtypes = {}
+ self.name = name
- def gettype(self, name):
- n = name.lower()
+ def addsubtype(self, type):
+ self._subtypes[type.name] = type
+
+ def getsubtype(self, name):
try:
- return self._types[n]
+ return self._subtypes[name]
except KeyError:
- raise ZConfig.SchemaError("unknown type name: " + `name`)
+ raise ZConfig.SchemaError("no subtype %s in group %s"
+ % (`name`, `self.name`))
- def gettypenames(self):
- return self._types.keys()
-
-
-class GroupType(TypeContainer):
- def __init__(self, name):
- TypeContainer.__init__(self)
- self.name = name
+ def getsubtypenames(self):
+ L = self._subtypes.keys()
+ L.sort()
+ return L
def istypegroup(self):
return True
class SectionType:
- def __init__(self, name, keytype, valuetype, datatype):
+ def __init__(self, name, keytype, valuetype, datatype, registry, types):
# name - name of the section, or '*' or '+'
# datatype - type for the section itself
# keytype - type for the keys themselves
@@ -223,9 +217,22 @@
self.datatype = datatype
self.keytype = keytype
self.valuetype = valuetype
+ self.handler = None
+ self.registry = registry
self._children = [] # [(key, info), ...]
self._attrmap = {} # {attribute: index, ...}
self._keymap = {} # {key: index, ...}
+ self._types = types
+
+ def gettype(self, name):
+ n = name.lower()
+ try:
+ return self._types[n]
+ except KeyError:
+ raise ZConfig.SchemaError("unknown type name: " + `name`)
+
+ def gettypenames(self):
+ return self._types.keys()
def __len__(self):
return len(self._children)
@@ -296,7 +303,7 @@
st = info.sectiontype
if st.istypegroup():
try:
- st = st.gettype(type)
+ st = st.getsubtype(type)
except ZConfig.ConfigurationError:
raise ZConfig.ConfigurationError(
"section type %s not allowed for name %s"
@@ -318,7 +325,7 @@
raise ZConfig.ConfigurationError(
"cannot define section with a sectiongroup type")
try:
- st = st.gettype(type)
+ st = st.getsubtype(type)
except ZConfig.ConfigurationError:
# not this one; maybe a different one
pass
@@ -337,13 +344,21 @@
return False
-class SchemaType(TypeContainer, SectionType):
- def __init__(self, name, keytype, valuetype, datatype, handler, url):
- SectionType.__init__(self, name, keytype, valuetype, datatype)
- TypeContainer.__init__(self)
+class SchemaType(SectionType):
+ def __init__(self, name, keytype, valuetype, datatype, handler, url,
+ registry):
+ SectionType.__init__(self, name, keytype, valuetype, datatype,
+ registry, {})
self.handler = handler
self.url = url
+ def addtype(self, typeinfo):
+ n = typeinfo.name.lower()
+ if self._types.has_key(n):
+ raise ZConfig.SchemaError("type name cannot be redefined: "
+ + `typeinfo.name`)
+ self._types[n] = typeinfo
+
def allowUnnamed(self):
return True
@@ -361,3 +376,9 @@
if self.name and self.name in alltypes:
alltypes.remove(self.name)
return alltypes
+
+ def createSectionType(self, name, keytype, valuetype, datatype):
+ t = SectionType(name, keytype, valuetype, datatype,
+ self.registry, self._types)
+ self.addtype(t)
+ return t
=== Packages/ZConfig/schema.py 1.2 => 1.3 ===
--- Packages/ZConfig/schema.py:1.2 Fri Jan 3 16:05:51 2003
+++ Packages/ZConfig/schema.py Mon Jan 6 13:19:38 2003
@@ -189,11 +189,10 @@
if name is not None:
name = self.basic_key(name)
self._schema = info.SchemaType(name, keytype, valuetype, datatype,
- handler, self._url)
+ handler, self._url, self._registry)
if name is not None:
# XXX circular reference
self._schema.addtype(self._schema)
- self._schema.registry = self._registry
self._stack = [self._schema]
def end_schema(self):
@@ -207,19 +206,19 @@
name = self.basic_key(name)
self.push_prefix(attrs)
keytype, valuetype, datatype = self.get_sect_typeinfo(attrs)
- sectinfo = info.SectionType(name, keytype, valuetype, datatype)
- self._schema.addtype(sectinfo)
+ sectinfo = self._schema.createSectionType(
+ name, keytype, valuetype, datatype)
if self._group is not None:
if attrs.has_key("group"):
self.error("sectiontype cannot specify group"
" if nested in a sectiongroup")
- self._group.addtype(sectinfo)
+ self._group.addsubtype(sectinfo)
elif attrs.has_key("group"):
groupname = self.basic_key(attrs["group"])
group = self._schema.gettype(groupname)
if not group.istypegroup():
self.error("type specified as group is not a sectiongroup")
- group.addtype(sectinfo)
+ group.addsubtype(sectinfo)
self._stack.append(sectinfo)
def end_sectiontype(self):