From 22fefd99b48f2e087377c48595634dc431adfd40 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 19 Dec 2020 16:40:14 -0800 Subject: [PATCH] closes #655 - support action='version' --- README.md | 1 + gooey/python_bindings/argparse_to_json.py | 10 ++++++++-- gooey/tests/test_argparse_to_json.py | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 873f35b..5e8b4a7 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,7 @@ Gooey does its best to choose sensible defaults based on the options it finds. C | store_const | CheckBox || | store_true | CheckBox | | | store_False | CheckBox| | +| version | CheckBox| | | append | TextCtrl | | | count | DropDown                  | | | Mutually Exclusive Group | RadioGroup | diff --git a/gooey/python_bindings/argparse_to_json.py b/gooey/python_bindings/argparse_to_json.py index 576b173..a0e9495 100644 --- a/gooey/python_bindings/argparse_to_json.py +++ b/gooey/python_bindings/argparse_to_json.py @@ -12,7 +12,8 @@ from argparse import ( _StoreFalseAction, _StoreTrueAction, _StoreAction, - _SubParsersAction) + _SubParsersAction, + _VersionAction) from collections import OrderedDict from functools import partial from uuid import uuid4 @@ -277,8 +278,10 @@ def categorize2(groups, widget_dict, options): def categorize(actions, widget_dict, options): _get_widget = partial(get_widget, widget_dict) for action in actions: + if is_version(action): + yield action_to_json(action, _get_widget(action, 'CheckBox'), options) - if is_mutex(action): + elif is_mutex(action): yield build_radio_group(action, widget_dict, options) elif is_standard(action): @@ -352,6 +355,9 @@ def is_file(action): ''' action with FileType ''' return isinstance(action.type, argparse.FileType) +def is_version(action): + return isinstance(action, _VersionAction) + def is_standard(action): """ actions which are general "store" instructions. e.g. anything which has an argument style like: diff --git a/gooey/tests/test_argparse_to_json.py b/gooey/tests/test_argparse_to_json.py index 77723fa..30d4d8d 100644 --- a/gooey/tests/test_argparse_to_json.py +++ b/gooey/tests/test_argparse_to_json.py @@ -155,6 +155,26 @@ class TestArgparse(unittest.TestCase): self.assertEqual(getin(item, ['data', 'default']), None) + def test_version_maps_to_checkbox(self): + testcases = [ + [['--version'], {}, 'TextField'], + # we only remap if the action is version + # i.e. we don't care about the argument name itself + [['--version'], {'action': 'store'}, 'TextField'], + # should get mapped to CheckBox becuase of the action + [['--version'], {'action': 'version'}, 'CheckBox'], + # ditto, even through the 'name' isn't 'version' + [['--foobar'], {'action': 'version'}, 'CheckBox'], + ] + for args, kwargs, expectedType in testcases: + with self.subTest([args, kwargs]): + parser = argparse.ArgumentParser(prog='test') + parser.add_argument(*args, **kwargs) + result = argparse_to_json.convert(parser, num_required_cols=2, num_optional_cols=2) + contents = getin(result, ['widgets', 'test', 'contents'])[0] + self.assertEqual(contents['items'][0]['type'], expectedType) + + def test_textinput_with_list_default_mapped_to_cli_friendly_value(self): """ Issue: #500