__init__.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """Simple API for XML (SAX) implementation for Python.
  2. This module provides an implementation of the SAX 2 interface;
  3. information about the Java version of the interface can be found at
  4. http://www.megginson.com/SAX/. The Python version of the interface is
  5. documented at <...>.
  6. This package contains the following modules:
  7. handler -- Base classes and constants which define the SAX 2 API for
  8. the 'client-side' of SAX for Python.
  9. saxutils -- Implementation of the convenience classes commonly used to
  10. work with SAX.
  11. xmlreader -- Base classes and constants which define the SAX 2 API for
  12. the parsers used with SAX for Python.
  13. expatreader -- Driver that allows use of the Expat parser with SAX.
  14. """
  15. from .xmlreader import InputSource
  16. from .handler import ContentHandler, ErrorHandler
  17. from ._exceptions import SAXException, SAXNotRecognizedException, \
  18. SAXParseException, SAXNotSupportedException, \
  19. SAXReaderNotAvailable
  20. def parse(source, handler, errorHandler=ErrorHandler()):
  21. parser = make_parser()
  22. parser.setContentHandler(handler)
  23. parser.setErrorHandler(errorHandler)
  24. parser.parse(source)
  25. def parseString(string, handler, errorHandler=ErrorHandler()):
  26. import io
  27. if errorHandler is None:
  28. errorHandler = ErrorHandler()
  29. parser = make_parser()
  30. parser.setContentHandler(handler)
  31. parser.setErrorHandler(errorHandler)
  32. inpsrc = InputSource()
  33. if isinstance(string, str):
  34. inpsrc.setCharacterStream(io.StringIO(string))
  35. else:
  36. inpsrc.setByteStream(io.BytesIO(string))
  37. parser.parse(inpsrc)
  38. # this is the parser list used by the make_parser function if no
  39. # alternatives are given as parameters to the function
  40. default_parser_list = ["xml.sax.expatreader"]
  41. # tell modulefinder that importing sax potentially imports expatreader
  42. _false = 0
  43. if _false:
  44. import xml.sax.expatreader
  45. import os, sys
  46. if not sys.flags.ignore_environment and "PY_SAX_PARSER" in os.environ:
  47. default_parser_list = os.environ["PY_SAX_PARSER"].split(",")
  48. del os, sys
  49. def make_parser(parser_list=()):
  50. """Creates and returns a SAX parser.
  51. Creates the first parser it is able to instantiate of the ones
  52. given in the iterable created by chaining parser_list and
  53. default_parser_list. The iterables must contain the names of Python
  54. modules containing both a SAX parser and a create_parser function."""
  55. for parser_name in list(parser_list) + default_parser_list:
  56. try:
  57. return _create_parser(parser_name)
  58. except ImportError:
  59. import sys
  60. if parser_name in sys.modules:
  61. # The parser module was found, but importing it
  62. # failed unexpectedly, pass this exception through
  63. raise
  64. except SAXReaderNotAvailable:
  65. # The parser module detected that it won't work properly,
  66. # so try the next one
  67. pass
  68. raise SAXReaderNotAvailable("No parsers found", None)
  69. # --- Internal utility methods used by make_parser
  70. def _create_parser(parser_name):
  71. drv_module = __import__(parser_name,{},{},['create_parser'])
  72. return drv_module.create_parser()