manager.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. """Option handling and Option management logic."""
  2. from __future__ import annotations
  3. import argparse
  4. import enum
  5. import functools
  6. import logging
  7. from typing import Any
  8. from typing import Callable
  9. from typing import Sequence
  10. from flake8 import utils
  11. from flake8.plugins.finder import Plugins
  12. LOG = logging.getLogger(__name__)
  13. # represent a singleton of "not passed arguments".
  14. # an enum is chosen to trick mypy
  15. _ARG = enum.Enum("_ARG", "NO")
  16. def _flake8_normalize(
  17. value: str,
  18. *args: str,
  19. comma_separated_list: bool = False,
  20. normalize_paths: bool = False,
  21. ) -> str | list[str]:
  22. ret: str | list[str] = value
  23. if comma_separated_list and isinstance(ret, str):
  24. ret = utils.parse_comma_separated_list(value)
  25. if normalize_paths:
  26. if isinstance(ret, str):
  27. ret = utils.normalize_path(ret, *args)
  28. else:
  29. ret = utils.normalize_paths(ret, *args)
  30. return ret
  31. class Option:
  32. """Our wrapper around an argparse argument parsers to add features."""
  33. def __init__(
  34. self,
  35. short_option_name: str | _ARG = _ARG.NO,
  36. long_option_name: str | _ARG = _ARG.NO,
  37. # Options below are taken from argparse.ArgumentParser.add_argument
  38. action: str | type[argparse.Action] | _ARG = _ARG.NO,
  39. default: Any | _ARG = _ARG.NO,
  40. type: Callable[..., Any] | _ARG = _ARG.NO,
  41. dest: str | _ARG = _ARG.NO,
  42. nargs: int | str | _ARG = _ARG.NO,
  43. const: Any | _ARG = _ARG.NO,
  44. choices: Sequence[Any] | _ARG = _ARG.NO,
  45. help: str | _ARG = _ARG.NO,
  46. metavar: str | _ARG = _ARG.NO,
  47. required: bool | _ARG = _ARG.NO,
  48. # Options below here are specific to Flake8
  49. parse_from_config: bool = False,
  50. comma_separated_list: bool = False,
  51. normalize_paths: bool = False,
  52. ) -> None:
  53. """Initialize an Option instance.
  54. The following are all passed directly through to argparse.
  55. :param short_option_name:
  56. The short name of the option (e.g., ``-x``). This will be the
  57. first argument passed to ``ArgumentParser.add_argument``
  58. :param long_option_name:
  59. The long name of the option (e.g., ``--xtra-long-option``). This
  60. will be the second argument passed to
  61. ``ArgumentParser.add_argument``
  62. :param default:
  63. Default value of the option.
  64. :param dest:
  65. Attribute name to store parsed option value as.
  66. :param nargs:
  67. Number of arguments to parse for this option.
  68. :param const:
  69. Constant value to store on a common destination. Usually used in
  70. conjunction with ``action="store_const"``.
  71. :param choices:
  72. Possible values for the option.
  73. :param help:
  74. Help text displayed in the usage information.
  75. :param metavar:
  76. Name to use instead of the long option name for help text.
  77. :param required:
  78. Whether this option is required or not.
  79. The following options may be passed directly through to :mod:`argparse`
  80. but may need some massaging.
  81. :param type:
  82. A callable to normalize the type (as is the case in
  83. :mod:`argparse`).
  84. :param action:
  85. Any action allowed by :mod:`argparse`.
  86. The following parameters are for Flake8's option handling alone.
  87. :param parse_from_config:
  88. Whether or not this option should be parsed out of config files.
  89. :param comma_separated_list:
  90. Whether the option is a comma separated list when parsing from a
  91. config file.
  92. :param normalize_paths:
  93. Whether the option is expecting a path or list of paths and should
  94. attempt to normalize the paths to absolute paths.
  95. """
  96. if (
  97. long_option_name is _ARG.NO
  98. and short_option_name is not _ARG.NO
  99. and short_option_name.startswith("--")
  100. ):
  101. short_option_name, long_option_name = _ARG.NO, short_option_name
  102. # flake8 special type normalization
  103. if comma_separated_list or normalize_paths:
  104. type = functools.partial(
  105. _flake8_normalize,
  106. comma_separated_list=comma_separated_list,
  107. normalize_paths=normalize_paths,
  108. )
  109. self.short_option_name = short_option_name
  110. self.long_option_name = long_option_name
  111. self.option_args = [
  112. x
  113. for x in (short_option_name, long_option_name)
  114. if x is not _ARG.NO
  115. ]
  116. self.action = action
  117. self.default = default
  118. self.type = type
  119. self.dest = dest
  120. self.nargs = nargs
  121. self.const = const
  122. self.choices = choices
  123. self.help = help
  124. self.metavar = metavar
  125. self.required = required
  126. self.option_kwargs: dict[str, Any | _ARG] = {
  127. "action": self.action,
  128. "default": self.default,
  129. "type": self.type,
  130. "dest": self.dest,
  131. "nargs": self.nargs,
  132. "const": self.const,
  133. "choices": self.choices,
  134. "help": self.help,
  135. "metavar": self.metavar,
  136. "required": self.required,
  137. }
  138. # Set our custom attributes
  139. self.parse_from_config = parse_from_config
  140. self.comma_separated_list = comma_separated_list
  141. self.normalize_paths = normalize_paths
  142. self.config_name: str | None = None
  143. if parse_from_config:
  144. if long_option_name is _ARG.NO:
  145. raise ValueError(
  146. "When specifying parse_from_config=True, "
  147. "a long_option_name must also be specified."
  148. )
  149. self.config_name = long_option_name[2:].replace("-", "_")
  150. self._opt = None
  151. @property
  152. def filtered_option_kwargs(self) -> dict[str, Any]:
  153. """Return any actually-specified arguments."""
  154. return {
  155. k: v for k, v in self.option_kwargs.items() if v is not _ARG.NO
  156. }
  157. def __repr__(self) -> str: # noqa: D105
  158. parts = []
  159. for arg in self.option_args:
  160. parts.append(arg)
  161. for k, v in self.filtered_option_kwargs.items():
  162. parts.append(f"{k}={v!r}")
  163. return f"Option({', '.join(parts)})"
  164. def normalize(self, value: Any, *normalize_args: str) -> Any:
  165. """Normalize the value based on the option configuration."""
  166. if self.comma_separated_list and isinstance(value, str):
  167. value = utils.parse_comma_separated_list(value)
  168. if self.normalize_paths:
  169. if isinstance(value, list):
  170. value = utils.normalize_paths(value, *normalize_args)
  171. else:
  172. value = utils.normalize_path(value, *normalize_args)
  173. return value
  174. def to_argparse(self) -> tuple[list[str], dict[str, Any]]:
  175. """Convert a Flake8 Option to argparse ``add_argument`` arguments."""
  176. return self.option_args, self.filtered_option_kwargs
  177. class OptionManager:
  178. """Manage Options and OptionParser while adding post-processing."""
  179. def __init__(
  180. self,
  181. *,
  182. version: str,
  183. plugin_versions: str,
  184. parents: list[argparse.ArgumentParser],
  185. formatter_names: list[str],
  186. ) -> None:
  187. """Initialize an instance of an OptionManager."""
  188. self.formatter_names = formatter_names
  189. self.parser = argparse.ArgumentParser(
  190. prog="flake8",
  191. usage="%(prog)s [options] file file ...",
  192. parents=parents,
  193. epilog=f"Installed plugins: {plugin_versions}",
  194. )
  195. self.parser.add_argument(
  196. "--version",
  197. action="version",
  198. version=(
  199. f"{version} ({plugin_versions}) "
  200. f"{utils.get_python_version()}"
  201. ),
  202. )
  203. self.parser.add_argument("filenames", nargs="*", metavar="filename")
  204. self.config_options_dict: dict[str, Option] = {}
  205. self.options: list[Option] = []
  206. self.extended_default_ignore: list[str] = []
  207. self.extended_default_select: list[str] = []
  208. self._current_group: argparse._ArgumentGroup | None = None
  209. # TODO: maybe make this a free function to reduce api surface area
  210. def register_plugins(self, plugins: Plugins) -> None:
  211. """Register the plugin options (if needed)."""
  212. groups: dict[str, argparse._ArgumentGroup] = {}
  213. def _set_group(name: str) -> None:
  214. try:
  215. self._current_group = groups[name]
  216. except KeyError:
  217. group = self.parser.add_argument_group(name)
  218. self._current_group = groups[name] = group
  219. for loaded in plugins.all_plugins():
  220. add_options = getattr(loaded.obj, "add_options", None)
  221. if add_options:
  222. _set_group(loaded.plugin.package)
  223. add_options(self)
  224. if loaded.plugin.entry_point.group == "flake8.extension":
  225. self.extend_default_select([loaded.entry_name])
  226. # isn't strictly necessary, but seems cleaner
  227. self._current_group = None
  228. def add_option(self, *args: Any, **kwargs: Any) -> None:
  229. """Create and register a new option.
  230. See parameters for :class:`~flake8.options.manager.Option` for
  231. acceptable arguments to this method.
  232. .. note::
  233. ``short_option_name`` and ``long_option_name`` may be specified
  234. positionally as they are with argparse normally.
  235. """
  236. option = Option(*args, **kwargs)
  237. option_args, option_kwargs = option.to_argparse()
  238. if self._current_group is not None:
  239. self._current_group.add_argument(*option_args, **option_kwargs)
  240. else:
  241. self.parser.add_argument(*option_args, **option_kwargs)
  242. self.options.append(option)
  243. if option.parse_from_config:
  244. name = option.config_name
  245. assert name is not None
  246. self.config_options_dict[name] = option
  247. self.config_options_dict[name.replace("_", "-")] = option
  248. LOG.debug('Registered option "%s".', option)
  249. def extend_default_ignore(self, error_codes: Sequence[str]) -> None:
  250. """Extend the default ignore list with the error codes provided.
  251. :param error_codes:
  252. List of strings that are the error/warning codes with which to
  253. extend the default ignore list.
  254. """
  255. LOG.debug("Extending default ignore list with %r", error_codes)
  256. self.extended_default_ignore.extend(error_codes)
  257. def extend_default_select(self, error_codes: Sequence[str]) -> None:
  258. """Extend the default select list with the error codes provided.
  259. :param error_codes:
  260. List of strings that are the error/warning codes with which
  261. to extend the default select list.
  262. """
  263. LOG.debug("Extending default select list with %r", error_codes)
  264. self.extended_default_select.extend(error_codes)
  265. def parse_args(
  266. self,
  267. args: Sequence[str] | None = None,
  268. values: argparse.Namespace | None = None,
  269. ) -> argparse.Namespace:
  270. """Proxy to calling the OptionParser's parse_args method."""
  271. if values:
  272. self.parser.set_defaults(**vars(values))
  273. return self.parser.parse_args(args)