__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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
  49. _key = "python.xml.sax.parser"
  50. if sys.platform[:4] == "java" and sys.registry.containsKey(_key):
  51. default_parser_list = sys.registry.getProperty(_key).split(",")
  52. def make_parser(parser_list=()):
  53. """Creates and returns a SAX parser.
  54. Creates the first parser it is able to instantiate of the ones
  55. given in the iterable created by chaining parser_list and
  56. default_parser_list. The iterables must contain the names of Python
  57. modules containing both a SAX parser and a create_parser function."""
  58. for parser_name in list(parser_list) + default_parser_list:
  59. try:
  60. return _create_parser(parser_name)
  61. except ImportError:
  62. import sys
  63. if parser_name in sys.modules:
  64. # The parser module was found, but importing it
  65. # failed unexpectedly, pass this exception through
  66. raise
  67. except SAXReaderNotAvailable:
  68. # The parser module detected that it won't work properly,
  69. # so try the next one
  70. pass
  71. raise SAXReaderNotAvailable("No parsers found", None)
  72. # --- Internal utility methods used by make_parser
  73. if sys.platform[ : 4] == "java":
  74. def _create_parser(parser_name):
  75. from org.python.core import imp
  76. drv_module = imp.importName(parser_name, 0, globals())
  77. return drv_module.create_parser()
  78. else:
  79. def _create_parser(parser_name):
  80. drv_module = __import__(parser_name,{},{},['create_parser'])
  81. return drv_module.create_parser()
  82. del sys