Browse Source

closes #848 - allows custom formatters for arguments

pull/849/head
Jonathan Soma 2 years ago
parent
commit
ed264117ad
2 changed files with 37 additions and 1 deletions
  1. 10
      gooey/gui/components/widgets/bases.py
  2. 28
      gooey/tests/test_common.py

10
gooey/gui/components/widgets/bases.py

@ -196,10 +196,18 @@ class TextContainer(BaseWidget):
else eval('lambda user_input: bool(%s)' % userValidator)
satisfies = testFunc if self._meta['required'] else ifPresent(testFunc)
value = self.getWidgetValue()
cmd = self.formatOutput(self._meta, value)
if 'cli_formatter' in self._options:
cmd = self._options['cli_formatter'].format(cmd=cmd,
value=value,
id=self._id,
meta=self._meta,
options=self._options,
dest=self._meta.get('dest'))
return t.FieldValue( # type: ignore
id=self._id,
cmd=self.formatOutput(self._meta, value),
cmd=cmd,
meta=self._meta,
rawValue= value,
# type=self.info['type'],

28
gooey/tests/test_common.py

@ -50,5 +50,33 @@ class TestCommonProperties(unittest.TestCase):
self.assertEqual(widget.getValue()['rawValue'], case.initialExpected)
def testCustomFormatter(self):
widgets = ['ColourChooser',
'CommandField',
'DateChooser', 'DirChooser', 'FileChooser', 'FileSaver',
'FilterableDropdown', 'MultiDirChooser', 'MultiFileChooser',
'PasswordField', 'TextField', 'Textarea', 'TimeChooser']
cases = [
Case(
{'default': 'default'},
'--widget \'default\''),
Case(
{'default': 'default', 'gooey_options': {'cli_formatter': 'test=value'}},
'test=value'),
Case(
{'default': 'default', 'gooey_options': {'cli_formatter': '{dest}={value}'}},
'widget=default'),
]
for widgetName in widgets:
with self.subTest(widgetName):
for case in cases:
parser = self.makeParser(widget=widgetName, **case.inputs)
with instrumentGooey(parser) as (app, gooeyApp):
widget = gooeyApp.configs[0].reifiedWidgets[0]
self.assertEqual(widget.getValue()['cmd'], case.initialExpected)
if __name__ == '__main__':
unittest.main()
Loading…
Cancel
Save