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.

118 lines
5.2 KiB

  1. import os
  2. import sys
  3. import warnings
  4. from gooey.python_bindings import argparse_to_json
  5. from gooey.gui.util.quoting import quote
  6. from gooey.python_bindings import constants
  7. default_layout = {
  8. 'widgets': [{
  9. 'type': 'CommandField',
  10. 'required': True,
  11. 'data': {
  12. 'display_name': 'Enter Commands',
  13. 'help': 'Enter command line arguments',
  14. 'nargs': '',
  15. 'commands': '',
  16. 'choices': [],
  17. 'default': None,
  18. }
  19. }],
  20. }
  21. def create_from_parser(parser, source_path, **kwargs):
  22. run_cmd = kwargs.get('target')
  23. if run_cmd is None:
  24. if hasattr(sys, 'frozen'):
  25. run_cmd = quote(source_path)
  26. else:
  27. run_cmd = '{} -u {}'.format(quote(sys.executable), quote(source_path))
  28. build_spec = {
  29. 'language': kwargs.get('language', 'english'),
  30. 'target': run_cmd,
  31. # when running with a custom target, there is no need to inject
  32. # --ignore-gooey into the CLI args
  33. 'suppress_gooey_flag': kwargs.get('suppress_gooey_flag') or False,
  34. 'program_name': kwargs.get('program_name') or os.path.basename(sys.argv[0]).replace('.py', ''),
  35. 'program_description': kwargs.get('program_description') or '',
  36. 'sidebar_title': kwargs.get('sidebar_title', 'Actions'),
  37. 'default_size': kwargs.get('default_size', (610, 530)),
  38. 'auto_start': kwargs.get('auto_start', False),
  39. 'show_advanced': kwargs.get('advanced', True),
  40. 'run_validators': kwargs.get('run_validators', True),
  41. 'encoding': kwargs.get('encoding', 'utf-8'),
  42. 'show_stop_warning': kwargs.get('show_stop_warning', True),
  43. 'show_success_modal': kwargs.get('show_success_modal', True),
  44. 'show_failure_modal': kwargs.get('show_failure_modal', True),
  45. 'force_stop_is_error': kwargs.get('force_stop_is_error', True),
  46. 'poll_external_updates':kwargs.get('poll_external_updates', False),
  47. 'return_to_config': kwargs.get('return_to_config', False),
  48. 'show_restart_button': kwargs.get('show_restart_button', True),
  49. 'requires_shell': kwargs.get('requires_shell', True),
  50. 'menu': kwargs.get('menu', []),
  51. 'clear_before_run': kwargs.get('clear_before_run', False),
  52. # Legacy/Backward compatibility interop
  53. 'use_legacy_titles': kwargs.get('use_legacy_titles', True),
  54. 'num_required_cols': kwargs.get('required_cols', 1),
  55. 'num_optional_cols': kwargs.get('optional_cols', 3),
  56. 'manual_start': False,
  57. 'monospace_display': kwargs.get('monospace_display', False),
  58. 'image_dir': kwargs.get('image_dir'),
  59. 'language_dir': kwargs.get('language_dir'),
  60. 'progress_regex': kwargs.get('progress_regex'),
  61. 'progress_expr': kwargs.get('progress_expr'),
  62. 'hide_progress_msg': kwargs.get('hide_progress_msg', False),
  63. 'disable_progress_bar_animation': kwargs.get('disable_progress_bar_animation'),
  64. 'disable_stop_button': kwargs.get('disable_stop_button'),
  65. # Layouts
  66. 'navigation': kwargs.get('navigation', constants.SIDEBAR),
  67. 'show_sidebar': kwargs.get('show_sidebar', False),
  68. 'tabbed_groups': kwargs.get('tabbed_groups', False),
  69. 'group_by_type': kwargs.get('group_by_type', True),
  70. # styles
  71. 'body_bg_color': kwargs.get('body_bg_color', '#f0f0f0'),
  72. 'header_bg_color': kwargs.get('header_bg_color', '#ffffff'),
  73. 'header_height': kwargs.get('header_height', 90),
  74. 'header_show_title': kwargs.get('header_show_title', True),
  75. 'header_show_subtitle': kwargs.get('header_show_subtitle', True),
  76. 'header_image_center': kwargs.get('header_image_center', False),
  77. 'footer_bg_color': kwargs.get('footer_bg_color', '#f0f0f0'),
  78. 'sidebar_bg_color': kwargs.get('sidebar_bg_color', '#f2f2f2'),
  79. # font family, weight, and size are determined at runtime
  80. 'terminal_panel_color': kwargs.get('terminal_panel_color', '#F0F0F0'),
  81. 'terminal_font_color': kwargs.get('terminal_font_color', '#000000'),
  82. 'terminal_font_family': kwargs.get('terminal_font_family', None),
  83. 'terminal_font_weight': kwargs.get('terminal_font_weight', None),
  84. 'terminal_font_size': kwargs.get('terminal_font_size', None),
  85. 'richtext_controls': kwargs.get('richtext_controls', False),
  86. 'error_color': kwargs.get('error_color', '#ea7878')
  87. }
  88. if build_spec['monospace_display']:
  89. warnings.warn('Gooey Option `monospace_display` is a legacy option.\n'
  90. 'See the terminal_font_x options for more flexible control '
  91. 'over Gooey\'s text formatting')
  92. build_spec['program_description'] = parser.description or build_spec['program_description']
  93. layout_data = (argparse_to_json.convert(parser, **build_spec)
  94. if build_spec['show_advanced']
  95. else default_layout.items())
  96. build_spec.update(layout_data)
  97. if len(build_spec['widgets']) > 1:
  98. # there are subparsers involved
  99. build_spec['show_sidebar'] = True
  100. return build_spec