Package pyxb :: Module exceptions_
[hide private]
[frames] | no frames]

Source Code for Module pyxb.exceptions_

  1  # Copyright 2009, Peter A. Bigot 
  2  # 
  3  # Licensed under the Apache License, Version 2.0 (the "License"); you may 
  4  # not use this file except in compliance with the License. You may obtain a 
  5  # copy of the License at: 
  6  # 
  7  #            http://www.apache.org/licenses/LICENSE-2.0 
  8  # 
  9  # Unless required by applicable law or agreed to in writing, software 
 10  # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 11  # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 12  # License for the specific language governing permissions and limitations 
 13  # under the License. 
 14   
 15  """Extensions of standard exceptions for PyXB events. 
 16   
 17  Yeah, I'd love this module to be named exceptions.py, but it can't 
 18  because the standard library has one of those, and we need to 
 19  reference it below. 
 20  """ 
 21   
 22  import exceptions 
23 24 -class PyXBException (exceptions.Exception):
25 """Base class for exceptions that indicate a problem that the user should fix.""" 26 27 """The arguments passed to the exception constructor.""" 28 _args = None 29 30 """The keywords passed to the exception constructor. 31 32 @note: Do not pop values from the keywords array in subclass 33 constructors that recognize and extract values from them. They 34 should be kept around so they're accessible generically.""" 35 _kw = None 36
37 - def __init__ (self, *args, **kw):
38 """Create an exception indicating a PyXB-related problem. 39 40 @keyword message : Text to provide the user with information about the problem. 41 """ 42 self._args = args 43 self._kw = kw 44 exceptions.Exception.__init__(self, *args)
45
46 -class SchemaValidationError (PyXBException):
47 """Raised when the XML hierarchy does not appear to be valid for an XML schema.""" 48 pass
49
50 -class NamespaceError (PyXBException):
51 """Violation of some rule relevant to XML Namespaces"""
52 - def __init__ (self, namespace, *args, **kw):
53 PyXBException.__init__(self, *args, **kw) 54 self.__namespace = namespace
55
56 - def namespace (self): return self.__namespace
57
58 -class NamespaceArchiveError (PyXBException):
59 """Problem related to namespace archives""" 60 pass
61
62 -class SchemaUniquenessError (PyXBException):
63 """Raised when somebody tries to create a schema component using a 64 schema that has already been used in that namespace. Import and 65 include processing would have avoided this, so somebody asked for 66 it specifically."""
67 - def __init__ (self, namespace, schema_location, existing_schema, *args, **kw):
68 # Prior to 2.5, exceptions did not inherit from object, and 69 # super could not be used. 70 #super(SchemaUniquenessError, self).__init__(*args, **kw) 71 PyXBException.__init__(self, *args, **kw) 72 self.__namespace = namespace 73 self.__schemaLocation = schema_location 74 self.__existingSchema = existing_schema
75
76 - def namespace (self): return self.__namespace
77 - def schemaLocation (self): return self.__schemaLocation
78 - def existingSchema (self): return self.__existingSchema
79
80 -class BindingGenerationError (PyXBException):
81 """Raised when something goes wrong generating the binding classes""" 82 pass
83
84 -class NamespaceUniquenessError (NamespaceError):
85 """Raised when an attempt is made to record multiple objects of the same name in the same namespace category.""" 86 pass
87
88 -class BadTypeValueError (PyXBException):
89 """Raised when a value in an XML attribute does not conform to the simple type.""" 90 pass
91
92 -class NotInNamespaceError (PyXBException):
93 '''Raised when a name is referenced that is not defined in the appropriate namespace.''' 94 __namespace = None 95 __ncName = None
96
97 -class BadPropertyError (PyXBException):
98 """Raised when a schema component property is accessed on a component instance that does not define that property.""" 99 pass
100
101 -class BadDocumentError (PyXBException):
102 """Raised when processing document content and an error is encountered.""" 103 pass
104
105 -class StructuralBadDocumentError (BadDocumentError):
106 """Raised when processing document and the content model is not satisfied.""" 107 @property
108 - def element_use (self):
109 """The L{pyxb.binding.content.ElementUse} instance to which the content should conform, if available.""" 110 return self.__elementUse
111 112 @property
113 - def container (self):
114 """The L{pyxb.binding.basis.complexTypeDefinition} instance to which the content would belong, if available.""" 115 return self.__container
116 117 @property
118 - def content (self):
119 """The value which could not be reconciled with the content model.""" 120 return self.__content
121
122 - def __init__ (self, *args, **kw):
123 """Raised when processing document and the content model is not satisfied. 124 125 @keyword content : The value that could not be reconciled with the content model 126 @keyword container : Optional binding instance into which the content was to be assigned 127 @keyword element_use : Optional reference to an element use identifying the element to which the value was to be reconciled 128 """ 129 self.__content = kw.pop('content', None) 130 if args: 131 self.__content = args[0] 132 self.__container = kw.pop('container', None) 133 self.__elementUse = kw.pop('element_use', None) 134 if self.__content is not None: 135 if self.__container is not None: 136 kw.setdefault('message', '%s cannot accept wildcard content %s' % (self.__container, self.__content)) 137 elif self.__elementUse is not None: 138 kw.setdefault('message', '%s not consistent with content model for %s' % (self.__content, self.__elementUse)) 139 else: 140 kw.setdefault('message', str(self.__content)) 141 BadDocumentError.__init__(self, **kw)
142
143 -class AbstractElementError (StructuralBadDocumentError):
144 """Raised when attempting to construct an element that is abstract.""" 145 pass
146
147 -class UnrecognizedContentError (StructuralBadDocumentError):
148 """Raised when processing document and an element does not match the content model.""" 149 pass
150
151 -class UnrecognizedElementError (UnrecognizedContentError):
152 """Raised when creating an instance from a document with an unrecognized root element.""" 153 154 @property
155 - def element_name (self):
156 """The L{pyxb.namespace.ExpandedName} of the element that was not recognized.""" 157 return self.__elementName
158 159 @property
160 - def dom_node (self):
161 """The DOM node associated with the unrecognized element, if available.""" 162 return self.__domNode
163
164 - def __init__ (self, **kw):
165 """Raised when creating an instance from a document with an unrecognized root element. 166 167 @keyword element_name : The expanded name of the outermost element 168 @keyword dom_node : The DOM node of the outermost element, if available 169 """ 170 self.__domNode = kw.get('dom_node') 171 self.__elementName = kw.get('element_name') 172 if self.__elementName is None: 173 if self.__domNode is not None: 174 import pyxb.namespace 175 self.__elementName = pyxb.namespace.ExpandedName(self.__domNode.namespaceURI, self.__domNode.localName) 176 else: 177 raise LogicError('No source for element_name in UnrecognizedElementError') 178 kw['content'] = self.__domNode 179 kw.setdefault('message', 'No element binding available for %s' % (self.__elementName,)) 180 UnrecognizedContentError.__init__(self, **kw)
181
182 -class MissingElementError (UnrecognizedContentError):
183 """Content requires an element that is not present.""" 184 pass
185
186 -class UnexpectedElementError (StructuralBadDocumentError):
187 """More instances of an element are present than permitted by the content model.""" 188 pass
189
190 -class ExtraContentError (StructuralBadDocumentError):
191 """Raised when processing document and there is more material in an element content than expected."""
192
193 -class ContentInNilElementError (ExtraContentError):
194 """Raised when an element that is marked to be nil has content.""" 195 pass
196
197 -class MissingContentError (StructuralBadDocumentError):
198 """Raised when processing document and expected content is not present. See also UnrecognizedContentError."""
199
200 -class NotAnElementError (UnrecognizedContentError):
201 """Raised when processing document and a tag that is a type but not an element is encountered.""" 202 203 @property
204 - def element_name (self):
205 """The L{pyxb.namespace.ExpandedName} of the element that was not recognized.""" 206 return self.__elementName
207 208 @property
209 - def containing_type (self):
210 """The L{pyxb.binding.content.complexTypeDefinition} in which the element was unrecognized.""" 211 return self.__containingType
212
213 - def __init__ (self, element_name, containing_type, **kw):
214 """Raised when a document inner element is recognized as a type rather than an element. 215 216 @param element_name : The name of the inner element from the document 217 @param containing_type : The L{pyxb.binding.content.complexTypeDefinition} class in which the lookup failed 218 """ 219 self.__elementName = element_name 220 self.__containingType = containing_type 221 kw.setdefault('message', 'Unable to locate element %s in type %s' % (element_name, self.__containingType._ExpandedName)) 222 UnrecognizedContentError.__init__(self, **kw)
223
224 -class UnrecognizedAttributeError (BadDocumentError):
225 """Raised when an attribute is found that is not sanctioned by the content model."""
226
227 -class ValidationError (PyXBException):
228 """Raised when something in the infoset fails to satisfy a content model or attribute requirement.""" 229 pass
230
231 -class AttributeValidationError (ValidationError):
232 """Raised when an attribute requirement is not satisfied.""" 233 pass
234
235 -class ProhibitedAttributeError (AttributeValidationError):
236 """Raised when an attribute that is prohibited is provided in an element."""
237
238 -class MissingAttributeError (AttributeValidationError):
239 """Raised when an attribute that is required is missing in an element."""
240
241 -class AttributeChangeError (BadDocumentError):
242 """Raised when an attribute with a fixed value constraint is set to a different value."""
243
244 -class AbstractInstantiationError (PyXBException):
245 """Raised when somebody tries to instantiate an abstract complex type."""
246
247 -class DOMGenerationError (PyXBException):
248 """Raised when converting binding to DOM and something goes wrong.""" 249 pass
250
251 -class NoNillableSupportError (PyXBException):
252 """Raised when checking _isNil on a type that does not support nillable.""" 253 pass
254
255 -class BindingValidationError (ValidationError):
256 """Raised when the content of a binding object is not consistent with its content model""" 257 pass
258
259 -class UnexpectedNonElementContentError (ValidationError):
260 """Raised when an element is given non-element content but may not contain such.""" 261 pass
262
263 -class NoContentModel (BindingValidationError):
264 """Raised when an operation is attempted that requires a content 265 model, but the complex type has empty or simple content.""" 266 pass
267
268 -class BindingError (PyXBException):
269 """Raised when the bindings are mis-used.""" 270 pass
271
272 -class NotSimpleContentError (BindingError):
273 """Raised when an operation that requires simple content is 274 invoked on a complex type that does not have simple content.""" 275 pass
276
277 -class NotComplexContentError (BindingError):
278 """Raised when an operation is attempted that requires a content 279 model, but the complex type has empty or simple content.""" 280 pass
281
282 -class PyXBError (exceptions.Exception):
283 """Base class for exceptions that indicate a problem that the user probably can't fix.""" 284 pass
285
286 -class UsageError (PyXBError):
287 """Raised when the code detects arguments to a public 288 operation."""
289
290 -class LogicError (PyXBError):
291 """Raised when the code detects an implementation problem."""
292
293 -class IncompleteImplementationError (LogicError):
294 """Raised when a code branch is taken that has not yet been implemented."""
295