 """<STR_LIT>""" <EOL> try : <EOL> import xml . etree . cElementTree as et <EOL> except : <EOL> import xml . etree . ElementTree as et <EOL> try : <EOL> from collections import OrderedDict <EOL> except : <EOL> from ordereddict import OrderedDict <EOL> try : <EOL> from cStringIO import StringIO <EOL> except : <EOL> from StringIO import StringIO <EOL> __all__ = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> class Message ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , type_ , message ) : <EOL> self . type = type_ <EOL> self . message = message <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . type , self . message ) <EOL> def __eq__ ( self , other ) : <EOL> return ( self . type , self . message ) == ( other . type , other . message ) <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . type , self . message ) ) <EOL> class _ConcatenatedStream ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * streams ) : <EOL> self . streams = list ( streams ) <EOL> def read ( self , n = None ) : <EOL> """<STR_LIT>""" <EOL> response = "<STR_LIT>" <EOL> while len ( self . streams ) > <NUM_LIT:0> and ( n is None or n > <NUM_LIT:0> ) : <EOL> txt = self . streams [ <NUM_LIT:0> ] . read ( n ) <EOL> response += txt <EOL> if n is not None : <EOL> n -= len ( txt ) <EOL> if n > <NUM_LIT:0> or n is None : <EOL> del self . streams [ <NUM_LIT:0> ] <EOL> return response <EOL> class _XMLDTDFilter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stream ) : <EOL> self . stream = stream <EOL> def read ( self , n = None ) : <EOL> """<STR_LIT>""" <EOL> response = "<STR_LIT>" <EOL> while n is None or n > <NUM_LIT:0> : <EOL> c = self . stream . read ( <NUM_LIT:1> ) <EOL> if c == "<STR_LIT>" : <EOL> break <EOL> elif c == "<STR_LIT:<>" : <EOL> c += self . stream . read ( <NUM_LIT:1> ) <EOL> if c == "<STR_LIT>" : <EOL> while True : <EOL> q = self . stream . read ( <NUM_LIT:1> ) <EOL> if q == "<STR_LIT:>>" : <EOL> break <EOL> else : <EOL> response += c <EOL> if n is not None : <EOL> n -= len ( c ) <EOL> else : <EOL> response += c <EOL> if n is not None : <EOL> n -= <NUM_LIT:1> <EOL> return response <EOL> class ResultsReader ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stream ) : <EOL> stream = _XMLDTDFilter ( stream ) <EOL> stream = _ConcatenatedStream ( StringIO ( "<STR_LIT>" ) , stream , StringIO ( "<STR_LIT>" ) ) <EOL> self . is_preview = None <EOL> self . _gen = self . _parse_results ( stream ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> return self . _gen . next ( ) <EOL> def _parse_results ( self , stream ) : <EOL> """<STR_LIT>""" <EOL> result = None <EOL> values = None <EOL> try : <EOL> for event , elem in et . iterparse ( stream , events = ( '<STR_LIT:start>' , '<STR_LIT:end>' ) ) : <EOL> if elem . tag == '<STR_LIT>' and event == '<STR_LIT:start>' : <EOL> is_preview = elem . attrib [ '<STR_LIT>' ] == '<STR_LIT:1>' <EOL> self . is_preview = is_preview <EOL> if elem . tag == '<STR_LIT:result>' : <EOL> if event == '<STR_LIT:start>' : <EOL> result = OrderedDict ( ) <EOL> elif event == '<STR_LIT:end>' : <EOL> yield result 
 result = None 
 """<STR_LIT>""" <EOL> try : <EOL> import xml . etree . cElementTree as et <EOL> except : <EOL> import xml . etree . ElementTree as et <EOL> try : <EOL> from collections import OrderedDict <EOL> except : <EOL> from ordereddict import OrderedDict <EOL> try : <EOL> from cStringIO import StringIO <EOL> except : <EOL> from StringIO import StringIO <EOL> __all__ = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> class Message ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , type_ , message ) : <EOL> self . type = type_ <EOL> self . message = message <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . type , self . message ) <EOL> def __eq__ ( self , other ) : <EOL> return ( self . type , self . message ) == ( other . type , other . message ) <EOL> def __hash__ ( self ) : <EOL> return hash ( ( self . type , self . message ) ) <EOL> class _ConcatenatedStream ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * streams ) : <EOL> self . streams = list ( streams ) <EOL> def read ( self , n = None ) : <EOL> """<STR_LIT>""" <EOL> response = "<STR_LIT>" <EOL> while len ( self . streams ) > <NUM_LIT:0> and ( n is None or n > <NUM_LIT:0> ) : <EOL> txt = self . streams [ <NUM_LIT:0> ] . read ( n ) <EOL> response += txt <EOL> if n is not None : <EOL> n -= len ( txt ) <EOL> if n > <NUM_LIT:0> or n is None : <EOL> del self . streams [ <NUM_LIT:0> ] <EOL> return response <EOL> class _XMLDTDFilter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stream ) : <EOL> self . stream = stream <EOL> def read ( self , n = None ) : <EOL> """<STR_LIT>""" <EOL> response = "<STR_LIT>" <EOL> while n is None or n > <NUM_LIT:0> : <EOL> c = self . stream . read ( <NUM_LIT:1> ) <EOL> if c == "<STR_LIT>" : <EOL> break <EOL> elif c == "<STR_LIT:<>" : <EOL> c += self . stream . read ( <NUM_LIT:1> ) <EOL> if c == "<STR_LIT>" : <EOL> while True : <EOL> q = self . stream . read ( <NUM_LIT:1> ) <EOL> if q == "<STR_LIT:>>" : <EOL> break <EOL> else : <EOL> response += c <EOL> if n is not None : <EOL> n -= len ( c ) <EOL> else : <EOL> response += c <EOL> if n is not None : <EOL> n -= <NUM_LIT:1> <EOL> return response <EOL> class ResultsReader ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stream ) : <EOL> stream = _XMLDTDFilter ( stream ) <EOL> stream = _ConcatenatedStream ( StringIO ( "<STR_LIT>" ) , stream , StringIO ( "<STR_LIT>" ) ) <EOL> self . is_preview = None <EOL> self . _gen = self . _parse_results ( stream ) <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> return self . _gen . next ( ) <EOL> def _parse_results ( self , stream ) : <EOL> """<STR_LIT>""" <EOL> result = None <EOL> values = None <EOL> try : <EOL> for event , elem in et . iterparse ( stream , events = ( '<STR_LIT:start>' , '<STR_LIT:end>' ) ) : <EOL> if elem . tag == '<STR_LIT>' and event == '<STR_LIT:start>' : <EOL> is_preview = elem . attrib [ '<STR_LIT>' ] == '<STR_LIT:1>' <EOL> self . is_preview = is_preview <EOL> if elem . tag == '<STR_LIT:result>' : <EOL> if event == '<STR_LIT:start>' : <EOL> result = OrderedDict ( ) <EOL> elif event == '<STR_LIT:end>' : <EOL> yield result <EOL> result = None <EOL> elem . clear ( ) <EOL> elif elem . tag == '<STR_LIT>' and result is not None : <EOL> if event == '<STR_LIT:start>' : <EOL> values = [ ] <EOL> elif event == '<STR_LIT:end>' : <EOL> field_name = elem . attrib [ '<STR_LIT:k>' ] . encode ( '<STR_LIT:utf8>' ) <EOL> if len ( values ) == <NUM_LIT:1> : <EOL> result [ field_name ] = values [ <NUM_LIT:0> ] <EOL> else : <EOL> result [ field_name ] = values <EOL> elem . clear ( ) <EOL> elif elem . tag in ( '<STR_LIT:text>' , '<STR_LIT:v>' ) and event == '<STR_LIT:end>' : <EOL> values . append ( elem . text . encode ( '<STR_LIT:utf8>' ) ) <EOL> elem . clear ( ) <EOL> elif elem . tag == '<STR_LIT>' : <EOL> if event == '<STR_LIT:start>' : <EOL> msg_type = elem . attrib [ '<STR_LIT:type>' ] <EOL> elif event == '<STR_LIT:end>' : <EOL> yield Message ( msg_type , elem . text . encode ( '<STR_LIT:utf8>' ) ) <EOL> elem . clear ( ) <EOL> except et . ParseError as pe : <EOL> if '<STR_LIT>' in pe . msg : <EOL> return <EOL> else : <EOL> raise </s>
