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.

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