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.

144 lines
4.6 KiB

11 years ago
9 years ago
11 years ago
9 years ago
9 years ago
4 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: use these defaults in the decorator and migrate to a flat **kwargs
  16. # They're pulled out here for wiring up instances in the tests.
  17. # Some fiddling is needed before I can make the changes to make the swap to
  18. # `defaults` + **kwargs overrides.
  19. defaults = {
  20. 'advanced': True,
  21. 'language': 'english',
  22. 'auto_start': False, # TODO: add this to the docs. Used to be `show_config=True`
  23. 'target': None,
  24. 'program_name': None,
  25. 'program_description': None,
  26. 'default_size': (610, 530),
  27. 'use_legacy_titles': True,
  28. 'required_cols': 2,
  29. 'optional_cols': 2,
  30. 'dump_build_config': False,
  31. 'load_build_config': None,
  32. 'monospace_display': False, # TODO: add this to the docs
  33. 'image_dir': '::gooey/default',
  34. 'language_dir': getResourcePath('languages'),
  35. 'progress_regex': None, # TODO: add this to the docs
  36. 'progress_expr': None, # TODO: add this to the docs
  37. 'hide_progress_msg': False, # TODO: add this to the docs
  38. 'disable_progress_bar_animation': False,
  39. 'disable_stop_button': False,
  40. 'group_by_type': True,
  41. 'header_height': 80,
  42. 'navigation': 'SIDEBAR', # TODO: add this to the docs
  43. 'tabbed_groups': False,
  44. 'use_cmd_args': False,
  45. }
  46. # TODO: kwargs all the things
  47. def Gooey(f=None,
  48. advanced=True,
  49. language='english',
  50. auto_start=False, # TODO: add this to the docs. Used to be `show_config=True`
  51. target=None,
  52. program_name=None,
  53. program_description=None,
  54. default_size=(610, 530),
  55. use_legacy_titles=True,
  56. required_cols=2,
  57. optional_cols=2,
  58. dump_build_config=False,
  59. load_build_config=None,
  60. monospace_display=False, # TODO: add this to the docs
  61. image_dir='::gooey/default',
  62. language_dir=getResourcePath('languages'),
  63. progress_regex=None, # TODO: add this to the docs
  64. progress_expr=None, # TODO: add this to the docs
  65. hide_progress_msg=False, # TODO: add this to the docs
  66. disable_progress_bar_animation=False,
  67. disable_stop_button=False,
  68. group_by_type=True,
  69. header_height=80,
  70. navigation='SIDEBAR', # TODO: add this to the docs
  71. tabbed_groups=False,
  72. use_cmd_args=False,
  73. **kwargs):
  74. '''
  75. Decorator for client code's main function.
  76. Serializes argparse data to JSON for use with the Gooey front end
  77. '''
  78. params = merge(locals(), locals()['kwargs'])
  79. def build(payload):
  80. def run_gooey(self, args=None, namespace=None):
  81. # This import is delayed so it is not in the --ignore-gooey codepath.
  82. from gooey.gui import application
  83. source_path = sys.argv[0]
  84. build_spec = None
  85. if load_build_config:
  86. try:
  87. exec_dir = os.path.dirname(sys.argv[0])
  88. open_path = os.path.join(exec_dir,load_build_config)
  89. build_spec = json.load(open(open_path, "r"))
  90. except Exception as e:
  91. print('Exception loading Build Config from {0}: {1}'.format(load_build_config, e))
  92. sys.exit(1)
  93. if not build_spec:
  94. if use_cmd_args:
  95. cmd_args.parse_cmd_args(self, args)
  96. build_spec = config_generator.create_from_parser(
  97. self,
  98. source_path,
  99. payload_name=payload.__name__,
  100. **params)
  101. if dump_build_config:
  102. config_path = os.path.join(os.path.dirname(sys.argv[0]), 'gooey_config.json')
  103. print('Writing Build Config to: {}'.format(config_path))
  104. with open(config_path, 'w') as f:
  105. f.write(json.dumps(build_spec, indent=2))
  106. application.run(build_spec)
  107. def inner2(*args, **kwargs):
  108. ArgumentParser.original_parse_args = ArgumentParser.parse_args
  109. ArgumentParser.parse_args = run_gooey
  110. return payload(*args, **kwargs)
  111. inner2.__name__ = payload.__name__
  112. return inner2
  113. def run_without_gooey(func):
  114. return lambda *args, **kwargs: func(*args, **kwargs)
  115. if IGNORE_COMMAND in sys.argv:
  116. sys.argv.remove(IGNORE_COMMAND)
  117. if callable(f):
  118. return run_without_gooey(f)
  119. return run_without_gooey
  120. if callable(f):
  121. return build(f)
  122. return build
  123. if __name__ == '__main__':
  124. pass