option_manager_mixin.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. # pylint: disable=duplicate-code
  5. from __future__ import annotations
  6. import collections
  7. import configparser
  8. import contextlib
  9. import copy
  10. import optparse # pylint: disable=deprecated-module
  11. import os
  12. import sys
  13. import warnings
  14. from collections.abc import Iterator
  15. from pathlib import Path
  16. from typing import TYPE_CHECKING, Any, TextIO
  17. from pylint import utils
  18. from pylint.config.option import Option
  19. from pylint.config.option_parser import OptionParser # type: ignore[attr-defined]
  20. from pylint.typing import OptionDict
  21. if TYPE_CHECKING:
  22. from pylint.config.options_provider_mixin import ( # type: ignore[attr-defined]
  23. OptionsProviderMixin,
  24. )
  25. if sys.version_info >= (3, 11):
  26. import tomllib
  27. else:
  28. import tomli as tomllib
  29. def _expand_default(self: optparse.HelpFormatter, option: Option) -> str:
  30. """Patch OptionParser.expand_default with custom behaviour.
  31. This will handle defaults to avoid overriding values in the
  32. configuration file.
  33. """
  34. if self.parser is None or not self.default_tag:
  35. return str(option.help)
  36. optname = option._long_opts[0][2:]
  37. try:
  38. provider = self.parser.options_manager._all_options[optname] # type: ignore[attr-defined]
  39. except KeyError:
  40. value = None
  41. else:
  42. optdict = provider.get_option_def(optname)
  43. optname = provider.option_attrname(optname, optdict)
  44. value = getattr(provider.config, optname, optdict)
  45. value = utils._format_option_value(optdict, value)
  46. if value is optparse.NO_DEFAULT or not value:
  47. value = self.NO_DEFAULT_VALUE
  48. return option.help.replace(self.default_tag, str(value)) # type: ignore[union-attr]
  49. @contextlib.contextmanager
  50. def _patch_optparse() -> Iterator[None]:
  51. # pylint: disable = redefined-variable-type
  52. orig_default = optparse.HelpFormatter
  53. try:
  54. optparse.HelpFormatter.expand_default = _expand_default # type: ignore[assignment]
  55. yield
  56. finally:
  57. optparse.HelpFormatter.expand_default = orig_default # type: ignore[assignment]
  58. class OptionsManagerMixIn:
  59. """Handle configuration from both a configuration file and command line options."""
  60. def __init__(self, usage: str) -> None:
  61. # TODO: 3.0: Remove deprecated class
  62. warnings.warn(
  63. "OptionsManagerMixIn has been deprecated and will be removed in pylint 3.0",
  64. DeprecationWarning,
  65. stacklevel=2,
  66. )
  67. self.reset_parsers(usage)
  68. # list of registered options providers
  69. self.options_providers: list[OptionsProviderMixin] = []
  70. # dictionary associating option name to checker
  71. self._all_options: collections.OrderedDict[Any, Any] = collections.OrderedDict()
  72. self._short_options: dict[Any, Any] = {}
  73. self._nocallback_options: dict[Any, Any] = {}
  74. self._mygroups: dict[Any, Any] = {}
  75. # verbosity
  76. self._maxlevel = 0
  77. def reset_parsers(self, usage: str = "") -> None:
  78. # configuration file parser
  79. self.cfgfile_parser = configparser.ConfigParser(
  80. inline_comment_prefixes=("#", ";")
  81. )
  82. # command line parser
  83. self.cmdline_parser = OptionParser(Option, usage=usage)
  84. self.cmdline_parser.options_manager = self
  85. self._optik_option_attrs = set(self.cmdline_parser.option_class.ATTRS)
  86. def register_options_provider(
  87. self, provider: OptionsProviderMixin, own_group: bool = True
  88. ) -> None:
  89. """Register an options provider."""
  90. self.options_providers.append(provider)
  91. non_group_spec_options = [
  92. option for option in provider.options if "group" not in option[1]
  93. ]
  94. groups = getattr(provider, "option_groups", ())
  95. if own_group and non_group_spec_options:
  96. self.add_option_group(
  97. provider.name.upper(),
  98. provider.__doc__,
  99. non_group_spec_options,
  100. provider,
  101. )
  102. else:
  103. for opt, optdict in non_group_spec_options:
  104. self.add_optik_option(provider, self.cmdline_parser, opt, optdict)
  105. for gname, gdoc in groups:
  106. gname = gname.upper()
  107. goptions = [
  108. option
  109. for option in provider.options
  110. if option[1].get("group", "").upper() == gname
  111. ]
  112. self.add_option_group(gname, gdoc, goptions, provider)
  113. def add_option_group(
  114. self, group_name: str, _: Any, options: Any, provider: OptionsProviderMixin
  115. ) -> None:
  116. # add option group to the command line parser
  117. if group_name in self._mygroups:
  118. group = self._mygroups[group_name]
  119. else:
  120. group = optparse.OptionGroup(
  121. self.cmdline_parser, title=group_name.capitalize()
  122. )
  123. self.cmdline_parser.add_option_group(group)
  124. self._mygroups[group_name] = group
  125. # add section to the config file
  126. if (
  127. group_name != "DEFAULT"
  128. and group_name not in self.cfgfile_parser._sections # type: ignore[attr-defined]
  129. ):
  130. self.cfgfile_parser.add_section(group_name)
  131. # add provider's specific options
  132. for opt, optdict in options:
  133. if not isinstance(optdict.get("action", "store"), str):
  134. optdict["action"] = "callback"
  135. self.add_optik_option(provider, group, opt, optdict)
  136. def add_optik_option(
  137. self,
  138. provider: OptionsProviderMixin,
  139. optikcontainer: Any,
  140. opt: str,
  141. optdict: OptionDict,
  142. ) -> None:
  143. args, optdict = self.optik_option(provider, opt, optdict)
  144. option = optikcontainer.add_option(*args, **optdict)
  145. self._all_options[opt] = provider
  146. self._maxlevel = max(self._maxlevel, option.level or 0)
  147. def optik_option(
  148. self, provider: OptionsProviderMixin, opt: str, optdict: OptionDict
  149. ) -> tuple[list[str], OptionDict]:
  150. """Get our personal option definition and return a suitable form for
  151. use with optik/optparse.
  152. """
  153. optdict = copy.copy(optdict)
  154. if "action" in optdict:
  155. self._nocallback_options[provider] = opt
  156. else:
  157. optdict["action"] = "callback"
  158. optdict["callback"] = self.cb_set_provider_option
  159. # default is handled here and *must not* be given to optik if you
  160. # want the whole machinery to work
  161. if "default" in optdict:
  162. if (
  163. "help" in optdict
  164. and optdict.get("default") is not None
  165. and optdict["action"] not in ("store_true", "store_false")
  166. ):
  167. optdict["help"] += " [current: %default]" # type: ignore[operator]
  168. del optdict["default"]
  169. args = ["--" + str(opt)]
  170. if "short" in optdict:
  171. self._short_options[optdict["short"]] = opt
  172. args.append("-" + optdict["short"]) # type: ignore[operator]
  173. del optdict["short"]
  174. # cleanup option definition dict before giving it to optik
  175. for key in list(optdict.keys()):
  176. if key not in self._optik_option_attrs:
  177. optdict.pop(key)
  178. return args, optdict
  179. def cb_set_provider_option(
  180. self, option: Option, opt: str, value: Any, parser: Any
  181. ) -> None:
  182. """Optik callback for option setting."""
  183. if opt.startswith("--"):
  184. # remove -- on long option
  185. opt = opt[2:]
  186. else:
  187. # short option, get its long equivalent
  188. opt = self._short_options[opt[1:]]
  189. # trick since we can't set action='store_true' on options
  190. if value is None:
  191. value = 1
  192. self.global_set_option(opt, value)
  193. def global_set_option(self, opt: str, value: Any) -> None:
  194. """Set option on the correct option provider."""
  195. self._all_options[opt].set_option(opt, value)
  196. def generate_config(
  197. self, stream: TextIO | None = None, skipsections: tuple[str, ...] = ()
  198. ) -> None:
  199. """Write a configuration file according to the current configuration
  200. into the given stream or stdout.
  201. """
  202. options_by_section: dict[str, list[tuple[str, OptionDict, Any]]] = {}
  203. sections = []
  204. for provider in self.options_providers:
  205. for section, options in provider.options_by_section():
  206. if section is None:
  207. section = provider.name
  208. if section in skipsections:
  209. continue
  210. options = [
  211. (n, d, v)
  212. for (n, d, v) in options
  213. if d.get("type") is not None and not d.get("deprecated")
  214. ]
  215. if not options:
  216. continue
  217. if section not in sections:
  218. sections.append(section)
  219. all_options = options_by_section.setdefault(section, [])
  220. all_options += options
  221. stream = stream or sys.stdout
  222. printed = False
  223. for section in sections:
  224. if printed:
  225. print("\n", file=stream)
  226. utils.format_section(
  227. stream, section.upper(), sorted(options_by_section[section])
  228. )
  229. printed = True
  230. def load_provider_defaults(self) -> None:
  231. """Initialize configuration using default values."""
  232. for provider in self.options_providers:
  233. provider.load_defaults()
  234. def read_config_file(
  235. self, config_file: Path | None = None, verbose: bool = False
  236. ) -> None:
  237. """Read the configuration file but do not load it (i.e. dispatching
  238. values to each option's provider).
  239. """
  240. if config_file:
  241. config_file = Path(os.path.expandvars(config_file)).expanduser()
  242. if not config_file.exists():
  243. raise OSError(f"The config file {str(config_file)} doesn't exist!")
  244. parser = self.cfgfile_parser
  245. if config_file.suffix == ".toml":
  246. try:
  247. self._parse_toml(config_file, parser)
  248. except tomllib.TOMLDecodeError:
  249. pass
  250. else:
  251. # Use this encoding in order to strip the BOM marker, if any.
  252. with open(config_file, encoding="utf_8_sig") as fp:
  253. parser.read_file(fp)
  254. # normalize each section's title
  255. for sect, values in list(parser._sections.items()): # type: ignore[attr-defined]
  256. if sect.startswith("pylint."):
  257. sect = sect[len("pylint.") :]
  258. if not sect.isupper() and values:
  259. parser._sections[sect.upper()] = values # type: ignore[attr-defined]
  260. if not verbose:
  261. return
  262. if config_file and config_file.exists():
  263. msg = f"Using config file '{config_file}'"
  264. else:
  265. msg = "No config file found, using default configuration"
  266. print(msg, file=sys.stderr)
  267. def _parse_toml(self, config_file: Path, parser: configparser.ConfigParser) -> None:
  268. """Parse and handle errors of a toml configuration file."""
  269. with open(config_file, mode="rb") as fp:
  270. content = tomllib.load(fp)
  271. try:
  272. sections_values = content["tool"]["pylint"]
  273. except KeyError:
  274. return
  275. for section, values in sections_values.items():
  276. section_name = section.upper()
  277. # TOML has rich types, convert values to
  278. # strings as ConfigParser expects.
  279. if not isinstance(values, dict):
  280. # This class is a mixin: add_message comes from the `PyLinter` class
  281. self.add_message( # type: ignore[attr-defined]
  282. "bad-configuration-section", line=0, args=(section, values)
  283. )
  284. continue
  285. for option, value in values.items():
  286. if isinstance(value, bool):
  287. values[option] = "yes" if value else "no"
  288. elif isinstance(value, list):
  289. values[option] = ",".join(value)
  290. else:
  291. values[option] = str(value)
  292. for option, value in values.items():
  293. try:
  294. parser.set(section_name, option, value=value)
  295. except configparser.NoSectionError:
  296. parser.add_section(section_name)
  297. parser.set(section_name, option, value=value)
  298. def load_config_file(self) -> None:
  299. """Dispatch values previously read from a configuration file to each
  300. option's provider.
  301. """
  302. parser = self.cfgfile_parser
  303. for section in parser.sections():
  304. for option, value in parser.items(section):
  305. try:
  306. self.global_set_option(option, value)
  307. except (KeyError, optparse.OptionError):
  308. continue
  309. def load_configuration(self, **kwargs: Any) -> None:
  310. """Override configuration according to given parameters."""
  311. return self.load_configuration_from_config(kwargs)
  312. def load_configuration_from_config(self, config: dict[str, Any]) -> None:
  313. for opt, opt_value in config.items():
  314. opt = opt.replace("_", "-")
  315. provider = self._all_options[opt]
  316. provider.set_option(opt, opt_value)
  317. def load_command_line_configuration(
  318. self, args: list[str] | None = None
  319. ) -> list[str]:
  320. """Override configuration according to command line parameters.
  321. return additional arguments
  322. """
  323. with _patch_optparse():
  324. args = sys.argv[1:] if args is None else list(args)
  325. (options, args) = self.cmdline_parser.parse_args(args=args)
  326. for provider in self._nocallback_options:
  327. config = provider.config
  328. for attr in config.__dict__.keys():
  329. value = getattr(options, attr, None)
  330. if value is None:
  331. continue
  332. setattr(config, attr, value)
  333. return args # type: ignore[return-value]
  334. def help(self, level: int = 0) -> str:
  335. """Return the usage string for available options."""
  336. self.cmdline_parser.formatter.output_level = level
  337. with _patch_optparse():
  338. return str(self.cmdline_parser.format_help())