annotate Lib/xml/sax/__init__.py @ 54294:dba767bc3155 legacy-trunk

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