From ed264117ad04fb6a0e8b2fb022bf612055267a06 Mon Sep 17 00:00:00 2001 From: Jonathan Soma Date: Thu, 6 Oct 2022 18:00:31 -0400 Subject: [PATCH] closes #848 - allows custom formatters for arguments --- gooey/gui/components/widgets/bases.py | 10 +++++++++- gooey/tests/test_common.py | 28 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/gooey/gui/components/widgets/bases.py b/gooey/gui/components/widgets/bases.py index 11a19bf4..89d5c95a 100644 --- a/gooey/gui/components/widgets/bases.py +++ b/gooey/gui/components/widgets/bases.py @@ -196,10 +196,18 @@ def getValue(self) -> t.FieldValue: 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'], diff --git a/gooey/tests/test_common.py b/gooey/tests/test_common.py index 57d68024..d176c5f9 100644 --- a/gooey/tests/test_common.py +++ b/gooey/tests/test_common.py @@ -50,5 +50,33 @@ def testInitialValue(self): 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()