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.

153 lines
6.3 KiB

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