本文整理汇总了Python中xml.sax.InputSource类的典型用法代码示例。如果您正苦于以下问题:Python InputSource类的具体用法?Python InputSource怎么用?Python InputSource使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了InputSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tmx_import
def tmx_import(self, file, REQUEST=None, RESPONSE=None):
""" Imports a TMX level 1 file.
We use the SAX parser. It has the benefit that it internally
converts everything to python unicode strings.
"""
self._v_srclang = self._default_language
# Create a parser
parser = make_parser()
chandler = HandleTMXParsing(self._tmx_tu, self._tmx_header)
# Tell the parser to use our handler
parser.setContentHandler(chandler)
# Don't load the DTD from the Internet
parser.setFeature(handler.feature_external_ges, 0)
inputsrc = InputSource()
if type(file) is StringType:
inputsrc.setByteStream(StringIO(file))
parser.parse(inputsrc)
else:
content = file.read()
inputsrc.setByteStream(StringIO(content))
parser.parse(inputsrc)
if hasattr(self, '_v_srclang'):
del self._v_srclang
if REQUEST is not None:
RESPONSE.redirect('manage_localPropertiesForm')
示例2: parseFile
def parseFile(self, inputFile, stream=None):
input = InputSource(inputFile)
if stream is None:
stream = file(inputFile)
input.setByteStream(stream)
self.parseSource(input)
示例3: parse
def parse(self, file=None, string=None):
"""
SAX parse XML text.
@param file: Parse a python I{file-like} object.
@type file: I{file-like} object.
@param string: Parse string XML.
@type string: str
"""
timer = metrics.Timer()
timer.start()
sax, handler = self.saxparser()
if file is not None:
sax.parse(file)
timer.stop()
metrics.log.debug('sax (%s) duration: %s', file, timer)
return handler.nodes[0]
if string is not None:
if isinstance(string, six.text_type):
string = string.encode("utf-8")
source = InputSource(None)
source.setByteStream(BytesIO(string))
sax.parse(source)
timer.stop()
metrics.log.debug('%s\nsax duration: %s', string, timer)
return handler.nodes[0]
示例4: parse
def parse(self, file=None, string=None):
"""
SAX parse XML text.
@param file: Parse a python I{file-like} object.
@type file: I{file-like} object.
@param string: Parse string XML.
@type string: str
"""
timer = metrics.Timer()
timer.start()
sax, handler = self.saxparser()
if file is not None:
sax.parse(file)
timer.stop()
metrics.log.debug('sax (%s) duration: %s', file, timer)
return handler.nodes[0]
if string is not None:
source = InputSource(None)
try:
source.setByteStream(StringIO(string.encode('utf8')))
except UnicodeDecodeError:
source.setByteStream(StringIO(string))
sax.parse(source)
timer.stop()
metrics.log.debug('%s\nsax duration: %s', string, timer)
return handler.nodes[0]
示例5: parse
def parse(self, file=None, string=None):
"""
SAX parse XML text.
@param file: Parse a python I{file-like} object.
@type file: I{file-like} object
@param string: Parse string XML.
@type string: str
@return: Parsed XML document.
@rtype: L{Document}
"""
if file is None and string is None:
return
timer = suds.metrics.Timer()
timer.start()
source = file
if file is None:
source = InputSource(None)
source.setByteStream(suds.BytesIO(string))
sax, handler = self.saxparser()
sax.parse(source)
timer.stop()
if file is None:
suds.metrics.log.debug("%s\nsax duration: %s", string, timer)
else:
suds.metrics.log.debug("sax (%s) duration: %s", file, timer)
return handler.nodes[0]
示例6: test_ignorable
def test_ignorable():
p = XMLValParserFactory.make_parser()
i = InputSource("doc3.xml")
i.setByteStream(StringIO(doc3))
h = H()
p.setContentHandler(h)
p.parse(i)
return h.passed
示例7: resolveEntity
def resolveEntity(self, publicId, systemId):
if systemId:
name = os.path.join(self._path, systemId)
if os.path.isfile(name):
source = InputSource()
source.setByteStream(open(name, "rb"))
return source
# Using default resolution
return EntityResolver.resolveEntity(self, publicId, systemId)
示例8: test_illformed
def test_illformed():
p = XMLValParserFactory.make_parser()
i = InputSource("doc2.xml")
i.setByteStream(StringIO(doc2))
try:
p.parse(i)
except SAXException,e:
print "PASS:",e
return 1
示例9: parseXLIFFSTring
def parseXLIFFSTring(self, xml_string):
""" """
chandler = XLIFFHandler()
parser = make_parser()
# Tell the parser to use our handler
parser.setContentHandler(chandler)
# Don't load the DTD from the Internet
parser.setFeature(handler.feature_external_ges, 0)
inpsrc = InputSource()
inpsrc.setByteStream(StringIO(xml_string))
try:
parser.parse(inpsrc)
return chandler
except:
return None
示例10: parseContent
def parseContent(self, file):
# Create a parser
try:
parser = make_parser()
chandler = GBoxHandler()
# Tell the parser to use our handler
parser.setContentHandler(chandler)
# Don't load the DTD from the Internet
parser.setFeature(handler.feature_external_ges, 0)
inputsrc = InputSource()
gbox_content = utils.utRead(file)
inputsrc.setByteStream(StringIO(gbox_content))
parser.parse(inputsrc)
except:
return 'err'
return chandler
示例11: tmx_import
def tmx_import(self, howmuch, file, REQUEST=None, RESPONSE=None):
""" Imports a TMX level 1 file.
We use the SAX parser. It has the benefit that it internally
converts everything to python unicode strings.
"""
if howmuch == 'clear':
# Clear the message catalogue prior to import
self._messages = {}
self._languages = ()
self._v_howmuch = howmuch
self._v_srclang = self._default_language
self._v_num_translations = 0
self._v_num_notes = 0
# Create a parser
parser = make_parser()
chandler = HandleTMXParsing(self._tmx_tu, self._tmx_header)
# Tell the parser to use our handler
parser.setContentHandler(chandler)
# Don't load the DTD from the Internet
parser.setFeature(handler.feature_external_ges, 0)
inputsrc = InputSource()
if type(file) is StringType:
inputsrc.setByteStream(StringIO(file))
else:
content = file.read()
inputsrc.setByteStream(StringIO(content))
parser.parse(inputsrc)
num_translations = self._v_num_translations
num_notes = self._v_num_notes
del self._v_srclang
del self._v_howmuch
del self._v_num_translations
del self._v_num_notes
if REQUEST is not None:
return MessageDialog(
title = _('Messages imported'),
message = _('Imported %d messages and %d notes')
% (num_translations, num_notes),
action = 'manage_messages')
示例12: parseXLIFFFile
def parseXLIFFFile(self, file):
# Create a parser
parser = make_parser()
chandler = XLIFFHandler()
# Tell the parser to use our handler
parser.setContentHandler(chandler)
# Don't load the DTD from the Internet
parser.setFeature(handler.feature_external_ges, 0)
inputsrc = InputSource()
try:
if type(file) is StringType:
inputsrc.setByteStream(StringIO(file))
else:
filecontent = file.read()
inputsrc.setByteStream(StringIO(filecontent))
parser.parse(inputsrc)
return chandler
except:
return None
示例13: parse
def parse(self, file=None, url=None, string=None):
timer = metrics.Timer()
timer.start()
sax, handler = self.saxparser()
if file is not None:
sax.parse(file)
timer.stop()
metrics.log.debug('sax (%s) duration: %s', file, timer)
return handler.nodes[0]
if url is not None:
fp = self.transport.open(Request(url))
sax.parse(fp)
timer.stop()
metrics.log.debug('sax (%s) duration: %s', url, timer)
return handler.nodes[0]
if string is not None:
source = InputSource(None)
source.setByteStream(StringIO(string))
sax.parse(source)
timer.stop()
metrics.log.debug('%s\nsax duration: %s', string, timer)
return handler.nodes[0]
示例14: addSectionTags
def addSectionTags(content):
from cStringIO import StringIO
src = InputSource()
src.setByteStream(StringIO(content))
# Create an XML parser
parser = make_parser() #("xml.sax.drivers2.drv_xmlproc")
dh = docHandler()
parser.setContentHandler(dh)
er = EntityResolver()
parser.setEntityResolver(er)
# Allow external entities
parser.setFeature(feature_external_ges, True)
# Parse the file; your handler's methods will get called
parser.parse(src)
return dh.document.encode('UTF-8')
示例15: parse
def parse(self, xml):
input = InputSource()
input.setByteStream(StringIO(xml))
self.parseSource(input)