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.

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