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.

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