def generator(self): """ Creates the generator that yields :class:`~xmpp2.model.XMLObject`\ s from the stream. """ # Create the handler. self.__handler = Handler() # Create the parser context (this is a C library). context = libxml2.createPushParser(self.__handler, '', 0, None) while True: # Read from the socket. try: chunk = self.read(block=self.block) except KeyboardInterrupt: # A SIGINT stops the show. raise StopIteration except: # Non-blocking read/recv threw an error yield None continue # Parse the chunk that was read. This will trigger the handler, # which will put finished top-level nodes like presence and # message into its queue. context.parseChunk(chunk, len(chunk), 0) # Empty out the queue, so that we can act on it. for node in self.__handler.empty_queue(): if self.__log_level == LOG_STREAM: self.__do_log('stream read:\n%s', node) yield node
def __init__(self, handler): """Initialize the reader. :Parameters: - `handler`: Object to handle stream start, end and stanzas. :Types: - `handler`: `StreamHandler` """ self.handler = handler self.sax = _SAXCallback(handler) self.parser = libxml2.createPushParser(self.sax, '', 0, 'stream')
def __init__(self,handler): """Initialize the reader. :Parameters: - `handler`: Object to handle stream start, end and stanzas. :Types: - `handler`: `StreamHandler` """ self.handler = handler self.sax = _SAXCallback(handler) self.parser = libxml2.createPushParser(self.sax, '', 0, 'stream')
if tag == "bar1": expect = self.ende + self.delta * self.count if ctxt.byteConsumed() != expect: print "element end at wrong index: %d expecting %d\n" % ( ctxt.byteConsumed(), expect) sys.exit(1) self.count = self.count + 1 def characters(self, data): pass # # First run a pure UTF-8 test # handler = callback(0, 13, 27, 198, 183) ctxt = libxml2.createPushParser(handler, "<foo>\n", 6, "test.xml") chunk = """ <bar1>chars1</bar1> <bar2>chars2</bar2> <bar3>chars3</bar3> <bar4>chars4</bar4> <bar5>chars5</bar5> <bar6><s6</bar6> <bar7>chars7</bar7> <bar8>&8</bar8> <bar9>chars9</bar9> """ i = 0 while i < 10000: ctxt.parseChunk(chunk, len(chunk), 0) i = i + 1 chunk = "</foo>"
global log log = log + "warning: %s:" % (msg) def error(self, msg): global log log = log + "error: %s:" % (msg) def fatalError(self, msg): global log log = log + "fatalError: %s:" % (msg) handler = callback() log="" chunk="""<foo><bar2/>""" ctxt = libxml2.createPushParser(handler, None, 0, "test.xml") ctxt.parseChunk(chunk, len(chunk), 0) ctxt=None reference = "startDocument:startElement foo None:startElement bar2 None:endElement bar2:" if log != reference: print("Error got: %s" % log) print("Expected: %s" % reference) sys.exit(1) log="" chunk="""<foo><bar2></bar2>""" ctxt = libxml2.createPushParser(handler, None, 0, "test.xml") ctxt.parseChunk(chunk, len(chunk), 0) ctxt=None
def endElement(self, element): print "End: %s" % element def characters(self, data): global chars chars.append(data) def error(self, msg): print "GGGGGGGGGGGGGGG", msg handler = Handler() f = open("/tmp/document.xml") libxml2.initParser() ctxt = libxml2.createPushParser(handler, '', 0, None) while 1: print "Chunk" chunk = f.read(10) if not len(chunk): break ctxt.parseChunk(chunk, len(chunk), 0) print "OK" ctxt.parseChunk('', 0, 1) data = string.join(chars, '') print len(data) print data
#!/usr/bin/python -u import sys import libxml2 # Memory debug specific libxml2.debugMemory(1) ctxt = libxml2.createPushParser(None, "<foo", 4, "test.xml") ctxt.parseChunk("/>", 2, 1) doc = ctxt.doc() ctxt=None if doc.name != "test.xml": print "document name error" sys.exit(1) root = doc.children if root.name != "foo": print "root element name error" sys.exit(1) doc.freeDoc() i = 10000 while i > 0: ctxt = libxml2.createPushParser(None, "<foo", 4, "test.xml") ctxt.parseChunk("/>", 2, 1) doc = ctxt.doc() doc.freeDoc() i = i -1 ctxt=None # Memory debug specific libxml2.cleanupParser() if libxml2.debugMemory(1) == 0:
expect = self.ende + self.delta * self.count if ctxt.byteConsumed() != expect: print("element end at wrong index: %d expecting %d\n" % (ctxt.byteConsumed(), expect)) sys.exit(1) self.count = self.count + 1 def characters(self, data): pass # # First run a pure UTF-8 test # handler = callback(0, 13, 27, 198, 183) ctxt = libxml2.createPushParser(handler, "<foo>\n", 6, "test.xml") chunk = """ <bar1>chars1</bar1> <bar2>chars2</bar2> <bar3>chars3</bar3> <bar4>chars4</bar4> <bar5>chars5</bar5> <bar6><s6</bar6> <bar7>chars7</bar7> <bar8>&8</bar8> <bar9>chars9</bar9> """ i = 0 while i < 10000: ctxt.parseChunk(chunk, len(chunk), 0) i = i + 1 chunk = "</foo>"
def parse(self, xml): createPushParser(self, xml, len(xml), 'filter').parseChunk('', 0, 1) if self.errorOccurred(): raise self.saxError()
# Memory debug specific libxml2.debugMemory(1) expect="""--> (3) xmlns: URI foo is not absolute --> (4) Opening and ending tag mismatch: x line 0 and y """ err="" def callback(arg,msg,severity,reserved): global err err = err + "%s (%d) %s" % (arg,severity,msg) s = """<x xmlns="foo"></y>""" parserCtxt = libxml2.createPushParser(None,"",0,"test.xml") parserCtxt.setErrorHandler(callback, "-->") if parserCtxt.getErrorHandler() != (callback,"-->"): print "getErrorHandler failed" sys.exit(1) parserCtxt.parseChunk(s,len(s),1) doc = parserCtxt.doc() doc.freeDoc() parserCtxt = None if err != expect: print "error" print "received %s" %(err) print "expected %s" %(expect) sys.exit(1)
def parse(self): handler = callback() ctxt = libxml2.createPushParser(handler, "", 0, "temp.xml") myfile = file(self.filename) for line in myfile.readlines(): ctxt.parseChunk(line, len(line), 0) ctxt.parseChunk("", 0, 1) # Let's find out what dirs were touched filelist = handler.filelist finished = 0 path = "" multidir = 0 while finished == 0: ok = 1 i = 0 subdirlinecount = 0 subdirslashpos = 0 maxi = len(filelist) if (i + 1) == maxi: ok = 0 while i < maxi and ok == 1: search = filelist[i].find("/") if search == -1: #okay, we have a fil in the "top" dir let's stop it ok = 0 break if search > -1: # we have a subdir in here, so count ;) subdirlinecount += 1 i += 1 if ok == 1 and multidir == 0: if subdirlinecount == maxi: #okay all dirs still have a slash # everything in the same dir? subdirlinecount = 0 lastdir = "" # count dirs filelist.sort() for dir in filelist: if dir.find("/") > -1: if dir[:dir.find("/") + 1] != lastdir: subdirlinecount += 1 lastdir = dir[:dir.find("/") + 1] if subdirlinecount == 1: # okay, all lines have the same slashpos. # strip everything up to it i = 0 path += filelist[0][:search + 1] while i < maxi: filelist[i] = filelist[i][search + 1:] i += 1 filelist.sort() for line in filelist: if line == "": filelist.remove("") else: #no we seem to have the topdir now.. dircount = subdirlinecount multidir = 1 else: finished = 1 filecount = 0 dircount = 0 lastdir = "" # count dirs filelist.sort() for dir in filelist: if dir.find("/") > -1: if dir[:dir.find("/") + 1] != lastdir: dircount += 1 lastdir = dir[:dir.find("/") + 1] filecount = maxi self.author = handler.author self.revision = handler.revision self.logmessage = handler.logmessage self.dircount = dircount self.filecount = filecount self.filelist = filelist self.path = path
log = log + "warning: %s:" % (msg) def error(self, msg): global log log = log + "error: %s:" % (msg) def fatalError(self, msg): global log log = log + "fatalError: %s:" % (msg) handler = callback() log = "" chunk = """<foo><bar2/>""" ctxt = libxml2.createPushParser(handler, None, 0, "test.xml") ctxt.parseChunk(chunk, len(chunk), 0) ctxt = None reference = "startDocument:startElement foo None:startElement bar2 None:endElement bar2:" if log != reference: print("Error got: %s" % log) print("Expected: %s" % reference) sys.exit(1) log = "" chunk = """<foo><bar2></bar2>""" ctxt = libxml2.createPushParser(handler, None, 0, "test.xml") ctxt.parseChunk(chunk, len(chunk), 0) ctxt = None
def warning(self, msg): global log log = log + "warning: %s:" % (msg) def error(self, msg): global log log = log + "error: %s:" % (msg) def fatalError(self, msg): global log log = log + "fatalError: %s:" % (msg) handler = callback() ctxt = libxml2.createPushParser(handler, "<foo", 4, "test.xml") chunk = " url='tst'>b" ctxt.parseChunk(chunk, len(chunk), 0) chunk = "ar</foo>" ctxt.parseChunk(chunk, len(chunk), 1) ctxt=None reference = "startDocument:startElement foo {'url': 'tst'}:characters: bar:endElement foo:endDocument:" if log != reference: print("Error got: %s" % log) print("Exprected: %s" % reference) sys.exit(1) # Memory debug specific libxml2.cleanupParser() if libxml2.debugMemory(1) == 0:
expect = """--> (3) xmlns: URI foo is not absolute --> (4) Opening and ending tag mismatch: x line 0 and y """ err = "" def callback(arg, msg, severity, reserved): global err err = err + "%s (%d) %s" % (arg, severity, msg) s = """<x xmlns="foo"></y>""" parserCtxt = libxml2.createPushParser(None, "", 0, "test.xml") parserCtxt.setErrorHandler(callback, "-->") if parserCtxt.getErrorHandler() != (callback, "-->"): print("getErrorHandler failed") sys.exit(1) parserCtxt.parseChunk(s, len(s), 1) doc = parserCtxt.doc() doc.freeDoc() parserCtxt = None if err != expect: print("error") print("received %s" % (err)) print("expected %s" % (expect)) sys.exit(1)
#!/usr/bin/python -u import sys import libxml2 # Memory debug specific libxml2.debugMemory(1) ctxt = libxml2.createPushParser(None, "<foo", 4, "test.xml") ctxt.parseChunk("/>", 2, 1) doc = ctxt.doc() ctxt = None if doc.name != "test.xml": print("document name error") sys.exit(1) root = doc.children if root.name != "foo": print("root element name error") sys.exit(1) doc.freeDoc() i = 10000 while i > 0: ctxt = libxml2.createPushParser(None, "<foo", 4, "test.xml") ctxt.parseChunk("/>", 2, 1) doc = ctxt.doc() doc.freeDoc() i = i - 1 ctxt = None # Memory debug specific libxml2.cleanupParser() if libxml2.debugMemory(1) == 0:
def parse(self, source): self.__parsing = 1 try: # prepare source and create reader source = saxutils.prepare_input_source(source) input = libxml2.inputBuffer(source.getByteStream()) reader = input.newTextReader(source.getSystemId()) reader.SetErrorHandler(self._errorHandler, None) # configure reader reader.SetParserProp(libxml2.PARSER_LOADDTD, 1) reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS, 1) reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES, 0) reader.SetParserProp(libxml2.PARSER_VALIDATE, 0) # we reuse attribute maps (for a slight performance gain) attributesImpl = xmlreader.AttributesImpl({}) # start loop self._cont_handler.startDocument() while 1: r = reader.Read() # check for errors if r == 1: pass if self.__errors is not None: self._reportErrors(0) elif r == 0: if self.__errors is not None: self._reportErrors(0) break # end of parse else: if self.__errors is not None: self._reportErrors(1) else: self._err_handler.fatalError( SAXException("Read failed (no details available)")) break # fatal parse error # get node type nodeType = reader.NodeType() # Element if nodeType == 1: eltName = reader.Name() attributesImpl._attrs = attrs = {} while reader.MoveToNextAttribute(): attName = reader.Name() attrs[attName] = reader.Value() reader.MoveToElement() self._cont_handler.startElement(eltName, attributesImpl) if reader.IsEmptyElement(): self._cont_handler.endElement(eltName) # EndElement elif nodeType == 15: self._cont_handler.endElement(reader.Name()) # Text elif nodeType == 3: self._cont_handler.characters(reader.Value()) # SignificantWhitespace elif nodeType == 14: self._cont_handler.characters(reader.Value()) # EntityReference elif nodeType == 5: # Treating entity as such self._cont_handler.entity(reader.Name()) elif nodeType == 10: # We parse the doctype with a SAX parser nodeText = str(reader.CurrentNode()) entityDeclParser = libxml2.createPushParser( self._cont_handler, nodeText, len(nodeText), "doctype") entityDeclParser.parseChunk("", 0, 1) pass # Ignore all other node types if r == 0: self._cont_handler.endDocument() reader.Close() finally: self.__parsing = 0
def parse(self): handler = callback() ctxt = libxml2.createPushParser(handler,"",0,"temp.xml") myfile = file(self.filename) for line in myfile.readlines(): ctxt.parseChunk(line,len(line),0) ctxt.parseChunk("",0,1) # Let's find out what dirs were touched filelist = handler.filelist finished = 0 path = "" multidir = 0 while finished == 0: ok = 1 i = 0 subdirlinecount = 0 subdirslashpos = 0 maxi = len(filelist) if (i+1) == maxi: ok = 0 while i < maxi and ok == 1: search = filelist[i].find("/") if search == -1: #okay, we have a fil in the "top" dir let's stop it ok = 0 break if search > -1: # we have a subdir in here, so count ;) subdirlinecount += 1 i += 1 if ok == 1 and multidir == 0: if subdirlinecount == maxi: #okay all dirs still have a slash # everything in the same dir? subdirlinecount = 0 lastdir = "" # count dirs filelist.sort() for dir in filelist: if dir.find("/") > -1: if dir[:dir.find("/")+1] != lastdir: subdirlinecount += 1 lastdir = dir[:dir.find("/")+1] if subdirlinecount == 1: # okay, all lines have the same slashpos. # strip everything up to it i = 0 path += filelist[0][:search+1] while i < maxi: filelist[i] = filelist[i][search+1:] i += 1 filelist.sort() for line in filelist: if line == "": filelist.remove("") else: #no we seem to have the topdir now.. dircount = subdirlinecount multidir = 1 else: finished=1 filecount = 0 dircount = 0 lastdir = "" # count dirs filelist.sort() for dir in filelist: if dir.find("/") > -1: if dir[:dir.find("/")+1] != lastdir: dircount += 1 lastdir = dir[:dir.find("/")+1] filecount = maxi self.author = handler.author self.revision = handler.revision self.logmessage = handler.logmessage self.dircount = dircount self.filecount = filecount self.filelist = filelist self.path = path