/scripts/tools/re_escape_output.py

https://bitbucket.org/h_morita_dbcls/galaxy-central · Python · 34 lines · 22 code · 4 blank · 8 comment · 8 complexity · 00a091e2d4462975e78bc0fb8b36dcda MD5 · raw file

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