<s> from __future__ import unicode_literals <EOL> r"""<STR_LIT>""" <EOL> import itertools <EOL> import re <EOL> import sys <EOL> import wcwidth <EOL> from . escseqparse import parse <EOL> from . termformatconstants import ( FG_COLORS , BG_COLORS , STYLES , <EOL> FG_NUMBER_TO_COLOR , BG_NUMBER_TO_COLOR , <EOL> RESET_ALL , RESET_BG , RESET_FG , <EOL> seq ) <EOL> PY3 = sys . version_info [ <NUM_LIT:0> ] >= <NUM_LIT:3> <EOL> if PY3 : <EOL> unicode = str <EOL> xforms = { <EOL> '<STR_LIT>' : lambda s , v : '<STR_LIT>' % ( seq ( v ) , s , seq ( RESET_FG ) ) , <EOL> '<STR_LIT>' : lambda s , v : seq ( v ) + s + seq ( RESET_BG ) , <EOL> '<STR_LIT>' : lambda s : seq ( STYLES [ '<STR_LIT>' ] ) + s + seq ( RESET_ALL ) , <EOL> '<STR_LIT>' : lambda s : seq ( STYLES [ '<STR_LIT>' ] ) + s + seq ( RESET_ALL ) , <EOL> '<STR_LIT>' : lambda s : seq ( STYLES [ '<STR_LIT>' ] ) + s + seq ( RESET_ALL ) , <EOL> '<STR_LIT>' : lambda s : seq ( STYLES [ '<STR_LIT>' ] ) + s + seq ( RESET_ALL ) , <EOL> } <EOL> class FrozenDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __setitem__ ( self , key , value ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def update ( self , dictlike ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def extend ( self , dictlike ) : <EOL> return FrozenDict ( itertools . chain ( self . items ( ) , dictlike . items ( ) ) ) <EOL> def remove ( self , * keys ) : <EOL> return FrozenDict ( ( k , v ) for k , v in self . items ( ) if k not in keys ) <EOL> class Chunk ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , string , atts = ( ) ) : <EOL> if not isinstance ( string , unicode ) : <EOL> raise ValueError ( "<STR_LIT>" % string ) <EOL> self . _s = string <EOL> self . _atts = FrozenDict ( atts ) <EOL> s = property ( lambda self : self . _s ) <EOL> atts = property ( lambda self : self . _atts , <EOL> None , None , <EOL> "<STR_LIT>" ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _s ) <EOL> @ property <EOL> def width ( self ) : <EOL> width = wcwidth . wcswidth ( self . _s ) <EOL> if len ( self . _s ) > <NUM_LIT:0> and width < <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" % self . _s ) <EOL> return width <EOL> @ property <EOL> def color_str ( self ) : <EOL> "<STR_LIT>" <EOL> s = self . s <EOL> for k , v in sorted ( self . atts . items ( ) ) : <EOL> assert k in xforms , "<STR_LIT>" <EOL> if v is False : <EOL> continue <EOL> elif v is True : <EOL> s = xforms [ k ] ( s ) <EOL> else : <EOL> s = xforms [ k ] ( s , v ) <EOL> return s <EOL> def __unicode__ ( self ) : <EOL> value = self . color_str <EOL> if isinstance ( value , bytes ) : <EOL> return value . decode ( '<STR_LIT:utf8>' , '<STR_LIT:replace>' ) <EOL> return value <EOL> def __eq__ ( self , other ) : <EOL> return self . s == other . s and self . atts == other . atts <EOL> if PY3 : <EOL> __str__ = __unicode__ <EOL> else : <EOL> def __str__ ( self ) : <EOL> return unicode ( self ) . encode ( '<STR_LIT:utf8>' ) <EOL> def __repr__ ( self ) : <EOL> def pp_att ( att ) : <EOL> if att == '<STR_LIT>' : return FG_NUMBER_TO_COLOR [ self . atts [ att ] ] <EOL> elif att == '<STR_LIT>' : return '<STR_LIT>' + BG_NUMBER_TO_COLOR [ self . atts [ att ] ] <EOL> else : return att <EOL> atts_out = dict ( ( k , v ) for ( k , v ) in self . atts . items ( ) if v ) <EOL> return ( '<STR_LIT>' . join ( pp_att ( att ) + '<STR_LIT:(>' for att in sorted ( atts_out ) ) <EOL> + ( repr ( self . s ) if PY3 else repr ( self . s ) [ <NUM_LIT:1> : ] ) + '<STR_LIT:)>' * len ( atts_out ) ) <EOL> class FmtStr ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * components ) : <EOL> self . basefmtstrs = list ( components ) <EOL> self . _str = None <EOL> self . _unicode = None <EOL> self . _len = None <EOL> self . _s = None <EOL> self . _width = None <EOL> @ classmethod <EOL> def from_str ( cls , s ) : <EOL> r"""<STR_LIT>""" <EOL> if '<STR_LIT>' in s : <EOL> tokens_and_strings = parse ( s ) <EOL> bases = [ ] <EOL> cur_fmt = { } <EOL> for x in tokens_and_strings : <EOL> if isinstance ( x , dict ) : <EOL> cur_fmt . update ( x ) <EOL> elif isinstance ( x , ( bytes , unicode ) ) : <EOL> atts = parse_args ( '<STR_LIT>' , dict ( ( k , v ) for k , v in cur_fmt . items ( ) if v is not None ) ) <EOL> bases . append ( Chunk ( x , atts = atts ) ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return FmtStr ( * bases ) <EOL> else : <EOL> return FmtStr ( Chunk ( s ) ) <EOL> def copy_with_new_str ( self , new_str ) : <EOL> """<STR_LIT>""" <EOL> old_atts = dict ( ( att , value ) for bfs in self . basefmtstrs <EOL> for ( att , value ) in bfs . atts . items ( ) ) <EOL> return FmtStr ( Chunk ( new_str , old_atts ) ) <EOL> def setitem ( self , startindex , fs ) : <EOL> """<STR_LIT>""" <EOL> return self . setslice_with_length ( startindex , startindex + <NUM_LIT:1> , fs , len ( self ) ) <EOL> def setslice_with_length ( self , startindex , endindex , fs , length ) : <EOL> """<STR_LIT>""" <EOL> if len ( self ) < startindex : <EOL> fs = '<STR_LIT:U+0020>' * ( startindex - len ( self ) ) + fs <EOL> if len ( self ) > endindex : <EOL> fs = fs + '<STR_LIT:U+0020>' * ( endindex - startindex - len ( fs ) ) <EOL> assert len ( fs ) == endindex - startindex , ( len ( fs ) , startindex , endindex ) <EOL> result = self . splice ( fs , startindex , endindex ) <EOL> assert len ( result ) <= length <EOL> return result <EOL> def splice ( self , new_str , start , end = None ) : <EOL> """<STR_LIT>""" <EOL> if len ( new_str ) == <NUM_LIT:0> : <EOL> return self <EOL> new_fs = new_str if isinstance ( new_str , FmtStr ) else fmtstr ( new_str ) <EOL> assert len ( new_fs . basefmtstrs ) > <NUM_LIT:0> , ( new_fs . basefmtstrs , new_fs ) <EOL> new_components = [ ] <EOL> inserted = False <EOL> if end is None : <EOL> end = start <EOL> tail = None <EOL> for bfs , bfs_start , bfs_end in zip ( self . basefmtstrs , <EOL> self . divides [ : - <NUM_LIT:1> ] , <EOL> self . divides [ <NUM_LIT:1> : ] ) : <EOL> if end == bfs_start == <NUM_LIT:0> : <EOL> new_components . extend ( new_fs . basefmtstrs ) <EOL> new_components . append ( bfs ) <EOL> inserted = True <EOL> elif bfs_start <= start < bfs_end : <EOL> divide = start - bfs_start <EOL> head = Chunk ( bfs . s [ : divide ] , atts = bfs . atts ) <EOL> tail = Chunk ( bfs . s [ end - bfs_start : ] , atts = bfs . atts ) <EOL> new_components . extend ( [ head ] + new_fs . basefmtstrs ) <EOL> inserted = True <EOL> if bfs_start < end < bfs_end : <EOL> tail = Chunk ( bfs . s [ end - bfs_start : ] , atts = bfs . atts ) <EOL> new_components . append ( tail ) <EOL> elif bfs_start < end < bfs_end : <EOL> divide = start - bfs_start <EOL> tail = Chunk ( bfs . s [ end - bfs_start : ] , atts = bfs . atts ) <EOL> new_components . append ( tail ) <EOL> elif bfs_start >= end or bfs_end <= start : <EOL> new_components . append ( bfs ) <EOL> if not inserted : <EOL> new_components . extend ( new_fs . basefmtstrs ) <EOL> inserted = True <EOL> return FmtStr ( * [ s for s in new_components if s . s ] ) <EOL> def append ( self , string ) : <EOL> return self . splice ( string , len ( self . s ) ) <EOL> def copy_with_new_atts ( self , ** attributes ) : <EOL> """<STR_LIT>""" <EOL> return FmtStr ( * [ Chunk ( bfs . s , bfs . atts . extend ( attributes ) ) <EOL> for bfs in self . basefmtstrs ] ) <EOL> def join ( self , iterable ) : <EOL> """<STR_LIT>""" <EOL> before = [ ] <EOL> basefmtstrs = [ ] <EOL> for i , s in enumerate ( iterable ) : <EOL> basefmtstrs . extend ( before ) <EOL> before = self . basefmtstrs <EOL> if isinstance ( s , FmtStr ) : <EOL> basefmtstrs . extend ( s . basefmtstrs ) <EOL> elif isinstance ( s , ( bytes , unicode ) ) : <EOL> basefmtstrs . extend ( fmtstr ( s ) . basefmtstrs ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % type ( s ) ) <EOL> return FmtStr ( * basefmtstrs ) <EOL> def split ( self , sep = None , maxsplit = None , regex = False ) : <EOL> """<STR_LIT>""" <EOL> if maxsplit is not None : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> s = self . s <EOL> if sep is None : <EOL> sep = r'<STR_LIT>' <EOL> elif not regex : <EOL> sep = re . escape ( sep ) <EOL> matches = list ( re . finditer ( sep , s ) ) <EOL> return [ self [ start : end ] for start , end in zip ( <EOL> [ <NUM_LIT:0> ] + [ m . end ( ) for m in matches ] , <EOL> [ m . start ( ) for m in matches ] + [ len ( s ) ] ) ] <EOL> def ljust ( self , width , fillchar = None ) : <EOL> """<STR_LIT>""" <EOL> if fillchar is not None : <EOL> return fmtstr ( self . s . ljust ( width , fillchar ) , ** self . shared_atts ) <EOL> to_add = '<STR_LIT:U+0020>' * ( width - len ( self . s ) ) <EOL> shared = self . shared_atts <EOL> if '<STR_LIT>' in shared : <EOL> return self + fmtstr ( to_add , bg = shared [ str ( '<STR_LIT>' ) ] ) if to_add else self <EOL> else : <EOL> uniform = self . new_with_atts_removed ( '<STR_LIT>' ) <EOL> return uniform + fmtstr ( to_add , ** self . shared_atts ) if to_add else uniform <EOL> def rjust ( self , width , fillchar = None ) : <EOL> """<STR_LIT>""" <EOL> if fillchar is not None : <EOL> return fmtstr ( self . s . rjust ( width , fillchar ) , ** self . shared_atts ) <EOL> to_add = '<STR_LIT:U+0020>' * ( width - len ( self . s ) ) <EOL> shared = self . shared_atts <EOL> if '<STR_LIT>' in shared : <EOL> return fmtstr ( to_add , bg = shared [ str ( '<STR_LIT>' ) ] ) + self if to_add else self <EOL> else : <EOL> uniform = self . new_with_atts_removed ( '<STR_LIT>' ) <EOL> return fmtstr ( to_add , ** self . shared_atts ) + uniform if to_add else uniform <EOL> def __unicode__ ( self ) : <EOL> if self . _unicode is not None : <EOL> return self . _unicode <EOL> self . _unicode = '<STR_LIT>' . join ( unicode ( fs ) for fs in self . basefmtstrs ) <EOL> return self . _unicode <EOL> if PY3 : <EOL> __str__ = __unicode__ <EOL> else : <EOL> def __str__ ( self ) : <EOL> if self . _str is not None : <EOL> return self . _str <EOL> self . _str = str ( '<STR_LIT>' ) . join ( str ( fs ) for fs in self . basefmtstrs ) <EOL> return self . _str <EOL> def __len__ ( self ) : <EOL> if self . _len is not None : <EOL> return self . _len <EOL> self . _len = sum ( len ( fs ) for fs in self . basefmtstrs ) <EOL> return self . _len <EOL> @ property <EOL> def width ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _width is not None : <EOL> return self . _width <EOL> self . _width = sum ( fs . width for fs in self . basefmtstrs ) <EOL> return self . _width <EOL> def width_at_offset ( self , n ) : <EOL> """<STR_LIT>""" <EOL> width = wcwidth . wcswidth ( self . s [ : n ] ) <EOL> assert width != - <NUM_LIT:1> <EOL> return width <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT:+>' . join ( repr ( fs ) for fs in self . basefmtstrs ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , ( unicode , bytes , FmtStr ) ) : <EOL> return str ( self ) == str ( other ) <EOL> return False <EOL> def __add__ ( self , other ) : <EOL> if isinstance ( other , FmtStr ) : <EOL> return FmtStr ( * ( self . basefmtstrs + other . basefmtstrs ) ) <EOL> elif isinstance ( other , ( bytes , unicode ) ) : <EOL> return FmtStr ( * ( self . basefmtstrs + [ Chunk ( other ) ] ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % ( self , other ) ) <EOL> def __radd__ ( self , other ) : <EOL> if isinstance ( other , FmtStr ) : <EOL> return FmtStr ( * ( x for x in ( other . basefmtstrs + self . basefmtstrs ) ) ) <EOL> elif isinstance ( other , ( bytes , unicode ) ) : <EOL> return FmtStr ( * ( x for x in ( [ Chunk ( other ) ] + self . basefmtstrs ) ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __mul__ ( self , other ) : <EOL> if isinstance ( other , int ) : <EOL> return sum ( [ self for _ in range ( other ) ] , FmtStr ( ) ) <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> @ property <EOL> def shared_atts ( self ) : <EOL> """<STR_LIT>""" <EOL> atts = { } <EOL> first = self . basefmtstrs [ <NUM_LIT:0> ] <EOL> for att in sorted ( first . atts ) : <EOL> if all ( fs . atts . get ( att , '<STR_LIT>' ) == first . atts [ att ] for fs in self . basefmtstrs if len ( fs ) > <NUM_LIT:0> ) : <EOL> atts [ att ] = first . atts [ att ] <EOL> return atts <EOL> def new_with_atts_removed ( self , * attributes ) : <EOL> """<STR_LIT>""" <EOL> return FmtStr ( * [ Chunk ( bfs . s , bfs . atts . remove ( * attributes ) ) <EOL> for bfs in self . basefmtstrs ] ) <EOL> def __getattr__ ( self , att ) : <EOL> if not hasattr ( self . s , att ) : <EOL> raise AttributeError ( "<STR_LIT>" % ( att , ) ) <EOL> def func_help ( * args , ** kwargs ) : <EOL> result = getattr ( self . s , att ) ( * args , ** kwargs ) <EOL> if isinstance ( result , ( bytes , unicode ) ) : <EOL> return fmtstr ( result , ** self . shared_atts ) <EOL> elif isinstance ( result , list ) : <EOL> return [ fmtstr ( x , ** self . shared_atts ) for x in result ] <EOL> else : <EOL> return result <EOL> return func_help <EOL> @ property <EOL> def divides ( self ) : <EOL> """<STR_LIT>""" <EOL> acc = [ <NUM_LIT:0> ] <EOL> for s in self . basefmtstrs : <EOL> acc . append ( acc [ - <NUM_LIT:1> ] + len ( s ) ) <EOL> return acc <EOL> @ property <EOL> def s ( self ) : <EOL> if self . _s is not None : <EOL> return self . _s <EOL> self . _s = "<STR_LIT>" . join ( fs . s for fs in self . basefmtstrs ) <EOL> return self . _s <EOL> def __getitem__ ( self , index ) : <EOL> index = normalize_slice ( len ( self ) , index ) <EOL> counter = <NUM_LIT:0> <EOL> parts = [ ] <EOL> for chunk in self . basefmtstrs : <EOL> if index . start < counter + len ( chunk ) and index . stop > counter : <EOL> start = max ( <NUM_LIT:0> , index . start - counter ) <EOL> end = min ( index . stop - counter , len ( chunk ) ) <EOL> if end - start == len ( chunk ) : <EOL> parts . append ( chunk ) <EOL> else : <EOL> s_part = chunk . s [ max ( <NUM_LIT:0> , index . start - counter ) : index . stop - counter ] <EOL> parts . append ( Chunk ( s_part , chunk . atts ) ) <EOL> counter += len ( chunk ) <EOL> if index . stop < counter : <EOL> break <EOL> return FmtStr ( * parts ) if parts else fmtstr ( '<STR_LIT>' ) <EOL> def width_aware_slice ( self , index ) : <EOL> """<STR_LIT>""" <EOL> if wcwidth . wcswidth ( self . s ) == - <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> index = normalize_slice ( self . width , index ) <EOL> counter = <NUM_LIT:0> <EOL> parts = [ ] <EOL> for chunk in self . basefmtstrs : <EOL> if index . start < counter + chunk . width and index . stop > counter : <EOL> start = max ( <NUM_LIT:0> , index . start - counter ) <EOL> end = min ( index . stop - counter , chunk . width ) <EOL> if end - start == chunk . width : <EOL> parts . append ( chunk ) <EOL> else : <EOL> s_part = width_aware_slice ( chunk . s , max ( <NUM_LIT:0> , index . start - counter ) , index . stop - counter ) <EOL> parts . append ( Chunk ( s_part , chunk . atts ) ) <EOL> counter += chunk . width <EOL> if index . stop < counter : <EOL> break <EOL> return FmtStr ( * parts ) if parts else fmtstr ( '<STR_LIT>' ) <EOL> def _getitem_normalized ( self , index ) : <EOL> """<STR_LIT>""" <EOL> index = normalize_slice ( len ( self ) , index ) <EOL> counter = <NUM_LIT:0> <EOL> output = '<STR_LIT>' <EOL> for fs in self . basefmtstrs : <EOL> if index . start < counter + len ( fs ) and index . stop > counter : <EOL> s_part = fs . s [ max ( <NUM_LIT:0> , index . start - counter ) : index . stop - counter ] <EOL> piece = Chunk ( s_part , fs . atts ) . color_str <EOL> output += piece <EOL> counter += len ( fs ) <EOL> if index . stop < counter : <EOL> break <EOL> return fmtstr ( output ) <EOL> def __setitem__ ( self , index , value ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def copy ( self ) : <EOL> return FmtStr ( * self . basefmtstrs ) <EOL> def interval_overlap ( a , b , x , y ) : <EOL> """<STR_LIT>""" <EOL> if b <= x or a >= y : <EOL> return <NUM_LIT:0> <EOL> elif x <= a <= y : <EOL> return min ( b , y ) - a <EOL> elif x <= b <= y : <EOL> return b - max ( a , x ) <EOL> elif a >= x and b <= y : <EOL> return b - a <EOL> else : <EOL> assert False <EOL> def width_aware_slice ( s , start , end , replacement_char = u'<STR_LIT:U+0020>' ) : <EOL> divides = [ wcwidth . wcswidth ( s , i ) for i in range ( len ( s ) + <NUM_LIT:1> ) ] <EOL> new_chunk_chars = [ ] <EOL> for char , char_start , char_end in zip ( s , divides [ : - <NUM_LIT:1> ] , divides [ <NUM_LIT:1> : ] ) : <EOL> if char_start >= start and char_end <= end : <EOL> new_chunk_chars . append ( char ) <EOL> else : <EOL> new_chunk_chars . extend ( replacement_char * interval_overlap ( char_start , char_end , start , end ) ) <EOL> return '<STR_LIT>' . join ( new_chunk_chars ) <EOL> def linesplit ( string , columns ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( string , FmtStr ) : <EOL> string = fmtstr ( string ) <EOL> string_s = string . s <EOL> matches = list ( re . finditer ( r'<STR_LIT>' , string_s ) ) <EOL> spaces = [ string [ m . start ( ) : m . end ( ) ] for m in matches if m . start ( ) != <NUM_LIT:0> and m . end ( ) != len ( string_s ) ] <EOL> words = [ string [ start : end ] for start , end in zip ( <EOL> [ <NUM_LIT:0> ] + [ m . end ( ) for m in matches ] , <EOL> [ m . start ( ) for m in matches ] + [ len ( string_s ) ] ) if start != end ] <EOL> word_to_lines = lambda word : [ word [ columns * i : columns * ( i + <NUM_LIT:1> ) ] for i in range ( ( len ( word ) - <NUM_LIT:1> ) // columns + <NUM_LIT:1> ) ] <EOL> lines = word_to_lines ( words [ <NUM_LIT:0> ] ) <EOL> for word , space in zip ( words [ <NUM_LIT:1> : ] , spaces ) : <EOL> if len ( lines [ - <NUM_LIT:1> ] ) + len ( word ) < columns : <EOL> lines [ - <NUM_LIT:1> ] += fmtstr ( '<STR_LIT:U+0020>' , ** space . shared_atts ) <EOL> lines [ - <NUM_LIT:1> ] += word <EOL> else : <EOL> lines . extend ( word_to_lines ( word ) ) <EOL> return lines <EOL> def normalize_slice ( length , index ) : <EOL> "<STR_LIT>" <EOL> is_int = False <EOL> if isinstance ( index , int ) : <EOL> is_int = True <EOL> index = slice ( index , index + <NUM_LIT:1> ) <EOL> if index . start is None : <EOL> index = slice ( <NUM_LIT:0> , index . stop , index . step ) <EOL> if index . stop is None : <EOL> index = slice ( index . start , length , index . step ) <EOL> if index . start < - <NUM_LIT:1> : <EOL> index = slice ( length - index . start , index . stop , index . step ) <EOL> if index . stop < - <NUM_LIT:1> : <EOL> index = slice ( index . start , length - index . stop , index . step ) <EOL> if index . step is not None : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> if is_int : <EOL> if index . start < <NUM_LIT:0> or index . start > length : <EOL> raise IndexError ( "<STR_LIT>" % ( index , length ) ) <EOL> return index <EOL> def parse_args ( args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> args += ( kwargs [ '<STR_LIT>' ] , ) <EOL> del kwargs [ '<STR_LIT>' ] <EOL> for arg in args : <EOL> if not isinstance ( arg , ( bytes , unicode ) ) : <EOL> raise ValueError ( "<STR_LIT>" + repr ( args ) ) <EOL> if arg . lower ( ) in FG_COLORS : <EOL> if '<STR_LIT>' in kwargs : raise ValueError ( "<STR_LIT>" ) <EOL> kwargs [ '<STR_LIT>' ] = FG_COLORS [ arg ] <EOL> elif arg . lower ( ) . startswith ( '<STR_LIT>' ) and arg [ <NUM_LIT:3> : ] . lower ( ) in BG_COLORS : <EOL> if '<STR_LIT>' in kwargs : raise ValueError ( "<STR_LIT>" ) <EOL> kwargs [ '<STR_LIT>' ] = BG_COLORS [ arg [ <NUM_LIT:3> : ] ] <EOL> elif arg . lower ( ) in STYLES : <EOL> kwargs [ arg ] = True <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" + repr ( arg ) ) <EOL> for k in kwargs : <EOL> if k not in [ '<STR_LIT>' , '<STR_LIT>' ] + list ( STYLES . keys ( ) ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in kwargs : <EOL> if kwargs [ '<STR_LIT>' ] in FG_COLORS : <EOL> kwargs [ '<STR_LIT>' ] = FG_COLORS [ kwargs [ '<STR_LIT>' ] ] <EOL> if kwargs [ '<STR_LIT>' ] not in list ( FG_COLORS . values ( ) ) : <EOL> raise ValueError ( "<STR_LIT>" % kwargs [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in kwargs : <EOL> if kwargs [ '<STR_LIT>' ] in BG_COLORS : <EOL> kwargs [ '<STR_LIT>' ] = BG_COLORS [ kwargs [ '<STR_LIT>' ] ] <EOL> if kwargs [ '<STR_LIT>' ] not in list ( BG_COLORS . values ( ) ) : <EOL> raise ValueError ( "<STR_LIT>" % kwargs [ '<STR_LIT>' ] ) <EOL> return kwargs <EOL> def fmtstr ( string , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> atts = parse_args ( args , kwargs ) <EOL> if isinstance ( string , FmtStr ) : <EOL> pass <EOL> elif isinstance ( string , ( bytes , unicode ) ) : <EOL> string = FmtStr . from_str ( string ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % ( string , type ( string ) , args , kwargs ) ) <EOL> return string . copy_with_new_atts ( ** atts ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import doctest <EOL> doctest . testmod ( verbose = True ) </s>
