Paul Prescod <paul@prescod.net>:

Add support for parsing already-opened files.  Make sure the parse()
method closes exactly those files that it opens.

Modified by FLD for better conformance to the Python style guide.

This closes SourceForge patch #101512.
This commit is contained in:
Fred Drake 2000-09-21 17:43:48 +00:00
parent 5644b7fad1
commit 07cbc4e5bd
1 changed files with 27 additions and 17 deletions

View File

@ -1,15 +1,16 @@
import handler
"""An XML Reader is the SAX 2 name for an XML parser. XML Parsers """An XML Reader is the SAX 2 name for an XML parser. XML Parsers
should be based on this code. """ should be based on this code. """
import handler
# ===== XMLREADER ===== # ===== XMLREADER =====
class XMLReader: class XMLReader:
def __init__(self): def __init__(self):
self._cont_handler = handler.ContentHandler() self._cont_handler = handler.ContentHandler()
#self._dtd_handler = handler.DTDHandler() #self._dtd_handler = handler.DTDHandler()
#self._ent_handler = handler.EntityResolver() #self._ent_handler = handler.EntityResolver()
self._err_handler = handler.ErrorHandler() self._err_handler = handler.ErrorHandler()
def parse(self, source): def parse(self, source):
"Parse an XML document from a system identifier or an InputSource." "Parse an XML document from a system identifier or an InputSource."
@ -92,22 +93,29 @@ class IncrementalParser(XMLReader):
interface using the feed, close and reset methods of the interface using the feed, close and reset methods of the
IncrementalParser interface as a convenience to SAX 2.0 driver IncrementalParser interface as a convenience to SAX 2.0 driver
writers.""" writers."""
def __init__(self, bufsize=2**16 ):
self._bufsize=bufsize def __init__(self, bufsize=2**16):
XMLReader.__init__( self ) self._bufsize = bufsize
XMLReader.__init__(self)
def parse(self, source):
self.prepareParser(source) def _parseOpenFile(self, source):
#FIXME: do some type checking: could be already stream, URL or buffer = source.read(self._bufsize)
# filename
inf=open( source )
buffer = inf.read(self._bufsize)
while buffer != "": while buffer != "":
self.feed(buffer) self.feed(buffer)
buffer = inf.read(self._bufsize) buffer = source.read(self._bufsize)
self.close() self.close()
self.reset() self.reset()
def parse(self, source):
if hasattr(source, "read"):
self._parseOpenFile(source)
else:
#FIXME: how to recognize if it is a URL instead of filename?
self.prepareParser(source)
file = open(source)
self._parseOpenFile(file)
file.close()
def feed(self, data): def feed(self, data):
"""This method gives the raw XML data in the data parameter to """This method gives the raw XML data in the data parameter to
the parser and makes it parse the data, emitting the the parser and makes it parse the data, emitting the
@ -116,6 +124,7 @@ def feed(self, data):
feed may raise SAXException.""" feed may raise SAXException."""
raise NotImplementedError("This method must be implemented!") raise NotImplementedError("This method must be implemented!")
def prepareParser(self, source): def prepareParser(self, source):
"""This method is called by the parse implementation to allow """This method is called by the parse implementation to allow
the SAX 2.0 driver to prepare itself for parsing.""" the SAX 2.0 driver to prepare itself for parsing."""
@ -215,11 +224,12 @@ def items(self):
def values(self): def values(self):
return self._attrs.values() return self._attrs.values()
def _test(): def _test():
XMLReader() XMLReader()
IncrementalParser() IncrementalParser()
Locator() Locator()
AttributesImpl() AttributesImpl()
if __name__=="__main__": if __name__ == "__main__":
_test() _test()