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.

152 lines
6.4 KiB

2 years ago
  1. import signal
  2. import sys
  3. import textwrap
  4. import os
  5. from typing import List
  6. from gooey.python_bindings.constants import Events
  7. from gooey.python_bindings import constants
  8. from gooey.gui.util.freeze import getResourcePath
  9. from gooey.python_bindings.types import GooeyParams
  10. from gooey.util.functional import merge
  11. def _get_font_weight(kwargs):
  12. error_msg = textwrap.dedent('''
  13. Unknown font weight {}.
  14. The available weights can be found in the `constants` module.
  15. They're prefixed with "FONTWEIGHT" (e.g. `FONTWEIGHT_BOLD`)
  16. example code:
  17. ```
  18. from gooey import constants
  19. @Gooey(terminal_font_weight=constants.FONTWEIGHT_NORMAL)
  20. ```
  21. ''')
  22. weights = {
  23. constants.FONTWEIGHT_THIN,
  24. constants.FONTWEIGHT_EXTRALIGHT,
  25. constants.FONTWEIGHT_LIGHT,
  26. constants.FONTWEIGHT_NORMAL,
  27. constants.FONTWEIGHT_MEDIUM,
  28. constants.FONTWEIGHT_SEMIBOLD,
  29. constants.FONTWEIGHT_BOLD,
  30. constants.FONTWEIGHT_EXTRABOLD,
  31. constants.FONTWEIGHT_HEAVY,
  32. constants.FONTWEIGHT_EXTRAHEAVY
  33. }
  34. weight = kwargs.get('terminal_font_weight', constants.FONTWEIGHT_NORMAL)
  35. if weight not in weights:
  36. raise ValueError(error_msg.format(weight))
  37. return weight
  38. # python can't type kwargs? wtf..
  39. def gooey_params(**kwargs) -> GooeyParams:
  40. """
  41. Builds the full GooeyParams object from an arbitrary subset of supplied values
  42. """
  43. return GooeyParams(**{ # type: ignore
  44. 'show_preview_warning': kwargs.get('show_preview_warning', True),
  45. 'language': kwargs.get('language', 'english'),
  46. 'target': kwargs.get('target'),
  47. 'dump_build_config': kwargs.get('dump_build_config', False),
  48. 'load_build_config': kwargs.get('load_build_config'),
  49. 'use_cmd_args': kwargs.get('use_cmd_args', False),
  50. 'suppress_gooey_flag': kwargs.get('suppress_gooey_flag') or False,
  51. # TODO: I should not read from the environment.
  52. # remains here for legacy reasons pending refactor
  53. 'program_name': kwargs.get('program_name') or os.path.basename(sys.argv[0]).replace('.py', ''),
  54. 'program_description': kwargs.get('program_description') or '',
  55. 'sidebar_title': kwargs.get('sidebar_title', 'Actions'),
  56. 'default_size': kwargs.get('default_size', (610, 530)),
  57. 'auto_start': kwargs.get('auto_start', False),
  58. 'advanced': kwargs.get('advanced', True),
  59. 'run_validators': kwargs.get('run_validators', True),
  60. 'encoding': kwargs.get('encoding', 'utf-8'),
  61. 'show_stop_warning': kwargs.get('show_stop_warning', True),
  62. 'show_success_modal': kwargs.get('show_success_modal', True),
  63. 'show_failure_modal': kwargs.get('show_failure_modal', True),
  64. 'force_stop_is_error': kwargs.get('force_stop_is_error', True),
  65. 'poll_external_updates': kwargs.get('poll_external_updates', False),
  66. 'return_to_config': kwargs.get('return_to_config', False),
  67. 'show_restart_button': kwargs.get('show_restart_button', True),
  68. 'requires_shell': kwargs.get('requires_shell', True),
  69. 'menu': kwargs.get('menu', []),
  70. 'clear_before_run': kwargs.get('clear_before_run', False),
  71. 'fullscreen': kwargs.get('fullscreen', False),
  72. 'use_legacy_titles': kwargs.get('use_legacy_titles', True),
  73. 'required_cols': kwargs.get('required_cols', 2),
  74. 'optional_cols': kwargs.get('optional_cols', 2),
  75. 'manual_start': False,
  76. 'monospace_display': kwargs.get('monospace_display', False),
  77. 'image_dir': kwargs.get('image_dir', '::gooey/default'),
  78. # TODO: this directory resolution shouldn't happen here!
  79. # TODO: leaving due to legacy for now
  80. 'language_dir': kwargs.get('language_dir', getResourcePath('languages')),
  81. 'progress_regex': kwargs.get('progress_regex'),
  82. 'progress_expr': kwargs.get('progress_expr'),
  83. 'hide_progress_msg': kwargs.get('hide_progress_msg', False),
  84. 'timing_options': merge({
  85. 'show_time_remaining': False,
  86. 'hide_time_remaining_on_complete': True
  87. }, kwargs.get('timing_options', {})),
  88. 'disable_progress_bar_animation': kwargs.get('disable_progress_bar_animation', False),
  89. 'disable_stop_button': kwargs.get('disable_stop_button'),
  90. 'shutdown_signal': kwargs.get('shutdown_signal', signal.SIGTERM),
  91. 'use_events': parse_events(kwargs.get('use_events', [])),
  92. 'navigation': kwargs.get('navigation', constants.SIDEBAR),
  93. 'show_sidebar': kwargs.get('show_sidebar', False),
  94. 'tabbed_groups': kwargs.get('tabbed_groups', False),
  95. 'group_by_type': kwargs.get('group_by_type', True),
  96. 'body_bg_color': kwargs.get('body_bg_color', '#f0f0f0'),
  97. 'header_bg_color': kwargs.get('header_bg_color', '#ffffff'),
  98. 'header_height': kwargs.get('header_height', 90),
  99. 'header_show_title': kwargs.get('header_show_title', True),
  100. 'header_show_subtitle': kwargs.get('header_show_subtitle', True),
  101. 'header_image_center': kwargs.get('header_image_center', False),
  102. 'footer_bg_color': kwargs.get('footer_bg_color', '#f0f0f0'),
  103. 'sidebar_bg_color': kwargs.get('sidebar_bg_color', '#f2f2f2'),
  104. 'terminal_panel_color': kwargs.get('terminal_panel_color', '#F0F0F0'),
  105. 'terminal_font_color': kwargs.get('terminal_font_color', '#000000'),
  106. 'terminal_font_family': kwargs.get('terminal_font_family', None),
  107. 'terminal_font_weight': _get_font_weight(kwargs),
  108. 'terminal_font_size': kwargs.get('terminal_font_size', None),
  109. 'richtext_controls': kwargs.get('richtext_controls', False),
  110. 'error_color': kwargs.get('error_color', '#ea7878'),
  111. # TODO: remove. Only useful for testing
  112. 'cli': kwargs.get('cli', sys.argv),
  113. })
  114. def parse_events(events: List[str]) -> List[str]:
  115. if not isinstance(events, list):
  116. raise TypeError(
  117. f"`use_events` requires a list of events. You provided "
  118. "{events}. \n"
  119. "Example: \n"
  120. "\tfrom gooey import Events"
  121. "\t@Gooey(use_events=[Events.VALIDATE_FORM]")
  122. unknown_events = set(events) - set(Events)
  123. if unknown_events:
  124. raise ValueError(
  125. f'nrecognized event(s) were passed to `use_events`: {unknown_events}\n'
  126. f'Must be one of {Events._fields}\n'
  127. f'Consider using the `Events` object: `from gooey import Events`')
  128. else:
  129. return events