<s> '''<STR_LIT>''' <EOL> try : <EOL> import cStringIO <EOL> except ImportError : <EOL> raise ImportError , "<STR_LIT>" <EOL> from bisect import bisect_right <EOL> _obj_SIO = cStringIO . StringIO <EOL> _type_SIO = cStringIO . OutputType <EOL> def make_SIO ( d ) : <EOL> t = _obj_SIO ( ) <EOL> t . write ( d ) <EOL> t . seek ( <NUM_LIT:0> ) <EOL> return t <EOL> def get_file_length ( f ) : <EOL> m = f . tell ( ) <EOL> f . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> r = f . tell ( ) <EOL> f . seek ( m ) <EOL> return r <EOL> class PipelineCloseRequest ( Exception ) : pass <EOL> class PipelineClosed ( Exception ) : pass <EOL> class PipelineItem ( object ) : <EOL> def __init__ ( self , d ) : <EOL> if type ( d ) is str : <EOL> self . f = make_SIO ( d ) <EOL> self . length = len ( d ) <EOL> self . is_sio = True <EOL> self . f . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> elif hasattr ( d , '<STR_LIT>' ) : <EOL> self . f = d <EOL> self . length = get_file_length ( d ) <EOL> self . is_sio = False <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . read = self . f . read <EOL> def merge ( self , s ) : <EOL> self . f . write ( s ) <EOL> self . length += len ( s ) <EOL> def reset ( self ) : <EOL> if self . is_sio : <EOL> self . is_sio = False <EOL> self . f . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> @ property <EOL> def done ( self ) : <EOL> return self . f . tell ( ) == self . length <EOL> def __cmp__ ( self , other ) : <EOL> if other is PipelineStandIn : <EOL> return - <NUM_LIT:1> <EOL> return cmp ( self , other ) <EOL> class PipelineStandIn ( object ) : pass <EOL> class Pipeline ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . line = [ ] <EOL> self . current = None <EOL> self . want_close = False <EOL> def add ( self , d , priority = <NUM_LIT:5> ) : <EOL> '''<STR_LIT>''' <EOL> if self . want_close : <EOL> raise PipelineClosed <EOL> priority *= - <NUM_LIT:1> <EOL> dummy = ( priority , PipelineStandIn ) <EOL> ind = bisect_right ( self . line , dummy ) <EOL> if ind > <NUM_LIT:0> and type ( d ) is str and self . line [ ind - <NUM_LIT:1> ] [ - <NUM_LIT:1> ] . is_sio : <EOL> a_pri , adjacent = self . line [ ind - <NUM_LIT:1> ] <EOL> if adjacent . is_sio and a_pri == priority : <EOL> adjacent . merge ( d ) <EOL> else : <EOL> self . line . insert ( ind , ( priority , PipelineItem ( d ) ) ) <EOL> else : <EOL> self . line . insert ( ind , ( priority , PipelineItem ( d ) ) ) <EOL> def close_request ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . want_close = True <EOL> def read ( self , amt ) : <EOL> '''<STR_LIT>''' <EOL> if not self . current and not self . line : <EOL> if self . want_close : <EOL> raise PipelineCloseRequest ( ) <EOL> return '<STR_LIT>' <EOL> if not self . current : <EOL> _ , self . current = self . line . pop ( <NUM_LIT:0> ) <EOL> self . current . reset ( ) <EOL> out = '<STR_LIT>' <EOL> while len ( out ) < amt : <EOL> try : <EOL> data = self . current . read ( amt - len ( out ) ) <EOL> except ValueError : <EOL> data = '<STR_LIT>' <EOL> if data == '<STR_LIT>' : <EOL> if not self . line : <EOL> self . current = None <EOL> break <EOL> _ , self . current = self . line . pop ( <NUM_LIT:0> ) <EOL> self . current . reset ( ) <EOL> else : <EOL> out += data <EOL> if self . current and self . current . done : <EOL> self . current = None <EOL> return out <EOL> def backup ( self , d ) : <EOL> '''<STR_LIT>''' <EOL> cur = self . current <EOL> self . current = PipelineItem ( d ) <EOL> self . current . reset ( ) <EOL> if cur : <EOL> self . line . insert ( <NUM_LIT:0> , ( - <NUM_LIT> , cur ) ) <EOL> @ property <EOL> def empty ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . want_close == False and not self . line and not self . current </s>
