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.

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