<s> """<STR_LIT>""" <EOL> import prettytable <EOL> import six <EOL> from cliff import utils <EOL> from . base import ListFormatter , SingleFormatter <EOL> class TableFormatter ( ListFormatter , SingleFormatter ) : <EOL> ALIGNMENTS = { <EOL> int : '<STR_LIT:r>' , <EOL> str : '<STR_LIT:l>' , <EOL> float : '<STR_LIT:r>' , <EOL> } <EOL> try : <EOL> ALIGNMENTS [ unicode ] = '<STR_LIT:l>' <EOL> except NameError : <EOL> pass <EOL> def add_argument_group ( self , parser ) : <EOL> group = parser . add_argument_group ( '<STR_LIT>' ) <EOL> group . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> default = <NUM_LIT:0> , <EOL> type = int , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> def emit_list ( self , column_names , data , stdout , parsed_args ) : <EOL> x = prettytable . PrettyTable ( <EOL> column_names , <EOL> print_empty = False , <EOL> ) <EOL> x . padding_width = <NUM_LIT:1> <EOL> data_iter = iter ( data ) <EOL> try : <EOL> first_row = next ( data_iter ) <EOL> except StopIteration : <EOL> pass <EOL> else : <EOL> for value , name in zip ( first_row , column_names ) : <EOL> alignment = self . ALIGNMENTS . get ( type ( value ) , '<STR_LIT:l>' ) <EOL> x . align [ name ] = alignment <EOL> x . add_row ( first_row ) <EOL> for row in data_iter : <EOL> row = [ r . replace ( '<STR_LIT:\r\n>' , '<STR_LIT:\n>' ) . replace ( '<STR_LIT:\r>' , '<STR_LIT:U+0020>' ) <EOL> if isinstance ( r , six . string_types ) else r <EOL> for r in row ] <EOL> x . add_row ( row ) <EOL> min_width = <NUM_LIT:8> <EOL> self . _assign_max_widths ( <EOL> stdout , x , int ( parsed_args . max_width ) , min_width ) <EOL> formatted = x . get_string ( ) <EOL> stdout . write ( formatted ) <EOL> stdout . write ( '<STR_LIT:\n>' ) <EOL> return <EOL> def emit_one ( self , column_names , data , stdout , parsed_args ) : <EOL> x = prettytable . PrettyTable ( field_names = ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> print_empty = False ) <EOL> x . padding_width = <NUM_LIT:1> <EOL> x . align [ '<STR_LIT>' ] = '<STR_LIT:l>' <EOL> x . align [ '<STR_LIT>' ] = '<STR_LIT:l>' <EOL> for name , value in zip ( column_names , data ) : <EOL> value = ( value . replace ( '<STR_LIT:\r\n>' , '<STR_LIT:\n>' ) . replace ( '<STR_LIT:\r>' , '<STR_LIT:U+0020>' ) if <EOL> isinstance ( value , six . string_types ) else value ) <EOL> x . add_row ( ( name , value ) ) <EOL> min_width = <NUM_LIT:16> <EOL> self . _assign_max_widths ( <EOL> stdout , x , int ( parsed_args . max_width ) , min_width ) <EOL> formatted = x . get_string ( ) <EOL> stdout . write ( formatted ) <EOL> stdout . write ( '<STR_LIT:\n>' ) <EOL> return <EOL> @ staticmethod <EOL> def _field_widths ( field_names , first_line ) : <EOL> widths = [ max ( <NUM_LIT:0> , len ( i ) - <NUM_LIT:2> ) for i in first_line . split ( '<STR_LIT:+>' ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] ] <EOL> return dict ( zip ( field_names , widths ) ) <EOL> @ staticmethod <EOL> def _width_info ( term_width , field_count ) : <EOL> usable_total_width = max ( <NUM_LIT:0> , term_width - <NUM_LIT:1> - <NUM_LIT:3> * field_count ) <EOL> if field_count == <NUM_LIT:0> : <EOL> optimal_width = <NUM_LIT:0> <EOL> else : <EOL> optimal_width = max ( <NUM_LIT:0> , usable_total_width // field_count ) <EOL> return usable_total_width , optimal_width <EOL> @ staticmethod <EOL> def _build_shrink_fields ( usable_total_width , optimal_width , <EOL> field_widths , field_names ) : <EOL> shrink_fields = [ ] <EOL> shrink_remaining = usable_total_width <EOL> for field in field_names : <EOL> w = field_widths [ field ] <EOL> if w <= optimal_width : <EOL> shrink_remaining -= w <EOL> else : <EOL> shrink_fields . append ( field ) <EOL> return shrink_fields , shrink_remaining <EOL> @ staticmethod <EOL> def _assign_max_widths ( stdout , x , max_width , min_width = <NUM_LIT:0> ) : <EOL> if min_width : <EOL> x . min_width = min_width <EOL> if max_width > <NUM_LIT:0> : <EOL> x . max_width = max_width <EOL> return <EOL> term_width = utils . terminal_width ( stdout ) <EOL> if not term_width : <EOL> return <EOL> field_count = len ( x . field_names ) <EOL> try : <EOL> first_line = x . get_string ( ) . splitlines ( ) [ <NUM_LIT:0> ] <EOL> if len ( first_line ) <= term_width : <EOL> return <EOL> except IndexError : <EOL> return <EOL> usable_total_width , optimal_width = TableFormatter . _width_info ( <EOL> term_width , field_count ) <EOL> field_widths = TableFormatter . _field_widths ( x . field_names , first_line ) <EOL> shrink_fields , shrink_remaining = TableFormatter . _build_shrink_fields ( <EOL> usable_total_width , optimal_width , field_widths , x . field_names ) <EOL> shrink_to = shrink_remaining // len ( shrink_fields ) <EOL> for field in shrink_fields [ : - <NUM_LIT:1> ] : <EOL> x . max_width [ field ] = max ( min_width , shrink_to ) <EOL> shrink_remaining -= shrink_to <EOL> field = shrink_fields [ - <NUM_LIT:1> ] <EOL> x . max_width [ field ] = max ( min_width , shrink_remaining ) </s>
