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.

123 lines
3.0 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. load_build_config=None,
  27. monospace_display=False):
  28. '''
  29. Decorator for client code's main function.
  30. Serializes argparse data to JSON for use with the Gooey front end
  31. '''
  32. params = locals()
  33. def build(payload):
  34. def run_gooey(self, args=None, namespace=None):
  35. source_path = sys.argv[0]
  36. build_spec = None
  37. if load_build_config:
  38. try:
  39. build_spec = json.load(open(load_build_config, "r"))
  40. except Exception, e:
  41. print( 'Exception loading Build Config from {0}: {1}'.format(load_build_config, e))
  42. sys.exit(1)
  43. if not build_spec:
  44. build_spec = config_generator.create_from_parser(self, source_path, payload_name=payload.__name__, **params)
  45. if dump_build_config:
  46. config_path = os.path.join(os.getcwd(), 'gooey_config.json')
  47. print( 'Writing Build Config to: {}'.format(config_path))
  48. with open(config_path, 'w') as f:
  49. f.write(json.dumps(build_spec, indent=2))
  50. application.run(build_spec)
  51. def inner2(*args, **kwargs):
  52. ArgumentParser.original_parse_args = ArgumentParser.parse_args
  53. ArgumentParser.parse_args = run_gooey
  54. return payload(*args, **kwargs)
  55. inner2.__name__ = payload.__name__
  56. return inner2
  57. def run_without_gooey(func):
  58. return lambda: func()
  59. if IGNORE_COMMAND in sys.argv:
  60. sys.argv.remove(IGNORE_COMMAND)
  61. if callable(f):
  62. return run_without_gooey(f)
  63. return run_without_gooey
  64. if callable(f):
  65. return build(f)
  66. return build
  67. def store_executable_copy():
  68. main_module_path = get_caller_path()
  69. _, filename = os.path.split(main_module_path)
  70. cleaned_source = clean_source(main_module_path)
  71. descriptor, tmp_filepath = tempfile.mkstemp(suffix='.py')
  72. atexit.register(cleanup, descriptor, tmp_filepath)
  73. with open(tmp_filepath, 'w') as f:
  74. f.write(cleaned_source)
  75. return tmp_filepath
  76. def clean_source(module_path):
  77. with open(module_path, 'r') as f:
  78. return ''.join(
  79. line for line in f.readlines()
  80. if '@gooey' not in line.lower())
  81. def get_parser(module_path):
  82. return source_parser.extract_parser(module_path)
  83. def get_caller_path():
  84. tmp_sys = __import__('sys')
  85. return tmp_sys.argv[0]
  86. def cleanup(descriptor, filepath):
  87. os.close(descriptor)
  88. os.remove(filepath)
  89. if __name__ == '__main__':
  90. pass