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.

103 lines
2.9 KiB

10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 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 import application
  11. from gooey.gui.util.freeze import getResourcePath
  12. from gooey.util.functional import merge
  13. from . import config_generator
  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. 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. source_path = sys.argv[0]
  49. build_spec = None
  50. if load_build_config:
  51. try:
  52. build_spec = json.load(open(load_build_config, "r"))
  53. except Exception as e:
  54. print( 'Exception loading Build Config from {0}: {1}'.format(load_build_config, e))
  55. sys.exit(1)
  56. if not build_spec:
  57. build_spec = config_generator.create_from_parser(
  58. self,
  59. source_path,
  60. payload_name=payload.__name__,
  61. **params)
  62. if dump_build_config:
  63. config_path = os.path.join(os.getcwd(), 'gooey_config.json')
  64. print('Writing Build Config to: {}'.format(config_path))
  65. with open(config_path, 'w') as f:
  66. f.write(json.dumps(build_spec, indent=2))
  67. application.run(build_spec)
  68. def inner2(*args, **kwargs):
  69. ArgumentParser.original_parse_args = ArgumentParser.parse_args
  70. ArgumentParser.parse_args = run_gooey
  71. return payload(*args, **kwargs)
  72. inner2.__name__ = payload.__name__
  73. return inner2
  74. def run_without_gooey(func):
  75. return lambda: func()
  76. if IGNORE_COMMAND in sys.argv:
  77. sys.argv.remove(IGNORE_COMMAND)
  78. if callable(f):
  79. return run_without_gooey(f)
  80. return run_without_gooey
  81. if callable(f):
  82. return build(f)
  83. return build
  84. if __name__ == '__main__':
  85. pass