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.

90 lines
2.2 KiB

  1. '''
  2. Main runner entry point for Gooey.
  3. '''
  4. import wx
  5. import os
  6. import sys
  7. import json
  8. import argparse
  9. from functools import partial
  10. from gooey.gui.lang import i18n
  11. from gooey.gui.windows.base_window import BaseWindow
  12. from gooey.gui.windows.advanced_config import ConfigPanel
  13. from gooey.python_bindings import config_generator, source_parser
  14. def main():
  15. parser = argparse.ArgumentParser(
  16. description='Gooey turns your command line programs into beautiful, user friendly GUIs')
  17. parser.add_argument(
  18. '-b', '--create-build-script',
  19. dest='build_script',
  20. help='Parse the supplied Python File and generate a runnable Gooey build script'
  21. )
  22. parser.add_argument(
  23. '-r', '--run',
  24. dest='run',
  25. nargs='?',
  26. const='',
  27. help='Run Gooey with build_config in local dir OR via the supplied config path'
  28. )
  29. args = parser.parse_args()
  30. if args.build_script:
  31. do_build_script(args.build_script)
  32. elif args.run is not None:
  33. do_run(args)
  34. def do_build_script(module_path):
  35. with open(module_path, 'r') as f:
  36. if not source_parser.has_argparse(f.read()):
  37. raise AssertionError('Argparse not found in module. Unable to continue')
  38. gooey_config = config_generator.create_from_parser(module_path, show_config=True)
  39. outfile = os.path.join(os.getcwd(), 'gooey_config.json')
  40. print 'Writing config file to: {}'.format(outfile)
  41. with open(outfile, 'w') as f:
  42. f.write(json.dumps(gooey_config, indent=2))
  43. def do_run(args):
  44. gooey_config = args.run or read_local_dir()
  45. if not os.path.exists(gooey_config):
  46. raise IOError('Gooey Config not found')
  47. with open(gooey_config, 'r') as f:
  48. build_spec = json.load(f)
  49. run(build_spec)
  50. def run(build_spec):
  51. app = wx.App(False)
  52. i18n.load(build_spec['language'])
  53. frame = BaseWindow(build_spec)
  54. frame.Show(True)
  55. app.MainLoop()
  56. def read_local_dir():
  57. local_files = os.listdir(os.getcwd())
  58. if 'gooey_config.json' not in local_files:
  59. print "Bugger! gooey_config.json not found!"
  60. sys.exit(1)
  61. return os.path.join(os.getcwd(), 'gooey_config.json')
  62. if __name__ == '__main__':
  63. main()