Browse Source

Subparser support in argparse -> json. pep8 changes

pull/90/merge
chriskiehl 10 years ago
parent
commit
00fa06ff9e
4 changed files with 66 additions and 32 deletions
  1. 4
      gooey/python_bindings/argparse_to_json.py
  2. 4
      gooey/python_bindings/config_generator.py
  3. 1
      gooey/python_bindings/gooey_decorator.py
  4. 89
      gooey/python_bindings/gooey_parser.py

4
gooey/python_bindings/argparse_to_json.py

@ -41,9 +41,9 @@ def convert(parser):
if has_subparsers(actions):
if has_required(actions):
raise UnsupportedConfiguration("Gooey doesn't currently support required arguments when subprocesers are present.")
raise UnsupportedConfiguration("Gooey doesn't currently support required arguments when subparsers are present.")
layout_type = 'column'
layout_data = {name: process(sub_parser, widget_dict) for name, sub_parser in get_subparser(actions).choices.iteritems()}
layout_data = {name.lower(): process(sub_parser, widget_dict) for name, sub_parser in get_subparser(actions).choices.iteritems()}
else:
layout_type = 'standard'
layout_data = process(parser, widget_dict)

4
gooey/python_bindings/config_generator.py

@ -18,8 +18,8 @@ def create_from_parser(parser, source_path, **kwargs):
'show_config': show_config,
'show_advanced': kwargs.get('show_advanced', True),
'default_size': kwargs.get('default_size', (610, 530)),
'requireds_cols': kwargs.get('required_cols', 1),
'optionals_cols': kwargs.get('optional_cols', 3),
'num_required_cols': kwargs.get('required_cols', 1),
'num_optional_cols': kwargs.get('optional_cols', 3),
'manual_start': False,
'layout_type': 'column'
}

1
gooey/python_bindings/gooey_decorator.py

@ -47,7 +47,6 @@ def Gooey(f=None,
print( 'Writing Build Config to: {}'.format(config_path))
with open(config_path, 'w') as f:
f.write(json.dumps(build_spec, indent=2))
application.run(build_spec)
def inner2(*args, **kwargs):

89
gooey/python_bindings/gooey_parser.py

@ -1,40 +1,75 @@
from argparse import ArgumentParser, _SubParsersAction
from argparse import ArgumentParser
class GooeySubParser(_SubParsersAction):
def __init__(self, *args, **kwargs):
super(GooeySubParser, self).__init__(*args, **kwargs)
class GooeyParser(object):
def __init__(self, **kwargs):
self.__dict__['parser'] = ArgumentParser(**kwargs)
self.widgets = {}
def __init__(self, **kwargs):
self.__dict__['parser'] = ArgumentParser(**kwargs)
self.widgets = {}
@property
def _mutually_exclusive_groups(self):
return self.parser._mutually_exclusive_groups
@property
def _actions(self):
return self.parser._actions
@property
def description(self):
return self.parser.description
def add_argument(self, *args, **kwargs):
widget = kwargs.pop('widget', None)
self.parser.add_argument(*args, **kwargs)
self.widgets[self.parser._actions[-1].dest] = widget
def add_mutually_exclusive_group(self, **kwargs):
return self.parser.add_mutually_exclusive_group(**kwargs)
def add_argument_group(self, *args, **kwargs):
return self.parser.add_argument_group(*args, **kwargs)
@property
def _mutually_exclusive_groups(self):
return self.parser._mutually_exclusive_groups
def parse_args(self, args=None, namespace=None):
return self.parser.parse_args(args, namespace)
@property
def _actions(self):
return self.parser._actions
def add_subparsers(self, **kwargs):
if self._subparsers is not None:
self.error(_('cannot have multiple subparser arguments'))
@property
def description(self):
return self.parser.description
# add the parser class to the arguments if it's not present
kwargs.setdefault('parser_class', type(self))
def add_argument(self, *args, **kwargs):
widget = kwargs.pop('widget', None)
self.parser.add_argument(*args, **kwargs)
self.widgets[self.parser._actions[-1].dest] = widget
if 'title' in kwargs or 'description' in kwargs:
title = _(kwargs.pop('title', 'subcommands'))
description = _(kwargs.pop('description', None))
self._subparsers = self.add_argument_group(title, description)
else:
self._subparsers = self._positionals
def add_mutually_exclusive_group(self, **kwargs):
return self.parser.add_mutually_exclusive_group(**kwargs)
# prog defaults to the usage message of this parser, skipping
# optional arguments and with no "usage:" prefix
if kwargs.get('prog') is None:
formatter = self._get_formatter()
positionals = self._get_positional_actions()
groups = self._mutually_exclusive_groups
formatter.add_usage(self.usage, positionals, groups, '')
kwargs['prog'] = formatter.format_help().strip()
def add_argument_group(self, *args, **kwargs):
return self.parser.add_argument_group(*args, **kwargs)
# create the parsers action and add it to the positionals list
parsers_class = self._pop_action_class(kwargs, 'parsers')
action = parsers_class(option_strings=[], **kwargs)
self._subparsers._add_action(action)
def parse_args(self, args=None, namespace=None):
return self.parser.parse_args(args, namespace)
# return the created parsers action
return action
def __getattr__(self, item):
return getattr(self.parser, item)
def __getattr__(self, item):
return getattr(self.parser, item)
def __setattr__(self, key, value):
return setattr(self.parser, key, value)
def __setattr__(self, key, value):
return setattr(self.parser, key, value)
Loading…
Cancel
Save