Advertisement
Guest User

Untitled

a guest
Apr 28th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.52 KB | None | 0 0
  1. """Simple API for XML (SAX) implementation for Python.
  2.  
  3. This module provides an implementation of the SAX 2 interface;
  4. information about the Java version of the interface can be found at
  5. http://www.megginson.com/SAX/.  The Python version of the interface is
  6. documented at <...>.
  7.  
  8. This package contains the following modules:
  9.  
  10. handler -- Base classes and constants which define the SAX 2 API for
  11.           the 'client-side' of SAX for Python.
  12.  
  13. saxutils -- Implementation of the convenience classes commonly used to
  14.            work with SAX.
  15.  
  16. xmlreader -- Base classes and constants which define the SAX 2 API for
  17.             the parsers used with SAX for Python.
  18.  
  19. expatreader -- Driver that allows use of the Expat parser with SAX.
  20. """
  21.  
  22. from .xmlreader import InputSource
  23. from .handler import ContentHandler, ErrorHandler
  24. from ._exceptions import SAXException, SAXNotRecognizedException, \
  25.                         SAXParseException, SAXNotSupportedException, \
  26.                         SAXReaderNotAvailable
  27.  
  28.  
  29. def parse(source, handler, errorHandler=ErrorHandler()):
  30.     parser = make_parser()
  31.     parser.setContentHandler(handler)
  32.     parser.setErrorHandler(errorHandler)
  33.     parser.parse(source)
  34.  
  35. def parseString(string, handler, errorHandler=ErrorHandler()):
  36.     from io import BytesIO
  37.  
  38.     if errorHandler is None:
  39.         errorHandler = ErrorHandler()
  40.     parser = make_parser()
  41.     parser.setContentHandler(handler)
  42.     parser.setErrorHandler(errorHandler)
  43.  
  44.     inpsrc = InputSource()
  45.     inpsrc.setByteStream(BytesIO(string))
  46.     parser.parse(inpsrc)
  47.  
  48. # this is the parser list used by the make_parser function if no
  49. # alternatives are given as parameters to the function
  50.  
  51. default_parser_list = ["xml.sax.expatreader"]
  52.  
  53. # tell modulefinder that importing sax potentially imports expatreader
  54. _false = 0
  55. if _false:
  56.     import xml.sax.expatreader
  57.  
  58. import os, sys
  59. if "PY_SAX_PARSER" in os.environ:
  60.     default_parser_list = os.environ["PY_SAX_PARSER"].split(",")
  61. del os
  62.  
  63. _key = "python.xml.sax.parser"
  64. if sys.platform[:4] == "java" and sys.registry.containsKey(_key):
  65.     default_parser_list = sys.registry.getProperty(_key).split(",")
  66.  
  67.  
  68. def make_parser(parser_list = []):
  69.     """Creates and returns a SAX parser.
  70.  
  71.    Creates the first parser it is able to instantiate of the ones
  72.    given in the list created by doing parser_list +
  73.    default_parser_list.  The lists must contain the names of Python
  74.    modules containing both a SAX parser and a create_parser function."""
  75.  
  76.     for parser_name in parser_list + default_parser_list:
  77.         try:
  78.             return _create_parser(parser_name)
  79.         except ImportError as e:
  80.             import sys
  81.             if parser_name in sys.modules:
  82.                 # The parser module was found, but importing it
  83.                 # failed unexpectedly, pass this exception through
  84.                 raise
  85.         except SAXReaderNotAvailable:
  86.             # The parser module detected that it won't work properly,
  87.             # so try the next one
  88.             pass
  89.  
  90.     raise SAXReaderNotAvailable("No parsers found", None)
  91.  
  92. # --- Internal utility methods used by make_parser
  93.  
  94. if sys.platform[ : 4] == "java":
  95.     def _create_parser(parser_name):
  96.         from org.python.core import imp
  97.         drv_module = imp.importName(parser_name, 0, globals())
  98.         return drv_module.create_parser()
  99.  
  100. else:
  101.     def _create_parser(parser_name):
  102.         drv_module = __import__(parser_name,{},{},['create_parser'])
  103.         return drv_module.create_parser()
  104.  
  105. del sys
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement