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.

93 lines
2.7 KiB

  1. from gooey.python_bindings import code_prep, source_parser
  2. __author__ = 'Chris'
  3. """
  4. Pretty Printing util for inspecting the various ast objects
  5. """
  6. import ast
  7. from _ast import Assign
  8. def pretty_print(node, indent):
  9. d = node.__dict__
  10. for k, v in d.iteritems():
  11. if isinstance(v, list):
  12. print '-' * indent, k, ": "
  13. for i in v:
  14. pretty_print(i, indent + 2)
  15. elif 'ast' in str(type(v)):
  16. pretty_print(v, indent + 2)
  17. else:
  18. print '-' * indent, k, ": ", v
  19. if __name__ == '__main__':
  20. lines = '''
  21. def main():
  22. x = 1
  23. y = 2
  24. foo, doo = ("poo", "poo")
  25. smarser = argparse.ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  26. random_junk = 123412353454356
  27. smarser.add_argument("filename", help="Name of the file you want to read") # positional'
  28. smarser.add_argument("outfile", help="Name of the file where you'll save the output") # positional
  29. bar = x + y
  30. baz = random_junk * 5
  31. '''
  32. lines2 = '''
  33. def main():
  34. try:
  35. foo, doo = ("poo", "poo")
  36. smarser = argparse.ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  37. smarser.add_argument("filename", help="Name of the file you want to read") # positional'
  38. smarser.add_argument("outfile", help="Name of the file where you'll save the output") # positional
  39. smarser.parse_args()
  40. except:
  41. pass
  42. '''
  43. git_example = '''
  44. from argparse import ArgumentParser
  45. def main():
  46. """Main"""
  47. bar = 'bar'
  48. print "Hello!"
  49. description='Desc'
  50. parser = ArgumentParser(description=bar)
  51. parser.add_argument(bar, help=('bar')) ##################
  52. return parser
  53. # args = parser.parse_args()
  54. # print(args)
  55. # return True
  56. '''
  57. nodes = ast.parse(git_example)
  58. assign = source_parser.get_nodes_by_instance_type(nodes, Assign)
  59. assignment = source_parser.get_nodes_by_containing_attr(assign, "ArgumentParser")
  60. print assignment
  61. print assignment[0].__dict__
  62. p = source_parser.convert_to_python(assignment)[0]
  63. print p
  64. varname, instruction = code_prep.split_line(source_parser.convert_to_python(assignment)[0])
  65. updated_code = git_example.replace(varname, "jello_maker")
  66. print 'Fusdo:', updated_code.split('\n')[8]
  67. # all_code_leading_up_to_parseargs = '\n'.join(itertools.takewhile(lambda line: 'parse_args()' not in line, updated_code.split('\n')))
  68. # code = compile(all_code_leading_up_to_parseargs, '', 'exec')
  69. # exec(code)
  70. # parser = main()
  71. # print parser._actions
  72. # print assign[0].value.func.__dict__
  73. # print assign[0].value.keywords[0].value.__dict__
  74. # pretty_print(assign[0], 1)