<s> from pikos . recorders . abstract_recorder import AbstractRecorder , RecorderError <EOL> class TextStreamRecorder ( AbstractRecorder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , text_stream , filter_ = None , formatted = False , <EOL> auto_flush = False ) : <EOL> """<STR_LIT>""" <EOL> self . _filter = ( lambda x : True ) if filter_ is None else filter_ <EOL> self . _stream = text_stream <EOL> self . _formatted = formatted <EOL> self . _auto_flush = auto_flush <EOL> self . _ready = False <EOL> def prepare ( self , record ) : <EOL> """<STR_LIT>""" <EOL> if not self . _ready : <EOL> if hasattr ( record , '<STR_LIT>' ) : <EOL> self . _writeheader ( record ) <EOL> self . _ready = True <EOL> def finalize ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _ready : <EOL> msg = '<STR_LIT>' <EOL> raise RecorderError ( msg ) <EOL> def record ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if self . _ready : <EOL> if self . _filter ( data ) : <EOL> line = self . _format ( data ) <EOL> self . _stream . write ( line ) <EOL> if self . _auto_flush : <EOL> self . _stream . flush ( ) <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> raise RecorderError ( msg ) <EOL> def _writeheader ( self , record ) : <EOL> """<STR_LIT>""" <EOL> if self . _formatted and hasattr ( record , '<STR_LIT>' ) : <EOL> header = record . header . format ( * record . _fields ) <EOL> else : <EOL> header = u'<STR_LIT:U+0020>' . join ( str ( value ) for value in record . _fields ) <EOL> header += u'<STR_LIT:\n>' <EOL> separator = u'<STR_LIT:->' * ( len ( header ) - <NUM_LIT:1> ) + u'<STR_LIT:\n>' <EOL> self . _stream . write ( header ) <EOL> self . _stream . write ( separator ) <EOL> if self . _auto_flush : <EOL> self . _stream . flush ( ) <EOL> def _format ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if self . _formatted and hasattr ( data , '<STR_LIT>' ) : <EOL> line = data . line . format ( * data ) + '<STR_LIT:\n>' <EOL> else : <EOL> line = '<STR_LIT:U+0020>' . join ( str ( value ) for value in data ) + '<STR_LIT:\n>' <EOL> return line </s>
