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
3.4 KiB

11 years ago
9 years ago
11 years ago
9 years ago
9 years ago
11 years ago
  1. '''
  2. Created on Jan 24, 2014
  3. @author: Chris
  4. TODO: this
  5. '''
  6. import json
  7. import os
  8. import sys
  9. from argparse import ArgumentParser
  10. from gooey.gui.util.freeze import getResourcePath
  11. from gooey.util.functional import merge
  12. from . import config_generator
  13. from . import cmd_args
  14. IGNORE_COMMAND = '--ignore-gooey'
  15. # TODO: kwargs all the things
  16. def Gooey(f=None,
  17. advanced=True,
  18. language='english',
  19. auto_start=False, # TODO: add this to the docs. Used to be `show_config=True`
  20. target=None,
  21. program_name=None,
  22. program_description=None,
  23. default_size=(610, 530),
  24. use_legacy_titles=True,
  25. required_cols=2,
  26. optional_cols=2,
  27. dump_build_config=False,
  28. load_build_config=None,
  29. monospace_display=False, # TODO: add this to the docs
  30. image_dir='::gooey/default',
  31. language_dir=getResourcePath('languages'),
  32. progress_regex=None, # TODO: add this to the docs
  33. progress_expr=None, # TODO: add this to the docs
  34. hide_progress_msg=False, # TODO: add this to the docs
  35. disable_progress_bar_animation=False,
  36. disable_stop_button=False,
  37. group_by_type=True,
  38. header_height=80,
  39. navigation='SIDEBAR', # TODO: add this to the docs
  40. tabbed_groups=False,
  41. use_cmd_args=False,
  42. **kwargs):
  43. '''
  44. Decorator for client code's main function.
  45. Serializes argparse data to JSON for use with the Gooey front end
  46. '''
  47. params = merge(locals(), locals()['kwargs'])
  48. def build(payload):
  49. def run_gooey(self, args=None, namespace=None):
  50. # This import is delayed so it is not in the --ignore-gooey codepath.
  51. from gooey.gui import application
  52. source_path = sys.argv[0]
  53. build_spec = None
  54. if load_build_config:
  55. try:
  56. exec_dir = os.path.dirname(sys.argv[0])
  57. open_path = os.path.join(exec_dir,load_build_config)
  58. build_spec = json.load(open(open_path, "r"))
  59. except Exception as e:
  60. print( 'Exception loading Build Config from {0}: {1}'.format(load_build_config, e))
  61. sys.exit(1)
  62. if not build_spec:
  63. if use_cmd_args:
  64. cmd_args.parse_cmd_args(self, args)
  65. build_spec = config_generator.create_from_parser(
  66. self,
  67. source_path,
  68. payload_name=payload.__name__,
  69. **params)
  70. if dump_build_config:
  71. config_path = os.path.join(os.path.dirname(sys.argv[0]), 'gooey_config.json')
  72. print('Writing Build Config to: {}'.format(config_path))
  73. with open(config_path, 'w') as f:
  74. f.write(json.dumps(build_spec, indent=2))
  75. application.run(build_spec)
  76. def inner2(*args, **kwargs):
  77. ArgumentParser.original_parse_args = ArgumentParser.parse_args
  78. ArgumentParser.parse_args = run_gooey
  79. return payload(*args, **kwargs)
  80. inner2.__name__ = payload.__name__
  81. return inner2
  82. def run_without_gooey(func):
  83. return lambda *args, **kwargs: func(*args, **kwargs)
  84. if IGNORE_COMMAND in sys.argv:
  85. sys.argv.remove(IGNORE_COMMAND)
  86. if callable(f):
  87. return run_without_gooey(f)
  88. return run_without_gooey
  89. if callable(f):
  90. return build(f)
  91. return build
  92. if __name__ == '__main__':
  93. pass