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.

100 lines
2.4 KiB

11 years ago
9 years ago
11 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
  1. '''
  2. Created on Jan 24, 2014
  3. @author: Chris
  4. TODO: this
  5. '''
  6. import os
  7. import json
  8. import atexit
  9. import tempfile
  10. from . import source_parser
  11. from . import config_generator
  12. from gooey.gui import application
  13. from argparse import ArgumentParser
  14. def Gooey(f=None,
  15. advanced=True,
  16. language='english',
  17. show_config=True,
  18. program_name=None,
  19. program_description=None,
  20. default_size=(610, 530),
  21. required_cols=2,
  22. optional_cols=2,
  23. dump_build_config=False):
  24. '''
  25. Decorator for client code's main function.
  26. Serializes argparse data to JSON for use with the Gooey front end
  27. '''
  28. params = locals()
  29. def build(payload):
  30. def run_gooey(self, args=None, namespace=None):
  31. source_path = store_executable_copy()
  32. build_spec = config_generator.create_from_parser(self, source_path, payload_name=payload.__name__, **params)
  33. if dump_build_config:
  34. config_path = os.path.join(os.getcwd(), 'gooey_config.json')
  35. print( 'Writing Build Config to: {}'.format(config_path))
  36. with open(config_path, 'w') as f:
  37. f.write(json.dumps(build_spec, indent=2))
  38. application.run(build_spec)
  39. def inner2(*args, **kwargs):
  40. ArgumentParser.original_parse_args = ArgumentParser.parse_args
  41. ArgumentParser.parse_args = run_gooey
  42. return payload(*args, **kwargs)
  43. inner2.__name__ = payload.__name__
  44. return inner2
  45. if callable(f):
  46. return build(f)
  47. return build
  48. def store_executable_copy():
  49. main_module_path = get_caller_path()
  50. _, filename = os.path.split(main_module_path)
  51. cleaned_source = clean_source(main_module_path)
  52. descriptor, tmp_filepath = tempfile.mkstemp(suffix='.py')
  53. atexit.register(cleanup, descriptor, tmp_filepath)
  54. with open(tmp_filepath, 'w') as f:
  55. f.write(cleaned_source)
  56. return tmp_filepath
  57. def clean_source(module_path):
  58. with open(module_path, 'r') as f:
  59. return ''.join(
  60. line for line in f.readlines()
  61. if '@gooey' not in line.lower())
  62. def get_parser(module_path):
  63. return source_parser.extract_parser(module_path)
  64. def get_caller_path():
  65. tmp_sys = __import__('sys')
  66. return tmp_sys.argv[0]
  67. def cleanup(descriptor, filepath):
  68. os.close(descriptor)
  69. os.remove(filepath)
  70. if __name__ == '__main__':
  71. pass