simple api for xml sax implementation for python this module provides an implementation of the sax 2 interface information about the java version of the interface can be found at http www megginson comsax the python version of the interface is documented at this package contains the following modules handler base classes and constants which define the sax 2 api for the clientside of sax for python saxutils implementation of the convenience classes commonly used to work with sax xmlreader base classes and constants which define the sax 2 api for the parsers used with sax for python expatreader driver that allows use of the expat parser with sax this is the parser list used by the makeparser function if no alternatives are given as parameters to the function tell modulefinder that importing sax potentially imports expatreader creates and returns a sax parser creates the first parser it is able to instantiate of the ones given in the iterable chaining parserlist and defaultparserlist the iterables must contain the names of python modules containing both a sax parser and a createparser function for parsername in listparserlist defaultparserlist try return createparserparsername except importerror import sys if parsername in sys modules the parser module was found but importing it failed unexpectedly pass this exception through raise except saxreadernotavailable the parser module detected that it won t work properly so try the next one pass raise saxreadernotavailableno parsers found none internal utility methods used by makeparser def createparserparsername drvmodule importparsername createparser return drvmodule createparser this is the parser list used by the make_parser function if no alternatives are given as parameters to the function tell modulefinder that importing sax potentially imports expatreader creates and returns a sax parser creates the first parser it is able to instantiate of the ones given in the iterable chaining parser_list and default_parser_list the iterables must contain the names of python modules containing both a sax parser and a create_parser function the parser module was found but importing it failed unexpectedly pass this exception through the parser module detected that it won t work properly so try the next one internal utility methods used by make_parser
from .xmlreader import InputSource
from .handler import ContentHandler, ErrorHandler
from ._exceptions import SAXException, SAXNotRecognizedException, \
                        SAXParseException, SAXNotSupportedException, \
                        SAXReaderNotAvailable


def parse(source, handler, errorHandler=ErrorHandler()):
    parser = make_parser()
    parser.setContentHandler(handler)
    parser.setErrorHandler(errorHandler)
    parser.parse(source)

def parseString(string, handler, errorHandler=ErrorHandler()):
    import io
    if errorHandler is None:
        errorHandler = ErrorHandler()
    parser = make_parser()
    parser.setContentHandler(handler)
    parser.setErrorHandler(errorHandler)

    inpsrc = InputSource()
    if isinstance(string, str):
        inpsrc.setCharacterStream(io.StringIO(string))
    else:
        inpsrc.setByteStream(io.BytesIO(string))
    parser.parse(inpsrc)




default_parser_list = ["xml.sax.expatreader"]


_false = 0
if _false:
    import xml.sax.expatreader

import os, sys
if not sys.flags.ignore_environment and "PY_SAX_PARSER" in os.environ:
    default_parser_list = os.environ["PY_SAX_PARSER"].split(",")
del os, sys


def make_parser(parser_list=()):
    

    for parser_name in list(parser_list) + default_parser_list:
        try:
            return _create_parser(parser_name)
        except ImportError:
            import sys
            if parser_name in sys.modules:
                
                
                raise
        except SAXReaderNotAvailable:
            
            
            pass

    raise SAXReaderNotAvailable("No parsers found", None)



def _create_parser(parser_name):
    drv_module = __import__(parser_name,{},{},['create_parser'])
    return drv_module.create_parser()