mirror of https://github.com/chriskiehl/Gooey.git
6 changed files with 145 additions and 87 deletions
Unified View
Diff Options
-
1README.md
-
6gooey/gui/model.py
-
10gooey/gui/widgets/components.py
-
35gooey/gui/widgets/widget_pack.py
-
3gooey/python_bindings/argparse_to_json.py
-
177gooey/python_bindings/gooey_parser.py
@ -1,96 +1,103 @@ |
|||||
from argparse import ArgumentParser, _SubParsersAction, _MutuallyExclusiveGroup |
from argparse import ArgumentParser, _SubParsersAction, _MutuallyExclusiveGroup |
||||
|
|
||||
from gooey.gui.lang.i18n import _ |
from gooey.gui.lang.i18n import _ |
||||
|
|
||||
|
|
||||
class GooeySubParser(_SubParsersAction): |
class GooeySubParser(_SubParsersAction): |
||||
def __init__(self, *args, **kwargs): |
def __init__(self, *args, **kwargs): |
||||
super(GooeySubParser, self).__init__(*args, **kwargs) |
super(GooeySubParser, self).__init__(*args, **kwargs) |
||||
|
|
||||
|
|
||||
class GooeyMutuallyExclusiveGroup(_MutuallyExclusiveGroup): |
class GooeyMutuallyExclusiveGroup(_MutuallyExclusiveGroup): |
||||
def __init__(self, parser, widgets, *args, **kwargs): |
def __init__(self, parser, widgets, *args, **kwargs): |
||||
self.parser = parser |
self.parser = parser |
||||
self.widgets = widgets |
self.widgets = widgets |
||||
super(GooeyMutuallyExclusiveGroup, self).__init__(self.parser, *args, **kwargs) |
super(GooeyMutuallyExclusiveGroup, self).__init__(self.parser, *args, |
||||
|
**kwargs) |
||||
|
|
||||
def add_argument(self, *args, **kwargs): |
def add_argument(self, *args, **kwargs): |
||||
widget = kwargs.pop('widget', None) |
widget = kwargs.pop('widget', None) |
||||
metavar = kwargs.pop('metavar', None) |
metavar = kwargs.pop('metavar', None) |
||||
super(GooeyMutuallyExclusiveGroup, self).add_argument(*args, **kwargs) |
super(GooeyMutuallyExclusiveGroup, self).add_argument(*args, **kwargs) |
||||
self.parser._actions[-1].metavar = metavar |
self.parser._actions[-1].metavar = metavar |
||||
self.widgets[self.parser._actions[-1].dest] = widget |
self.widgets[self.parser._actions[-1].dest] = widget |
||||
|
|
||||
|
|
||||
class GooeyParser(object): |
class GooeyParser(object): |
||||
def __init__(self, **kwargs): |
def __init__(self, **kwargs): |
||||
self.__dict__['parser'] = ArgumentParser(**kwargs) |
self.__dict__['parser'] = ArgumentParser(**kwargs) |
||||
self.widgets = {} |
self.widgets = {} |
||||
|
@property |
||||
@property |
def _mutually_exclusive_groups(self): |
||||
def _mutually_exclusive_groups(self): |
return self.parser._mutually_exclusive_groups |
||||
return self.parser._mutually_exclusive_groups |
@property |
||||
|
def _actions(self): |
||||
@property |
return self.parser._actions |
||||
def _actions(self): |
@property |
||||
return self.parser._actions |
def description(self): |
||||
|
return self.parser.description |
||||
@property |
def add_argument(self, *args, **kwargs): |
||||
def description(self): |
widget = kwargs.pop('widget', None) |
||||
return self.parser.description |
metavar = kwargs.pop('metavar', None) |
||||
|
if widget and widget == 'Listbox': |
||||
def add_argument(self, *args, **kwargs): |
if not 'nargs' in kwargs or kwargs['nargs'] not in ['*', '+']: |
||||
widget = kwargs.pop('widget', None) |
raise ValueError( |
||||
metavar = kwargs.pop('metavar', None) |
'Gooey\'s Listbox widget requires that nargs be specified.\n' |
||||
self.parser.add_argument(*args, **kwargs) |
'Nargs must be set to either `*` or `+` (e.g. nargs="*")' |
||||
self.parser._actions[-1].metavar = metavar |
) |
||||
self.widgets[self.parser._actions[-1].dest] = widget |
self.parser.add_argument(*args, **kwargs) |
||||
|
self.parser._actions[-1].metavar = metavar |
||||
# def add_mutually_exclusive_group(self, **kwargs): |
self.widgets[self.parser._actions[-1].dest] = widget |
||||
# return self.parser.add_mutually_exclusive_group(**kwargs) |
def add_mutually_exclusive_group(self, **kwargs): |
||||
|
group = GooeyMutuallyExclusiveGroup(self.parser, self.widgets, **kwargs) |
||||
def add_mutually_exclusive_group(self, **kwargs): |
self.parser._mutually_exclusive_groups.append(group) |
||||
group = GooeyMutuallyExclusiveGroup(self.parser, self.widgets, **kwargs) |
return group |
||||
self.parser._mutually_exclusive_groups.append(group) |
def add_argument_group(self, *args, **kwargs): |
||||
return group |
return self.parser.add_argument_group(*args, **kwargs) |
||||
|
def parse_args(self, args=None, namespace=None): |
||||
def add_argument_group(self, *args, **kwargs): |
return self.parser.parse_args(args, namespace) |
||||
return self.parser.add_argument_group(*args, **kwargs) |
def add_subparsers(self, **kwargs): |
||||
|
if self._subparsers is not None: |
||||
def parse_args(self, args=None, namespace=None): |
self.error(_('cannot have multiple subparser arguments')) |
||||
return self.parser.parse_args(args, namespace) |
# add the parser class to the arguments if it's not present |
||||
|
kwargs.setdefault('parser_class', type(self)) |
||||
def add_subparsers(self, **kwargs): |
if 'title' in kwargs or 'description' in kwargs: |
||||
if self._subparsers is not None: |
title = _(kwargs.pop('title', 'subcommands')) |
||||
self.error(_('cannot have multiple subparser arguments')) |
description = _(kwargs.pop('description', None)) |
||||
|
self._subparsers = self.add_argument_group(title, description) |
||||
# add the parser class to the arguments if it's not present |
else: |
||||
kwargs.setdefault('parser_class', type(self)) |
self._subparsers = self._positionals |
||||
|
# prog defaults to the usage message of this parser, skipping |
||||
if 'title' in kwargs or 'description' in kwargs: |
# optional arguments and with no "usage:" prefix |
||||
title = _(kwargs.pop('title', 'subcommands')) |
if kwargs.get('prog') is None: |
||||
description = _(kwargs.pop('description', None)) |
formatter = self._get_formatter() |
||||
self._subparsers = self.add_argument_group(title, description) |
positionals = self._get_positional_actions() |
||||
else: |
groups = self._mutually_exclusive_groups |
||||
self._subparsers = self._positionals |
formatter.add_usage(self.usage, positionals, groups, '') |
||||
|
kwargs['prog'] = formatter.format_help().strip() |
||||
# prog defaults to the usage message of this parser, skipping |
# create the parsers action and add it to the positionals list |
||||
# optional arguments and with no "usage:" prefix |
parsers_class = self._pop_action_class(kwargs, 'parsers') |
||||
if kwargs.get('prog') is None: |
action = parsers_class(option_strings=[], **kwargs) |
||||
formatter = self._get_formatter() |
self._subparsers._add_action(action) |
||||
positionals = self._get_positional_actions() |
# return the created parsers action |
||||
groups = self._mutually_exclusive_groups |
return action |
||||
formatter.add_usage(self.usage, positionals, groups, '') |
def __getattr__(self, item): |
||||
kwargs['prog'] = formatter.format_help().strip() |
return getattr(self.parser, item) |
||||
|
def __setattr__(self, key, value): |
||||
# create the parsers action and add it to the positionals list |
return setattr(self.parser, key, value) |
||||
parsers_class = self._pop_action_class(kwargs, 'parsers') |
|
||||
action = parsers_class(option_strings=[], **kwargs) |
|
||||
self._subparsers._add_action(action) |
|
||||
|
|
||||
# return the created parsers action |
|
||||
return action |
|
||||
|
|
||||
def __getattr__(self, item): |
|
||||
return getattr(self.parser, item) |
|
||||
|
|
||||
def __setattr__(self, key, value): |
|
||||
return setattr(self.parser, key, value) |
|
xxxxxxxxxx