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.

119 lines
5.3 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. 'fullscreen': kwargs.get('fullscreen', False),
  53. # Legacy/Backward compatibility interop
  54. 'use_legacy_titles': kwargs.get('use_legacy_titles', True),
  55. 'num_required_cols': kwargs.get('required_cols', 1),
  56. 'num_optional_cols': kwargs.get('optional_cols', 3),
  57. 'manual_start': False,
  58. 'monospace_display': kwargs.get('monospace_display', False),
  59. 'image_dir': kwargs.get('image_dir'),
  60. 'language_dir': kwargs.get('language_dir'),
  61. 'progress_regex': kwargs.get('progress_regex'),
  62. 'progress_expr': kwargs.get('progress_expr'),
  63. 'hide_progress_msg': kwargs.get('hide_progress_msg', False),
  64. 'disable_progress_bar_animation': kwargs.get('disable_progress_bar_animation'),
  65. 'disable_stop_button': kwargs.get('disable_stop_button'),
  66. # Layouts
  67. 'navigation': kwargs.get('navigation', constants.SIDEBAR),
  68. 'show_sidebar': kwargs.get('show_sidebar', False),
  69. 'tabbed_groups': kwargs.get('tabbed_groups', False),
  70. 'group_by_type': kwargs.get('group_by_type', True),
  71. # styles
  72. 'body_bg_color': kwargs.get('body_bg_color', '#f0f0f0'),
  73. 'header_bg_color': kwargs.get('header_bg_color', '#ffffff'),
  74. 'header_height': kwargs.get('header_height', 90),
  75. 'header_show_title': kwargs.get('header_show_title', True),
  76. 'header_show_subtitle': kwargs.get('header_show_subtitle', True),
  77. 'header_image_center': kwargs.get('header_image_center', False),
  78. 'footer_bg_color': kwargs.get('footer_bg_color', '#f0f0f0'),
  79. 'sidebar_bg_color': kwargs.get('sidebar_bg_color', '#f2f2f2'),
  80. # font family, weight, and size are determined at runtime
  81. 'terminal_panel_color': kwargs.get('terminal_panel_color', '#F0F0F0'),
  82. 'terminal_font_color': kwargs.get('terminal_font_color', '#000000'),
  83. 'terminal_font_family': kwargs.get('terminal_font_family', None),
  84. 'terminal_font_weight': kwargs.get('terminal_font_weight', None),
  85. 'terminal_font_size': kwargs.get('terminal_font_size', None),
  86. 'richtext_controls': kwargs.get('richtext_controls', False),
  87. 'error_color': kwargs.get('error_color', '#ea7878')
  88. }
  89. if build_spec['monospace_display']:
  90. warnings.warn('Gooey Option `monospace_display` is a legacy option.\n'
  91. 'See the terminal_font_x options for more flexible control '
  92. 'over Gooey\'s text formatting')
  93. build_spec['program_description'] = build_spec['program_description'] or parser.description or ''
  94. layout_data = (argparse_to_json.convert(parser, **build_spec)
  95. if build_spec['show_advanced']
  96. else default_layout.items())
  97. build_spec.update(layout_data)
  98. if len(build_spec['widgets']) > 1:
  99. # there are subparsers involved
  100. build_spec['show_sidebar'] = True
  101. return build_spec