<s> try : <EOL> import csv <EOL> except ImportError : <EOL> csv = None <EOL> from robotide . lib . robot import utils <EOL> from . formatters import TsvFormatter , TxtFormatter , PipeFormatter <EOL> from . htmlformatter import HtmlFormatter <EOL> from . htmltemplate import TEMPLATE_START , TEMPLATE_END <EOL> def FileWriter ( context ) : <EOL> """<STR_LIT>""" <EOL> if context . format == context . html_format : <EOL> return HtmlFileWriter ( context ) <EOL> if context . format == context . tsv_format : <EOL> return TsvFileWriter ( context ) <EOL> if context . pipe_separated : <EOL> return PipeSeparatedTxtWriter ( context ) <EOL> return SpaceSeparatedTxtWriter ( context ) <EOL> class _DataFileWriter ( object ) : <EOL> def __init__ ( self , formatter , configuration ) : <EOL> self . _formatter = formatter <EOL> self . _output = configuration . output <EOL> self . _line_separator = configuration . line_separator <EOL> self . _encoding = configuration . encoding <EOL> def write ( self , datafile ) : <EOL> tables = [ table for table in datafile if table ] <EOL> for table in tables : <EOL> self . _write_table ( table , is_last = table is tables [ - <NUM_LIT:1> ] ) <EOL> def _write_table ( self , table , is_last ) : <EOL> self . _write_header ( table ) <EOL> self . _write_rows ( self . _formatter . format_table ( table ) ) <EOL> if not is_last : <EOL> self . _write_empty_row ( table ) <EOL> def _write_header ( self , table ) : <EOL> self . _write_row ( self . _formatter . format_header ( table ) ) <EOL> def _write_rows ( self , rows ) : <EOL> for row in rows : <EOL> self . _write_row ( row ) <EOL> def _write_empty_row ( self , table ) : <EOL> self . _write_row ( self . _formatter . empty_row_after ( table ) ) <EOL> def _encode ( self , row ) : <EOL> return row . encode ( self . _encoding ) <EOL> def _write_row ( self , row ) : <EOL> raise NotImplementedError <EOL> class SpaceSeparatedTxtWriter ( _DataFileWriter ) : <EOL> def __init__ ( self , configuration ) : <EOL> formatter = TxtFormatter ( configuration . txt_column_count ) <EOL> self . _separator = '<STR_LIT:U+0020>' * configuration . txt_separating_spaces <EOL> _DataFileWriter . __init__ ( self , formatter , configuration ) <EOL> def _write_row ( self , row ) : <EOL> line = self . _separator . join ( row ) . rstrip ( ) + self . _line_separator <EOL> self . _output . write ( self . _encode ( line ) ) <EOL> class PipeSeparatedTxtWriter ( _DataFileWriter ) : <EOL> _separator = '<STR_LIT>' <EOL> def __init__ ( self , configuration ) : <EOL> formatter = PipeFormatter ( configuration . txt_column_count ) <EOL> _DataFileWriter . __init__ ( self , formatter , configuration ) <EOL> def _write_row ( self , row ) : <EOL> row = self . _separator . join ( row ) <EOL> if row : <EOL> row = '<STR_LIT>' + row + '<STR_LIT>' <EOL> self . _output . write ( self . _encode ( row + self . _line_separator ) ) <EOL> class TsvFileWriter ( _DataFileWriter ) : <EOL> def __init__ ( self , configuration ) : <EOL> if not csv : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> formatter = TsvFormatter ( configuration . tsv_column_count ) <EOL> _DataFileWriter . __init__ ( self , formatter , configuration ) <EOL> self . _writer = self . _get_writer ( configuration ) <EOL> def _get_writer ( self , configuration ) : <EOL> dialect = csv . excel_tab ( ) <EOL> dialect . lineterminator = configuration . line_separator <EOL> return csv . writer ( configuration . output , dialect = dialect ) <EOL> def _write_row ( self , row ) : <EOL> self . _writer . writerow ( [ self . _encode ( c ) for c in row ] ) <EOL> class HtmlFileWriter ( _DataFileWriter ) : <EOL> def __init__ ( self , configuration ) : <EOL> formatter = HtmlFormatter ( configuration . html_column_count ) <EOL> _DataFileWriter . __init__ ( self , formatter , configuration ) <EOL> self . _name = configuration . datafile . name <EOL> self . _writer = utils . HtmlWriter ( configuration . output , <EOL> configuration . line_separator , <EOL> encoding = self . _encoding ) <EOL> def write ( self , datafile ) : <EOL> self . _writer . content ( TEMPLATE_START % { '<STR_LIT>' : self . _name } , <EOL> escape = False , replace_newlines = True ) <EOL> _DataFileWriter . write ( self , datafile ) <EOL> self . _writer . content ( TEMPLATE_END , escape = False , replace_newlines = True ) <EOL> def _write_table ( self , table , is_last ) : <EOL> self . _writer . start ( '<STR_LIT>' , { '<STR_LIT:id>' : table . type . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT:1>' } ) <EOL> _DataFileWriter . _write_table ( self , table , is_last ) <EOL> self . _writer . end ( '<STR_LIT>' ) <EOL> def _write_row ( self , row ) : <EOL> self . _writer . start ( '<STR_LIT>' ) <EOL> for cell in row : <EOL> self . _writer . element ( cell . tag , cell . content , cell . attributes , <EOL> escape = False ) <EOL> self . _writer . end ( '<STR_LIT>' ) </s>
