You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
4.0 KiB

  1. from argparse import ArgumentParser, _SubParsersAction, _MutuallyExclusiveGroup
  2. from gooey.gui.lang.i18n import _
  3. class GooeySubParser(_SubParsersAction):
  4. def __init__(self, *args, **kwargs):
  5. super(GooeySubParser, self).__init__(*args, **kwargs)
  6. class GooeyMutuallyExclusiveGroup(_MutuallyExclusiveGroup):
  7. def __init__(self, parser, metadata, *args, **kwargs):
  8. self.parser = parser
  9. self.metadata = metadata
  10. title = kwargs.pop('title', None) # todo inc group num?
  11. super(GooeyMutuallyExclusiveGroup, self).__init__(self.parser, *args, **kwargs)
  12. self.title = title
  13. def add_argument(self, *args, **kwargs):
  14. widget = kwargs.pop('widget', None)
  15. validator = kwargs.pop('validate', None)
  16. metavar = kwargs.pop('metavar', None)
  17. super(GooeyMutuallyExclusiveGroup, self).add_argument(*args, **kwargs)
  18. self.parser._actions[-1].metavar = metavar
  19. self.metadata['widgets'][self.parser._actions[-1].dest] = widget
  20. self.metadata['validators'][self.parser._actions[-1].dest] = validator
  21. class GooeyParser(object):
  22. def __init__(self, **kwargs):
  23. self.__dict__['parser'] = ArgumentParser(**kwargs)
  24. # self.widgets = {}
  25. # self.validators = {}
  26. self.metadata = {
  27. 'widgets': {},
  28. 'validators': {}
  29. }
  30. @property
  31. def _mutually_exclusive_groups(self):
  32. return self.parser._mutually_exclusive_groups
  33. @property
  34. def _actions(self):
  35. return self.parser._actions
  36. @property
  37. def description(self):
  38. return self.parser.description
  39. def add_argument(self, *args, **kwargs):
  40. widget = kwargs.pop('widget', None)
  41. validator = kwargs.pop('validate', None)
  42. metavar = kwargs.pop('metavar', None)
  43. self.parser.add_argument(*args, **kwargs)
  44. self.parser._actions[-1].metavar = metavar
  45. self.metadata['widgets'][self.parser._actions[-1].dest] = widget
  46. self.metadata['validators'][self.parser._actions[-1].dest] = validator
  47. def add_mutually_exclusive_group(self, **kwargs):
  48. group = GooeyMutuallyExclusiveGroup(self.parser, self.metadata, **kwargs)
  49. self.parser._mutually_exclusive_groups.append(group)
  50. return group
  51. def add_argument_group(self, *args, **kwargs):
  52. return self.parser.add_argument_group(*args, **kwargs)
  53. def parse_args(self, args=None, namespace=None):
  54. return self.parser.parse_args(args, namespace)
  55. def add_subparsers(self, **kwargs):
  56. if self._subparsers is not None:
  57. self.error(_('cannot have multiple subparser arguments'))
  58. # add the parser class to the arguments if it's not present
  59. kwargs.setdefault('parser_class', type(self))
  60. if 'title' in kwargs or 'description' in kwargs:
  61. title = _(kwargs.pop('title', 'subcommands'))
  62. description = _(kwargs.pop('description', None))
  63. self._subparsers = self.add_argument_group(title, description)
  64. else:
  65. self._subparsers = self._positionals
  66. # prog defaults to the usage message of this parser, skipping
  67. # optional arguments and with no "usage:" prefix
  68. if kwargs.get('prog') is None:
  69. formatter = self._get_formatter()
  70. positionals = self._get_positional_actions()
  71. groups = self._mutually_exclusive_groups
  72. formatter.add_usage(self.usage, positionals, groups, '')
  73. kwargs['prog'] = formatter.format_help().strip()
  74. # create the parsers action and add it to the positionals list
  75. parsers_class = self._pop_action_class(kwargs, 'parsers')
  76. action = parsers_class(option_strings=[], **kwargs)
  77. self._subparsers._add_action(action)
  78. # return the created parsers action
  79. return action
  80. def __getattr__(self, item):
  81. return getattr(self.parser, item)
  82. def __setattr__(self, key, value):
  83. return setattr(self.parser, key, value)