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.

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