<s> import copy <EOL> import math <EOL> import random <EOL> import re <EOL> import textwrap <EOL> import unicodedata <EOL> from . _compact import py3k , itermap , uni_chr , escape <EOL> from . _compact import str_types <EOL> from . _compact import unicode_ , basestring_ <EOL> FRAME = <NUM_LIT:0> <EOL> ALL = <NUM_LIT:1> <EOL> NONE = <NUM_LIT:2> <EOL> HEADER = <NUM_LIT:3> <EOL> DEFAULT = <NUM_LIT:10> <EOL> MSWORD_FRIENDLY = <NUM_LIT:11> <EOL> PLAIN_COLUMNS = <NUM_LIT:12> <EOL> RANDOM = <NUM_LIT:20> <EOL> _re = re . compile ( "<STR_LIT>" ) <EOL> def _get_size ( text ) : <EOL> lines = text . split ( "<STR_LIT:\n>" ) <EOL> height = len ( lines ) <EOL> width = max ( [ _str_block_width ( line ) for line in lines ] ) <EOL> return width , height <EOL> class PrettyTable ( object ) : <EOL> def __init__ ( self , field_names = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . encoding = kwargs . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . _field_names = [ ] <EOL> self . _rows = [ ] <EOL> self . align = { } <EOL> self . valign = { } <EOL> self . max_width = { } <EOL> self . min_width = { } <EOL> self . int_format = { } <EOL> self . float_format = { } <EOL> if field_names : <EOL> self . field_names = field_names <EOL> else : <EOL> self . _widths = [ ] <EOL> self . _options = "<STR_LIT>" . split ( ) <EOL> self . _options . extend ( <EOL> "<STR_LIT>" . split ( ) ) <EOL> self . _options . extend ( <EOL> "<STR_LIT>" . split ( ) ) <EOL> self . _options . extend ( "<STR_LIT>" . split ( ) ) <EOL> for option in self . _options : <EOL> if option in kwargs : <EOL> self . _validate_option ( option , kwargs [ option ] ) <EOL> else : <EOL> kwargs [ option ] = None <EOL> self . _title = kwargs [ "<STR_LIT:title>" ] or None <EOL> self . _start = kwargs [ "<STR_LIT:start>" ] or <NUM_LIT:0> <EOL> self . _end = kwargs [ "<STR_LIT:end>" ] or None <EOL> self . _fields = kwargs [ "<STR_LIT>" ] or None <EOL> if kwargs [ "<STR_LIT>" ] in ( True , False ) : <EOL> self . _header = kwargs [ "<STR_LIT>" ] <EOL> else : <EOL> self . _header = True <EOL> self . _header_style = kwargs [ "<STR_LIT>" ] or None <EOL> if kwargs [ "<STR_LIT>" ] in ( True , False ) : <EOL> self . _border = kwargs [ "<STR_LIT>" ] <EOL> else : <EOL> self . _border = True <EOL> self . _hrules = kwargs [ "<STR_LIT>" ] or FRAME <EOL> self . _vrules = kwargs [ "<STR_LIT>" ] or ALL <EOL> self . _sortby = kwargs [ "<STR_LIT>" ] or None <EOL> if kwargs [ "<STR_LIT>" ] in ( True , False ) : <EOL> self . _reversesort = kwargs [ "<STR_LIT>" ] <EOL> else : <EOL> self . _reversesort = False <EOL> self . _sort_key = kwargs [ "<STR_LIT>" ] or ( lambda x : x ) <EOL> self . align = kwargs [ "<STR_LIT>" ] or { } <EOL> self . valign = kwargs [ "<STR_LIT>" ] or { } <EOL> self . max_width = kwargs [ "<STR_LIT>" ] or { } <EOL> self . min_width = kwargs [ "<STR_LIT>" ] or { } <EOL> self . int_format = kwargs [ "<STR_LIT>" ] or { } <EOL> self . float_format = kwargs [ "<STR_LIT>" ] or { } <EOL> self . _min_table_width = kwargs [ "<STR_LIT>" ] or None <EOL> self . _max_table_width = kwargs [ "<STR_LIT>" ] or None <EOL> self . _padding_width = kwargs [ "<STR_LIT>" ] or <NUM_LIT:1> <EOL> self . _left_padding_width = kwargs [ "<STR_LIT>" ] or None <EOL> self . _right_padding_width = kwargs [ "<STR_LIT>" ] or None <EOL> self . _vertical_char = kwargs [ "<STR_LIT>" ] or self . _unicode ( "<STR_LIT:|>" ) <EOL> self . _horizontal_char = kwargs [ "<STR_LIT>" ] or self . _unicode ( "<STR_LIT:->" ) <EOL> self . _junction_char = kwargs [ "<STR_LIT>" ] or self . _unicode ( "<STR_LIT:+>" ) <EOL> if kwargs [ "<STR_LIT>" ] in ( True , False ) : <EOL> self . _print_empty = kwargs [ "<STR_LIT>" ] <EOL> else : <EOL> self . _print_empty = True <EOL> if kwargs [ "<STR_LIT>" ] in ( True , False ) : <EOL> self . _oldsortslice = kwargs [ "<STR_LIT>" ] <EOL> else : <EOL> self . _oldsortslice = False <EOL> self . _format = kwargs [ "<STR_LIT>" ] or False <EOL> self . _xhtml = kwargs [ "<STR_LIT>" ] or False <EOL> self . _attributes = kwargs [ "<STR_LIT>" ] or { } <EOL> def _unicode ( self , value ) : <EOL> if not isinstance ( value , basestring_ ) : <EOL> value = str ( value ) <EOL> if not isinstance ( value , unicode_ ) : <EOL> value = unicode_ ( value , self . encoding , "<STR_LIT:strict>" ) <EOL> return value <EOL> def _justify ( self , text , width , align ) : <EOL> excess = width - _str_block_width ( text ) <EOL> if align == "<STR_LIT:l>" : <EOL> return text + excess * "<STR_LIT:U+0020>" <EOL> elif align == "<STR_LIT:r>" : <EOL> return excess * "<STR_LIT:U+0020>" + text <EOL> else : <EOL> if excess % <NUM_LIT:2> : <EOL> if _str_block_width ( text ) % <NUM_LIT:2> : <EOL> return ( excess // <NUM_LIT:2> ) * "<STR_LIT:U+0020>" + text + ( excess // <NUM_LIT:2> + <NUM_LIT:1> ) * "<STR_LIT:U+0020>" <EOL> else : <EOL> return ( excess // <NUM_LIT:2> + <NUM_LIT:1> ) * "<STR_LIT:U+0020>" + text + ( excess // <NUM_LIT:2> ) * "<STR_LIT:U+0020>" <EOL> else : <EOL> return ( excess // <NUM_LIT:2> ) * "<STR_LIT:U+0020>" + text + ( excess // <NUM_LIT:2> ) * "<STR_LIT:U+0020>" <EOL> def __getattr__ ( self , name ) : <EOL> if name == "<STR_LIT>" : <EOL> return len ( self . _rows ) <EOL> elif name == "<STR_LIT>" : <EOL> if self . _field_names : <EOL> return len ( self . _field_names ) <EOL> elif self . _rows : <EOL> return len ( self . _rows [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> raise AttributeError ( name ) <EOL> def __getitem__ ( self , index ) : <EOL> new = PrettyTable ( ) <EOL> new . field_names = self . field_names <EOL> for attr in self . _options : <EOL> setattr ( new , "<STR_LIT:_>" + attr , getattr ( self , "<STR_LIT:_>" + attr ) ) <EOL> setattr ( new , "<STR_LIT>" , getattr ( self , "<STR_LIT>" ) ) <EOL> if isinstance ( index , slice ) : <EOL> for row in self . _rows [ index ] : <EOL> new . add_row ( row ) <EOL> elif isinstance ( index , int ) : <EOL> new . add_row ( self . _rows [ index ] ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % str ( index ) ) <EOL> return new <EOL> if py3k : <EOL> def __str__ ( self ) : <EOL> return self . __unicode__ ( ) <EOL> else : <EOL> def __str__ ( self ) : <EOL> return self . __unicode__ ( ) . encode ( self . encoding ) <EOL> def __unicode__ ( self ) : <EOL> return self . get_string ( ) <EOL> def _validate_option ( self , option , val ) : <EOL> if option in ( "<STR_LIT>" ) : <EOL> self . _validate_field_names ( val ) <EOL> elif option in ( "<STR_LIT:start>" , "<STR_LIT:end>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> self . _validate_nonnegative_int ( option , val ) <EOL> elif option in ( "<STR_LIT>" ) : <EOL> self . _validate_field_name ( option , val ) <EOL> elif option in ( "<STR_LIT>" ) : <EOL> self . _validate_function ( option , val ) <EOL> elif option in ( "<STR_LIT>" ) : <EOL> self . _validate_hrules ( option , val ) <EOL> elif option in ( "<STR_LIT>" ) : <EOL> self . _validate_vrules ( option , val ) <EOL> elif option in ( "<STR_LIT>" ) : <EOL> self . _validate_all_field_names ( option , val ) <EOL> elif option in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> self . _validate_true_or_false ( option , val ) <EOL> elif option in ( "<STR_LIT>" ) : <EOL> self . _validate_header_style ( val ) <EOL> elif option in ( "<STR_LIT>" ) : <EOL> self . _validate_int_format ( option , val ) <EOL> elif option in ( "<STR_LIT>" ) : <EOL> self . _validate_float_format ( option , val ) <EOL> elif option in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> self . _validate_single_char ( option , val ) <EOL> elif option in ( "<STR_LIT>" ) : <EOL> self . _validate_attributes ( option , val ) <EOL> def _validate_field_names ( self , val ) : <EOL> if self . _field_names : <EOL> try : <EOL> assert len ( val ) == len ( self . _field_names ) <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" % ( <EOL> len ( val ) , len ( self . _field_names ) ) ) <EOL> if self . _rows : <EOL> try : <EOL> assert len ( val ) == len ( self . _rows [ <NUM_LIT:0> ] ) <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" % ( <EOL> len ( val ) , len ( self . _rows [ <NUM_LIT:0> ] ) ) ) <EOL> try : <EOL> assert len ( val ) == len ( set ( val ) ) <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def _validate_header_style ( self , val ) : <EOL> try : <EOL> assert val in ( "<STR_LIT>" , "<STR_LIT:title>" , "<STR_LIT>" , "<STR_LIT>" , None ) <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def _validate_align ( self , val ) : <EOL> try : <EOL> assert val in [ "<STR_LIT:l>" , "<STR_LIT:c>" , "<STR_LIT:r>" ] <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" % val ) <EOL> def _validate_valign ( self , val ) : <EOL> try : <EOL> assert val in [ "<STR_LIT:t>" , "<STR_LIT:m>" , "<STR_LIT:b>" , None ] <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" % val ) <EOL> def _validate_nonnegative_int ( self , name , val ) : <EOL> try : <EOL> assert int ( val ) >= <NUM_LIT:0> <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" % ( name , self . _unicode ( val ) ) ) <EOL> def _validate_true_or_false ( self , name , val ) : <EOL> try : <EOL> assert val in ( True , False ) <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" % name ) <EOL> def _validate_int_format ( self , name , val ) : <EOL> if val == "<STR_LIT>" : <EOL> return <EOL> try : <EOL> assert type ( val ) in str_types <EOL> assert val . isdigit ( ) <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" % name ) <EOL> def _validate_float_format ( self , name , val ) : <EOL> if val == "<STR_LIT>" : <EOL> return <EOL> try : <EOL> val = val . rsplit ( '<STR_LIT:f>' ) [ <NUM_LIT:0> ] <EOL> assert type ( val ) in str_types <EOL> assert "<STR_LIT:.>" in val <EOL> bits = val . split ( "<STR_LIT:.>" ) <EOL> assert len ( bits ) <= <NUM_LIT:2> <EOL> assert bits [ <NUM_LIT:0> ] == "<STR_LIT>" or bits [ <NUM_LIT:0> ] . isdigit ( ) <EOL> assert bits [ <NUM_LIT:1> ] == "<STR_LIT>" or bits [ <NUM_LIT:1> ] . isdigit ( ) <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" % name ) <EOL> def _validate_function ( self , name , val ) : <EOL> try : <EOL> assert hasattr ( val , "<STR_LIT>" ) <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" % name ) <EOL> def _validate_hrules ( self , name , val ) : <EOL> try : <EOL> assert val in ( ALL , FRAME , HEADER , NONE ) <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" % name ) <EOL> def _validate_vrules ( self , name , val ) : <EOL> try : <EOL> assert val in ( ALL , FRAME , NONE ) <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" % name ) <EOL> def _validate_field_name ( self , name , val ) : <EOL> try : <EOL> assert ( val in self . _field_names ) or ( val is None ) <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" % val ) <EOL> def _validate_all_field_names ( self , name , val ) : <EOL> try : <EOL> for x in val : <EOL> self . _validate_field_name ( name , x ) <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def _validate_single_char ( self , name , val ) : <EOL> try : <EOL> assert _str_block_width ( val ) == <NUM_LIT:1> <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" % name ) <EOL> def _validate_attributes ( self , name , val ) : <EOL> try : <EOL> assert isinstance ( val , dict ) <EOL> except AssertionError : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> @ property <EOL> def field_names ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _field_names <EOL> @ field_names . setter <EOL> def field_names ( self , val ) : <EOL> val = [ self . _unicode ( x ) for x in val ] <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> if self . _field_names : <EOL> old_names = self . _field_names [ : ] <EOL> self . _field_names = val <EOL> if self . _align and old_names : <EOL> for old_name , new_name in zip ( old_names , val ) : <EOL> self . _align [ new_name ] = self . _align [ old_name ] <EOL> for old_name in old_names : <EOL> if old_name not in self . _align : <EOL> self . _align . pop ( old_name ) <EOL> else : <EOL> self . align = "<STR_LIT:c>" <EOL> if self . _valign and old_names : <EOL> for old_name , new_name in zip ( old_names , val ) : <EOL> self . _valign [ new_name ] = self . _valign [ old_name ] <EOL> for old_name in old_names : <EOL> if old_name not in self . _valign : <EOL> self . _valign . pop ( old_name ) <EOL> else : <EOL> self . valign = "<STR_LIT:t>" <EOL> @ property <EOL> def align ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _align <EOL> @ align . setter <EOL> def align ( self , val ) : <EOL> if not self . _field_names : <EOL> self . _align = { } <EOL> elif val is None or ( isinstance ( val , dict ) and len ( val ) is <NUM_LIT:0> ) : <EOL> for field in self . _field_names : <EOL> self . _align [ field ] = "<STR_LIT:c>" <EOL> else : <EOL> self . _validate_align ( val ) <EOL> for field in self . _field_names : <EOL> self . _align [ field ] = val <EOL> @ property <EOL> def valign ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _valign <EOL> @ valign . setter <EOL> def valign ( self , val ) : <EOL> if not self . _field_names : <EOL> self . _valign = { } <EOL> elif val is None or ( isinstance ( val , dict ) and len ( val ) is <NUM_LIT:0> ) : <EOL> for field in self . _field_names : <EOL> self . _valign [ field ] = "<STR_LIT:t>" <EOL> else : <EOL> self . _validate_valign ( val ) <EOL> for field in self . _field_names : <EOL> self . _valign [ field ] = val <EOL> @ property <EOL> def max_width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _max_width <EOL> @ max_width . setter <EOL> def max_width ( self , val ) : <EOL> if val is None or ( isinstance ( val , dict ) and len ( val ) is <NUM_LIT:0> ) : <EOL> self . _max_width = { } <EOL> else : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> for field in self . _field_names : <EOL> self . _max_width [ field ] = val <EOL> @ property <EOL> def min_width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _min_width <EOL> @ min_width . setter <EOL> def min_width ( self , val ) : <EOL> if val is None or ( isinstance ( val , dict ) and len ( val ) is <NUM_LIT:0> ) : <EOL> self . _min_width = { } <EOL> else : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> for field in self . _field_names : <EOL> self . _min_width [ field ] = val <EOL> @ property <EOL> def min_table_width ( self ) : <EOL> return self . _min_table_width <EOL> @ min_table_width . setter <EOL> def min_table_width ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _min_table_width = val <EOL> @ property <EOL> def max_table_width ( self ) : <EOL> return self . _max_table_width <EOL> @ max_table_width . setter <EOL> def max_table_width ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _max_table_width = val <EOL> @ property <EOL> def fields ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _fields <EOL> @ fields . setter <EOL> def fields ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _fields = val <EOL> @ property <EOL> def title ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _title <EOL> @ title . setter <EOL> def title ( self , val ) : <EOL> self . _title = self . _unicode ( val ) <EOL> @ property <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _start <EOL> @ start . setter <EOL> def start ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT:start>" , val ) <EOL> self . _start = val <EOL> @ property <EOL> def end ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _end <EOL> @ end . setter <EOL> def end ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT:end>" , val ) <EOL> self . _end = val <EOL> @ property <EOL> def sortby ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _sortby <EOL> @ sortby . setter <EOL> def sortby ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _sortby = val <EOL> @ property <EOL> def reversesort ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _reversesort <EOL> @ reversesort . setter <EOL> def reversesort ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _reversesort = val <EOL> @ property <EOL> def sort_key ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _sort_key <EOL> @ sort_key . setter <EOL> def sort_key ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _sort_key = val <EOL> @ property <EOL> def header ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _header <EOL> @ header . setter <EOL> def header ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _header = val <EOL> @ property <EOL> def header_style ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _header_style <EOL> @ header_style . setter <EOL> def header_style ( self , val ) : <EOL> self . _validate_header_style ( val ) <EOL> self . _header_style = val <EOL> @ property <EOL> def border ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _border <EOL> @ border . setter <EOL> def border ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _border = val <EOL> @ property <EOL> def hrules ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _hrules <EOL> @ hrules . setter <EOL> def hrules ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _hrules = val <EOL> @ property <EOL> def vrules ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _vrules <EOL> @ vrules . setter <EOL> def vrules ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _vrules = val <EOL> @ property <EOL> def int_format ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _int_format <EOL> @ int_format . setter <EOL> def int_format ( self , val ) : <EOL> if val is None or ( isinstance ( val , dict ) and len ( val ) is <NUM_LIT:0> ) : <EOL> self . _int_format = { } <EOL> else : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> for field in self . _field_names : <EOL> self . _int_format [ field ] = val <EOL> @ property <EOL> def float_format ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _float_format <EOL> @ float_format . setter <EOL> def float_format ( self , val ) : <EOL> if val is None or ( isinstance ( val , dict ) and len ( val ) is <NUM_LIT:0> ) : <EOL> self . _float_format = { } <EOL> else : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> for field in self . _field_names : <EOL> self . _float_format [ field ] = val <EOL> @ property <EOL> def padding_width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _padding_width <EOL> @ padding_width . setter <EOL> def padding_width ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _padding_width = val <EOL> @ property <EOL> def left_padding_width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _left_padding_width <EOL> @ left_padding_width . setter <EOL> def left_padding_width ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _left_padding_width = val <EOL> @ property <EOL> def right_padding_width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _right_padding_width <EOL> @ right_padding_width . setter <EOL> def right_padding_width ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _right_padding_width = val <EOL> @ property <EOL> def vertical_char ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _vertical_char <EOL> @ vertical_char . setter <EOL> def vertical_char ( self , val ) : <EOL> val = self . _unicode ( val ) <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _vertical_char = val <EOL> @ property <EOL> def horizontal_char ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _horizontal_char <EOL> @ horizontal_char . setter <EOL> def horizontal_char ( self , val ) : <EOL> val = self . _unicode ( val ) <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _horizontal_char = val <EOL> @ property <EOL> def junction_char ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _junction_char <EOL> @ junction_char . setter <EOL> def junction_char ( self , val ) : <EOL> val = self . _unicode ( val ) <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _junction_char = val <EOL> @ property <EOL> def format ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _format <EOL> @ format . setter <EOL> def format ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _format = val <EOL> @ property <EOL> def print_empty ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _print_empty <EOL> @ print_empty . setter <EOL> def print_empty ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _print_empty = val <EOL> @ property <EOL> def attributes ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _attributes <EOL> @ attributes . setter <EOL> def attributes ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _attributes = val <EOL> @ property <EOL> def oldsortslice ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _oldsortslice <EOL> @ oldsortslice . setter <EOL> def oldsortslice ( self , val ) : <EOL> self . _validate_option ( "<STR_LIT>" , val ) <EOL> self . _oldsortslice = val <EOL> def _get_options ( self , kwargs ) : <EOL> options = { } <EOL> for option in self . _options : <EOL> if option in kwargs : <EOL> self . _validate_option ( option , kwargs [ option ] ) <EOL> options [ option ] = kwargs [ option ] <EOL> else : <EOL> options [ option ] = getattr ( self , "<STR_LIT:_>" + option ) <EOL> return options <EOL> def set_style ( self , style ) : <EOL> if style == DEFAULT : <EOL> self . _set_default_style ( ) <EOL> elif style == MSWORD_FRIENDLY : <EOL> self . _set_msword_style ( ) <EOL> elif style == PLAIN_COLUMNS : <EOL> self . _set_columns_style ( ) <EOL> elif style == RANDOM : <EOL> self . _set_random_style ( ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def _set_default_style ( self ) : <EOL> self . header = True <EOL> self . border = True <EOL> self . _hrules = FRAME <EOL> self . _vrules = ALL <EOL> self . padding_width = <NUM_LIT:1> <EOL> self . left_padding_width = <NUM_LIT:1> <EOL> self . right_padding_width = <NUM_LIT:1> <EOL> self . vertical_char = "<STR_LIT:|>" <EOL> self . horizontal_char = "<STR_LIT:->" <EOL> self . junction_char = "<STR_LIT:+>" <EOL> def _set_msword_style ( self ) : <EOL> self . header = True <EOL> self . border = True <EOL> self . _hrules = NONE <EOL> self . padding_width = <NUM_LIT:1> <EOL> self . left_padding_width = <NUM_LIT:1> <EOL> self . right_padding_width = <NUM_LIT:1> <EOL> self . vertical_char = "<STR_LIT:|>" <EOL> def _set_columns_style ( self ) : <EOL> self . header = True <EOL> self . border = False <EOL> self . padding_width = <NUM_LIT:1> <EOL> self . left_padding_width = <NUM_LIT:0> <EOL> self . right_padding_width = <NUM_LIT:8> <EOL> def _set_random_style ( self ) : <EOL> self . header = random . choice ( ( True , False ) ) <EOL> self . border = random . choice ( ( True , False ) ) <EOL> self . _hrules = random . choice ( ( ALL , FRAME , HEADER , NONE ) ) <EOL> self . _vrules = random . choice ( ( ALL , FRAME , NONE ) ) <EOL> self . left_padding_width = random . randint ( <NUM_LIT:0> , <NUM_LIT:5> ) <EOL> self . right_padding_width = random . randint ( <NUM_LIT:0> , <NUM_LIT:5> ) <EOL> self . vertical_char = random . choice ( "<STR_LIT>" ) <EOL> self . horizontal_char = random . choice ( "<STR_LIT>" ) <EOL> self . junction_char = random . choice ( "<STR_LIT>" ) <EOL> def add_row ( self , row ) : <EOL> """<STR_LIT>""" <EOL> if self . _field_names and len ( row ) != len ( self . _field_names ) : <EOL> raise Exception ( <EOL> "<STR_LIT>" % ( len ( row ) , len ( self . _field_names ) ) ) <EOL> if not self . _field_names : <EOL> self . field_names = [ ( "<STR_LIT>" % ( n + <NUM_LIT:1> ) ) for n in range ( <NUM_LIT:0> , len ( row ) ) ] <EOL> self . _rows . append ( list ( row ) ) <EOL> def del_row ( self , row_index ) : <EOL> """<STR_LIT>""" <EOL> if row_index > len ( self . _rows ) - <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" % ( row_index , len ( self . _rows ) ) ) <EOL> del self . _rows [ row_index ] <EOL> def add_column ( self , fieldname , column , align = "<STR_LIT:c>" , valign = "<STR_LIT:t>" ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . _rows ) in ( <NUM_LIT:0> , len ( column ) ) : <EOL> self . _validate_align ( align ) <EOL> self . _validate_valign ( valign ) <EOL> self . _field_names . append ( fieldname ) <EOL> self . _align [ fieldname ] = align <EOL> self . _valign [ fieldname ] = valign <EOL> for i in range ( <NUM_LIT:0> , len ( column ) ) : <EOL> if len ( self . _rows ) < i + <NUM_LIT:1> : <EOL> self . _rows . append ( [ ] ) <EOL> self . _rows [ i ] . append ( column [ i ] ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % ( len ( column ) , len ( self . _rows ) ) ) <EOL> def clear_rows ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _rows = [ ] <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _rows = [ ] <EOL> self . _field_names = [ ] <EOL> self . _widths = [ ] <EOL> def copy ( self ) : <EOL> return copy . deepcopy ( self ) <EOL> def _format_value ( self , field , value ) : <EOL> if isinstance ( value , int ) and field in self . _int_format : <EOL> value = self . _unicode ( ( "<STR_LIT>" % self . _int_format [ field ] ) % value ) <EOL> elif isinstance ( value , float ) and field in self . _float_format : <EOL> value = self . _unicode ( ( "<STR_LIT>" % self . _float_format [ field ] ) % value ) <EOL> return self . _unicode ( value ) <EOL> def _compute_table_width ( self , options ) : <EOL> table_width = <NUM_LIT:2> if options [ "<STR_LIT>" ] in ( FRAME , ALL ) else <NUM_LIT:0> <EOL> per_col_padding = sum ( self . _get_padding_widths ( options ) ) <EOL> for index , fieldname in enumerate ( self . field_names ) : <EOL> if not options [ "<STR_LIT>" ] or ( options [ "<STR_LIT>" ] and fieldname in options [ "<STR_LIT>" ] ) : <EOL> table_width += self . _widths [ index ] + per_col_padding <EOL> return table_width <EOL> def _compute_widths ( self , rows , options ) : <EOL> if options [ "<STR_LIT>" ] : <EOL> widths = [ _get_size ( field ) [ <NUM_LIT:0> ] for field in self . _field_names ] <EOL> else : <EOL> widths = len ( self . field_names ) * [ <NUM_LIT:0> ] <EOL> for row in rows : <EOL> for index , value in enumerate ( row ) : <EOL> fieldname = self . field_names [ index ] <EOL> if fieldname in self . max_width : <EOL> widths [ index ] = max ( widths [ index ] , min ( _get_size ( value ) [ <NUM_LIT:0> ] , self . max_width [ fieldname ] ) ) <EOL> else : <EOL> widths [ index ] = max ( widths [ index ] , _get_size ( value ) [ <NUM_LIT:0> ] ) <EOL> if fieldname in self . min_width : <EOL> widths [ index ] = max ( widths [ index ] , self . min_width [ fieldname ] ) <EOL> self . _widths = widths <EOL> if self . _max_table_width : <EOL> table_width = self . _compute_table_width ( options ) <EOL> if table_width > self . _max_table_width : <EOL> scale = <NUM_LIT:1.0> * self . _max_table_width / table_width <EOL> widths = [ int ( math . floor ( w * scale ) ) for w in widths ] <EOL> self . _widths = widths <EOL> if self . _min_table_width or options [ "<STR_LIT:title>" ] : <EOL> if options [ "<STR_LIT:title>" ] : <EOL> title_width = len ( options [ "<STR_LIT:title>" ] ) + sum ( self . _get_padding_widths ( options ) ) <EOL> if options [ "<STR_LIT>" ] in ( FRAME , ALL ) : <EOL> title_width += <NUM_LIT:2> <EOL> else : <EOL> title_width = <NUM_LIT:0> <EOL> min_table_width = self . min_table_width or <NUM_LIT:0> <EOL> min_width = max ( title_width , min_table_width ) <EOL> table_width = self . _compute_table_width ( options ) <EOL> if table_width < min_width : <EOL> scale = <NUM_LIT:1.0> * min_width / table_width <EOL> widths = [ int ( math . ceil ( w * scale ) ) for w in widths ] <EOL> self . _widths = widths <EOL> def _get_padding_widths ( self , options ) : <EOL> if options [ "<STR_LIT>" ] is not None : <EOL> lpad = options [ "<STR_LIT>" ] <EOL> else : <EOL> lpad = options [ "<STR_LIT>" ] <EOL> if options [ "<STR_LIT>" ] is not None : <EOL> rpad = options [ "<STR_LIT>" ] <EOL> else : <EOL> rpad = options [ "<STR_LIT>" ] <EOL> return lpad , rpad <EOL> def _get_rows ( self , options ) : <EOL> """<STR_LIT>""" <EOL> if options [ "<STR_LIT>" ] : <EOL> rows = copy . deepcopy ( self . _rows [ options [ "<STR_LIT:start>" ] : options [ "<STR_LIT:end>" ] ] ) <EOL> else : <EOL> rows = copy . deepcopy ( self . _rows ) <EOL> if options [ "<STR_LIT>" ] : <EOL> sortindex = self . _field_names . index ( options [ "<STR_LIT>" ] ) <EOL> rows = [ [ row [ sortindex ] ] + row for row in rows ] <EOL> rows . sort ( reverse = options [ "<STR_LIT>" ] , key = options [ "<STR_LIT>" ] ) <EOL> rows = [ row [ <NUM_LIT:1> : ] for row in rows ] <EOL> if not options [ "<STR_LIT>" ] : <EOL> rows = rows [ options [ "<STR_LIT:start>" ] : options [ "<STR_LIT:end>" ] ] <EOL> return rows <EOL> def _format_row ( self , row , options ) : <EOL> return [ self . _format_value ( field , value ) for ( field , value ) in zip ( self . _field_names , row ) ] <EOL> def _format_rows ( self , rows , options ) : <EOL> return [ self . _format_row ( row , options ) for row in rows ] <EOL> def get_string ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> options = self . _get_options ( kwargs ) <EOL> lines = [ ] <EOL> if self . rowcount == <NUM_LIT:0> and ( not options [ "<STR_LIT>" ] or not options [ "<STR_LIT>" ] ) : <EOL> return "<STR_LIT>" <EOL> rows = self . _get_rows ( options ) <EOL> formatted_rows = self . _format_rows ( rows , options ) <EOL> self . _compute_widths ( formatted_rows , options ) <EOL> self . _hrule = self . _stringify_hrule ( options ) <EOL> title = options [ "<STR_LIT:title>" ] or self . _title <EOL> if title : <EOL> lines . append ( self . _stringify_title ( title , options ) ) <EOL> if options [ "<STR_LIT>" ] : <EOL> lines . append ( self . _stringify_header ( options ) ) <EOL> elif options [ "<STR_LIT>" ] and options [ "<STR_LIT>" ] in ( ALL , FRAME ) : <EOL> lines . append ( self . _hrule ) <EOL> for row in formatted_rows : <EOL> lines . append ( self . _stringify_row ( row , options ) ) <EOL> if options [ "<STR_LIT>" ] and options [ "<STR_LIT>" ] == FRAME : <EOL> lines . append ( self . _hrule ) <EOL> return self . _unicode ( "<STR_LIT:\n>" ) . join ( lines ) <EOL> def _stringify_hrule ( self , options ) : <EOL> if not options [ "<STR_LIT>" ] : <EOL> return "<STR_LIT>" <EOL> lpad , rpad = self . _get_padding_widths ( options ) <EOL> if options [ '<STR_LIT>' ] in ( ALL , FRAME ) : <EOL> bits = [ options [ "<STR_LIT>" ] ] <EOL> else : <EOL> bits = [ options [ "<STR_LIT>" ] ] <EOL> if not self . _field_names : <EOL> bits . append ( options [ "<STR_LIT>" ] ) <EOL> return "<STR_LIT>" . join ( bits ) <EOL> for field , width in zip ( self . _field_names , self . _widths ) : <EOL> if options [ "<STR_LIT>" ] and field not in options [ "<STR_LIT>" ] : <EOL> continue <EOL> bits . append ( ( width + lpad + rpad ) * options [ "<STR_LIT>" ] ) <EOL> if options [ '<STR_LIT>' ] == ALL : <EOL> bits . append ( options [ "<STR_LIT>" ] ) <EOL> else : <EOL> bits . append ( options [ "<STR_LIT>" ] ) <EOL> if options [ "<STR_LIT>" ] == FRAME : <EOL> bits . pop ( ) <EOL> bits . append ( options [ "<STR_LIT>" ] ) <EOL> return "<STR_LIT>" . join ( bits ) <EOL> def _stringify_title ( self , title , options ) : <EOL> lines = [ ] <EOL> lpad , rpad = self . _get_padding_widths ( options ) <EOL> if options [ "<STR_LIT>" ] : <EOL> if options [ "<STR_LIT>" ] == ALL : <EOL> options [ "<STR_LIT>" ] = FRAME <EOL> lines . append ( self . _stringify_hrule ( options ) ) <EOL> options [ "<STR_LIT>" ] = ALL <EOL> elif options [ "<STR_LIT>" ] == FRAME : <EOL> lines . append ( self . _stringify_hrule ( options ) ) <EOL> bits = [ ] <EOL> endpoint = options [ "<STR_LIT>" ] if options [ "<STR_LIT>" ] in ( ALL , FRAME ) else "<STR_LIT:U+0020>" <EOL> bits . append ( endpoint ) <EOL> title = "<STR_LIT:U+0020>" * lpad + title + "<STR_LIT:U+0020>" * rpad <EOL> bits . append ( self . _justify ( title , len ( self . _hrule ) - <NUM_LIT:2> , "<STR_LIT:c>" ) ) <EOL> bits . append ( endpoint ) <EOL> lines . append ( "<STR_LIT>" . join ( bits ) ) <EOL> return "<STR_LIT:\n>" . join ( lines ) <EOL> def _stringify_header ( self , options ) : <EOL> bits = [ ] <EOL> lpad , rpad = self . _get_padding_widths ( options ) <EOL> if options [ "<STR_LIT>" ] : <EOL> if options [ "<STR_LIT>" ] in ( ALL , FRAME ) : <EOL> bits . append ( self . _hrule ) <EOL> bits . append ( "<STR_LIT:\n>" ) <EOL> if options [ "<STR_LIT>" ] in ( ALL , FRAME ) : <EOL> bits . append ( options [ "<STR_LIT>" ] ) <EOL> else : <EOL> bits . append ( "<STR_LIT:U+0020>" ) <EOL> if not self . _field_names : <EOL> if options [ "<STR_LIT>" ] in ( ALL , FRAME ) : <EOL> bits . append ( options [ "<STR_LIT>" ] ) <EOL> else : <EOL> bits . append ( "<STR_LIT:U+0020>" ) <EOL> for field , width , in zip ( self . _field_names , self . _widths ) : <EOL> if options [ "<STR_LIT>" ] and field not in options [ "<STR_LIT>" ] : <EOL> continue <EOL> if self . _header_style == "<STR_LIT>" : <EOL> fieldname = field . capitalize ( ) <EOL> elif self . _header_style == "<STR_LIT:title>" : <EOL> fieldname = field . title ( ) <EOL> elif self . _header_style == "<STR_LIT>" : <EOL> fieldname = field . upper ( ) <EOL> elif self . _header_style == "<STR_LIT>" : <EOL> fieldname = field . lower ( ) <EOL> else : <EOL> fieldname = field <EOL> bits . append ( "<STR_LIT:U+0020>" * lpad + self . _justify ( fieldname , width , self . _align [ field ] ) + "<STR_LIT:U+0020>" * rpad ) <EOL> if options [ "<STR_LIT>" ] : <EOL> if options [ "<STR_LIT>" ] == ALL : <EOL> bits . append ( options [ "<STR_LIT>" ] ) <EOL> else : <EOL> bits . append ( "<STR_LIT:U+0020>" ) <EOL> if options [ "<STR_LIT>" ] and options [ "<STR_LIT>" ] == FRAME : <EOL> bits . pop ( ) <EOL> bits . append ( options [ "<STR_LIT>" ] ) <EOL> if options [ "<STR_LIT>" ] and options [ "<STR_LIT>" ] != NONE : <EOL> bits . append ( "<STR_LIT:\n>" ) <EOL> bits . append ( self . _hrule ) <EOL> return "<STR_LIT>" . join ( bits ) <EOL> def _stringify_row ( self , row , options ) : <EOL> for index , field , value , width , in zip ( range ( <NUM_LIT:0> , len ( row ) ) , self . _field_names , row , self . _widths ) : <EOL> lines = value . split ( "<STR_LIT:\n>" ) <EOL> new_lines = [ ] <EOL> for line in lines : <EOL> if _str_block_width ( line ) > width : <EOL> line = textwrap . fill ( line , width ) <EOL> new_lines . append ( line ) <EOL> lines = new_lines <EOL> value = "<STR_LIT:\n>" . join ( lines ) <EOL> row [ index ] = value <EOL> row_height = <NUM_LIT:0> <EOL> for c in row : <EOL> h = _get_size ( c ) [ <NUM_LIT:1> ] <EOL> if h > row_height : <EOL> row_height = h <EOL> bits = [ ] <EOL> lpad , rpad = self . _get_padding_widths ( options ) <EOL> for y in range ( <NUM_LIT:0> , row_height ) : <EOL> bits . append ( [ ] ) <EOL> if options [ "<STR_LIT>" ] : <EOL> if options [ "<STR_LIT>" ] in ( ALL , FRAME ) : <EOL> bits [ y ] . append ( self . vertical_char ) <EOL> else : <EOL> bits [ y ] . append ( "<STR_LIT:U+0020>" ) <EOL> for field , value , width , in zip ( self . _field_names , row , self . _widths ) : <EOL> valign = self . _valign [ field ] <EOL> lines = value . split ( "<STR_LIT:\n>" ) <EOL> dHeight = row_height - len ( lines ) <EOL> if dHeight : <EOL> if valign == "<STR_LIT:m>" : <EOL> lines = [ "<STR_LIT>" ] * int ( dHeight / <NUM_LIT:2> ) + lines + [ "<STR_LIT>" ] * ( dHeight - int ( dHeight / <NUM_LIT:2> ) ) <EOL> elif valign == "<STR_LIT:b>" : <EOL> lines = [ "<STR_LIT>" ] * dHeight + lines <EOL> else : <EOL> lines = lines + [ "<STR_LIT>" ] * dHeight <EOL> y = <NUM_LIT:0> <EOL> for l in lines : <EOL> if options [ "<STR_LIT>" ] and field not in options [ "<STR_LIT>" ] : <EOL> continue <EOL> bits [ y ] . append ( "<STR_LIT:U+0020>" * lpad + self . _justify ( l , width , self . _align [ field ] ) + "<STR_LIT:U+0020>" * rpad ) <EOL> if options [ "<STR_LIT>" ] : <EOL> if options [ "<STR_LIT>" ] == ALL : <EOL> bits [ y ] . append ( self . vertical_char ) <EOL> else : <EOL> bits [ y ] . append ( "<STR_LIT:U+0020>" ) <EOL> y += <NUM_LIT:1> <EOL> for y in range ( <NUM_LIT:0> , row_height ) : <EOL> if options [ "<STR_LIT>" ] and options [ "<STR_LIT>" ] == FRAME : <EOL> bits [ y ] . pop ( ) <EOL> bits [ y ] . append ( options [ "<STR_LIT>" ] ) <EOL> if options [ "<STR_LIT>" ] and options [ "<STR_LIT>" ] == ALL : <EOL> bits [ row_height - <NUM_LIT:1> ] . append ( "<STR_LIT:\n>" ) <EOL> bits [ row_height - <NUM_LIT:1> ] . append ( self . _hrule ) <EOL> for y in range ( <NUM_LIT:0> , row_height ) : <EOL> bits [ y ] = "<STR_LIT>" . join ( bits [ y ] ) <EOL> return "<STR_LIT:\n>" . join ( bits ) <EOL> def paginate ( self , page_length = <NUM_LIT> , ** kwargs ) : <EOL> pages = [ ] <EOL> kwargs [ "<STR_LIT:start>" ] = kwargs . get ( "<STR_LIT:start>" , <NUM_LIT:0> ) <EOL> true_end = kwargs . get ( "<STR_LIT:end>" , self . rowcount ) <EOL> while True : <EOL> kwargs [ "<STR_LIT:end>" ] = min ( kwargs [ "<STR_LIT:start>" ] + page_length , true_end ) <EOL> pages . append ( self . get_string ( ** kwargs ) ) <EOL> if kwargs [ "<STR_LIT:end>" ] == true_end : <EOL> break <EOL> kwargs [ "<STR_LIT:start>" ] += page_length <EOL> return "<STR_LIT>" . join ( pages ) <EOL> def get_html_string ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> options = self . _get_options ( kwargs ) <EOL> if options [ "<STR_LIT>" ] : <EOL> string = self . _get_formatted_html_string ( options ) <EOL> else : <EOL> string = self . _get_simple_html_string ( options ) <EOL> return string <EOL> def _get_simple_html_string ( self , options ) : <EOL> lines = [ ] <EOL> if options [ "<STR_LIT>" ] : <EOL> linebreak = "<STR_LIT>" <EOL> else : <EOL> linebreak = "<STR_LIT>" <EOL> open_tag = list ( ) <EOL> open_tag . append ( "<STR_LIT>" ) <EOL> if options [ "<STR_LIT>" ] : <EOL> for attr_name in options [ "<STR_LIT>" ] : <EOL> open_tag . append ( "<STR_LIT>" % ( attr_name , options [ "<STR_LIT>" ] [ attr_name ] ) ) <EOL> open_tag . append ( "<STR_LIT:>>" ) <EOL> lines . append ( "<STR_LIT>" . join ( open_tag ) ) <EOL> title = options [ "<STR_LIT:title>" ] or self . _title <EOL> if title : <EOL> cols = len ( options [ "<STR_LIT>" ] ) if options [ "<STR_LIT>" ] else len ( self . field_names ) <EOL> lines . append ( "<STR_LIT>" ) <EOL> lines . append ( "<STR_LIT>" % ( cols , title ) ) <EOL> lines . append ( "<STR_LIT>" ) <EOL> if options [ "<STR_LIT>" ] : <EOL> lines . append ( "<STR_LIT>" ) <EOL> for field in self . _field_names : <EOL> if options [ "<STR_LIT>" ] and field not in options [ "<STR_LIT>" ] : <EOL> continue <EOL> lines . append ( "<STR_LIT>" % escape ( field ) . replace ( "<STR_LIT:\n>" , linebreak ) ) <EOL> lines . append ( "<STR_LIT>" ) <EOL> rows = self . _get_rows ( options ) <EOL> formatted_rows = self . _format_rows ( rows , options ) <EOL> for row in formatted_rows : <EOL> lines . append ( "<STR_LIT>" ) <EOL> for field , datum in zip ( self . _field_names , row ) : <EOL> if options [ "<STR_LIT>" ] and field not in options [ "<STR_LIT>" ] : <EOL> continue <EOL> lines . append ( "<STR_LIT>" % escape ( datum ) . replace ( "<STR_LIT:\n>" , linebreak ) ) <EOL> lines . append ( "<STR_LIT>" ) <EOL> lines . append ( "<STR_LIT>" ) <EOL> return self . _unicode ( "<STR_LIT:\n>" ) . join ( lines ) <EOL> def _get_formatted_html_string ( self , options ) : <EOL> lines = [ ] <EOL> lpad , rpad = self . _get_padding_widths ( options ) <EOL> if options [ "<STR_LIT>" ] : <EOL> linebreak = "<STR_LIT>" <EOL> else : <EOL> linebreak = "<STR_LIT>" <EOL> open_tag = list ( ) <EOL> open_tag . append ( "<STR_LIT>" ) <EOL> if options [ "<STR_LIT>" ] : <EOL> if options [ "<STR_LIT>" ] == ALL and options [ "<STR_LIT>" ] == ALL : <EOL> open_tag . append ( "<STR_LIT>" ) <EOL> elif options [ "<STR_LIT>" ] == FRAME and options [ "<STR_LIT>" ] == FRAME : <EOL> open_tag . append ( "<STR_LIT>" ) <EOL> elif options [ "<STR_LIT>" ] == FRAME and options [ "<STR_LIT>" ] == ALL : <EOL> open_tag . append ( "<STR_LIT>" ) <EOL> elif options [ "<STR_LIT>" ] == FRAME : <EOL> open_tag . append ( "<STR_LIT>" ) <EOL> elif options [ "<STR_LIT>" ] == ALL : <EOL> open_tag . append ( "<STR_LIT>" ) <EOL> elif options [ "<STR_LIT>" ] == FRAME : <EOL> open_tag . append ( "<STR_LIT>" ) <EOL> elif options [ "<STR_LIT>" ] == ALL : <EOL> open_tag . append ( "<STR_LIT>" ) <EOL> if options [ "<STR_LIT>" ] : <EOL> for attr_name in options [ "<STR_LIT>" ] : <EOL> open_tag . append ( "<STR_LIT>" % ( attr_name , options [ "<STR_LIT>" ] [ attr_name ] ) ) <EOL> open_tag . append ( "<STR_LIT:>>" ) <EOL> lines . append ( "<STR_LIT>" . join ( open_tag ) ) <EOL> title = options [ "<STR_LIT:title>" ] or self . _title <EOL> if title : <EOL> cols = len ( options [ "<STR_LIT>" ] ) if options [ "<STR_LIT>" ] else len ( self . field_names ) <EOL> lines . append ( "<STR_LIT>" ) <EOL> lines . append ( "<STR_LIT>" % ( cols , title ) ) <EOL> lines . append ( "<STR_LIT>" ) <EOL> if options [ "<STR_LIT>" ] : <EOL> lines . append ( "<STR_LIT>" ) <EOL> for field in self . _field_names : <EOL> if options [ "<STR_LIT>" ] and field not in options [ "<STR_LIT>" ] : <EOL> continue <EOL> lines . append ( <EOL> "<STR_LIT>" % ( <EOL> lpad , rpad , escape ( field ) . replace ( "<STR_LIT:\n>" , linebreak ) ) ) <EOL> lines . append ( "<STR_LIT>" ) <EOL> rows = self . _get_rows ( options ) <EOL> formatted_rows = self . _format_rows ( rows , options ) <EOL> aligns = [ ] <EOL> valigns = [ ] <EOL> for field in self . _field_names : <EOL> aligns . append ( { "<STR_LIT:l>" : "<STR_LIT:left>" , "<STR_LIT:r>" : "<STR_LIT:right>" , "<STR_LIT:c>" : "<STR_LIT>" } [ self . _align [ field ] ] ) <EOL> valigns . append ( { "<STR_LIT:t>" : "<STR_LIT>" , "<STR_LIT:m>" : "<STR_LIT>" , "<STR_LIT:b>" : "<STR_LIT>" } [ self . _valign [ field ] ] ) <EOL> for row in formatted_rows : <EOL> lines . append ( "<STR_LIT>" ) <EOL> for field , datum , align , valign in zip ( self . _field_names , row , aligns , valigns ) : <EOL> if options [ "<STR_LIT>" ] and field not in options [ "<STR_LIT>" ] : <EOL> continue <EOL> lines . append ( <EOL> "<STR_LIT>" % ( <EOL> lpad , rpad , align , valign , escape ( datum ) . replace ( "<STR_LIT:\n>" , linebreak ) ) ) <EOL> lines . append ( "<STR_LIT>" ) <EOL> lines . append ( "<STR_LIT>" ) <EOL> return self . _unicode ( "<STR_LIT:\n>" ) . join ( lines ) <EOL> def _char_block_width ( char ) : <EOL> if <NUM_LIT> <= char <= <NUM_LIT> : <EOL> return <NUM_LIT:1> <EOL> if <NUM_LIT> <= char <= <NUM_LIT> : <EOL> return <NUM_LIT:2> <EOL> if <NUM_LIT> <= char <= <NUM_LIT> : <EOL> return <NUM_LIT:2> <EOL> if unicodedata . combining ( uni_chr ( char ) ) : <EOL> return <NUM_LIT:0> <EOL> if <NUM_LIT> <= char <= <NUM_LIT> or <NUM_LIT> <= char <= <NUM_LIT> : <EOL> return <NUM_LIT:2> <EOL> if <NUM_LIT> <= char <= <NUM_LIT> : <EOL> return <NUM_LIT:2> <EOL> if <NUM_LIT> <= char <= <NUM_LIT> : <EOL> return <NUM_LIT:2> <EOL> if char in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> return - <NUM_LIT:1> <EOL> elif char in ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) : <EOL> return <NUM_LIT:0> <EOL> return <NUM_LIT:1> <EOL> def _str_block_width ( val ) : <EOL> return sum ( itermap ( _char_block_width , itermap ( ord , _re . sub ( "<STR_LIT>" , val ) ) ) ) </s>
