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 @property
38 - def message (self):
39 '''A message to help a human understand the problem.''' 40 if self.__message is None: 41 return str(self) 42 return self.__message
43
44 - def __str__ (self):
45 """Override to use the system-provided message, if available.""" 46 if self.__message is not None: 47 return '%s: %s' % (type(self).__name__, self.__message) 48 return exceptions.Exception.__str__(self)
49
50 - def __init__ (self, *args, **kw):
51 """Create an exception indicating a PyXB-related problem. 52 53 @keyword message : Text to provide the user with information about the problem. 54 """ 55 self.__message = kw.get('message') 56 self._args = args 57 self._kw = kw 58 exceptions.Exception.__init__(self, *args)
59
60 -class SchemaValidationError (PyXBException):
61 """Raised when the XML hierarchy does not appear to be valid for an XML schema.""" 62 pass
63
64 -class NamespaceError (PyXBException):
65 """Violation of some rule relevant to XML Namespaces"""
66 - def __init__ (self, namespace, *args, **kw):
67 PyXBException.__init__(self, *args, **kw) 68 self.__namespace = namespace
69
70 - def namespace (self): return self.__namespace
71
72 -class NamespaceArchiveError (PyXBException):
73 """Problem related to namespace archives""" 74 pass
75
76 -class SchemaUniquenessError (PyXBException):
77 """Raised when somebody tries to create a schema component using a 78 schema that has already been used in that namespace. Import and 79 include processing would have avoided this, so somebody asked for 80 it specifically."""
81 - def __init__ (self, namespace, schema_location, existing_schema, *args, **kw):
82 # Prior to 2.5, exceptions did not inherit from object, and 83 # super could not be used. 84 #super(SchemaUniquenessError, self).__init__(*args, **kw) 85 PyXBException.__init__(self, *args, **kw) 86 self.__namespace = namespace 87 self.__schemaLocation = schema_location 88 self.__existingSchema = existing_schema
89
90 - def namespace (self): return self.__namespace
91 - def schemaLocation (self): return self.__schemaLocation
92 - def existingSchema (self): return self.__existingSchema
93
94 -class BindingGenerationError (PyXBException):
95 """Raised when something goes wrong generating the binding classes""" 96 pass
97
98 -class NamespaceUniquenessError (NamespaceError):
99 """Raised when an attempt is made to record multiple objects of the same name in the same namespace category.""" 100 pass
101
102 -class BadTypeValueError (PyXBException):
103 """Raised when a value in an XML attribute does not conform to the simple type.""" 104 pass
105
106 -class NotInNamespaceError (PyXBException):
107 '''Raised when a name is referenced that is not defined in the appropriate namespace.''' 108 __namespace = None 109 __ncName = None
110
111 -class BadPropertyError (PyXBException):
112 """Raised when a schema component property is accessed on a component instance that does not define that property.""" 113 pass
114
115 -class BadDocumentError (PyXBException):
116 """Raised when processing document content and an error is encountered.""" 117 pass
118
119 -class StructuralBadDocumentError (BadDocumentError):
120 """Raised when processing document and the content model is not satisfied.""" 121 @property
122 - def element_use (self):
123 """The L{pyxb.binding.content.ElementUse} instance to which the content should conform, if available.""" 124 return self.__elementUse
125 126 @property
127 - def container (self):
128 """The L{pyxb.binding.basis.complexTypeDefinition} instance to which the content would belong, if available.""" 129 return self.__container
130 131 @property
132 - def content (self):
133 """The value which could not be reconciled with the content model.""" 134 return self.__content
135
136 - def __init__ (self, *args, **kw):
137 """Raised when processing document and the content model is not satisfied. 138 139 @keyword content : The value that could not be reconciled with the content model 140 @keyword container : Optional binding instance into which the content was to be assigned 141 @keyword element_use : Optional reference to an element use identifying the element to which the value was to be reconciled 142 """ 143 self.__content = kw.pop('content', None) 144 if args: 145 self.__content = args[0] 146 self.__container = kw.pop('container', None) 147 self.__elementUse = kw.pop('element_use', None) 148 if self.__content is not None: 149 if self.__container is not None: 150 kw.setdefault('message', '%s cannot accept wildcard content %s' % (self.__container, self.__content)) 151 elif self.__elementUse is not None: 152 kw.setdefault('message', '%s not consistent with content model for %s' % (self.__content, self.__elementUse)) 153 else: 154 kw.setdefault('message', str(self.__content)) 155 BadDocumentError.__init__(self, **kw)
156
157 -class AbstractElementError (StructuralBadDocumentError):
158 """Raised when attempting to construct an element that is abstract.""" 159 pass
160
161 -class UnrecognizedContentError (StructuralBadDocumentError):
162 """Raised when processing document and an element does not match the content model.""" 163 pass
164
165 -class UnrecognizedElementError (UnrecognizedContentError):
166 """Raised when creating an instance from a document with an unrecognized root element.""" 167 168 @property
169 - def element_name (self):
170 """The L{pyxb.namespace.ExpandedName} of the element that was not recognized.""" 171 return self.__elementName
172 173 @property
174 - def dom_node (self):
175 """The DOM node associated with the unrecognized element, if available.""" 176 return self.__domNode
177
178 - def __init__ (self, **kw):
179 """Raised when creating an instance from a document with an unrecognized root element. 180 181 @keyword element_name : The expanded name of the outermost element 182 @keyword dom_node : The DOM node of the outermost element, if available 183 """ 184 self.__domNode = kw.get('dom_node') 185 self.__elementName = kw.get('element_name') 186 if self.__elementName is None: 187 if self.__domNode is not None: 188 import pyxb.namespace 189 self.__elementName = pyxb.namespace.ExpandedName(self.__domNode.namespaceURI, self.__domNode.localName) 190 else: 191 raise LogicError('No source for element_name in UnrecognizedElementError') 192 kw['content'] = self.__domNode 193 kw.setdefault('message', 'No element binding available for %s' % (self.__elementName,)) 194 UnrecognizedContentError.__init__(self, **kw)
195
196 -class MissingElementError (UnrecognizedContentError):
197 """Content requires an element that is not present.""" 198 pass
199
200 -class UnexpectedElementError (StructuralBadDocumentError):
201 """More instances of an element are present than permitted by the content model.""" 202 pass
203
204 -class ExtraContentError (StructuralBadDocumentError):
205 """Raised when processing document and there is more material in an element content than expected."""
206
207 -class ContentInNilElementError (ExtraContentError):
208 """Raised when an element that is marked to be nil has content.""" 209 pass
210
211 -class MissingContentError (StructuralBadDocumentError):
212 """Raised when processing document and expected content is not present. See also UnrecognizedContentError."""
213
214 -class NotAnElementError (UnrecognizedContentError):
215 """Raised when processing document and a tag that is a type but not an element is encountered.""" 216 217 @property
218 - def element_name (self):
219 """The L{pyxb.namespace.ExpandedName} of the element that was not recognized.""" 220 return self.__elementName
221 222 @property
223 - def containing_type (self):
224 """The L{pyxb.binding.content.complexTypeDefinition} in which the element was unrecognized.""" 225 return self.__containingType
226
227 - def __init__ (self, element_name, containing_type, **kw):
228 """Raised when a document inner element is recognized as a type rather than an element. 229 230 @param element_name : The name of the inner element from the document 231 @param containing_type : The L{pyxb.binding.content.complexTypeDefinition} class in which the lookup failed 232 """ 233 self.__elementName = element_name 234 self.__containingType = containing_type 235 kw.setdefault('message', 'Unable to locate element %s in type %s' % (element_name, self.__containingType._ExpandedName)) 236 UnrecognizedContentError.__init__(self, **kw)
237
238 -class UnrecognizedAttributeError (BadDocumentError):
239 """Raised when an attribute is found that is not sanctioned by the content model."""
240
241 -class ValidationError (PyXBException):
242 """Raised when something in the infoset fails to satisfy a content model or attribute requirement.""" 243 pass
244
245 -class AttributeValidationError (ValidationError):
246 """Raised when an attribute requirement is not satisfied.""" 247 pass
248
249 -class ProhibitedAttributeError (AttributeValidationError):
250 """Raised when an attribute that is prohibited is provided in an element."""
251
252 -class MissingAttributeError (AttributeValidationError):
253 """Raised when an attribute that is required is missing in an element."""
254
255 -class AttributeChangeError (BadDocumentError):
256 """Raised when an attribute with a fixed value constraint is set to a different value."""
257
258 -class AbstractInstantiationError (PyXBException):
259 """Raised when somebody tries to instantiate an abstract complex type."""
260
261 -class DOMGenerationError (PyXBException):
262 """Raised when converting binding to DOM and something goes wrong.""" 263 pass
264
265 -class NoNillableSupportError (PyXBException):
266 """Raised when checking _isNil on a type that does not support nillable.""" 267 pass
268
269 -class BindingValidationError (ValidationError):
270 """Raised when the content of a binding object is not consistent with its content model""" 271 pass
272
273 -class UnexpectedNonElementContentError (ValidationError):
274 """Raised when an element is given non-element content but may not contain such.""" 275 pass
276
277 -class NoContentModel (BindingValidationError):
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 BindingError (PyXBException):
283 """Raised when the bindings are mis-used.""" 284 pass
285
286 -class NotSimpleContentError (BindingError):
287 """Raised when an operation that requires simple content is 288 invoked on a complex type that does not have simple content.""" 289 pass
290
291 -class NotComplexContentError (BindingError):
292 """Raised when an operation is attempted that requires a content 293 model, but the complex type has empty or simple content.""" 294 pass
295
296 -class PyXBError (exceptions.Exception):
297 """Base class for exceptions that indicate a problem that the user probably can't fix.""" 298 pass
299
300 -class UsageError (PyXBError):
301 """Raised when the code detects arguments to a public 302 operation."""
303
304 -class LogicError (PyXBError):
305 """Raised when the code detects an implementation problem."""
306
307 -class IncompleteImplementationError (LogicError):
308 """Raised when a code branch is taken that has not yet been implemented."""
309