Skip to content

Commit ae43e52

Browse files
committed
Added the make_parser function (patch 101571).
1 parent c5cec51 commit ae43e52

1 file changed

Lines changed: 67 additions & 2 deletions

File tree

Lib/xml/sax/__init__.py

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"""
2323

2424
from handler import ContentHandler, ErrorHandler
25-
from expatreader import ExpatParser
2625
from _exceptions import SAXException, SAXNotRecognizedException, \
2726
SAXParseException, SAXNotSupportedException
2827

@@ -33,7 +32,6 @@ def parse(filename_or_stream, handler, errorHandler=ErrorHandler()):
3332
parser.setErrorHandler(errorHandler)
3433
parser.parse(filename_or_stream)
3534

36-
3735
def parseString(string, handler, errorHandler=ErrorHandler()):
3836
try:
3937
from cStringIO import StringIO
@@ -46,3 +44,70 @@ def parseString(string, handler, errorHandler=ErrorHandler()):
4644
parser.setContentHandler(handler)
4745
parser.setErrorHandler(errorHandler)
4846
parser.parse(StringIO(string))
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+
import os, string, sys
54+
if os.environ.has_key("PY_SAX_PARSER"):
55+
default_parser_list = string.split(os.environ["PY_SAX_PARSER"], ",")
56+
del os
57+
58+
_key = "python.xml.sax.parser"
59+
if sys.platform[:4] == "java" and sys.registry.containsKey(_key):
60+
default_parser_list = string.split(sys.registry.getProperty(_key), ",")
61+
62+
63+
def make_parser(parser_list = []):
64+
"""Creates and returns a SAX parser.
65+
66+
Creates the first parser it is able to instantiate of the ones
67+
given in the list created by doing parser_list +
68+
default_parser_list. The lists must contain the names of Python
69+
modules containing both a SAX parser and a create_parser function."""
70+
71+
for parser_name in parser_list + default_parser_list:
72+
try:
73+
return _create_parser(parser_name)
74+
except ImportError,e:
75+
pass
76+
77+
raise SAXException("No parsers found", None)
78+
79+
# --- Internal utility methods used by make_parser
80+
81+
if sys.platform[ : 4] == "java":
82+
def _create_parser(parser_name):
83+
from org.python.core import imp
84+
drv_module = imp.importName(parser_name, 0, globals())
85+
return drv_module.create_parser()
86+
87+
else:
88+
import imp as _imp
89+
90+
def _rec_find_module(module):
91+
"Improvement over imp.find_module which finds submodules."
92+
path=""
93+
for mod in string.split(module,"."):
94+
if path == "":
95+
info = (mod,) + _imp.find_module(mod)
96+
else:
97+
info = (mod,) + _imp.find_module(mod, [path])
98+
99+
lastmod = apply(imp.load_module, info)
100+
101+
try:
102+
path = lastmod.__path__[0]
103+
except AttributeError, e:
104+
pass
105+
106+
return info
107+
108+
def _create_parser(parser_name):
109+
info = _rec_find_module(parser_name)
110+
drv_module = apply(imp.load_module, info)
111+
return drv_module.create_parser()
112+
113+
del sys

0 commit comments

Comments
 (0)