Browse Source

Issue 321 Pt II - convert default arguments to strings

pull/473/head
Chris 6 years ago
parent
commit
5ebcdd668c
3 changed files with 14 additions and 3 deletions
  1. 3
      gooey/gui/components/widgets/counter.py
  2. 10
      gooey/python_bindings/argparse_to_json.py
  3. 4
      gooey/tests/test_argparse_to_json.py

3
gooey/gui/components/widgets/counter.py

@ -6,7 +6,8 @@ from gooey.gui import formatters
class Counter(Dropdown):
def setValue(self, value):
self.widget.SetSelection(value)
index = self._meta['choices'].index(value) + 1
self.widget.SetSelection(index)
def formatOutput(self, metadata, value):
return formatters.counter(metadata, value)

10
gooey/python_bindings/argparse_to_json.py

@ -331,6 +331,13 @@ def action_to_json(action, widget, options):
},
})
# Issue #321:
# Defaults for choice types must be coerced to strings
# to be able to match the stringified `choices` used by `wx.ComboBox`
default = (str(clean_default(action.default))
if widget in dropdown_types
else clean_default(action.default))
return {
'id': action.option_strings[0] if action.option_strings else action.dest,
'type': widget,
@ -343,7 +350,7 @@ def action_to_json(action, widget, options):
'nargs': action.nargs or '',
'commands': action.option_strings,
'choices': list(map(str, action.choices)) if action.choices else [],
'default': clean_default(action.default),
'default': default,
'dest': action.dest,
},
'options': merge(base, options.get(action.dest) or {})
@ -355,6 +362,7 @@ def choose_cli_type(action):
if action.required and not action.option_strings \
else 'optional'
def clean_default(default):
'''
Attemps to safely coalesce the default value down to

4
gooey/tests/test_argparse_to_json.py

@ -53,9 +53,11 @@ class TestArgparse(unittest.TestCase):
Issue 321 - must coerce choice types to string to support wx.ComboBox
"""
parser = ArgumentParser()
parser.add_argument('--foo', choices=[1, 2, 3])
parser.add_argument('--foo', default=1, choices=[1, 2, 3])
choice_action = parser._actions[-1]
result = argparse_to_json.action_to_json(choice_action, 'Dropdown', {})
self.assertEqual(getin(result, ['data', 'choices']), ['1', '2', '3'])
# default value is also converted to a string type
self.assertEqual(getin(result, ['data', 'default']), '1')
Loading…
Cancel
Save