Logo Search packages:      
Sourcecode: harpia version File versions  Download package

def harpia::amara::binderytools::pushbind (   xpatterns,
  source = None,
  string = None,
  prefixes = None,
  rules = None,
  lookahead = 2 
)

Generator that yields subtrees of Python object bindings from XML
according to the given pattern.  The patterns are must be XSLT patterns
matching elements.

Example: for an xml document

XML = '<a> <b><c/></b> <b><c/></b> <b><c/></b> </a>'

pushbind('b', string=XML)
Will return a generator that yields the first, second and third b
element in turn as Python objects containing the full subtree.

Warning: this function uses threads and only works in Python distros
with threading support.

lookahead - how far ahead the SAX engine looks for events before waiting
for the user to request another subtree from the generator.  Must be 0 or
at least 2.  0 means the SAX engine will just load and queue up object for
the entire file in the background.

Definition at line 670 of file binderytools.py.

                                     :
    """
    Generator that yields subtrees of Python object bindings from XML
    according to the given pattern.  The patterns are must be XSLT patterns
    matching elements.
    
    Example: for an xml document
    
    XML = '<a> <b><c/></b> <b><c/></b> <b><c/></b> </a>'
    
    pushbind('b', string=XML)
    Will return a generator that yields the first, second and third b
    element in turn as Python objects containing the full subtree.
    
    Warning: this function uses threads and only works in Python distros
    with threading support.

    lookahead - how far ahead the SAX engine looks for events before waiting
    for the user to request another subtree from the generator.  Must be 0 or
    at least 2.  0 means the SAX engine will just load and queue up object for
    the entire file in the background.
    """
    #Saved some pondering by reference to Jimmy Retzlaff
    #http://groups-beta.google.com/group/comp.lang.python/msg/982bd32c08450352
    if lookahead == 2: lookahead = 1
    import Queue
    try:
        import threading
    except ImportError:
        import dummy_threading as threading
    queue = Queue.Queue(lookahead)
    def handle_chunk(docfrag):
        queue.put(docfrag)
        return

    #Create an instance of the chunker
    handler = saxbind_chunker(xpatterns=xpatterns, prefixes=prefixes,
        chunk_consumer=handle_chunk, rules=rules
    )

    def launch_sax():
        parser = sax.make_parser()

        #The chunker is the SAX handler
        parser.setContentHandler(handler)
        parser.setFeature(sax.handler.feature_namespaces, 1)
        if source:
            parser.parse(source)
        else:
            from cStringIO import StringIO
            parser.parse(StringIO(string))
        queue.put(None)  #Sentinel to mark that we're done
        return

    sax_thread = threading.Thread(target=launch_sax)
    sax_thread.setDaemon(True)
    sax_thread.start()

    while True:
        elem = queue.get()
        if elem:
            yield elem
        else:
            break
    return


class saxbind_chunker(bindery.binder, saxtools.sax2dom_chunker):


Generated by  Doxygen 1.6.0   Back to index