manager.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. """Option handling and Option management logic."""
  2. import argparse
  3. import enum
  4. import functools
  5. import logging
  6. from typing import Any
  7. from typing import Callable
  8. from typing import Dict
  9. from typing import List
  10. from typing import Mapping
  11. from typing import Optional
  12. from typing import Sequence
  13. from typing import Tuple
  14. from typing import Type
  15. from typing import Union
  16. from flake8 import utils
  17. from flake8.plugins.finder import Plugins
  18. LOG = logging.getLogger(__name__)
  19. # represent a singleton of "not passed arguments".
  20. # an enum is chosen to trick mypy
  21. _ARG = enum.Enum("_ARG", "NO")
  22. _optparse_callable_map: Dict[str, Union[Type[Any], _ARG]] = {
  23. "int": int,
  24. "long": int,
  25. "string": str,
  26. "float": float,
  27. "complex": complex,
  28. "choice": _ARG.NO,
  29. # optparse allows this but does not document it
  30. "str": str,
  31. }
  32. class _CallbackAction(argparse.Action):
  33. """Shim for optparse-style callback actions."""
  34. def __init__(
  35. self,
  36. *args: Any,
  37. callback: Callable[..., Any],
  38. callback_args: Sequence[Any] = (),
  39. callback_kwargs: Optional[Dict[str, Any]] = None,
  40. **kwargs: Any,
  41. ) -> None:
  42. self._callback = callback
  43. self._callback_args = callback_args
  44. self._callback_kwargs = callback_kwargs or {}
  45. super().__init__(*args, **kwargs)
  46. def __call__(
  47. self,
  48. parser: argparse.ArgumentParser,
  49. namespace: argparse.Namespace,
  50. values: Optional[Union[Sequence[str], str]],
  51. option_string: Optional[str] = None,
  52. ) -> None:
  53. if not values:
  54. values = None
  55. elif isinstance(values, list) and len(values) > 1:
  56. values = tuple(values)
  57. self._callback(
  58. self,
  59. option_string,
  60. values,
  61. parser,
  62. *self._callback_args,
  63. **self._callback_kwargs,
  64. )
  65. def _flake8_normalize(
  66. value: str,
  67. *args: str,
  68. comma_separated_list: bool = False,
  69. normalize_paths: bool = False,
  70. ) -> Union[str, List[str]]:
  71. ret: Union[str, List[str]] = value
  72. if comma_separated_list and isinstance(ret, str):
  73. ret = utils.parse_comma_separated_list(value)
  74. if normalize_paths:
  75. if isinstance(ret, str):
  76. ret = utils.normalize_path(ret, *args)
  77. else:
  78. ret = utils.normalize_paths(ret, *args)
  79. return ret
  80. class Option:
  81. """Our wrapper around an argparse argument parsers to add features."""
  82. def __init__(
  83. self,
  84. short_option_name: Union[str, _ARG] = _ARG.NO,
  85. long_option_name: Union[str, _ARG] = _ARG.NO,
  86. # Options below here are taken from the optparse.Option class
  87. action: Union[str, Type[argparse.Action], _ARG] = _ARG.NO,
  88. default: Union[Any, _ARG] = _ARG.NO,
  89. type: Union[str, Callable[..., Any], _ARG] = _ARG.NO,
  90. dest: Union[str, _ARG] = _ARG.NO,
  91. nargs: Union[int, str, _ARG] = _ARG.NO,
  92. const: Union[Any, _ARG] = _ARG.NO,
  93. choices: Union[Sequence[Any], _ARG] = _ARG.NO,
  94. help: Union[str, _ARG] = _ARG.NO,
  95. metavar: Union[str, _ARG] = _ARG.NO,
  96. # deprecated optparse-only options
  97. callback: Union[Callable[..., Any], _ARG] = _ARG.NO,
  98. callback_args: Union[Sequence[Any], _ARG] = _ARG.NO,
  99. callback_kwargs: Union[Mapping[str, Any], _ARG] = _ARG.NO,
  100. # Options below are taken from argparse.ArgumentParser.add_argument
  101. required: Union[bool, _ARG] = _ARG.NO,
  102. # Options below here are specific to Flake8
  103. parse_from_config: bool = False,
  104. comma_separated_list: bool = False,
  105. normalize_paths: bool = False,
  106. ) -> None:
  107. """Initialize an Option instance.
  108. The following are all passed directly through to argparse.
  109. :param short_option_name:
  110. The short name of the option (e.g., ``-x``). This will be the
  111. first argument passed to ``ArgumentParser.add_argument``
  112. :param long_option_name:
  113. The long name of the option (e.g., ``--xtra-long-option``). This
  114. will be the second argument passed to
  115. ``ArgumentParser.add_argument``
  116. :param default:
  117. Default value of the option.
  118. :param dest:
  119. Attribute name to store parsed option value as.
  120. :param nargs:
  121. Number of arguments to parse for this option.
  122. :param const:
  123. Constant value to store on a common destination. Usually used in
  124. conjunction with ``action="store_const"``.
  125. :param choices:
  126. Possible values for the option.
  127. :param help:
  128. Help text displayed in the usage information.
  129. :param metavar:
  130. Name to use instead of the long option name for help text.
  131. :param required:
  132. Whether this option is required or not.
  133. The following options may be passed directly through to :mod:`argparse`
  134. but may need some massaging.
  135. :param type:
  136. A callable to normalize the type (as is the case in
  137. :mod:`argparse`). Deprecated: you can also pass through type
  138. strings such as ``'int'`` which are handled by :mod:`optparse`.
  139. :param action:
  140. Any action allowed by :mod:`argparse`. Deprecated: this also
  141. understands the ``action='callback'`` action from :mod:`optparse`.
  142. :param callback:
  143. Callback used if the action is ``"callback"``. Deprecated: please
  144. use ``action=`` instead.
  145. :param callback_args:
  146. Additional positional arguments to the callback callable.
  147. Deprecated: please use ``action=`` instead (probably with
  148. ``functools.partial``).
  149. :param callback_kwargs:
  150. Keyword arguments to the callback callable. Deprecated: please
  151. use ``action=`` instead (probably with ``functools.partial``).
  152. The following parameters are for Flake8's option handling alone.
  153. :param parse_from_config:
  154. Whether or not this option should be parsed out of config files.
  155. :param comma_separated_list:
  156. Whether the option is a comma separated list when parsing from a
  157. config file.
  158. :param normalize_paths:
  159. Whether the option is expecting a path or list of paths and should
  160. attempt to normalize the paths to absolute paths.
  161. """
  162. if (
  163. long_option_name is _ARG.NO
  164. and short_option_name is not _ARG.NO
  165. and short_option_name.startswith("--")
  166. ):
  167. short_option_name, long_option_name = _ARG.NO, short_option_name
  168. # optparse -> argparse `%default` => `%(default)s`
  169. if help is not _ARG.NO and "%default" in help:
  170. LOG.warning(
  171. "option %s: please update `help=` text to use %%(default)s "
  172. "instead of %%default -- this will be an error in the future",
  173. long_option_name,
  174. )
  175. help = help.replace("%default", "%(default)s")
  176. # optparse -> argparse for `callback`
  177. if action == "callback":
  178. LOG.warning(
  179. "option %s: please update from optparse `action='callback'` "
  180. "to argparse action classes -- this will be an error in the "
  181. "future",
  182. long_option_name,
  183. )
  184. action = _CallbackAction
  185. if type is _ARG.NO:
  186. nargs = 0
  187. # optparse -> argparse for `type`
  188. if isinstance(type, str):
  189. LOG.warning(
  190. "option %s: please update from optparse string `type=` to "
  191. "argparse callable `type=` -- this will be an error in the "
  192. "future",
  193. long_option_name,
  194. )
  195. type = _optparse_callable_map[type]
  196. # flake8 special type normalization
  197. if comma_separated_list or normalize_paths:
  198. type = functools.partial(
  199. _flake8_normalize,
  200. comma_separated_list=comma_separated_list,
  201. normalize_paths=normalize_paths,
  202. )
  203. self.short_option_name = short_option_name
  204. self.long_option_name = long_option_name
  205. self.option_args = [
  206. x
  207. for x in (short_option_name, long_option_name)
  208. if x is not _ARG.NO
  209. ]
  210. self.action = action
  211. self.default = default
  212. self.type = type
  213. self.dest = dest
  214. self.nargs = nargs
  215. self.const = const
  216. self.choices = choices
  217. self.callback = callback
  218. self.callback_args = callback_args
  219. self.callback_kwargs = callback_kwargs
  220. self.help = help
  221. self.metavar = metavar
  222. self.required = required
  223. self.option_kwargs: Dict[str, Union[Any, _ARG]] = {
  224. "action": self.action,
  225. "default": self.default,
  226. "type": self.type,
  227. "dest": self.dest,
  228. "nargs": self.nargs,
  229. "const": self.const,
  230. "choices": self.choices,
  231. "callback": self.callback,
  232. "callback_args": self.callback_args,
  233. "callback_kwargs": self.callback_kwargs,
  234. "help": self.help,
  235. "metavar": self.metavar,
  236. "required": self.required,
  237. }
  238. # Set our custom attributes
  239. self.parse_from_config = parse_from_config
  240. self.comma_separated_list = comma_separated_list
  241. self.normalize_paths = normalize_paths
  242. self.config_name: Optional[str] = None
  243. if parse_from_config:
  244. if long_option_name is _ARG.NO:
  245. raise ValueError(
  246. "When specifying parse_from_config=True, "
  247. "a long_option_name must also be specified."
  248. )
  249. self.config_name = long_option_name[2:].replace("-", "_")
  250. self._opt = None
  251. @property
  252. def filtered_option_kwargs(self) -> Dict[str, Any]:
  253. """Return any actually-specified arguments."""
  254. return {
  255. k: v for k, v in self.option_kwargs.items() if v is not _ARG.NO
  256. }
  257. def __repr__(self) -> str: # noqa: D105
  258. parts = []
  259. for arg in self.option_args:
  260. parts.append(arg)
  261. for k, v in self.filtered_option_kwargs.items():
  262. parts.append(f"{k}={v!r}")
  263. return f"Option({', '.join(parts)})"
  264. def normalize(self, value: Any, *normalize_args: str) -> Any:
  265. """Normalize the value based on the option configuration."""
  266. if self.comma_separated_list and isinstance(value, str):
  267. value = utils.parse_comma_separated_list(value)
  268. if self.normalize_paths:
  269. if isinstance(value, list):
  270. value = utils.normalize_paths(value, *normalize_args)
  271. else:
  272. value = utils.normalize_path(value, *normalize_args)
  273. return value
  274. def to_argparse(self) -> Tuple[List[str], Dict[str, Any]]:
  275. """Convert a Flake8 Option to argparse ``add_argument`` arguments."""
  276. return self.option_args, self.filtered_option_kwargs
  277. class OptionManager:
  278. """Manage Options and OptionParser while adding post-processing."""
  279. def __init__(
  280. self,
  281. *,
  282. version: str,
  283. plugin_versions: str,
  284. parents: List[argparse.ArgumentParser],
  285. ) -> None:
  286. """Initialize an instance of an OptionManager.
  287. :param prog:
  288. Name of the actual program (e.g., flake8).
  289. :param version:
  290. Version string for the program.
  291. :param usage:
  292. Basic usage string used by the OptionParser.
  293. :param parents:
  294. A list of ArgumentParser objects whose arguments should also be
  295. included.
  296. """
  297. self.parser = argparse.ArgumentParser(
  298. prog="flake8",
  299. usage="%(prog)s [options] file file ...",
  300. parents=parents,
  301. epilog=f"Installed plugins: {plugin_versions}",
  302. )
  303. self.parser.add_argument(
  304. "--version",
  305. action="version",
  306. version=(
  307. f"{version} ({plugin_versions}) "
  308. f"{utils.get_python_version()}"
  309. ),
  310. )
  311. self.parser.add_argument("filenames", nargs="*", metavar="filename")
  312. self.config_options_dict: Dict[str, Option] = {}
  313. self.options: List[Option] = []
  314. self.extended_default_ignore: List[str] = []
  315. self.extended_default_select: List[str] = []
  316. self._current_group: Optional[argparse._ArgumentGroup] = None
  317. # TODO: maybe make this a free function to reduce api surface area
  318. def register_plugins(self, plugins: Plugins) -> None:
  319. """Register the plugin options (if needed)."""
  320. groups: Dict[str, argparse._ArgumentGroup] = {}
  321. def _set_group(name: str) -> None:
  322. try:
  323. self._current_group = groups[name]
  324. except KeyError:
  325. group = self.parser.add_argument_group(name)
  326. self._current_group = groups[name] = group
  327. for loaded in plugins.all_plugins():
  328. add_options = getattr(loaded.obj, "add_options", None)
  329. if add_options:
  330. _set_group(loaded.plugin.package)
  331. add_options(self)
  332. if loaded.plugin.entry_point.group == "flake8.extension":
  333. self.extend_default_select([loaded.entry_name])
  334. # isn't strictly necessary, but seems cleaner
  335. self._current_group = None
  336. def add_option(self, *args: Any, **kwargs: Any) -> None:
  337. """Create and register a new option.
  338. See parameters for :class:`~flake8.options.manager.Option` for
  339. acceptable arguments to this method.
  340. .. note::
  341. ``short_option_name`` and ``long_option_name`` may be specified
  342. positionally as they are with argparse normally.
  343. """
  344. option = Option(*args, **kwargs)
  345. option_args, option_kwargs = option.to_argparse()
  346. if self._current_group is not None:
  347. self._current_group.add_argument(*option_args, **option_kwargs)
  348. else:
  349. self.parser.add_argument(*option_args, **option_kwargs)
  350. self.options.append(option)
  351. if option.parse_from_config:
  352. name = option.config_name
  353. assert name is not None
  354. self.config_options_dict[name] = option
  355. self.config_options_dict[name.replace("_", "-")] = option
  356. LOG.debug('Registered option "%s".', option)
  357. def extend_default_ignore(self, error_codes: Sequence[str]) -> None:
  358. """Extend the default ignore list with the error codes provided.
  359. :param error_codes:
  360. List of strings that are the error/warning codes with which to
  361. extend the default ignore list.
  362. """
  363. LOG.debug("Extending default ignore list with %r", error_codes)
  364. self.extended_default_ignore.extend(error_codes)
  365. def extend_default_select(self, error_codes: Sequence[str]) -> None:
  366. """Extend the default select list with the error codes provided.
  367. :param error_codes:
  368. List of strings that are the error/warning codes with which
  369. to extend the default select list.
  370. """
  371. LOG.debug("Extending default select list with %r", error_codes)
  372. self.extended_default_select.extend(error_codes)
  373. def parse_args(
  374. self,
  375. args: Optional[Sequence[str]] = None,
  376. values: Optional[argparse.Namespace] = None,
  377. ) -> argparse.Namespace:
  378. """Proxy to calling the OptionParser's parse_args method."""
  379. if values:
  380. self.parser.set_defaults(**vars(values))
  381. return self.parser.parse_args(args)