def __init__(self, name, sectiontype, minOccurs, maxOccurs, handler, attribute): # name - name of the section; one of '*', '+', or name1 # sectiontype - SectionType instance # minOccurs - minimum number of occurances of the section # maxOccurs - maximum number of occurances; if > 1, name # must be '*' or '+' # handler - handler name called when value(s) must take effect, # or None # attribute - name of the attribute on the SectionValue object if maxOccurs > 1: if name not in ('*', '+'): raise ZConfig.SchemaError( "sections which can occur more than once must" " use a name of '*' or '+'") if not attribute: raise ZConfig.SchemaError( "sections which can occur more than once must" " specify a target attribute name") if sectiontype.isabstract(): datatype = None else: datatype = sectiontype.datatype BaseInfo.__init__(self, name, datatype, minOccurs, maxOccurs, handler, attribute) self.sectiontype = sectiontype
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") self.add_valueinfo(ValueInfo(value, position), key)
def add_valueinfo(self, vi, key): if self.name == "+": if key in self._default: # not ideal: we're presenting the unconverted # version of the key raise ZConfig.SchemaError( "duplicate default value for key %s" % repr(key)) self._default[key] = vi elif self._default is not None: raise ZConfig.SchemaError( "cannot set more than one default to key with maxOccurs == 1") else: self._default = vi
def _add_child(self, key, info): # check naming constraints assert key or info.attribute if key and key in self._keymap: raise ZConfig.SchemaError("child name %s already used" % key) if info.attribute and info.attribute in self._attrmap: raise ZConfig.SchemaError("child attribute name %s already used" % info.attribute) # a-ok, add the item to the appropriate maps if info.attribute: self._attrmap[info.attribute] = info if key: self._keymap[key] = info self._children.append((key, info))
def __init__(self, name, datatype, minOccurs, maxOccurs, handler, attribute): if maxOccurs is not None and maxOccurs < 1: if maxOccurs < 1: raise ZConfig.SchemaError("maxOccurs must be at least 1") if minOccurs is not None and minOccurs < maxOccurs: raise ZConfig.SchemaError( "minOccurs must be at least maxOccurs") self.name = name self.datatype = datatype self.minOccurs = minOccurs self.maxOccurs = maxOccurs self.handler = handler self.attribute = attribute
def __init__(self, name, datatype, minOccurs, maxOccurs, handler, attribute): assert maxOccurs is not None, "Use Unbounded for an upper bound, not None" assert minOccurs is not None, "Use 0 for a lower bound, not None" if maxOccurs < 1: raise ZConfig.SchemaError("maxOccurs must be at least 1") if minOccurs > maxOccurs: raise ZConfig.SchemaError( "minOccurs cannot be more than maxOccurs") self.name = name self.datatype = datatype self.minOccurs = minOccurs self.maxOccurs = maxOccurs self.handler = handler self.attribute = attribute
def __init__(self, schema): if schema.isabstract(): raise ZConfig.SchemaError( "cannot check a configuration an abstract type") BaseLoader.__init__(self) self.schema = schema self._private_schema = False
def schemaComponentSource(self, package, filename): parts = package.split(".") if not parts: # pragma: no cover raise ZConfig.SchemaError("illegal schema component name: " + repr(package)) if "" in parts: # '' somewhere in the package spec; still illegal raise ZConfig.SchemaError("illegal schema component name: " + repr(package)) filename = filename or "component.xml" try: __import__(package) except ImportError as e: raise ZConfig.SchemaResourceError("could not load package %s: %s" % (package, str(e)), filename=filename, package=package) pkg = sys.modules[package] if not hasattr(pkg, "__path__"): raise ZConfig.SchemaResourceError( "import name does not refer to a package", filename=filename, package=package) return "package:%s:%s" % (package, filename)
def deriveSectionType(self, base, name, keytype, valuetype, datatype): if isinstance(base, SchemaType): raise ZConfig.SchemaError( "cannot derive sectiontype from top-level schema") t = self.createSectionType(name, keytype, valuetype, datatype) t._attrmap.update(base._attrmap) t._keymap.update(base._keymap) t._children.extend(base._children) for i in range(len(t._children)): key, info = t._children[i] if isinstance(info, BaseKeyInfo) and info.name == "+": # need to create a new info object and recompute the # default mapping based on the new keytype info = copy.copy(info) info.computedefault(t.keytype) t._children[i] = (key, info) return t
def addComponent(self, name): if name in self._components: raise ZConfig.SchemaError("already have component %s" % name) self._components[name] = name
def addtype(self, typeinfo): n = typeinfo.name if n in self._types: raise ZConfig.SchemaError("type name cannot be redefined: " + repr(typeinfo.name)) self._types[n] = typeinfo
def gettype(self, name): n = name.lower() try: return self._types[n] except KeyError: raise ZConfig.SchemaError("unknown type name: " + repr(name))
def finish(self): if self._finished: raise ZConfig.SchemaError("cannot finish KeyInfo more than once") self._finished = True
def error(self, message): raise_with_same_tb(self.initerror(ZConfig.SchemaError(message)))
def error(self, message): raise self.initerror(ZConfig.SchemaError(message))
def getsubtype(self, name): try: return self._subtypes[name] except KeyError: raise ZConfig.SchemaError("no sectiontype %s in abstracttype %s" % (repr(name), repr(self.name)))
def addComponent(self, name): if self._components.has_key(name): raise ZConfig.SchemaError("already have component %s" % name) self._components[name] = name