<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from builtins import object , range <EOL> import re <EOL> import copy <EOL> from evennia . utils . evtable import EvCell , EvTable <EOL> from evennia . utils . utils import all_from_module , to_str , to_unicode <EOL> from evennia . utils . ansi import ANSIString <EOL> INVALID_FORMCHARS = r"<STR_LIT>" <EOL> _ANSI_ESCAPE = re . compile ( r"<STR_LIT>" ) <EOL> def _to_ansi ( obj , regexable = False ) : <EOL> "<STR_LIT>" <EOL> if isinstance ( obj , basestring ) : <EOL> obj = _ANSI_ESCAPE . sub ( r"<STR_LIT>" , obj ) <EOL> if isinstance ( obj , dict ) : <EOL> return dict ( ( key , _to_ansi ( value , regexable = regexable ) ) for key , value in obj . items ( ) ) <EOL> elif hasattr ( obj , "<STR_LIT>" ) : <EOL> return [ _to_ansi ( o ) for o in obj ] <EOL> else : <EOL> return ANSIString ( to_unicode ( obj ) , regexable = regexable ) <EOL> class EvForm ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename = None , cells = None , tables = None , form = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . filename = filename <EOL> self . input_form_dict = form <EOL> self . cells_mapping = dict ( ( to_str ( key , force_string = True ) , value ) for key , value in cells . items ( ) ) if cells else { } <EOL> self . tables_mapping = dict ( ( to_str ( key , force_string = True ) , value ) for key , value in tables . items ( ) ) if tables else { } <EOL> self . cellchar = "<STR_LIT:x>" <EOL> self . tablechar = "<STR_LIT:c>" <EOL> self . raw_form = [ ] <EOL> self . form = [ ] <EOL> kwargs . pop ( "<STR_LIT>" , None ) <EOL> kwargs . pop ( "<STR_LIT:width>" , None ) <EOL> kwargs . pop ( "<STR_LIT>" , None ) <EOL> self . options = kwargs <EOL> self . reload ( ) <EOL> def _parse_rectangles ( self , cellchar , tablechar , form , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> custom_options = copy . copy ( self . options ) <EOL> custom_options . update ( kwargs ) <EOL> nform = len ( form ) <EOL> mapping = { } <EOL> cell_coords = { } <EOL> table_coords = { } <EOL> re_cellchar = re . compile ( r"<STR_LIT>" % ( cellchar , INVALID_FORMCHARS , cellchar , cellchar ) ) <EOL> re_tablechar = re . compile ( r"<STR_LIT>" % ( tablechar , INVALID_FORMCHARS , tablechar , tablechar ) ) <EOL> for iy , line in enumerate ( _to_ansi ( form , regexable = True ) ) : <EOL> ix0 = <NUM_LIT:0> <EOL> while True : <EOL> match = re_cellchar . search ( line , ix0 ) <EOL> if match : <EOL> cell_coords [ match . group ( <NUM_LIT:1> ) ] = [ iy , match . start ( ) , match . end ( ) ] <EOL> ix0 = match . end ( ) <EOL> else : <EOL> break <EOL> ix0 = <NUM_LIT:0> <EOL> while True : <EOL> match = re_tablechar . search ( line , ix0 ) <EOL> if match : <EOL> table_coords [ match . group ( <NUM_LIT:1> ) ] = [ iy , match . start ( ) , match . end ( ) ] <EOL> ix0 = match . end ( ) <EOL> else : <EOL> break <EOL> for key , ( iy , leftix , rightix ) in cell_coords . items ( ) : <EOL> dy_up = <NUM_LIT:0> <EOL> if iy > <NUM_LIT:0> : <EOL> for i in range ( <NUM_LIT:1> , iy ) : <EOL> if all ( form [ iy - i ] [ ix ] == cellchar for ix in range ( leftix , rightix ) ) : <EOL> dy_up += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> dy_down = <NUM_LIT:0> <EOL> if iy < nform - <NUM_LIT:1> : <EOL> for i in range ( <NUM_LIT:1> , nform - iy - <NUM_LIT:1> ) : <EOL> if all ( form [ iy + i ] [ ix ] == cellchar for ix in range ( leftix , rightix ) ) : <EOL> dy_down += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> iyup = iy - dy_up <EOL> iydown = iy + dy_down <EOL> width = rightix - leftix <EOL> height = abs ( iyup - iydown ) + <NUM_LIT:1> <EOL> data = self . cells_mapping . get ( key , "<STR_LIT>" ) <EOL> options = { "<STR_LIT>" : <NUM_LIT:0> , "<STR_LIT>" : <NUM_LIT:0> , "<STR_LIT>" : <NUM_LIT:0> , "<STR_LIT>" : <NUM_LIT:0> , "<STR_LIT>" : "<STR_LIT:l>" , "<STR_LIT>" : "<STR_LIT:t>" , "<STR_LIT>" : True } <EOL> options . update ( custom_options ) <EOL> mapping [ key ] = ( iyup , leftix , width , height , EvCell ( data , width = width , height = height , ** options ) ) <EOL> for key , ( iy , leftix , rightix ) in table_coords . items ( ) : <EOL> dy_up = <NUM_LIT:0> <EOL> if iy > <NUM_LIT:0> : <EOL> for i in range ( <NUM_LIT:1> , iy ) : <EOL> if all ( form [ iy - i ] [ ix ] == tablechar for ix in range ( leftix , rightix ) ) : <EOL> dy_up += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> dy_down = <NUM_LIT:0> <EOL> if iy < nform - <NUM_LIT:1> : <EOL> for i in range ( <NUM_LIT:1> , nform - iy - <NUM_LIT:1> ) : <EOL> if all ( form [ iy + i ] [ ix ] == tablechar for ix in range ( leftix , rightix ) ) : <EOL> dy_down += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> iyup = iy - dy_up <EOL> iydown = iy + dy_down <EOL> width = rightix - leftix <EOL> height = abs ( iyup - iydown ) + <NUM_LIT:1> <EOL> table = self . tables_mapping . get ( key , None ) <EOL> options = { "<STR_LIT>" : <NUM_LIT:0> , "<STR_LIT>" : <NUM_LIT:0> , "<STR_LIT>" : <NUM_LIT:0> , "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : "<STR_LIT:l>" , "<STR_LIT>" : "<STR_LIT:t>" , "<STR_LIT>" : True } <EOL> options . update ( custom_options ) <EOL> if table : <EOL> table . reformat ( width = width , height = height , ** options ) <EOL> else : <EOL> table = EvTable ( width = width , height = height , ** options ) <EOL> mapping [ key ] = ( iyup , leftix , width , height , table ) <EOL> return mapping <EOL> def _populate_form ( self , raw_form , mapping ) : <EOL> """<STR_LIT>""" <EOL> form = copy . copy ( raw_form ) <EOL> for key , ( iy0 , ix0 , width , height , cell_or_table ) in mapping . items ( ) : <EOL> rect = cell_or_table . get ( ) <EOL> for il , rectline in enumerate ( rect ) : <EOL> formline = form [ iy0 + il ] <EOL> form [ iy0 + il ] = formline = formline [ : ix0 ] + rectline + formline [ ix0 + width : ] <EOL> return form <EOL> def map ( self , cells = None , tables = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs . pop ( "<STR_LIT>" , None ) <EOL> kwargs . pop ( "<STR_LIT:width>" , None ) <EOL> kwargs . pop ( "<STR_LIT>" , None ) <EOL> new_cells = dict ( ( to_str ( key , force_string = True ) , value ) for key , value in cells . items ( ) ) if cells else { } <EOL> new_tables = dict ( ( to_str ( key , force_string = True ) , value ) for key , value in tables . items ( ) ) if tables else { } <EOL> self . cells_mapping . update ( new_cells ) <EOL> self . tables_mapping . update ( new_tables ) <EOL> self . reload ( ) <EOL> def reload ( self , filename = None , form = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs . pop ( "<STR_LIT>" , None ) <EOL> kwargs . pop ( "<STR_LIT:width>" , None ) <EOL> kwargs . pop ( "<STR_LIT>" , None ) <EOL> if form or self . input_form_dict : <EOL> datadict = form if form else self . input_form_dict <EOL> self . input_form_dict = datadict <EOL> elif filename or self . filename : <EOL> filename = filename if filename else self . filename <EOL> datadict = all_from_module ( filename ) <EOL> self . filename = filename <EOL> else : <EOL> datadict = { } <EOL> cellchar = to_str ( datadict . get ( "<STR_LIT>" , "<STR_LIT:x>" ) ) <EOL> self . cellchar = to_str ( cellchar [ <NUM_LIT:0> ] if len ( cellchar ) > <NUM_LIT:1> else cellchar ) <EOL> tablechar = datadict . get ( "<STR_LIT>" , "<STR_LIT:c>" ) <EOL> self . tablechar = tablechar [ <NUM_LIT:0> ] if len ( tablechar ) > <NUM_LIT:1> else tablechar <EOL> self . raw_form = _to_ansi ( to_unicode ( datadict . get ( "<STR_LIT>" , "<STR_LIT>" ) ) . split ( "<STR_LIT:\n>" ) ) <EOL> self . raw_form = self . raw_form [ <NUM_LIT:1> : ] if self . raw_form else self . raw_form <EOL> self . options . update ( kwargs ) <EOL> self . mapping = self . _parse_rectangles ( self . cellchar , self . tablechar , self . raw_form , ** kwargs ) <EOL> self . form = self . _populate_form ( self . raw_form , self . mapping ) <EOL> def __str__ ( self ) : <EOL> "<STR_LIT>" <EOL> return ANSIString ( "<STR_LIT:\n>" ) . join ( [ line for line in self . form ] ) <EOL> def __unicode__ ( self ) : <EOL> "<STR_LIT>" <EOL> return unicode ( ANSIString ( "<STR_LIT:\n>" ) . join ( [ line for line in self . form ] ) ) <EOL> def _test ( ) : <EOL> "<STR_LIT>" <EOL> form = EvForm ( "<STR_LIT>" ) <EOL> form . map ( cells = { <NUM_LIT:1> : "<STR_LIT>" , <EOL> <NUM_LIT:2> : "<STR_LIT>" , <EOL> <NUM_LIT:3> : "<STR_LIT>" , <EOL> <NUM_LIT:4> : <NUM_LIT:12> , <EOL> <NUM_LIT:5> : <NUM_LIT:10> , <EOL> <NUM_LIT:6> : <NUM_LIT:5> , <EOL> <NUM_LIT:7> : <NUM_LIT> , <EOL> <NUM_LIT:8> : <NUM_LIT:10> , <EOL> <NUM_LIT:9> : <NUM_LIT:3> } ) <EOL> tableA = EvTable ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> table = [ [ "<STR_LIT>" ] , [ "<STR_LIT>" ] , [ "<STR_LIT>" ] ] , <EOL> border = "<STR_LIT>" ) <EOL> tableB = EvTable ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> table = [ [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT:9> ] , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ] , <EOL> border = "<STR_LIT>" ) <EOL> form . map ( tables = { "<STR_LIT:A>" : tableA , <EOL> "<STR_LIT:B>" : tableB } ) <EOL> return form </s>
