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