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.

63 lines
1.7 KiB

  1. '''
  2. Created on Dec 8, 2013
  3. @author: Chris
  4. '''
  5. import itertools
  6. import components
  7. from gooey.gui import argparse_test_data
  8. class ComponentFactory(object):
  9. '''
  10. Aggregates all of the actions and
  11. '''
  12. def __init__(self, sorted_actions):
  13. self._actions = sorted_actions
  14. self.required_args = self.BuildPositionals(self._actions)
  15. self.flags = self.BuildFlags(self._actions)
  16. self.general_options = (self.BuildChoices(self._actions)
  17. + self.BuildOptionals(self._actions)
  18. + self.BuildCounters(self._actions))
  19. def BuildPositionals(self, actions):
  20. return self._AssembleWidgetsFromActions(actions, 'Positional', '_positionals')
  21. def BuildChoices(self, actions):
  22. return self._AssembleWidgetsFromActions(actions, 'Choice', '_choices')
  23. def BuildOptionals(self, actions):
  24. return self._AssembleWidgetsFromActions(actions, 'Optional', '_optionals')
  25. def BuildFlags(self, actions):
  26. return self._AssembleWidgetsFromActions(actions, 'Flag', '_flags')
  27. def BuildCounters(self, actions):
  28. return self._AssembleWidgetsFromActions(actions, 'Counter', '_counters')
  29. def _AssembleWidgetsFromActions(self, actions, classname, actiontype):
  30. cls = getattr(components, classname)
  31. actions_list = getattr(actions, actiontype)
  32. return [cls(action)
  33. for action in actions_list]
  34. def __iter__(self):
  35. '''
  36. return an iterator for all of the contained gui
  37. '''
  38. return itertools.chain(self.required_args,
  39. self.flags,
  40. self.general_options)
  41. if __name__ == '__main__':
  42. a = ComponentFactory(argparse_test_data.parser)