Skip to content

Latest commit

 

History

History
107 lines (82 loc) · 3.56 KB

__init__.py

File metadata and controls

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