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

Source Code for Module pyxb.exceptions_

  1  # -*- coding: utf-8 -*- 
  2  # Copyright 2009-2013, 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 pyxb 
 24  import exceptions 
25 26 -class PyXBException (exceptions.Exception):
27 """Base class for exceptions that indicate a problem that the user should fix.""" 28 29 """The arguments passed to the exception constructor.""" 30 _args = None 31 32 """The keywords passed to the exception constructor. 33 34 @note: Do not pop values from the keywords array in subclass 35 constructors that recognize and extract values from them. They 36 should be kept around so they're accessible generically.""" 37 _kw = None 38
39 - def __init__ (self, *args, **kw):
40 """Create an exception indicating a PyXB-related problem. 41 42 If no args are present, a default argument is taken from the 43 C{message} keyword. 44 45 @keyword message : Text to provide the user with information about the problem. 46 """ 47 if 0 == len(args) and 'message' in kw: 48 args = (kw.pop('message'),) 49 self._args = args 50 self._kw = kw 51 exceptions.Exception.__init__(self, *args)
52
53 - def _str_from_unicode (self):
54 return unicode(self).encode(pyxb._OutputEncoding)
55
56 -class PyXBVersionError (PyXBException):
57 """Raised on import of a binding generated with a different version of PYXB""" 58 pass
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 NotInNamespaceError (PyXBException):
103 '''Raised when a name is referenced that is not defined in the appropriate namespace.''' 104 __namespace = None 105 __ncName = None
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.ElementDeclaration} 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._Name(), 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', unicode(self.__content)) 147 BadDocumentError.__init__(self, **kw)
148
149 -class UnrecognizedDOMRootNodeError (StructuralBadDocumentError):
150 """A root DOM node could not be resolved to a schema element""" 151 152 node = None 153 """The L{xml.dom.Element} instance that could not be recognized""" 154
155 - def __get_node_name (self):
156 """The QName of the L{node} as a L{pyxb.namespace.ExpandedName}""" 157 import pyxb.namespace 158 return pyxb.namespace.ExpandedName(self.node.namespaceURI, self.node.localName)
159 node_name = property(__get_node_name) 160
161 - def __init__ (self, node):
162 """@param node: the value for the L{node} attribute.""" 163 self.node = node 164 super(UnrecognizedDOMRootNodeError, self).__init__(node)
165
166 -class ValidationError (PyXBException):
167 """Raised when something in the infoset fails to satisfy a content model or attribute requirement. 168 169 All validation errors include a L{location} attribute which shows 170 where in the original XML the problem occurred. The attribute may 171 be C{None} if the content did not come from an XML document, or 172 the underlying XML infrastructure did not provide a location. 173 174 More refined validation error exception classes add more attributes.""" 175 176 location = None 177 """Where the error occurred in the document being parsed, if 178 available. This will be C{None}, or an instance of 179 L{pyxb.utils.utility.Location}.""" 180
181 - def details (self):
182 """Provide information describing why validation failed. 183 184 In many cases, this is simply the informal string content that 185 would be obtained through the C{str} built-in function. For 186 certain errors this method gives more details on what would be 187 acceptable and where the descriptions can be found in the 188 original schema. 189 190 @return: a string description of validation failure""" 191 return unicode(self)
192
193 -class ElementValidationError (ValidationError):
194 """Raised when a validation requirement for an element is not satisfied.""" 195 pass
196
197 -class AbstractElementError (ElementValidationError):
198 """Attempt to create an instance of an abstract element. 199 200 Raised when an element is created and the identified binding is 201 abstract. Such elements cannot be created directly; instead the 202 creation must derive from an instance of the abstract element's 203 substitution group. 204 205 Since members of the substitution group self-identify using the 206 C{substitutionGroup} attribute, there is no general way to find 207 the set of elements which would be acceptable in place of the 208 abstract element.""" 209 210 element = None 211 """The abstract L{pyxb.binding.basis.element} in question""" 212 213 value = None 214 """The value proposed for the L{element}. This is usually going 215 to be a C{xml.dom.Node} used in the attempt to create the element, 216 C{None} if the abstract element was invoked without a node, or 217 another type if 218 L{pyxb.binding.content.ElementDeclaration.toDOM} is 219 mis-used.""" 220
221 - def __init__ (self, element, location, value=None):
222 """@param element: the value for the L{element} attribute. 223 @param location: the value for the L{location} attribute. 224 @param value: the value for the L{value} attribute.""" 225 self.element = element 226 self.location = location 227 self.value = value 228 super(AbstractElementError, self).__init__(element, location, value)
229
230 - def __unicode__ (self):
231 return 'Cannot instantiate abstract element %s directly' % (self.element.name(),)
232 __str__ = PyXBException._str_from_unicode
233
234 -class ContentInNilInstanceError (ElementValidationError):
235 """Raised when an element that is marked to be nil is assigned content.""" 236 237 instance = None 238 """The binding instance which is xsi:nil""" 239 240 content = None 241 """The content that was to be assigned to the instance.""" 242
243 - def __init__ (self, instance, content, location=None):
244 """@param instance: the value for the L{instance} attribute. 245 @param content: the value for the L{content} attribute. 246 @param location: the value for the L{location} attribute. Default taken from C{instance} if possible.""" 247 248 self.instance = instance 249 self.content = content 250 if location is None: 251 location = self.instance._location() 252 self.location = location 253 super(ContentInNilInstanceError, self).__init__(instance, content, location)
254
255 - def __unicode__ (self):
256 from pyxb.namespace.builtin import XMLSchema_instance as XSI 257 return '%s with %s=true cannot have content' % (self.instance._diagnosticName(), XSI.nil)
258 __str__ = PyXBException._str_from_unicode
259
260 -class NoNillableSupportError (ElementValidationError):
261 """Raised when invoking L{_setIsNil<pyxb.binding.basis._TypeBinding_mixin._setIsNil>} on a type that does not support nillable.""" 262 263 instance = None 264 """The binding instance on which an inappropriate operation was invoked.""" 265
266 - def __init__ (self, instance, location=None):
267 """@param instance: the value for the L{instance} attribute. 268 @param location: the value for the L{location} attribute. Default taken from C{instance} if possible.""" 269 self.instance = instance 270 if location is None: 271 location = self.instance._location() 272 self.location = location 273 super(NoNillableSupportError, self).__init__(instance, location)
274
275 -class ElementChangeError (ElementValidationError):
276 """Attempt to change an element that has a fixed value constraint.""" 277 278 element = None 279 """The L{pyxb.binding.basis.element} that has a fixed value.""" 280 281 value = None 282 """The value that was to be assigned to the element.""" 283
284 - def __init__ (self, element, value, location=None):
285 """@param element: the value for the L{element} attribute. 286 @param value: the value for the L{value} attribute. 287 @param location: the value for the L{location} attribute. Default taken from C{value} if possible.""" 288 289 import pyxb.utils.utility 290 self.element = element 291 self.value = value 292 if (location is None) and isinstance(value, pyxb.utils.utility.Locatable_mixin): 293 location = value._location() 294 self.location = location 295 super(ElementChangeError, self).__init__(element, value, location)
296
297 - def __unicode__ (self):
298 return 'Element %s has fixed content %s' % (self.element.name(), self.value)
299 __str__ = PyXBException._str_from_unicode
300
301 -class ComplexTypeValidationError (ValidationError):
302 """Raised when a validation requirement for a complex type is not satisfied.""" 303 pass
304
305 -class AbstractInstantiationError (ComplexTypeValidationError):
306 """Attempt to create an instance of an abstract complex type. 307 308 These types are analogous to abstract base classes, and cannot be 309 created directly. A type should be used that extends the abstract 310 class. 311 312 When an incoming document is missing the xsi:type attribute which 313 redirects an element with an abstract type to the correct type, 314 the L{node} attribute is provided so the user can get a clue as to 315 where the problem occured. When this exception is a result of 316 constructor mis-use in Python code, the traceback will tell you 317 where the problem lies. 318 """ 319 320 type = None 321 """The abstract L{pyxb.binding.basis.complexTypeDefinition} subclass used.""" 322 323 node = None 324 """The L{xml.dom.Element} from which instantiation was attempted, if available.""" 325
326 - def __init__ (self, type, location, node):
327 """@param type: the value for the L{type} attribute. 328 @param location: the value for the L{location} attribute. 329 @param node: the value for the L{node} attribute.""" 330 self.type = type 331 self.location = location 332 self.node = node 333 super(AbstractInstantiationError, self).__init__(type, location, node)
334
335 - def __unicode__ (self):
336 # If the type is abstract, it has to have a name. 337 return 'Cannot instantiate abstract type %s directly' % (self.type._ExpandedName,)
338 __str__ = PyXBException._str_from_unicode
339
340 -class AttributeOnSimpleTypeError (ComplexTypeValidationError):
341 """Attempt made to set an attribute on an element with simple type. 342 343 Note that elements with complex type and simple content may have 344 attributes; elements with simple type must not.""" 345 346 instance = None 347 """The simple type binding instance on which no attributes exist.""" 348 349 tag = None 350 """The name of the proposed attribute.""" 351 352 value = None 353 """The value proposed to be assigned to the non-existent attribute.""" 354
355 - def __init__ (self, instance, tag, value, location=None):
356 """@param instance: the value for the L{instance} attribute. 357 @param tag: the value for the L{tag} attribute. 358 @param value: the value for the L{value} attribute. 359 @param location: the value for the L{location} attribute. Default taken from C{instance} if possible.""" 360 361 self.instance = instance 362 self.tag = tag 363 self.value = value 364 if location is None: 365 location = self.instance._location() 366 self.location = location 367 super(AttributeOnSimpleTypeError, self).__init__(instance, tag, value, location)
368
369 - def __unicode__ (self):
370 return 'Simple type %s cannot support attribute %s' % (self.instance._Name(), self.tag)
371 __str__ = PyXBException._str_from_unicode
372
373 -class ContentValidationError (ComplexTypeValidationError):
374 """Violation of a complex type content model.""" 375 pass
376
377 -class SimpleContentAbsentError (ContentValidationError):
378 """An instance with simple content was not provided with a value.""" 379 380 instance = None 381 """The binding instance for which simple content is missing.""" 382
383 - def __init__ (self, instance, location):
384 """@param instance: the value for the L{instance} attribute. 385 @param location: the value for the L{location} attribute.""" 386 self.instance = instance 387 self.location = location 388 super(SimpleContentAbsentError, self).__init__(instance, location)
389
390 - def __unicode__ (self):
391 return 'Type %s requires content' % (self.instance._Name(),)
392 __str__ = PyXBException._str_from_unicode
393
394 -class ExtraSimpleContentError (ContentValidationError):
395 """A complex type with simple content was provided too much content.""" 396 397 instance = None 398 """The binding instance that already has simple content assigned.""" 399 400 value = None 401 """The proposed addition to that simple content.""" 402
403 - def __init__ (self, instance, value, location=None):
404 """@param instance: the value for the L{instance} attribute. 405 @param value: the value for the L{value} attribute. 406 @param location: the value for the L{location} attribute.""" 407 self.instance = instance 408 self.value = value 409 self.location = location 410 super(ExtraSimpleContentError, self).__init__(instance, value, location)
411
412 - def __unicode__ (self):
413 return 'Instance of %s already has simple content value assigned' % (self.instance._Name(),)
414 __str__ = PyXBException._str_from_unicode
415
416 -class NonPluralAppendError (ContentValidationError):
417 """Attempt to append to an element which does not accept multiple instances.""" 418 419 instance = None 420 """The binding instance containing the element""" 421 422 element_declaration = None 423 """The L{pyxb.binding.content.ElementDeclaration} contained in C{instance} that does not accept multiple instances""" 424 425 value = None 426 """The proposed addition to the element in the instance""" 427
428 - def __init__ (self, instance, element_declaration, value):
429 """@param instance: the value for the L{instance} attribute. 430 @param element_declaration: the value for the L{element_declaration} attribute. 431 @param value: the value for the L{value} attribute.""" 432 self.instance = instance 433 self.element_declaration = element_declaration 434 self.value = value 435 super(NonPluralAppendError, self).__init__(instance, element_declaration, value)
436
437 - def __unicode__ (self):
438 return 'Instance of %s cannot append to element %s' % (self.instance._Name(), self.element_declaration.name())
439 __str__ = PyXBException._str_from_unicode
440
441 -class MixedContentError (ContentValidationError):
442 """Non-element content added to a complex type instance that does not support mixed content.""" 443 444 instance = None 445 """The binding instance.""" 446 447 value = None 448 """The non-element content.""" 449
450 - def __init__ (self, instance, value, location=None):
451 """@param instance: the value for the L{instance} attribute. 452 @param value: the value for the L{value} attribute. 453 @param location: the value for the L{location} attribute.""" 454 self.instance = instance 455 self.value = value 456 self.location = location 457 super(MixedContentError, self).__init__(instance, value, location)
458
459 - def __unicode__ (self):
460 if self.location is not None: 461 return 'Invalid non-element content at %s' % (self.location,) 462 return 'Invalid non-element content'
463 __str__ = PyXBException._str_from_unicode
464
465 -class UnprocessedKeywordContentError (ContentValidationError):
466 """A complex type constructor was provided with keywords that could not be recognized.""" 467 468 instance = None 469 """The binding instance being constructed.""" 470 471 keywords = None 472 """The keywords that could not be recognized. These may have been 473 intended to be attributes or elements, but cannot be identified as 474 either.""" 475
476 - def __init__ (self, instance, keywords, location=None):
477 """@param instance: the value for the L{instance} attribute. 478 @param keywords: the value for the L{keywords} attribute. 479 @param location: the value for the L{location} attribute.""" 480 self.instance = instance 481 self.keywords = keywords 482 self.location = location 483 super(UnprocessedKeywordContentError, self).__init__(instance, keywords, location)
484
485 - def __unicode__ (self):
486 return 'Unprocessed keywords instantiating %s: %s' % (self.instance._Name(), ' '.join(self.keywords.keys()))
487 __str__ = PyXBException._str_from_unicode
488
489 -class IncrementalElementContentError (ContentValidationError):
490 """Element or element-like content could not be validly associated with an sub-element in the content model. 491 492 This exception occurs when content is added to an element during 493 incremental validation, such as when positional arguments are used 494 in a constructor or material is appended either explicitly or 495 through parsing a DOM instance.""" 496 497 instance = None 498 """The binding for which the L{value} could not be associated with an element.""" 499 500 automaton_configuration = None 501 """The L{pyxb.binding.content.AutomatonConfiguration} representing the current state of the L{instance} content.""" 502 503 value = None 504 """The value that could not be associated with allowable content.""" 505
506 - def __init__ (self, instance, automaton_configuration, value, location=None):
507 """@param instance: the value for the L{instance} attribute. 508 @param automaton_configuration: the value for the L{automaton_configuration} attribute. 509 @param value: the value for the L{value} attribute. 510 @param location: the value for the L{location} attribute.""" 511 self.instance = instance 512 self.automaton_configuration = automaton_configuration 513 self.value = value 514 self.location = location 515 super(IncrementalElementContentError, self).__init__(instance, automaton_configuration, value, location)
516
517 -class UnrecognizedContentError (IncrementalElementContentError):
518 """Element or element-like content could not be validly associated with an sub-element in the content model. 519 520 This exception occurs when content is added to an element during incremental validation.""" 521
522 - def __unicode__ (self):
523 value = self.value 524 try: 525 value = unicode(self.value._element().name()) 526 except: 527 pass 528 acceptable = self.automaton_configuration.acceptableContent() 529 if 0 == acceptable: 530 expect = 'no more content' 531 else: 532 import pyxb.binding.content 533 seen = set() 534 names = [] 535 for u in acceptable: 536 if isinstance(u, pyxb.binding.content.ElementUse): 537 n = unicode(u.elementBinding().name()) 538 else: 539 assert isinstance(u, pyxb.binding.content.WildcardUse) 540 n = 'xs:any' 541 if not (n in seen): 542 names.append(n) 543 seen.add(n) 544 expect = ' or '.join(names) 545 location = '' 546 if self.location is not None: 547 location = ' at %s' % (self.location,) 548 return 'Invalid content %s%s (expect %s)' % (value, location, expect)
549 __str__ = PyXBException._str_from_unicode 550
551 - def details (self):
552 import pyxb.binding.basis 553 import pyxb.binding.content 554 i = self.instance 555 rv = [ ] 556 if i._element() is not None: 557 rv.append('The containing element %s is defined at %s.' % (i._element().name(), i._element().xsdLocation())) 558 rv.append('The containing element type %s is defined at %s' % (self.instance._Name(), unicode(self.instance._XSDLocation))) 559 if self.location is not None: 560 rv.append('The unrecognized content %s begins at %s' % (self.value._diagnosticName(), self.location)) 561 else: 562 rv.append('The unrecognized content is %s' % (self.value._diagnosticName(),)) 563 ty = type(self.instance) 564 rv.append('The %s automaton %s in an accepting state.' % (self.instance._Name(), self.automaton_configuration.isAccepting() and "is" or "is not")) 565 if isinstance(self.instance, pyxb.binding.basis.complexTypeDefinition) and self.instance._IsMixed(): 566 rv.append('Character information content would be permitted.') 567 acceptable = self.automaton_configuration.acceptableContent() 568 if 0 == len(acceptable): 569 rv.append('No elements or wildcards would be accepted at this point.') 570 else: 571 rv.append('The following element and wildcard content would be accepted:') 572 rv2 = [] 573 for u in acceptable: 574 if isinstance(u, pyxb.binding.content.ElementUse): 575 rv2.append('An element %s per %s' % (u.elementBinding().name(), u.xsdLocation())) 576 else: 577 assert isinstance(u, pyxb.binding.content.WildcardUse) 578 rv2.append('A wildcard per %s' % (u.xsdLocation(),)) 579 rv.append('\t' + '\n\t'.join(rv2)) 580 return '\n'.join(rv)
581
582 -class BatchElementContentError (ContentValidationError):
583 """Element/wildcard content cannot be reconciled with the required content model. 584 585 This exception occurs in post-construction validation using a 586 fresh validating automaton.""" 587 588 instance = None 589 """The binding instance being constructed.""" 590 591 fac_configuration = None 592 """The L{pyxb.utils.fac.Configuration} representing the current state of the L{instance} automaton.""" 593 594 symbols = None 595 """The sequence of symbols that were accepted as content prior to the error.""" 596 597 symbol_set = None 598 """The leftovers from L{pyxb.binding.basis.complexTypeDefinition._symbolSet} that could not be reconciled with the content model.""" 599
600 - def __init__ (self, instance, fac_configuration, symbols, symbol_set):
601 """@param instance: the value for the L{instance} attribute. 602 @param fac_configuration: the value for the L{fac_configuration} attribute. 603 @param symbols: the value for the L{symbols} attribute. 604 @param symbol_set: the value for the L{symbol_set} attribute.""" 605 self.instance = instance 606 self.fac_configuration = fac_configuration 607 self.symbols = symbols 608 self.symbol_set = symbol_set 609 super(BatchElementContentError, self).__init__(instance, fac_configuration, symbols, symbol_set)
610
611 - def details (self):
612 import pyxb.binding.basis 613 import pyxb.binding.content 614 i = self.instance 615 rv = [ ] 616 if i._element() is not None: 617 rv.append('The containing element %s is defined at %s.' % (i._element().name(), i._element().xsdLocation())) 618 rv.append('The containing element type %s is defined at %s' % (self.instance._Name(), unicode(self.instance._XSDLocation))) 619 ty = type(self.instance) 620 rv.append('The %s automaton %s in an accepting state.' % (self.instance._Name(), self.fac_configuration.isAccepting() and "is" or "is not")) 621 if self.symbols is None: 622 rv.append('Any accepted content has been stored in instance') 623 elif 0 == len(self.symbols): 624 rv.append('No content has been accepted') 625 else: 626 rv.append('The last accepted content was %s' % (self.symbols[-1].value._diagnosticName(),)) 627 if isinstance(self.instance, pyxb.binding.basis.complexTypeDefinition) and self.instance._IsMixed(): 628 rv.append('Character information content would be permitted.') 629 acceptable = self.fac_configuration.acceptableSymbols() 630 if 0 == len(acceptable): 631 rv.append('No elements or wildcards would be accepted at this point.') 632 else: 633 rv.append('The following element and wildcard content would be accepted:') 634 rv2 = [] 635 for u in acceptable: 636 if isinstance(u, pyxb.binding.content.ElementUse): 637 rv2.append('An element %s per %s' % (u.elementBinding().name(), u.xsdLocation())) 638 else: 639 assert isinstance(u, pyxb.binding.content.WildcardUse) 640 rv2.append('A wildcard per %s' % (u.xsdLocation(),)) 641 rv.append('\t' + '\n\t'.join(rv2)) 642 if (self.symbol_set is None) or (0 == len(self.symbol_set)): 643 rv.append('No content remains unconsumed') 644 else: 645 rv.append('The following content was not processed by the automaton:') 646 rv2 = [] 647 for (ed, syms) in self.symbol_set.iteritems(): 648 if ed is None: 649 rv2.append('xs:any (%u instances)' % (len(syms),)) 650 else: 651 rv2.append('%s (%u instances)' % (ed.name(), len(syms))) 652 rv.append('\t' + '\n\t'.join(rv2)) 653 return '\n'.join(rv)
654
655 -class IncompleteElementContentError (BatchElementContentError):
656 """Validation of an instance failed to produce an accepting state. 657 658 This exception occurs in batch-mode validation.""" 659 pass
660
661 -class UnprocessedElementContentError (BatchElementContentError):
662 """Validation of an instance produced an accepting state but left element material unconsumed. 663 664 This exception occurs in batch-mode validation.""" 665 pass
666
667 -class InvalidPreferredElementContentError (BatchElementContentError):
668 """Use of a preferred element led to inability to generate a valid document""" 669 670 preferred_symbol = None 671 """The element symbol which was not accepted.""" 672
673 - def __init__ (self, instance, fac_configuration, symbols, symbol_set, preferred_symbol):
674 """@param instance: the value for the L{instance} attribute. 675 @param fac_configuration: the value for the L{fac_configuration} attribute. 676 @param symbols: the value for the L{symbols} attribute. 677 @param symbol_set: the value for the L{symbol_set} attribute. 678 @param preferred_symbol: the value for the L{preferred_symbol} attribute. 679 """ 680 self.instance = instance 681 self.fac_configuration = fac_configuration 682 self.symbols = symbols 683 self.symbol_set = symbol_set 684 self.preferred_symbol = preferred_symbol 685 # Bypass immediate parent so we preserve the last argument 686 super(BatchElementContentError, self).__init__(instance, fac_configuration, symbols, symbol_set, preferred_content)
687
688 689 -class OrphanElementContentError (ContentValidationError):
690 """An element expected to be used in content is not present in the instance. 691 692 This exception occurs in batch-mode validation when 693 L{pyxb.ValidationConfig.contentInfluencesGeneration} applies, 694 L{pyxb.ValidationConfig.orphanElementInContent} is set to 695 L{pyxb.ValidationConfig.RAISE_EXCEPTION}, and the content list 696 includes an element that is not in the binding instance 697 content. 698 """ 699 700 instance = None 701 """The binding instance.""" 702 703 preferred = None 704 """An element value from the L{instance} L{content<pyxb.binding.basis.complexTypeDefinition.content>} list which was not found in the L{instance}.""" 705
706 - def __init__ (self, instance, preferred):
707 """@param instance: the value for the L{instance} attribute. 708 @param preferred: the value for the L{preferred} attribute. 709 """ 710 self.instance = instance 711 self.preferred = preferred 712 super(OrphanElementContentError, self).__init__(instance, preferred)
713
714 - def __unicode__ (self):
715 return 'Preferred content element not found in instance'
716 __str__ = PyXBException._str_from_unicode
717
718 -class SimpleTypeValueError (ValidationError):
719 """Raised when a simple type value does not satisfy its constraints.""" 720 type = None 721 """The L{pyxb.binding.basis.simpleTypeDefinition} that constrains values.""" 722 723 value = None 724 """The value that violates the constraints of L{type}. In some 725 cases this is a tuple of arguments passed to a constructor that 726 failed with a built-in exception likeC{ValueError} or 727 C{OverflowError}.""" 728
729 - def __init__ (self, type, value, location=None):
730 """@param type: the value for the L{type} attribute. 731 @param value: the value for the L{value} attribute. 732 @param location: the value for the L{location} attribute. Default taken from C{value} if possible.""" 733 import pyxb.utils.utility 734 self.type = type 735 self.value = value 736 if (location is None) and isinstance(value, pyxb.utils.utility.Locatable_mixin): 737 location = value._location() 738 self.location = location 739 super(SimpleTypeValueError, self).__init__(type, value, location)
740
741 - def __unicode__ (self):
742 import pyxb.binding.basis 743 if isinstance(self.value, pyxb.binding.basis._TypeBinding_mixin): 744 return 'Type %s cannot be created from %s: %s' % (self.type._Name(), self.value._Name(), self.value) 745 return 'Type %s cannot be created from: %s' % (self.type._Name(), self.value)
746 __str__ = PyXBException._str_from_unicode
747
748 -class SimpleListValueError (SimpleTypeValueError):
749 """Raised when a list simple type contains a member that does not satisfy its constraints. 750 751 In this case, L{type} is the type of the list, and value 752 C{type._ItemType} is the type for which the L{value} is 753 unacceptable.""" 754
755 - def __unicode__ (self):
756 return 'Member type %s of list type %s cannot accept %s' % (self.type._ItemType._Name(), self.type._Name(), self.value)
757 __str__ = PyXBException._str_from_unicode
758
759 -class SimpleUnionValueError (SimpleTypeValueError):
760 """Raised when a union simple type contains a member that does not satisfy its constraints. 761 762 In this case, L{type} is the type of the union, and the value 763 C{type._MemberTypes} is the set of types for which the value is 764 unacceptable. 765 766 The L{value} itself is the tuple of arguments passed to the 767 constructor for the union.""" 768
769 - def __unicode__ (self):
770 return 'No memberType of %s can be constructed from %s' % (self.type._Name(), self.value)
771 __str__ = PyXBException._str_from_unicode
772
773 -class SimpleFacetValueError (SimpleTypeValueError):
774 """Raised when a simple type value does not satisfy a facet constraint. 775 776 This extends L{SimpleTypeValueError} with the L{facet} field which 777 can be used to determine why the value is unacceptable.""" 778 779 type = None 780 """The L{pyxb.binding.basis.simpleTypeDefinition} that constrains values.""" 781 782 value = None 783 """The value that violates the constraints of L{type}. In some 784 cases this is a tuple of arguments passed to a constructor that 785 failed with a built-in exception likeC{ValueError} or 786 C{OverflowError}.""" 787 788 facet = None 789 """The specific facet that is violated by the value.""" 790
791 - def __init__ (self, type, value, facet, location=None):
792 """@param type: the value for the L{type} attribute. 793 @param value: the value for the L{value} attribute. 794 @param facet: the value for the L{facet} attribute. 795 @param location: the value for the L{location} attribute. Default taken from C{value} if possible.""" 796 import pyxb.utils.utility 797 798 self.type = type 799 self.value = value 800 self.facet = facet 801 if (location is None) and isinstance(value, pyxb.utils.utility.Locatable_mixin): 802 location = value._location() 803 self.location = location 804 # Bypass immediate parent 805 super(SimpleTypeValueError, self).__init__(type, value, facet)
806
807 - def __unicode__ (self):
808 return 'Type %s %s constraint violated by value %s' % (self.type._Name(), self.facet._Name, self.value)
809 __str__ = PyXBException._str_from_unicode
810
811 -class SimplePluralValueError (SimpleTypeValueError):
812 """Raised when context requires a plural value. 813 814 Unlike L{SimpleListValueError}, in this case the plurality is 815 external to C{type}, for example when an element has simple 816 content and allows multiple occurrences.""" 817 pass
818
819 -class AttributeValidationError (ValidationError):
820 """Raised when an attribute requirement is not satisfied.""" 821 822 type = None 823 """The L{pyxb.binding.basis.complexTypeDefinition} subclass of the instance.""" 824 825 tag = None 826 """The name of the attribute.""" 827 828 instance = None 829 """The binding instance, if available.""" 830
831 - def __init__ (self, type, tag, instance=None, location=None):
832 """@param type: the value for the L{type} attribute. 833 @param tag: the value for the L{tag} attribute. 834 @param instance: the value for the L{instance} attribute. 835 @param location: the value for the L{location} attribute. Default taken from C{instance} if possible. 836 """ 837 import pyxb.utils.utility as utility 838 self.type = type 839 self.tag = tag 840 self.instance = instance 841 if (location is None) and isinstance(instance, utility.Locatable_mixin): 842 location = instance._location() 843 self.location = location 844 super(AttributeValidationError, self).__init__(type, tag, instance, location)
845
846 -class UnrecognizedAttributeError (AttributeValidationError):
847 """Attempt to reference an attribute not sanctioned by content model.""" 848 pass
849
850 -class ProhibitedAttributeError (AttributeValidationError):
851 """Raised when an attribute that is prohibited is set or referenced in an element."""
852 - def __unicode__ (self):
853 return 'Attempt to reference prohibited attribute %s in type %s' % (self.tag, self.type)
854 __str__ = PyXBException._str_from_unicode
855
856 -class MissingAttributeError (AttributeValidationError):
857 """Raised when an attribute that is required is missing in an element."""
858 - def __unicode__ (self):
859 return 'Instance of %s lacks required attribute %s' % (self.type, self.tag)
860 __str__ = PyXBException._str_from_unicode
861
862 -class AttributeChangeError (AttributeValidationError):
863 """Attempt to change an attribute that has a fixed value constraint."""
864 - def __unicode__ (self):
865 return 'Cannot change fixed attribute %s in type %s' % (self.tag, self.type)
866 __str__ = PyXBException._str_from_unicode
867
868 -class BindingError (PyXBException):
869 """Raised when the bindings are mis-used. 870 871 These are not validation errors, but rather structural errors. 872 For example, attempts to extract complex content from a type that 873 requires simple content, or vice versa. """
874
875 -class NotSimpleContentError (BindingError):
876 """An operation that requires simple content was invoked on a 877 complex type instance that does not have simple content.""" 878 879 instance = None 880 """The binding instance which should have had simple content.""" 881
882 - def __init__ (self, instance):
883 """@param instance: the binding instance that was mis-used. 884 This will be available in the L{instance} attribute.""" 885 self.instance = instance 886 super(BindingError, self).__init__(instance)
887 pass 888
889 - def __unicode__ (self):
890 return 'type %s does not have simple content' % (self.instance._Name(),)
891 __str__ = PyXBException._str_from_unicode
892
893 -class NotComplexContentError (BindingError):
894 """An operation that requires a content model was invoked on a 895 complex type instance that has empty or simple content.""" 896 897 instance = None 898 """The binding instance which should have had a content model.""" 899
900 - def __init__ (self, instance):
901 """@param instance: the binding instance that was mis-used. 902 This will be available in the L{instance} attribute.""" 903 self.instance = instance 904 super(BindingError, self).__init__(instance)
905
906 - def __unicode__ (self):
907 return 'type %s has simple/empty content' % (self.instance._Name(),)
908 __str__ = PyXBException._str_from_unicode
909
910 -class ReservedNameError (BindingError):
911 """Reserved name set in binding instance.""" 912 913 instance = None 914 """The binding instance.""" 915 916 name = None 917 """The name that was caught being assigned""" 918
919 - def __init__ (self, instance, name):
920 """@param instance: the value for the L{instance} attribute. 921 p@param name: the value for the L{name} attribute.""" 922 self.instance = instance 923 self.name = name 924 super(ReservedNameError, self).__init__(instance, name)
925
926 - def __unicode__ (self):
927 return '%s is a reserved name within %s' % (self.name, self.instance._Name())
928 __str__ = PyXBException._str_from_unicode
929
930 -class PyXBError (exceptions.Exception):
931 """Base class for exceptions that indicate a problem that the user probably can't fix.""" 932 pass
933
934 -class UsageError (PyXBError):
935 """Raised when the code detects user violation of an API."""
936
937 -class LogicError (PyXBError):
938 """Raised when the code detects an implementation problem."""
939
940 -class IncompleteImplementationError (LogicError):
941 """Raised when required capability has not been implemented. 942 943 This is only used where it is reasonable to expect the capability 944 to be present, such as a feature of XML schema that is not 945 supported (e.g., the redefine directive)."""
946