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.

158 lines
6.1 KiB

  1. from argparse import ArgumentParser, _SubParsersAction
  2. from argparse import _MutuallyExclusiveGroup, _ArgumentGroup
  3. from textwrap import dedent
  4. from gooey.gui.lang.i18n import _
  5. class GooeySubParser(_SubParsersAction):
  6. def __init__(self, *args, **kwargs):
  7. super(GooeySubParser, self).__init__(*args, **kwargs)
  8. # TODO: figure out how to correctly dispatch all of these
  9. # so that the individual wrappers aren't needed
  10. class GooeyArgumentGroup(_ArgumentGroup):
  11. def __init__(self, parser, widgets, options, *args, **kwargs):
  12. self.parser = parser
  13. self.widgets = widgets
  14. self.options = options
  15. super(GooeyArgumentGroup, self).__init__(self.parser, *args, **kwargs)
  16. def add_argument(self, *args, **kwargs):
  17. widget = kwargs.pop('widget', None)
  18. metavar = kwargs.pop('metavar', None)
  19. options = kwargs.pop('gooey_options', None)
  20. super(GooeyArgumentGroup, self).add_argument(*args, **kwargs)
  21. self.parser._actions[-1].metavar = metavar
  22. self.widgets[self.parser._actions[-1].dest] = widget
  23. self.options[self.parser._actions[-1].dest] = options
  24. def add_argument_group(self, *args, **kwargs):
  25. options = kwargs.pop('gooey_options', {})
  26. group = GooeyArgumentGroup(self.parser, self.widgets, self.options, *args, **kwargs)
  27. group.gooey_options = options
  28. self._action_groups.append(group)
  29. return group
  30. def add_mutually_exclusive_group(self, *args, **kwargs):
  31. options = kwargs.pop('gooey_options', {})
  32. container = self
  33. group = GooeyMutuallyExclusiveGroup(container, self.parser, self.widgets, self.options, *args, **kwargs)
  34. group.gooey_options = options
  35. self.parser._mutually_exclusive_groups.append(group)
  36. return group
  37. class GooeyMutuallyExclusiveGroup(_MutuallyExclusiveGroup):
  38. def __init__(self, container, parser, widgets, options, *args, **kwargs):
  39. self.parser = parser
  40. self.widgets = widgets
  41. self.options = options
  42. super(GooeyMutuallyExclusiveGroup, self).__init__(container, *args, **kwargs)
  43. def add_argument(self, *args, **kwargs):
  44. widget = kwargs.pop('widget', None)
  45. metavar = kwargs.pop('metavar', None)
  46. options = kwargs.pop('gooey_options', None)
  47. super(GooeyMutuallyExclusiveGroup, self).add_argument(*args, **kwargs)
  48. self.parser._actions[-1].metavar = metavar
  49. self.widgets[self.parser._actions[-1].dest] = widget
  50. self.options[self.parser._actions[-1].dest] = options
  51. class GooeyParser(object):
  52. def __init__(self, **kwargs):
  53. self.__dict__['parser'] = ArgumentParser(**kwargs)
  54. self.widgets = {}
  55. self.options = {}
  56. @property
  57. def _mutually_exclusive_groups(self):
  58. return self.parser._mutually_exclusive_groups
  59. @property
  60. def _actions(self):
  61. return self.parser._actions
  62. @property
  63. def description(self):
  64. return self.parser.description
  65. def add_argument(self, *args, **kwargs):
  66. widget = kwargs.pop('widget', None)
  67. metavar = kwargs.pop('metavar', None)
  68. options = kwargs.pop('gooey_options', None)
  69. self.parser.add_argument(*args, **kwargs)
  70. self.parser._actions[-1].metavar = metavar
  71. self.widgets[self.parser._actions[-1].dest] = widget
  72. self.options[self.parser._actions[-1].dest] = options
  73. self._validate_constraints(
  74. self.parser._actions[-1],
  75. widget,
  76. options or {},
  77. **kwargs
  78. )
  79. def add_mutually_exclusive_group(self, *args, **kwargs):
  80. options = kwargs.pop('gooey_options', {})
  81. group = GooeyMutuallyExclusiveGroup(self, self.parser, self.widgets, self.options, *args, **kwargs)
  82. group.gooey_options = options
  83. self.parser._mutually_exclusive_groups.append(group)
  84. return group
  85. def add_argument_group(self, *args, **kwargs):
  86. options = kwargs.pop('gooey_options', {})
  87. group = GooeyArgumentGroup(self.parser, self.widgets, self.options, *args, **kwargs)
  88. group.gooey_options = options
  89. self.parser._action_groups.append(group)
  90. return group
  91. def parse_args(self, args=None, namespace=None):
  92. return self.parser.parse_args(args, namespace)
  93. def add_subparsers(self, **kwargs):
  94. if self._subparsers is not None:
  95. self.error(_('cannot have multiple subparser arguments'))
  96. # add the parser class to the arguments if it's not present
  97. kwargs.setdefault('parser_class', type(self))
  98. if 'title' in kwargs or 'description' in kwargs:
  99. title = _(kwargs.pop('title', 'subcommands'))
  100. description = _(kwargs.pop('description', None))
  101. self._subparsers = self.add_argument_group(title, description)
  102. else:
  103. self._subparsers = self._positionals
  104. # prog defaults to the usage message of this parser, skipping
  105. # optional arguments and with no "usage:" prefix
  106. if kwargs.get('prog') is None:
  107. formatter = self._get_formatter()
  108. positionals = self._get_positional_actions()
  109. groups = self._mutually_exclusive_groups
  110. formatter.add_usage(self.usage, positionals, groups, '')
  111. kwargs['prog'] = formatter.format_help().strip()
  112. # create the parsers action and add it to the positionals list
  113. parsers_class = self._pop_action_class(kwargs, 'parsers')
  114. action = parsers_class(option_strings=[], **kwargs)
  115. self._subparsers._add_action(action)
  116. # return the created parsers action
  117. return action
  118. def _validate_constraints(self, parser_action, widget, options, **kwargs):
  119. from gooey.python_bindings import constraints
  120. constraints.assert_listbox_constraints(widget, **kwargs)
  121. constraints.assert_visibility_requirements(parser_action, options)
  122. def __getattr__(self, item):
  123. return getattr(self.parser, item)
  124. def __setattr__(self, key, value):
  125. return setattr(self.parser, key, value)