本文整理汇总了Python中xml.sax.InputSource.setByteStream方法的典型用法代码示例。如果您正苦于以下问题:Python InputSource.setByteStream方法的具体用法?Python InputSource.setByteStream怎么用?Python InputSource.setByteStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.sax.InputSource
的用法示例。
在下文中一共展示了InputSource.setByteStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tmx_import
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
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: parse
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
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]
示例3: parseFile
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
def parseFile(self, inputFile, stream=None):
input = InputSource(inputFile)
if stream is None:
stream = file(inputFile)
input.setByteStream(stream)
self.parseSource(input)
示例4: parse
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
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
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
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
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
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
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
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
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
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
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
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
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
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
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
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
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
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
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
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
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
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: SaxParser
# 需要导入模块: from xml.sax import InputSource [as 别名]
# 或者: from xml.sax.InputSource import setByteStream [as 别名]
class SaxParser(BaseParser):
""" Default SAX based parser. Creates SaxRecord """
_possibleSettings = {'namespaces' : {'docs' : "Enable namespace processing in SAX"},
'stripWhitespace' : {'docs' : "Strip additional whitespace when processing."},
'attrHash' : {'docs' : "Tag/Attribute combinations to include in hash."}
}
def __init__(self, session, config, parent):
Parser.__init__(self, session, config, parent)
self.parser = make_parser()
self.errorHandler = ErrorHandler()
self.parser.setErrorHandler(self.errorHandler)
self.inputSource = SaxInput()
ch = SaxContentHandler()
self.contentHandler = ch
self.parser.setContentHandler(ch)
self.keepError = 1
if (self.get_setting(session, 'namespaces')):
self.parser.setFeature('http://xml.org/sax/features/namespaces', 1)
p = self.get_setting(session, 'attrHash')
if (p):
l = p.split()
for i in l:
(a,b) = i.split("@")
try:
ch.hashAttributesNames[a].append(b)
except:
ch.hashAttributesNames[a] = [b]
if self.get_setting(session, 'stripWhitespace'):
ch.stripWS = 1
def process_document(self, session, doc):
xml = doc.get_raw(session)
self.inputSource.setByteStream(cStringIO.StringIO(xml))
ch = self.contentHandler
ch.reinit()
try:
self.parser.parse(self.inputSource)
except:
# Splat. Reset self and reraise
if self.keepError:
# Work out path
path = []
for l in ch.pathLines:
line = ch.currentText[l]
elemName = line[2:line.index('{')-1]
path.append("%s[@SAXID='%s']" % (elemName, l))
self.errorPath = '/'.join(path)
else:
ch.reinit()
raise
rec = SaxRecord(ch.currentText, xml, wordCount=ch.recordWordCount)
rec.elementHash = ch.elementHash
rec.byteCount = len(xml)
self._copyData(doc, rec)
ch.reinit()
return rec