<s> import re <EOL> import sys <EOL> import parser <EOL> d = { } <EOL> d [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> d [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> d [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> d [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> d [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , [ <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> d [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , [ <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> d [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , [ <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> d [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , [ <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> d [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , [ <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> d [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , [ <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> d [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> d [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> d [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> d [ '<STR_LIT>' ] = ( '<STR_LIT>' , '<STR_LIT>' , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> d [ '<STR_LIT>' ] = d [ '<STR_LIT>' ] <EOL> d [ '<STR_LIT>' ] = d [ '<STR_LIT>' ] <EOL> d [ '<STR_LIT>' ] = d [ '<STR_LIT>' ] <EOL> d [ '<STR_LIT>' ] = d [ '<STR_LIT>' ] <EOL> d [ '<STR_LIT>' ] = d [ '<STR_LIT>' ] <EOL> d [ '<STR_LIT>' ] = d [ '<STR_LIT>' ] <EOL> leading_spaces = re . compile ( r'<STR_LIT>' ) <EOL> pat = '<STR_LIT>' <EOL> for k in d . keys ( ) : <EOL> pat += '<STR_LIT:|>' + r'<STR_LIT>' + '<STR_LIT>' + k + r'<STR_LIT>' <EOL> old_names = re . compile ( pat [ <NUM_LIT:1> : ] ) <EOL> linesep = '<STR_LIT:\n>' <EOL> def blocksplitter ( fp ) : <EOL> '''<STR_LIT>''' <EOL> blocklist = [ ] <EOL> blockstring = '<STR_LIT>' <EOL> for line in fp : <EOL> interesting = old_names . match ( line ) <EOL> if interesting : <EOL> if blockstring : <EOL> blocklist . append ( blockstring ) <EOL> blockstring = line <EOL> else : <EOL> blockstring += line <EOL> blocklist . append ( blockstring ) <EOL> return blocklist <EOL> def rewrite_utest ( block ) : <EOL> '''<STR_LIT>''' <EOL> '''<STR_LIT>''' <EOL> utest = old_names . match ( block ) <EOL> if not utest : <EOL> return block <EOL> old = utest . group ( <NUM_LIT:0> ) . lstrip ( ) [ <NUM_LIT:5> : - <NUM_LIT:1> ] <EOL> new = d [ old ] [ <NUM_LIT:0> ] <EOL> op = d [ old ] [ <NUM_LIT:1> ] <EOL> possible_args = d [ old ] [ <NUM_LIT:2> ] <EOL> if possible_args == [ '<STR_LIT>' ] : <EOL> return re . sub ( '<STR_LIT>' + old , new , block ) <EOL> message_pos = possible_args [ - <NUM_LIT:1> ] <EOL> try : <EOL> indent , argl , trailer = decompose_unittest ( old , block ) <EOL> except SyntaxError : <EOL> return block <EOL> argnum = len ( argl ) <EOL> if argnum not in possible_args : <EOL> return block <EOL> elif argnum == message_pos : <EOL> message = argl [ - <NUM_LIT:1> ] <EOL> argl = argl [ : - <NUM_LIT:1> ] <EOL> else : <EOL> message = None <EOL> if argnum is <NUM_LIT:0> or ( argnum is <NUM_LIT:1> and argnum is message_pos ) : <EOL> string = '<STR_LIT>' <EOL> if message : <EOL> message = '<STR_LIT:U+0020>' + message <EOL> elif message_pos is <NUM_LIT:4> : <EOL> try : <EOL> pos = argl [ <NUM_LIT:2> ] . lstrip ( ) <EOL> except IndexError : <EOL> pos = '<STR_LIT>' <EOL> string = '<STR_LIT>' % ( argl [ <NUM_LIT:0> ] , argl [ <NUM_LIT:1> ] , pos , op ) <EOL> else : <EOL> string = '<STR_LIT:U+0020>' + op . join ( argl ) <EOL> if message : <EOL> string = string + '<STR_LIT:U+002C>' + message <EOL> return indent + new + string + trailer <EOL> def decompose_unittest ( old , block ) : <EOL> '''<STR_LIT>''' <EOL> '''<STR_LIT>''' <EOL> indent = re . match ( r'<STR_LIT>' , block ) . group ( ) <EOL> pat = re . search ( '<STR_LIT>' + old + r'<STR_LIT>' , block ) <EOL> args , trailer = get_expr ( block [ pat . end ( ) : ] , '<STR_LIT:)>' ) <EOL> arglist = break_args ( args , [ ] ) <EOL> if arglist == [ '<STR_LIT>' ] : <EOL> return indent , [ ] , trailer <EOL> for i in range ( len ( arglist ) ) : <EOL> try : <EOL> parser . expr ( arglist [ i ] . lstrip ( '<STR_LIT>' ) ) <EOL> except SyntaxError : <EOL> if i == <NUM_LIT:0> : <EOL> arglist [ i ] = '<STR_LIT:(>' + arglist [ i ] + '<STR_LIT:)>' <EOL> else : <EOL> arglist [ i ] = '<STR_LIT>' + arglist [ i ] + '<STR_LIT:)>' <EOL> return indent , arglist , trailer <EOL> def break_args ( args , arglist ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> first , rest = get_expr ( args , '<STR_LIT:U+002C>' ) <EOL> if not rest : <EOL> return arglist + [ first ] <EOL> else : <EOL> return [ first ] + break_args ( rest , arglist ) <EOL> except SyntaxError : <EOL> return arglist + [ args ] <EOL> def get_expr ( s , char ) : <EOL> '''<STR_LIT>''' <EOL> pos = [ ] <EOL> for i in range ( len ( s ) ) : <EOL> if s [ i ] == char : <EOL> pos . append ( i ) <EOL> if pos == [ ] : <EOL> raise SyntaxError <EOL> for p in pos : <EOL> try : <EOL> parser . expr ( '<STR_LIT:(>' + s [ : p ] + '<STR_LIT:)>' ) <EOL> return s [ : p ] , s [ p + <NUM_LIT:1> : ] <EOL> except SyntaxError : <EOL> pass <EOL> raise SyntaxError <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> import py <EOL> usage = "<STR_LIT>" <EOL> optparser = py . compat . optparse . OptionParser ( usage ) <EOL> def select_output ( option , opt , value , optparser , ** kw ) : <EOL> if hasattr ( optparser , '<STR_LIT>' ) : <EOL> optparser . error ( <EOL> '<STR_LIT>' ) <EOL> else : <EOL> optparser . output = kw [ '<STR_LIT>' ] <EOL> optparser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT>" , <EOL> callback = select_output , <EOL> callback_kwargs = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT>" , <EOL> callback = select_output , <EOL> callback_kwargs = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( "<STR_LIT:-c>" , "<STR_LIT>" , action = "<STR_LIT>" , <EOL> callback = select_output , <EOL> callback_kwargs = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> help = "<STR_LIT>" ) <EOL> options , args = optparser . parse_args ( ) <EOL> output = getattr ( optparser , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if output in [ '<STR_LIT>' , '<STR_LIT>' ] and not args : <EOL> optparser . error ( <EOL> '<STR_LIT>' ) <EOL> if not args : <EOL> s = '<STR_LIT>' <EOL> for block in blocksplitter ( sys . stdin ) : <EOL> s += rewrite_utest ( block ) <EOL> sys . stdout . write ( s ) <EOL> else : <EOL> for infilename in args : <EOL> infile = file ( infilename ) <EOL> s = '<STR_LIT>' <EOL> for block in blocksplitter ( infile ) : <EOL> s += rewrite_utest ( block ) <EOL> if output == '<STR_LIT>' : <EOL> outfile = file ( infilename , '<STR_LIT>' ) <EOL> elif output == '<STR_LIT>' : <EOL> outfile = file ( infilename [ : - <NUM_LIT:3> ] + '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> outfile = sys . stdout <EOL> outfile . write ( s ) </s>
