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

Source Code for Module pyxb.exceptions_

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