MCPcopy Create free account
hub / github.com/ActiveState/code / prepare_input_source

Function prepare_input_source

recipes/Python/173071_ReseekFile/recipe-173071.py:128–161  ·  view source on GitHub ↗

given a URL, returns a xml.sax.xmlreader.InputSource Works like xml.sax.saxutils.prepare_input_source. Wraps the InputSource in a ReseekFile if the URL returns a non-seekable file. To turn the buffer off if that happens, you'll need to do something like f = source.getChar

(source)

Source from the content-addressed store, hash-verified

126 self._use_buffer = 0
127
128def prepare_input_source(source):
129 """given a URL, returns a xml.sax.xmlreader.InputSource
130
131 Works like xml.sax.saxutils.prepare_input_source. Wraps the
132 InputSource in a ReseekFile if the URL returns a non-seekable
133 file.
134
135 To turn the buffer off if that happens, you'll need to do
136 something like
137
138 f = source.getCharacterStream()
139 ...
140 try:
141 f.nobuffer()
142 except AttributeError:
143 pass
144
145 or
146
147 if isinstance(f, ReseekFile):
148 f.nobuffer()
149
150 """
151 from xml.sax import saxutils
152 source = saxutils.prepare_input_source(source)
153 # Is this correct? Don't know - don't have Unicode exprerience
154 f = source.getCharacterStream() or source.getByteStream()
155 try:
156 f.tell()
157 except (AttributeError, IOError):
158 f = ReseekFile.ReseekFile(f)
159 source.setByteStream(f)
160 source.setCharacterStream(None)
161 return source

Callers

nothing calls this directly

Calls 1

tellMethod · 0.95

Tested by

no test coverage detected