Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 756 nargs initial values #757

Open
wants to merge 1 commit into
base: 1.0.9-release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 15 additions & 25 deletions gooey/python_bindings/argparse_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,15 +448,13 @@ def action_to_json(action, widget, options):
},
})

if (options.get(action.dest) or {}).get('initial_value') != None:
if (options.get(action.dest) or {}).get('initial_value') is not None:
value = options[action.dest]['initial_value']
options[action.dest]['initial_value'] = handle_initial_values(action, widget, value)
default = handle_initial_values(action, widget, action.default)
if default == argparse.SUPPRESS:
default = None



final_options = merge(base, options.get(action.dest) or {})
validate_gooey_options(action, widget, final_options)

Expand Down Expand Up @@ -529,19 +527,18 @@ def coerce_default(default, widget):


def handle_initial_values(action, widget, value):
handlers = [
[textinput_with_nargs_and_list_default, coerse_nargs_list],
[is_widget('Listbox'), clean_list_defaults],
[is_widget('Dropdown'), coerce_str],
[is_widget('Counter'), safe_string]
]
for matches, apply_coercion in handlers:
if matches(action, widget):
return apply_coercion(value)
return clean_default(value)


def coerse_nargs_list(default):
if textinput_with_nargs_and_list_default(action, widget, value):
return coerce_nargs_list(value)

dispatcher = {
'Listbox': clean_list_defaults,
'Dropdown': coerce_str,
'Counter': safe_string
}
return dispatcher.get(widget, clean_default)(value)


def coerce_nargs_list(default):
"""
nargs=* and defaults which are collection types
must be transformed into a CLI equivalent form. So, for
Expand All @@ -555,13 +552,8 @@ def coerse_nargs_list(default):
"""
return ' '.join('"{}"'.format(x) for x in default)

def is_widget(name):
def equals(action, widget):
return widget == name
return equals


def textinput_with_nargs_and_list_default(action, widget):
def textinput_with_nargs_and_list_default(action, widget, value):
"""
Vanilla TextInputs which have nargs options which produce lists (i.e.
nargs +, *, N, or REMAINDER) need to have their default values transformed
Expand All @@ -570,7 +562,7 @@ def textinput_with_nargs_and_list_default(action, widget):
"""
return (
widget in {'TextField', 'Textarea', 'PasswordField'}
and (isinstance(action.default, list) or isinstance(action.default, tuple))
and (isinstance(value, list) or isinstance(value, tuple))
and is_list_based_nargs(action))


Expand All @@ -579,7 +571,6 @@ def is_list_based_nargs(action):
return isinstance(action.nargs, int) or action.nargs in {'*', '+', '...'}



def clean_list_defaults(default_values):
"""
Listbox's default's can be passed as a single value
Expand Down Expand Up @@ -627,7 +618,6 @@ def coerce_str(value):
return str(value) if value is not None else value



def this_is_a_comment(action, widget):
"""
TODO:
Expand Down
9 changes: 9 additions & 0 deletions gooey/tests/test_argparse_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ def test_textinput_with_list_default_mapped_to_cli_friendly_value(self):

Using nargs and a `default` value with a list causes the literal list string
to be put into the UI.

Also tests initial_value - see issue #756.
"""
testcases = [
{'nargs': '+', 'default': ['a b', 'c'], 'gooey_default': '"a b" "c"', 'w': 'TextField'},
Expand Down Expand Up @@ -216,6 +218,13 @@ def test_textinput_with_list_default_mapped_to_cli_friendly_value(self):
result = argparse_to_json.handle_initial_values(action, case['w'], action.default)
self.assertEqual(result, case['gooey_default'])

gooey_parser = GooeyParser(prog='test_program')
options = {'initial_value': case['default']}
gooey_parser.add_argument('--foo', nargs=case['nargs'], gooey_options=options)
action = gooey_parser._actions[-1]
result = argparse_to_json.handle_initial_values(action, case['w'], case['default'])
self.assertEqual(result, case['gooey_default'])

def test_nargs(self):
"""
so there are just a few simple rules here:
Expand Down