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. print '|-------------------------'
  58. def _display(self, _type, something):
  59. for i in something:
  60. print _type, i
  61. def get_counter_actions(self, actions):
  62. """
  63. Returns all instances of type _CountAction
  64. """
  65. return [action
  66. for action in actions
  67. if isinstance(action, _CountAction)]
  68. def get_positionals(self, actions):
  69. """
  70. Get all required (positional) actions
  71. """
  72. return [action
  73. for action in actions
  74. if not action.option_strings]
  75. def get_optionals_without_choices(self, actions):
  76. """
  77. All actions which are:
  78. (a) Optional, but without required choices
  79. (b) Not of a "boolean" type (storeTrue, etc..)
  80. (c) Of type _AppendAction
  81. e.g. anything which has an argument style like:
  82. >>> -f myfilename.txt
  83. """
  84. boolean_actions = (
  85. _StoreConstAction, _StoreFalseAction,
  86. _StoreTrueAction
  87. )
  88. return [action
  89. for action in actions
  90. if action.option_strings
  91. and not action.choices
  92. and not isinstance(action, _CountAction)
  93. and not isinstance(action, _HelpAction)
  94. and type(action) not in boolean_actions]
  95. def get_optionals_with_choices(self, actions):
  96. """
  97. All optional arguments which are constrained
  98. to specific choices.
  99. """
  100. return [action
  101. for action in actions
  102. if action.choices]
  103. def get_flag_style_optionals(self, actions):
  104. """
  105. Gets all instances of "flag" type options.
  106. i.e. options which either store a const, or
  107. store boolean style options (e.g. StoreTrue).
  108. Types:
  109. _StoreTrueAction
  110. _StoreFalseAction
  111. _StoreConst
  112. """
  113. return [action
  114. for action in actions
  115. if isinstance(action, _StoreTrueAction)
  116. or isinstance(action, _StoreFalseAction)
  117. or isinstance(action, _StoreConstAction)]
  118. if __name__ == '__main__':
  119. pass