ยปCore Development>Code coverage>Lib/xml/sax/__init__.py

Python code coverage for Lib/xml/sax/__init__.py

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