<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from behave . textutil import compute_words_maxsize , text as _text <EOL> import codecs <EOL> import six <EOL> class DocumentWriter ( object ) : <EOL> """<STR_LIT>""" <EOL> heading_styles = [ "<STR_LIT:=>" , "<STR_LIT:=>" , "<STR_LIT:->" , "<STR_LIT>" ] <EOL> default_encoding = "<STR_LIT:utf-8>" <EOL> default_toctree_title = "<STR_LIT>" <EOL> def __init__ ( self , stream , filename = None , should_close = True ) : <EOL> self . stream = stream <EOL> self . filename = filename <EOL> self . should_close = should_close <EOL> @ classmethod <EOL> def open ( cls , filename , encoding = None ) : <EOL> encoding = encoding or cls . default_encoding <EOL> stream = codecs . open ( filename , "<STR_LIT:wb>" , encoding ) <EOL> return cls ( stream , filename ) <EOL> def write ( self , * args ) : <EOL> return self . stream . write ( * args ) <EOL> def close ( self ) : <EOL> if self . stream and self . should_close : <EOL> self . stream . close ( ) <EOL> self . stream = None <EOL> def write_heading ( self , heading , level = <NUM_LIT:0> , index_id = None , label = None ) : <EOL> assert self . stream <EOL> assert heading , "<STR_LIT>" <EOL> assert <NUM_LIT:0> <= level < len ( self . heading_styles ) <EOL> if level >= len ( self . heading_styles ) : <EOL> level = len ( self . heading_styles ) - <NUM_LIT:1> <EOL> heading_size = len ( heading ) <EOL> heading_style = self . heading_styles [ level ] <EOL> if level == <NUM_LIT:0> and heading_size < <NUM_LIT> : <EOL> heading_size = <NUM_LIT> <EOL> separator = heading_style * heading_size <EOL> if index_id : <EOL> if isinstance ( index_id , ( list , tuple ) ) : <EOL> index_id = "<STR_LIT:U+002CU+0020>" . join ( index_id ) <EOL> self . stream . write ( "<STR_LIT>" % index_id ) <EOL> if label : <EOL> self . stream . write ( "<STR_LIT>" % label ) <EOL> if level == <NUM_LIT:0> : <EOL> self . stream . write ( "<STR_LIT>" % separator ) <EOL> self . stream . write ( "<STR_LIT>" % heading ) <EOL> self . stream . write ( "<STR_LIT>" % separator ) <EOL> self . stream . write ( "<STR_LIT:\n>" ) <EOL> def write_toctree ( self , entries , title = None , maxdepth = <NUM_LIT:2> ) : <EOL> if title is None : <EOL> title = self . default_toctree_title <EOL> line_prefix = "<STR_LIT:U+0020>" * <NUM_LIT:4> <EOL> if title : <EOL> self . stream . write ( "<STR_LIT>" % title ) <EOL> self . stream . write ( "<STR_LIT>" ) <EOL> self . stream . write ( "<STR_LIT>" % ( line_prefix , maxdepth ) ) <EOL> for entry in entries : <EOL> self . stream . write ( "<STR_LIT>" % ( line_prefix , entry ) ) <EOL> self . stream . write ( "<STR_LIT:\n>" ) <EOL> def write_table ( self , table ) : <EOL> """<STR_LIT>""" <EOL> assert self . stream <EOL> cols_size = [ ] <EOL> separator_parts = [ ] <EOL> row_schema_parts = [ ] <EOL> for col_index , heading in enumerate ( table . headings ) : <EOL> column = [ six . text_type ( row [ col_index ] ) for row in table . rows ] <EOL> column . append ( heading ) <EOL> column_size = compute_words_maxsize ( column ) <EOL> cols_size . append ( column_size ) <EOL> separator_parts . append ( "<STR_LIT:=>" * column_size ) <EOL> row_schema_parts . append ( "<STR_LIT>" + _text ( column_size ) + "<STR_LIT:s>" ) <EOL> separator = "<STR_LIT:U+0020>" . join ( separator_parts ) + "<STR_LIT:\n>" <EOL> row_schema = "<STR_LIT:U+0020>" . join ( row_schema_parts ) + "<STR_LIT:\n>" <EOL> self . stream . write ( "<STR_LIT:\n>" ) <EOL> self . stream . write ( separator ) <EOL> self . stream . write ( row_schema % tuple ( table . headings ) ) <EOL> self . stream . write ( separator ) <EOL> for row in table . rows : <EOL> self . stream . write ( row_schema % tuple ( row ) ) <EOL> self . stream . write ( separator ) <EOL> self . stream . write ( "<STR_LIT:\n>" ) </s>
