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
5.3 KiB

2 years ago
2 years ago
2 years ago
  1. import os
  2. import itertools
  3. from gooey.gui.util.quoting import quote
  4. from gooey.python_bindings.types import EnrichedItem, FormField
  5. from gooey.gui.constants import VALUE_PLACEHOLDER, RADIO_PLACEHOLDER
  6. from gooey.util.functional import assoc, associnMany
  7. def value(field: FormField):
  8. if field['type'] in ['Checkbox', 'BlockCheckbox']:
  9. return field['checked'] # type: ignore
  10. elif field['type'] in ['Dropdown', 'Listbox', 'Counter']:
  11. return field['selected'] # type: ignore
  12. elif field['type'] == 'RadioGroup':
  13. if field['selected'] is not None: # type: ignore
  14. return value(field['options'][field['selected']]) # type: ignore
  15. else:
  16. return None
  17. else:
  18. return field['value'] # type: ignore
  19. def add_placeholder(field: FormField, placeholder=VALUE_PLACEHOLDER):
  20. """
  21. TODO: Docs about placeholders
  22. """
  23. if field['type'] in ['Checkbox', 'CheckBox', 'BlockCheckbox']:
  24. # there's no sane placeholder we can make for this one, as
  25. # it's kind of a nonsensical case: a required optional flag.
  26. # We set it to True here, which is equally nonsensical, but
  27. # ultimately will allow the validation to pass. We have no
  28. # way of passing a placeholder without even MORE monket patching
  29. # of the user's parser to rewrite the action type
  30. return assoc(field, 'checked', True)
  31. elif field['type'] in ['Dropdown', 'Listbox', 'Counter']:
  32. return assoc(field, 'selected', placeholder)
  33. elif field['type'] == 'RadioGroup':
  34. # We arbitrarily attach a placeholder for first RadioGroup option
  35. # and mark it as the selected one.
  36. return {
  37. **field,
  38. 'selected': 0,
  39. 'options': [
  40. add_placeholder(field['options'][0], placeholder=RADIO_PLACEHOLDER), # type: ignore
  41. *field['options'][1:] # type: ignore
  42. ]
  43. }
  44. else:
  45. return assoc(field, 'value', placeholder)
  46. def formatArgument(item: EnrichedItem):
  47. if item['type'] in ['Checkbox', 'CheckBox', 'BlockCheckbox']:
  48. return checkbox(item['data'], value(item['field']))
  49. elif item['type'] == 'MultiFileChooser':
  50. return multiFileChooser(item['data'], value(item['field']))
  51. elif item['type'] == 'Textarea':
  52. return textArea(item['data'], value(item['field']))
  53. elif item['type'] == 'CommandField':
  54. return textArea(item['data'], value(item['field']))
  55. elif item['type'] == 'Counter':
  56. return counter(item['data'], value(item['field']))
  57. elif item['type'] == 'Dropdown':
  58. return dropdown(item['data'], value(item['field']))
  59. elif item['type'] == 'Listbox':
  60. return listbox(item['data'], value(item['field']))
  61. elif item['type'] == 'RadioGroup':
  62. selected = item['field']['selected'] # type: ignore
  63. if selected is not None:
  64. formField = item['field']['options'][selected] # type: ignore
  65. argparseDefinition = item['data']['widgets'][selected] # type: ignore
  66. return formatArgument(assoc(argparseDefinition, 'field', formField)) # type: ignore
  67. else:
  68. return None
  69. else:
  70. return general(item['data'], value(item['field']))
  71. def placeholder(item: EnrichedItem):
  72. pass
  73. def checkbox(metadata, value):
  74. return metadata['commands'][0] if value else None
  75. def multiFileChooser(metadata, value):
  76. paths = ' '.join(quote(x) for x in value.split(os.pathsep) if x)
  77. if metadata['commands'] and paths:
  78. return u'{} {}'.format(metadata['commands'][0], paths)
  79. return paths or None
  80. def textArea(metadata, value):
  81. if metadata['commands'] and value:
  82. return '{} {}'.format(metadata['commands'][0], quote(value.encode('unicode_escape')))
  83. else:
  84. return quote(value.encode('unicode_escape')) if value else ''
  85. def commandField(metadata, value):
  86. if metadata['commands'] and value:
  87. return u'{} {}'.format(metadata['commands'][0], value)
  88. else:
  89. return value or None
  90. def counter(metatdata, value):
  91. '''
  92. Returns
  93. str(option_string * DropDown Value)
  94. e.g.
  95. -vvvvv
  96. '''
  97. if not str(value).isdigit():
  98. return None
  99. command = str(metatdata['commands'][0]).strip()
  100. return ' '.join(itertools.repeat(command, int(value)))
  101. def dropdown(metadata, value):
  102. if value == 'Select Option':
  103. return None
  104. elif metadata['commands'] and value:
  105. return u'{} {}'.format(metadata['commands'][0], quote(value))
  106. else:
  107. return quote(value) if value else ''
  108. def listbox(meta, value):
  109. if meta['commands'] and value:
  110. return u'{} {}'.format(meta['commands'][0], ' '.join(map(quote, value)))
  111. else:
  112. return ' '.join(map(quote, value)) if value else ''
  113. def general(metadata, value):
  114. if metadata.get('commands') and value:
  115. if not metadata.get('nargs'):
  116. v = quote(value)
  117. else:
  118. v = value
  119. return u'{0} {1}'.format(metadata['commands'][0], v)
  120. else:
  121. if not value:
  122. return None
  123. elif not metadata.get('nargs'):
  124. return quote(value)
  125. else:
  126. return value