<s> import operator <EOL> import sys <EOL> try : <EOL> import prettytable <EOL> except ImportError : <EOL> pass <EOL> from requestbuilder import Arg <EOL> class TabifyingMixin ( object ) : <EOL> '''<STR_LIT>''' <EOL> ARGS = [ Arg ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , route_to = None , <EOL> help = '<STR_LIT>' ) ] <EOL> def tabify ( self , fields , include = None ) : <EOL> """<STR_LIT>""" <EOL> if self . args [ '<STR_LIT>' ] : <EOL> fstr = '<STR_LIT>' <EOL> else : <EOL> fstr = '<STR_LIT>' <EOL> return '<STR_LIT:\t>' . join ( str ( s ) for s in <EOL> _filter_row_values ( fields , fstr , include = include ) ) <EOL> class TableOutputMixin ( object ) : <EOL> ARGS = [ Arg ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , route_to = None , <EOL> help = '<STR_LIT>' ) , <EOL> Arg ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , route_to = None , <EOL> help = '<STR_LIT>' ) ] <EOL> def get_table ( self , field_names ) : <EOL> table = _FilteredTable ( field_names = field_names , <EOL> show_empty = self . args . get ( '<STR_LIT>' ) ) <EOL> table . border = False <EOL> table . header = self . args . get ( '<STR_LIT>' ) or False <EOL> table . header_style = '<STR_LIT>' <EOL> table . align = '<STR_LIT:l>' <EOL> table . left_padding_width = <NUM_LIT:0> <EOL> table . right_padding_width = <NUM_LIT:2> <EOL> return table <EOL> if '<STR_LIT>' in sys . modules : <EOL> class _FilteredTable ( prettytable . PrettyTable ) : <EOL> def __init__ ( self , show_empty = False , ** kwargs ) : <EOL> if show_empty : <EOL> self . __empty = '<STR_LIT>' <EOL> else : <EOL> self . __empty = '<STR_LIT>' <EOL> prettytable . PrettyTable . __init__ ( self , ** kwargs ) <EOL> def add_row ( self , row ) : <EOL> prettytable . PrettyTable . add_row ( <EOL> self , _filter_row_values ( row , self . __empty ) ) <EOL> else : <EOL> class _FilteredTable ( object ) : <EOL> def __init__ ( self , field_names , show_empty = False ) : <EOL> self . field_names = field_names <EOL> self . header = False <EOL> self . reversesort = False <EOL> self . _rows = [ ] <EOL> self . _sortindex = <NUM_LIT:0> <EOL> if show_empty : <EOL> self . __empty = '<STR_LIT>' <EOL> else : <EOL> self . __empty = '<STR_LIT>' <EOL> def add_row ( self , row ) : <EOL> if len ( row ) != len ( self . field_names ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( len ( row ) , len ( self . field_names ) ) ) <EOL> self . _rows . append ( _filter_row_values ( row , self . __empty ) ) <EOL> @ property <EOL> def sortby ( self ) : <EOL> return self . field_names [ self . _sortindex ] <EOL> @ sortby . setter <EOL> def sortby ( self , field ) : <EOL> self . _sortindex = self . field_names . index ( field ) <EOL> def get_string ( self ) : <EOL> lines = [ ] <EOL> if self . header : <EOL> lines . append ( '<STR_LIT:\t>' . join ( name . upper ( ) for name in <EOL> self . field_names ) ) <EOL> for row in sorted ( self . _rows , reverse = self . reversesort , <EOL> key = operator . itemgetter ( self . _sortindex ) ) : <EOL> lines . append ( '<STR_LIT:\t>' . join ( map ( str , row ) ) ) <EOL> return '<STR_LIT:\n>' . join ( lines ) <EOL> def __str__ ( self ) : <EOL> return self . get_string ( ) <EOL> def _filter_row_values ( row , empty_str , include = None ) : <EOL> filtered = [ ] <EOL> for field in row : <EOL> if ( field or field is <NUM_LIT:0> or ( isinstance ( field , float ) and field == <NUM_LIT:0> ) <EOL> or field in ( include or [ ] ) ) : <EOL> filtered . append ( field ) <EOL> else : <EOL> filtered . append ( empty_str ) <EOL> return filtered </s>
