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.

115 lines
5.0 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. 'program_name': kwargs.get('program_name') or os.path.basename(sys.argv[0]).replace('.py', ''),
  32. 'program_description': kwargs.get('program_description') or '',
  33. 'sidebar_title': kwargs.get('sidebar_title', 'Actions'),
  34. 'default_size': kwargs.get('default_size', (610, 530)),
  35. 'auto_start': kwargs.get('auto_start', False),
  36. 'show_advanced': kwargs.get('advanced', True),
  37. 'run_validators': kwargs.get('run_validators', True),
  38. 'encoding': kwargs.get('encoding', 'utf-8'),
  39. 'show_stop_warning': kwargs.get('show_stop_warning', True),
  40. 'show_success_modal': kwargs.get('show_success_modal', True),
  41. 'show_failure_modal': kwargs.get('show_failure_modal', True),
  42. 'force_stop_is_error': kwargs.get('force_stop_is_error', True),
  43. 'poll_external_updates':kwargs.get('poll_external_updates', False),
  44. 'return_to_config': kwargs.get('return_to_config', False),
  45. 'show_restart_button': kwargs.get('show_restart_button', True),
  46. 'requires_shell': kwargs.get('requires_shell', True),
  47. 'menu': kwargs.get('menu', []),
  48. 'clear_before_run': kwargs.get('clear_before_run', False),
  49. # Legacy/Backward compatibility interop
  50. 'use_legacy_titles': kwargs.get('use_legacy_titles', True),
  51. 'num_required_cols': kwargs.get('required_cols', 1),
  52. 'num_optional_cols': kwargs.get('optional_cols', 3),
  53. 'manual_start': False,
  54. 'monospace_display': kwargs.get('monospace_display', False),
  55. 'image_dir': kwargs.get('image_dir'),
  56. 'language_dir': kwargs.get('language_dir'),
  57. 'progress_regex': kwargs.get('progress_regex'),
  58. 'progress_expr': kwargs.get('progress_expr'),
  59. 'hide_progress_msg': kwargs.get('hide_progress_msg', False),
  60. 'disable_progress_bar_animation': kwargs.get('disable_progress_bar_animation'),
  61. 'disable_stop_button': kwargs.get('disable_stop_button'),
  62. # Layouts
  63. 'navigation': kwargs.get('navigation', constants.SIDEBAR),
  64. 'show_sidebar': kwargs.get('show_sidebar', False),
  65. 'tabbed_groups': kwargs.get('tabbed_groups', False),
  66. 'group_by_type': kwargs.get('group_by_type', True),
  67. # styles
  68. 'body_bg_color': kwargs.get('body_bg_color', '#f0f0f0'),
  69. 'header_bg_color': kwargs.get('header_bg_color', '#ffffff'),
  70. 'header_height': kwargs.get('header_height', 90),
  71. 'header_show_title': kwargs.get('header_show_title', True),
  72. 'header_show_subtitle': kwargs.get('header_show_subtitle', True),
  73. 'header_image_center': kwargs.get('header_image_center', False),
  74. 'footer_bg_color': kwargs.get('footer_bg_color', '#f0f0f0'),
  75. 'sidebar_bg_color': kwargs.get('sidebar_bg_color', '#f2f2f2'),
  76. # font family, weight, and size are determined at runtime
  77. 'terminal_panel_color': kwargs.get('terminal_panel_color', '#F0F0F0'),
  78. 'terminal_font_color': kwargs.get('terminal_font_color', '#000000'),
  79. 'terminal_font_family': kwargs.get('terminal_font_family', None),
  80. 'terminal_font_weight': kwargs.get('terminal_font_weight', None),
  81. 'terminal_font_size': kwargs.get('terminal_font_size', None),
  82. 'richtext_controls': kwargs.get('richtext_controls', False),
  83. 'error_color': kwargs.get('error_color', '#ea7878')
  84. }
  85. if build_spec['monospace_display']:
  86. warnings.warn('Gooey Option `monospace_display` is a legacy option.\n'
  87. 'See the terminal_font_x options for more flexible control '
  88. 'over Gooey\'s text formatting')
  89. build_spec['program_description'] = parser.description or build_spec['program_description']
  90. layout_data = (argparse_to_json.convert(parser, **build_spec)
  91. if build_spec['show_advanced']
  92. else default_layout.items())
  93. build_spec.update(layout_data)
  94. if len(build_spec['widgets']) > 1:
  95. # there are subparsers involved
  96. build_spec['show_sidebar'] = True
  97. return build_spec