Browse Source

closes #655 - support action='version'

pull/662/head
Chris 3 years ago
parent
commit
22fefd99b4
3 changed files with 29 additions and 2 deletions
  1. 1
      README.md
  2. 10
      gooey/python_bindings/argparse_to_json.py
  3. 20
      gooey/tests/test_argparse_to_json.py

1
README.md

@ -163,6 +163,7 @@ Gooey does its best to choose sensible defaults based on the options it finds. C
| store_const | CheckBox |<img src="https://github.com/chriskiehl/GooeyImages/raw/images/readme-images/f538c850-07c5-11e5-8cbe-864badfa54a9.png"/>| | store_const | CheckBox |<img src="https://github.com/chriskiehl/GooeyImages/raw/images/readme-images/f538c850-07c5-11e5-8cbe-864badfa54a9.png"/>|
| store_true | CheckBox | <img src="https://github.com/chriskiehl/GooeyImages/raw/images/readme-images/f538c850-07c5-11e5-8cbe-864badfa54a9.png"/>| | store_true | CheckBox | <img src="https://github.com/chriskiehl/GooeyImages/raw/images/readme-images/f538c850-07c5-11e5-8cbe-864badfa54a9.png"/>|
| store_False | CheckBox| <img src="https://github.com/chriskiehl/GooeyImages/raw/images/readme-images/f538c850-07c5-11e5-8cbe-864badfa54a9.png"/> | | store_False | CheckBox| <img src="https://github.com/chriskiehl/GooeyImages/raw/images/readme-images/f538c850-07c5-11e5-8cbe-864badfa54a9.png"/> |
| version | CheckBox| <img src="https://github.com/chriskiehl/GooeyImages/raw/images/readme-images/f538c850-07c5-11e5-8cbe-864badfa54a9.png"/> |
| append | TextCtrl | <img src="https://github.com/chriskiehl/GooeyImages/raw/images/readme-images/f54e9f5e-07c5-11e5-86e5-82f011c538cf.png"/> | | append | TextCtrl | <img src="https://github.com/chriskiehl/GooeyImages/raw/images/readme-images/f54e9f5e-07c5-11e5-86e5-82f011c538cf.png"/> |
| count | DropDown &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | <img src="https://github.com/chriskiehl/GooeyImages/raw/images/readme-images/f53ccbe4-07c5-11e5-80e5-510e2aa22922.png"/> | | count | DropDown &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | <img src="https://github.com/chriskiehl/GooeyImages/raw/images/readme-images/f53ccbe4-07c5-11e5-80e5-510e2aa22922.png"/> |
| Mutually Exclusive Group | RadioGroup | <img src="https://github.com/chriskiehl/GooeyImages/raw/images/readme-images/f553feb8-07c5-11e5-9d5b-eaa4772075a9.png"/> | Mutually Exclusive Group | RadioGroup | <img src="https://github.com/chriskiehl/GooeyImages/raw/images/readme-images/f553feb8-07c5-11e5-9d5b-eaa4772075a9.png"/>

10
gooey/python_bindings/argparse_to_json.py

@ -12,7 +12,8 @@ from argparse import (
_StoreFalseAction, _StoreFalseAction,
_StoreTrueAction, _StoreTrueAction,
_StoreAction, _StoreAction,
_SubParsersAction)
_SubParsersAction,
_VersionAction)
from collections import OrderedDict from collections import OrderedDict
from functools import partial from functools import partial
from uuid import uuid4 from uuid import uuid4
@ -277,8 +278,10 @@ def categorize2(groups, widget_dict, options):
def categorize(actions, widget_dict, options): def categorize(actions, widget_dict, options):
_get_widget = partial(get_widget, widget_dict) _get_widget = partial(get_widget, widget_dict)
for action in actions: 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) yield build_radio_group(action, widget_dict, options)
elif is_standard(action): elif is_standard(action):
@ -352,6 +355,9 @@ def is_file(action):
''' action with FileType ''' ''' action with FileType '''
return isinstance(action.type, argparse.FileType) return isinstance(action.type, argparse.FileType)
def is_version(action):
return isinstance(action, _VersionAction)
def is_standard(action): def is_standard(action):
""" actions which are general "store" instructions. """ actions which are general "store" instructions.
e.g. anything which has an argument style like: e.g. anything which has an argument style like:

20
gooey/tests/test_argparse_to_json.py

@ -155,6 +155,26 @@ class TestArgparse(unittest.TestCase):
self.assertEqual(getin(item, ['data', 'default']), None) 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): def test_textinput_with_list_default_mapped_to_cli_friendly_value(self):
""" """
Issue: #500 Issue: #500

Loading…
Cancel
Save