<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> __version__ = "<STR_LIT>" <EOL> import codecs <EOL> import os <EOL> from pprint import pprint , pformat <EOL> import re <EOL> import sys <EOL> from collections import defaultdict <EOL> p = print <EOL> def e ( * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT:file>' ] = sys . stderr <EOL> p ( * args , ** kwargs ) <EOL> def tables_align_columns ( path ) : <EOL> def _table_sub ( match ) : <EOL> head , underline , body = match . groups ( ) <EOL> data_rows = [ <EOL> [ cell . strip ( ) for cell in head . strip ( ) . strip ( '<STR_LIT:|>' ) . split ( '<STR_LIT:|>' ) ] , <EOL> ] <EOL> for line in body . strip ( '<STR_LIT:\n>' ) . split ( '<STR_LIT:\n>' ) : <EOL> data_rows . append ( [ cell . strip ( ) for cell in line . strip ( ) . strip ( '<STR_LIT:|>' ) . split ( '<STR_LIT:|>' ) ] ) <EOL> width_from_col_idx = defaultdict ( int ) <EOL> for data_row in data_rows : <EOL> for col_idx , cell in enumerate ( data_row ) : <EOL> width_from_col_idx [ col_idx ] = max ( <EOL> <NUM_LIT:2> , width_from_col_idx [ col_idx ] , len ( cell ) ) <EOL> ucells = [ cell . strip ( ) for cell in underline . strip ( '<STR_LIT>' ) . split ( '<STR_LIT:|>' ) ] <EOL> align_from_col_idx = { } <EOL> for col_idx , cell in enumerate ( ucells ) : <EOL> if cell [ <NUM_LIT:0> ] == '<STR_LIT::>' and cell [ - <NUM_LIT:1> ] == '<STR_LIT::>' : <EOL> align_from_col_idx [ col_idx ] = '<STR_LIT>' <EOL> elif cell [ <NUM_LIT:0> ] == '<STR_LIT::>' : <EOL> align_from_col_idx [ col_idx ] = '<STR_LIT:left>' <EOL> elif cell [ - <NUM_LIT:1> ] == '<STR_LIT::>' : <EOL> align_from_col_idx [ col_idx ] = '<STR_LIT:right>' <EOL> else : <EOL> align_from_col_idx [ col_idx ] = None <EOL> table = [ ] <EOL> for data_row in data_rows : <EOL> row = [ ] <EOL> for col_idx , cell in enumerate ( data_row ) : <EOL> width = width_from_col_idx [ col_idx ] <EOL> try : <EOL> align = align_from_col_idx [ col_idx ] <EOL> except KeyError : <EOL> e ( '<STR_LIT>' <EOL> '<STR_LIT>' % match . group ( <NUM_LIT:0> ) ) <EOL> return match . group ( <NUM_LIT:0> ) <EOL> if align == '<STR_LIT>' : <EOL> space = width - len ( cell ) <EOL> left = space / <NUM_LIT:2> <EOL> right = space - left <EOL> row . append ( '<STR_LIT:U+0020>' * left + cell + '<STR_LIT:U+0020>' * right ) <EOL> elif align == '<STR_LIT:right>' : <EOL> row . append ( '<STR_LIT>' % width % cell ) <EOL> else : <EOL> row . append ( '<STR_LIT>' % width % cell ) <EOL> table . append ( row ) <EOL> underline = [ ] <EOL> for col_idx , cell in enumerate ( data_rows [ <NUM_LIT:0> ] ) : <EOL> width = width_from_col_idx [ col_idx ] <EOL> align = align_from_col_idx [ col_idx ] <EOL> if align == '<STR_LIT>' : <EOL> underline . append ( '<STR_LIT::>' + u'<STR_LIT:->' * ( width - <NUM_LIT:2> ) + '<STR_LIT::>' ) <EOL> elif align == '<STR_LIT:right>' : <EOL> underline . append ( u'<STR_LIT:->' * ( width - <NUM_LIT:1> ) + '<STR_LIT::>' ) <EOL> elif align == '<STR_LIT:left>' : <EOL> underline . append ( '<STR_LIT::>' + u'<STR_LIT:->' * ( width - <NUM_LIT:1> ) ) <EOL> else : <EOL> underline . append ( u'<STR_LIT:->' * width ) <EOL> table [ <NUM_LIT:1> : <NUM_LIT:1> ] = [ underline ] <EOL> table_str = u'<STR_LIT:\n>' . join ( ( '<STR_LIT>' + u'<STR_LIT>' . join ( r ) + '<STR_LIT>' ) for r in table ) <EOL> return table_str + '<STR_LIT:\n>' <EOL> text = codecs . open ( path , '<STR_LIT:rb>' , '<STR_LIT:utf8>' ) . read ( ) <EOL> less_than_tab = <NUM_LIT:3> <EOL> table_re = re . compile ( r'''<STR_LIT>''' % ( less_than_tab , less_than_tab , less_than_tab ) , re . M | re . X ) <EOL> return table_re . sub ( _table_sub , text ) <EOL> def main ( argv ) : <EOL> for path in argv [ <NUM_LIT:1> : ] : <EOL> text = tables_align_columns ( path ) <EOL> sys . stdout . write ( text . encode ( <EOL> sys . stdout . encoding or "<STR_LIT:utf-8>" , '<STR_LIT>' ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
