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.

154 lines
4.9 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. '''
  2. Created on Jan 24, 2014
  3. @author: Chris
  4. Hey, whaduya know. This is out of date again. TODO: update giant doctring.
  5. ##How things work these days (though, likely to change)
  6. The decorator is used solely as a nice way to get the location
  7. of the executing script. It no longer returns a decorated version
  8. of the client code, but in fact completely hijacks the execution.
  9. So, rather than returning a reference to the client's main, it now
  10. returns itself, thus short-circuiting the execution of the client
  11. program.
  12. What it DOES do now is grab where the client module is stored and
  13. read it in as a file so that it can hack away at it.
  14. The first step, as before, is getting the ArgumentParser reference
  15. so that the needed values can be extracted. This is done by reading
  16. the source file up to the point where the `parse_args()` method is
  17. called. This puts us smack in the middle of the client's `main` method.
  18. This first half guarantees that all imports, modules, variable assignments,
  19. etc.. are caught (unlike before).
  20. Next step: getting the rest of the source code that's relevant
  21. The file is again read up to the `parse_args` call, but this time everything
  22. leading up to that point is dropped and we keep only the remainder of the file.
  23. So, now the top and the bottom is located, but the bottom needs to be trimmed a
  24. little more -- we want to drop everything remaining in the main method.
  25. So, we `dropwhile` lines are currently indented (and thus still part of the `main`
  26. method)
  27. Finally, we arrive at the end, which gives us an exact copy of the original source
  28. file, minus all of it's main logic. The two pieces are then sandwiched together,
  29. saved to a file, and imported as a new module. Now all that has to be done is call
  30. it (moddified) main function, and bam! It returns to fully populated parser object
  31. to us. No more complicated ast stuff. Just a little bit of string parsing and we're
  32. done.
  33. '''
  34. from argparse import ArgumentParser
  35. import json
  36. import os
  37. import sys
  38. import atexit
  39. import tempfile
  40. import source_parser
  41. import config_generator
  42. from gooey.gui import application
  43. from gooey.gui.windows import layouts
  44. from gooey.python_bindings import argparse_to_json
  45. def decorator(func):
  46. def graphical_parse_args(self, args=None, namespace=None):
  47. values = [raw_input("Enter %s (%s):" % (a.dest, a.help)) for a in self._actions]
  48. raw_input('Press enter to start')
  49. # update new args with what you get from the graphical widgets
  50. return self.original_parse_args(arg_lst, namespace)
  51. def inner(*args, **kwargs):
  52. ArgumentParser.original_parse_args = ArgumentParser.parse_args
  53. ArgumentParser.parse_args = graphical_parse_args
  54. return func(*args, **kwargs)
  55. return inner
  56. def store_executable_copy():
  57. main_module_path = get_caller_path()
  58. _, filename = os.path.split(main_module_path)
  59. cleaned_source = clean_source(main_module_path)
  60. descriptor, tmp_filepath = tempfile.mkstemp(suffix='.py')
  61. atexit.register(cleanup, descriptor, tmp_filepath)
  62. with open(tmp_filepath, 'w') as f:
  63. f.write(cleaned_source)
  64. return tmp_filepath
  65. def Gooey(f=None,
  66. advanced=True,
  67. language='english',
  68. show_config=True,
  69. program_name=None,
  70. program_description=None,
  71. default_size=(610, 530),
  72. required_cols=3,
  73. optional_cols=2,
  74. dump_build_config=False):
  75. '''
  76. Decorator for client code's main function.
  77. Serializes argparse data to JSON for use with the Gooey front end
  78. '''
  79. params = locals()
  80. def build(payload):
  81. def run_gooey(self, args=None, namespace=None):
  82. source_path = store_executable_copy()
  83. build_spec = config_generator.create_from_parser(self, source_path, payload_name=payload.__name__, **params)
  84. if dump_build_config:
  85. config_path = os.path.join(os.getcwd(), 'gooey_config.json')
  86. print 'Writing Build Config to: {}'.format(config_path)
  87. with open(config_path, 'w') as f:
  88. f.write(json.dumps(build_spec, indent=2))
  89. application.run(build_spec)
  90. def inner2(*args, **kwargs):
  91. ArgumentParser.original_parse_args = ArgumentParser.parse_args
  92. ArgumentParser.parse_args = run_gooey
  93. return payload(*args, **kwargs)
  94. inner2.__name__ = payload.__name__
  95. return inner2
  96. if callable(f):
  97. return build(f)
  98. return build
  99. def clean_source(module_path):
  100. with open(module_path, 'r') as f:
  101. return ''.join(
  102. line for line in f.readlines()
  103. if '@gooey' not in line.lower())
  104. def get_parser(module_path):
  105. return source_parser.extract_parser(module_path)
  106. def get_caller_path():
  107. tmp_sys = __import__('sys')
  108. return tmp_sys.argv[0]
  109. def cleanup(descriptor, filepath):
  110. os.close(descriptor)
  111. os.remove(filepath)
  112. if __name__ == '__main__':
  113. pass