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.

140 lines
5.3 KiB

  1. """
  2. Created on Dec 8, 2013
  3. @author: Chris
  4. """
  5. from argparse import (
  6. _CountAction,
  7. _HelpAction,
  8. _StoreConstAction,
  9. _StoreFalseAction,
  10. _StoreTrueAction
  11. )
  12. class ActionSorter(object):
  13. """
  14. Sorts all of the actions into their appropriate containers.
  15. Containers are based on the following map:
  16. COMPONENT MAP
  17. Action WxWidget
  18. --------------------------
  19. store TextCtrl
  20. store_const CheckBox
  21. store_true CheckBox
  22. store_False CheckBox
  23. append TextCtrl
  24. count DropDown
  25. choice DropDown
  26. Instance Variables:
  27. self._positionals
  28. self._choices
  29. self._optionals
  30. self._flags
  31. self._counters
  32. Example Argparse Def
  33. _HelpAction(option_strings=['-h', '--help'], dest='help', nargs=0, const=None, default='==SUPPRESS==', type=None, choices=None, help='show this help message and exit', metavar=None)
  34. _StoreAction(option_strings=[], dest='filename', nargs=None, const=None, default=None, type=None, choices=None, help='filename', metavar=None)
  35. _StoreTrueAction(option_strings=['-r', '--recursive'], dest='recurse', nargs=0, const=True, default=False, type=None, choices=None, help='recurse into subfolders [default: %(default)s]', metavar=None)
  36. _CountAction(option_strings=['-v', '--verbose'], dest='verbose', nargs=0, const=None, default=None, type=None, choices=None, help='set verbosity level [default: %(default)s]', metavar=None)
  37. _AppendAction(option_strings=['-i', '--include'], dest='include', nargs=None, const=None, default=None, type=None, choices=None, help='only include paths matching this regex pattern. Note: exclude is given preference over include. [default: %(default)s]', metavar='RE')
  38. _StoreAction(option_strings=['-e', '--exclude'], dest='exclude', nargs=None, const=None, default=None, type=None, choices=None, help='exclude paths matching this regex pattern. [default: %(default)s]', metavar='RE')
  39. _VersionAction(option_strings=['-V', '--version'], dest='version', nargs=0, const=None, default='==SUPPRESS==', type=None, choices=None, help="show program's version number and exit", metavar=None)
  40. _StoreAction(option_strings=['-T', '--tester'], dest='tester', nargs=None, const=None, default=None, type=None, choices=['yes', 'no'], help=None, metavar=None)
  41. _StoreAction(option_strings=[], dest='paths', nargs='+', const=None, default=None, type=None, choices=None, help='paths to folder(s) with source file(s) [default: %(default)s]', metavar='path')
  42. usage: example_argparse_souce.py [-h] [-r] [-v] [-i RE] [-e RE] [-V]
  43. """
  44. def __init__(self, actions):
  45. self._actions = actions[:]
  46. self._positionals = self.get_positionals(self._actions)
  47. self._choices = self.get_optionals_with_choices(self._actions)
  48. self._optionals = self.get_optionals_without_choices(self._actions)
  49. self._flags = self.get_flag_style_optionals(self._actions)
  50. self._counters = self.get_counter_actions(self._actions)
  51. def verbose(self):
  52. self._display('ActionSorter: positionals', self._positionals)
  53. self._display('ActionSorter: choices', self._choices)
  54. self._display('ActionSorter: optionals', self._optionals)
  55. self._display('ActionSorter: booleans', self._flags)
  56. self._display('ActionSorter: counters', self._counters)
  57. def _display(self, _type, something):
  58. for i in something:
  59. print _type, i
  60. def get_counter_actions(self, actions):
  61. """
  62. Returns all instances of type _CountAction
  63. """
  64. return [action
  65. for action in actions
  66. if isinstance(action, _CountAction)]
  67. def get_positionals(self, actions):
  68. """
  69. Get all required (positional) actions
  70. """
  71. return [action
  72. for action in actions
  73. if not action.option_strings]
  74. def get_optionals_without_choices(self, actions):
  75. """
  76. All actions which are:
  77. (a) Optional, but without required choices
  78. (b) Not of a "boolean" type (storeTrue, etc..)
  79. (c) Of type _AppendAction
  80. e.g. anything which has an argument style like:
  81. >>> -f myfilename.txt
  82. """
  83. boolean_actions = (
  84. _StoreConstAction, _StoreFalseAction,
  85. _StoreTrueAction
  86. )
  87. return [action
  88. for action in actions
  89. if action.option_strings
  90. and not action.choices
  91. and not isinstance(action, _CountAction)
  92. and not isinstance(action, _HelpAction)
  93. and type(action) not in boolean_actions]
  94. def get_optionals_with_choices(self, actions):
  95. """
  96. All optional arguments which are constrained
  97. to specific choices.
  98. """
  99. return [action
  100. for action in actions
  101. if action.choices]
  102. def get_flag_style_optionals(self, actions):
  103. """
  104. Gets all instances of "flag" type options.
  105. i.e. options which either store a const, or
  106. store boolean style options (e.g. StoreTrue).
  107. Types:
  108. _StoreTrueAction
  109. _StoreFalseAction
  110. _StoreConst
  111. """
  112. return [action
  113. for action in actions
  114. if isinstance(action, _StoreTrueAction)
  115. or isinstance(action, _StoreFalseAction)
  116. or isinstance(action, _StoreConstAction)]
  117. if __name__ == '__main__':
  118. pass