[2] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ |
---|
| 4 | Escapes a file into a form suitable for use with tool tests using re_match or re_match_multiline (when -m/--multiline option is used) |
---|
| 5 | |
---|
| 6 | usage: re_escape_output.py [options] input_file [output_file] |
---|
| 7 | -m: Use Multiline Matching |
---|
| 8 | """ |
---|
| 9 | |
---|
| 10 | import optparse, re |
---|
| 11 | |
---|
| 12 | def __main__(): |
---|
| 13 | #Parse Command Line |
---|
| 14 | parser = optparse.OptionParser() |
---|
| 15 | parser.add_option( "-m", "--multiline", action="store_true", dest="multiline", default=False, help="Use Multiline Matching") |
---|
| 16 | ( options, args ) = parser.parse_args() |
---|
| 17 | input = open( args[0] ,'rb' ) |
---|
| 18 | if len( args ) > 1: |
---|
| 19 | output = open( args[1], 'wb' ) |
---|
| 20 | else: |
---|
| 21 | if options.multiline: |
---|
| 22 | suffix = 're_match_multiline' |
---|
| 23 | else: |
---|
| 24 | suffix = 're_match' |
---|
| 25 | output = open( "%s.%s" % ( args[0], suffix ), 'wb' ) |
---|
| 26 | if options.multiline: |
---|
| 27 | lines = [ re.escape( input.read() ) ] |
---|
| 28 | else: |
---|
| 29 | lines = [ "%s\n" % re.escape( line.rstrip( '\n\r' ) ) for line in input ] |
---|
| 30 | output.writelines( lines ) |
---|
| 31 | output.close() |
---|
| 32 | |
---|
| 33 | if __name__ == "__main__": |
---|
| 34 | __main__() |
---|