You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.3 KiB

  1. __author__ = 'Chris'
  2. """
  3. Preps the extracted Python code so that it can be evaled by the
  4. monkey_parser
  5. """
  6. from itertools import *
  7. source = '''
  8. import sys
  9. import os
  10. import doctest
  11. import cProfile
  12. import pstats
  13. from argparse import ArgumentParser
  14. from argparse import RawDescriptionHelpFormatter
  15. from gooey import Gooey
  16. parser = ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  17. parser.add_argument('filename', help='filename')
  18. parser.add_argument('-r', '--recursive', dest='recurse', action='store_true', help='recurse into subfolders [default: %(default)s]')
  19. parser.add_argument('-v', '--verbose', dest='verbose', action='count', help='set verbosity level [default: %(default)s]')
  20. parser.add_argument('-i', '--include', action='append', help='only include paths matching this regex pattern. Note: exclude is given preference over include. [default: %(default)s]', metavar='RE')
  21. parser.add_argument('-m', '--mycoolargument', help='mycoolargument')
  22. parser.add_argument('-e', '--exclude', dest='exclude', help='exclude paths matching this regex pattern. [default: %(default)s]', metavar='RE')
  23. parser.add_argument('-V', '--version', action='version')
  24. parser.add_argument('-T', '--tester', choices=['yes', 'no'])
  25. parser.add_argument(dest='paths', help='paths to folder(s) with source file(s) [default: %(default)s]', metavar='path', nargs='+')
  26. '''
  27. def take_imports(code):
  28. return takewhile(lambda line: 'import' in line, code)
  29. def drop_imports(code):
  30. return dropwhile(lambda line: 'import' in line, code)
  31. def split_line(line):
  32. # splits an assignment statement into varname and command strings
  33. # in: "parser = ArgumentParser(description='Example Argparse Program')"
  34. # out: "parser", "ArgumentParser(description='Example Argparse Program"
  35. variable, instruction = line.split('=', 1)
  36. return variable.strip(), instruction.strip()
  37. def update_parser_varname(new_varname, code):
  38. # lines = source.split('\n')[1:]
  39. lines = filter(lambda x: x != '', code)
  40. argparse_code = dropwhile(lambda line: 'import' in line, lines)
  41. old_argparser_varname, _ = split_line(argparse_code.next())
  42. updated_code = [line.replace(old_argparser_varname, new_varname)
  43. for line in lines]
  44. return updated_code
  45. if __name__ == '__main__':
  46. pass