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.

106 lines
4.1 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import json
  2. from itertools import chain
  3. from copy import deepcopy
  4. from gooey.util.functional import compact
  5. from typing import List, Optional
  6. from gooey.gui.constants import VALUE_PLACEHOLDER
  7. from gooey.gui.formatters import formatArgument
  8. from gooey.python_bindings.types import FieldValue, Group, Item
  9. from gooey.util.functional import merge # type: ignore
  10. from gooey.gui.state import FullGooeyState
  11. '''
  12. primary :: Target -> Command -> Array Arg -> Array Arg -> Boolean -> CliString
  13. validateForm :: Target -> Command -> Array Arg -> Array Arg -> CliString
  14. validateField :: Target -> Command -> Array Arg -> Array Arg -> ArgId -> CliString
  15. completed :: Target -> Command -> FromState -> CliString
  16. failed :: Target -> Command -> FromState -> CliString
  17. fieldAction :: Target -> Command ->
  18. '''
  19. def buildSuccessCmd(state: FullGooeyState):
  20. subcommand = state['subcommands'][state['activeSelection']]
  21. widgets = state['widgets'][subcommand]
  22. def onSuccessCmd(target: str, subCommand: str, formState: List[str]) -> str:
  23. command = subCommand if not subCommand == '::gooey/default' else ''
  24. return f'{target} {command} --gooey-on-success {json.dumps(formState)}'
  25. def onErrorCmd(target: str, subCommand: str, formState: List[str]) -> str:
  26. command = subCommand if not subCommand == '::gooey/default' else ''
  27. return f'{target} {command} --gooey-on-error {json.dumps(formState)}'
  28. def formValidationCmd(target: str, subCommand: str, positionals: List[FieldValue], optionals: List[FieldValue]) -> str:
  29. positional_args = [cmdOrPlaceholderOrNone(x) for x in positionals]
  30. optional_args = [cmdOrPlaceholderOrNone(x) for x in optionals]
  31. command = subCommand if not subCommand == '::gooey/default' else ''
  32. return u' '.join(compact([
  33. target,
  34. command,
  35. *optional_args,
  36. '--gooey-validate-form',
  37. '--' if positional_args else '',
  38. *positional_args]))
  39. def cliCmd(target: str,
  40. subCommand: str,
  41. positionals: List[FieldValue],
  42. optionals: List[FieldValue],
  43. suppress_gooey_flag=False) -> str:
  44. positional_args = [arg['cmd'] for arg in positionals]
  45. optional_args = [arg['cmd'] for arg in optionals]
  46. command = subCommand if not subCommand == '::gooey/default' else ''
  47. ignore_flag = '' if suppress_gooey_flag else '--ignore-gooey'
  48. return u' '.join(compact([
  49. target,
  50. command,
  51. *optional_args,
  52. ignore_flag,
  53. '--' if positional_args else '',
  54. *positional_args]))
  55. def cmdOrPlaceholderOrNone(field: FieldValue) -> Optional[str]:
  56. # Argparse has a fail-fast-and-exit behavior for any missing
  57. # values. This poses a problem for dynamic validation, as we
  58. # want to collect _all_ errors to be more useful to the user.
  59. # As such, if there is no value currently available, we pass
  60. # through a stock placeholder values which allows GooeyParser
  61. # to handle it being missing without Argparse exploding due to
  62. # it actually being missing.
  63. if field['clitype'] == 'positional':
  64. return field['cmd'] or VALUE_PLACEHOLDER
  65. elif field['clitype'] != 'positional' and field['meta']['required']:
  66. # same rationale applies here. We supply the argument
  67. # along with a fixed placeholder (when relevant i.e. `store`
  68. # actions)
  69. return field['cmd'] or formatArgument(field['meta'], VALUE_PLACEHOLDER)
  70. else:
  71. # Optional values are, well, optional. So, like usual, we send
  72. # them if present or drop them if not.
  73. return field['cmd']
  74. def buildCliString(target, subCommand, positional, optional, suppress_gooey_flag=False):
  75. positionals = deepcopy(positional)
  76. if positionals:
  77. positionals.insert(0, "--")
  78. arguments = ' '.join(compact(chain(optional, positionals)))
  79. if subCommand != '::gooey/default':
  80. arguments = u'{} {}'.format(subCommand, arguments)
  81. ignore_flag = '' if suppress_gooey_flag else '--ignore-gooey'
  82. return u'{} {} {}'.format(target, ignore_flag, arguments)