<s> class OutputHandler : <EOL> def __init__ ( self , mod , output_file ) : <EOL> self . mod = mod <EOL> self . output_file = output_file <EOL> class OutputBuffer : <EOL> def __init__ ( self , mod , opts = None ) : <EOL> self . mod = mod <EOL> self . strio = mod . _root . cStringIO . StringIO ( ) <EOL> if opts == None : <EOL> opts = { } <EOL> self . opts = opts <EOL> self . lines = [ '<STR_LIT>' ] <EOL> self . line_no = <NUM_LIT:0> <EOL> def getopt ( self , opt ) : <EOL> return self . opts . get ( opt ) <EOL> def getvalue ( self ) : <EOL> return '<STR_LIT:\n>' . join ( self . lines ) <EOL> def new_line ( self ) : <EOL> self . line_no += <NUM_LIT:1> <EOL> self . lines . append ( '<STR_LIT>' ) <EOL> def write ( self , s ) : <EOL> lines = s . split ( '<STR_LIT:\n>' ) <EOL> for line in lines [ : - <NUM_LIT:1> ] : <EOL> self . write_seg ( line ) <EOL> self . new_line ( ) <EOL> self . write_seg ( lines [ - <NUM_LIT:1> ] ) <EOL> def write_seg ( self , s ) : <EOL> self . lines [ self . line_no ] += s <EOL> class MorePrinter : <EOL> _oh_next_lineno = None <EOL> def __init__ ( self , printer , previous ) : <EOL> self . _oh_printer = printer <EOL> self . _oh_previous = previous <EOL> self . _hiding_tag_ = printer . _hiding_tag_ <EOL> def __getattr__ ( self , attr ) : <EOL> return self . _oh_printer . getattr ( self , attr ) <EOL> def _oh_get_next_lineno ( self ) : <EOL> next_lineno = self . _oh_next_lineno <EOL> if next_lineno is None : <EOL> repr ( self ) <EOL> next_lineno = self . _oh_next_lineno <EOL> return next_lineno <EOL> def _oh_get_start_lineno ( self ) : <EOL> return self . _oh_previous . _oh_get_next_lineno ( ) <EOL> class Printer : <EOL> def __init__ ( self , mod , client , get_line_iter = None , max_top_lines = None , max_more_lines = None , <EOL> get_num_lines = None , <EOL> get_more_msg = None , <EOL> get_more_state_msg = None , <EOL> stop_only_when_told = None <EOL> ) : <EOL> if get_line_iter is None : <EOL> get_line_iter = client . _oh_get_line_iter <EOL> if max_top_lines is None : <EOL> max_top_lines = mod . max_top_lines <EOL> if max_more_lines is None : <EOL> max_more_lines = mod . max_more_lines <EOL> self . mod = mod <EOL> self . _hiding_tag_ = mod . _hiding_tag_ <EOL> self . client = client <EOL> self . get_line_iter = get_line_iter <EOL> self . max_top_lines = max_top_lines <EOL> self . max_more_lines = max_more_lines <EOL> if get_num_lines is not None : <EOL> self . get_num_lines = get_num_lines <EOL> if get_more_msg is not None : <EOL> self . get_more_msg = get_more_msg <EOL> if get_more_state_msg is None : <EOL> get_more_state_msg = getattr ( client , '<STR_LIT>' , None ) <EOL> if get_more_state_msg is not None : <EOL> self . get_more_state_msg = get_more_state_msg <EOL> self . stop_only_when_told = stop_only_when_told <EOL> self . reset ( ) <EOL> def getattr ( self , mp , attr ) : <EOL> try : <EOL> g = getattr ( self , '<STR_LIT>' + attr ) <EOL> except AttributeError : <EOL> return getattr ( self . client , attr ) <EOL> else : <EOL> return g ( mp ) <EOL> def line_at ( self , idx ) : <EOL> while idx >= len ( self . lines_seen ) : <EOL> try : <EOL> li = self . line_iter . next ( ) <EOL> except StopIteration : <EOL> raise IndexError <EOL> else : <EOL> if isinstance ( li , tuple ) : <EOL> cmd , line = li <EOL> if cmd == '<STR_LIT>' : <EOL> self . stop_linenos [ len ( self . lines_seen ) ] = <NUM_LIT:1> <EOL> else : <EOL> line = li <EOL> self . lines_seen . append ( line ) <EOL> return self . lines_seen [ idx ] <EOL> def lines_from ( self , idx = <NUM_LIT:0> ) : <EOL> line_iter = self . line_iter <EOL> if line_iter is None : <EOL> line_iter = self . line_iter = self . get_line_iter ( ) <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> yield self . line_at ( idx ) <EOL> except IndexError : <EOL> return <EOL> idx += <NUM_LIT:1> <EOL> def _get_more ( self , mp ) : <EOL> return MorePrinter ( self , mp ) <EOL> def _oh_get_next_lineno ( self ) : <EOL> next_lineno = getattr ( self , '<STR_LIT>' , None ) <EOL> if next_lineno is None : <EOL> self . get_str_of_top ( ) <EOL> next_lineno = self . _oh_next_lineno <EOL> return next_lineno <EOL> def _get_prev ( self , mp ) : <EOL> return mp . _oh_previous <EOL> def _oh_get_start_lineno ( self ) : <EOL> return <NUM_LIT:0> <EOL> def _get_top ( self , mp ) : <EOL> return self . client <EOL> def _get___repr__ ( self , mp ) : <EOL> return lambda : self . get_str ( mp , self . max_more_lines ) <EOL> _get___str__ = _get___repr__ <EOL> def get_str_of_top ( self ) : <EOL> return self . get_str ( self , self . max_top_lines ) <EOL> def get_more_state_msg ( self , start_lineno , end_lineno ) : <EOL> num_lines = self . get_num_lines ( ) <EOL> if num_lines is None : <EOL> of_num_lines = '<STR_LIT>' <EOL> else : <EOL> of_num_lines = '<STR_LIT>' % num_lines <EOL> return "<STR_LIT>" % ( start_lineno , end_lineno , of_num_lines ) <EOL> def get_more_msg ( self , start_lineno , end_lineno ) : <EOL> state_msg = self . get_more_state_msg ( start_lineno , end_lineno ) <EOL> return "<STR_LIT>" % ( state_msg ) <EOL> def get_num_lines ( self ) : <EOL> return None <EOL> def get_str ( self , printer , max_lines ) : <EOL> def f ( ) : <EOL> _hiding_tag_ = printer . _hiding_tag_ <EOL> start_lineno = printer . _oh_get_start_lineno ( ) <EOL> ob = self . mod . output_buffer ( ) <EOL> it = self . lines_from ( start_lineno ) <EOL> numlines = <NUM_LIT:0> <EOL> lineno = start_lineno <EOL> for line in it : <EOL> if ( numlines >= max_lines and <EOL> ( ( not self . stop_only_when_told ) or self . stop_linenos . get ( lineno - <NUM_LIT:1> ) ) ) : <EOL> try : <EOL> self . line_at ( lineno + <NUM_LIT:1> ) <EOL> except IndexError : <EOL> print >> ob , line <EOL> lineno += <NUM_LIT:1> <EOL> break <EOL> else : <EOL> print >> ob , self . get_more_msg ( start_lineno , lineno - <NUM_LIT:1> ) <EOL> break <EOL> numlines += <NUM_LIT:1> <EOL> print >> ob , line <EOL> lineno += <NUM_LIT:1> <EOL> printer . _oh_next_lineno = lineno <EOL> return ob . getvalue ( ) . rstrip ( ) <EOL> return printer . mod . _parent . View . enter ( lambda : f ( ) ) <EOL> def reset ( self ) : <EOL> self . lines_seen = [ ] <EOL> self . stop_linenos = { } <EOL> self . line_iter = None <EOL> class BasicMorePrinter : <EOL> def __init__ ( self , mod , top , handler , startindex = None ) : <EOL> self . mod = mod <EOL> self . top = top <EOL> self . handler = handler <EOL> if startindex is None : <EOL> startindex = handler . get_more_index ( ) <EOL> self . startindex = startindex <EOL> self . _hiding_tag_ = mod . _hiding_tag_ <EOL> def __getattr__ ( self , attr ) : <EOL> if attr == '<STR_LIT>' : <EOL> return self . __class__ ( self . mod , self . top , self . handler , <EOL> self . handler . get_more_index ( self . startindex ) ) <EOL> else : <EOL> return getattr ( self . top , attr ) <EOL> def __repr__ ( self ) : <EOL> return self . __str__ ( ) <EOL> def __str__ ( self ) : <EOL> ob = self . mod . output_buffer ( ) <EOL> self . handler . ppob ( ob , self . startindex ) <EOL> return ob . getvalue ( ) . rstrip ( ) <EOL> def at ( self , idx ) : <EOL> return self . __class__ ( self . mod , self . top , self . handler , <EOL> idx ) <EOL> class _GLUECLAMP_ : <EOL> _chgable_ = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> _preload_ = ( '<STR_LIT>' , ) <EOL> max_top_lines = <NUM_LIT:10> <EOL> max_more_lines = <NUM_LIT:10> <EOL> def _get__hiding_tag_ ( self ) : return self . _parent . View . _hiding_tag_ <EOL> def _get_output_file ( self ) : return self . _root . sys . stdout <EOL> def more_printer ( self , client , ** kwds ) : <EOL> printer = Printer ( self , client , ** kwds ) <EOL> return MorePrinter ( printer , printer ) <EOL> def output_buffer ( self ) : <EOL> return OutputBuffer ( self ) <EOL> def output_handler ( self , output_file = None ) : <EOL> if output_file is None : <EOL> output_file = self . output_file <EOL> return OutputHandler ( self , output_file ) <EOL> def setup_printing ( self , client , ** kwds ) : <EOL> more = self . more_printer ( client , ** kwds ) <EOL> printer = more . _oh_printer <EOL> client . more = more <EOL> client . printer = printer <EOL> client . __str__ = client . __repr__ = ( lambda : <EOL> printer . get_str_of_top ( ) ) <EOL> def basic_more_printer ( self , top , handler , startindex = None ) : <EOL> return BasicMorePrinter ( self , top , handler , startindex ) <EOL> def _get_stdout ( self ) : return self . _root . sys . stdout </s>
