arguments_provider.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. """Arguments provider class used to expose options."""
  5. from __future__ import annotations
  6. from collections.abc import Iterator
  7. from typing import Any
  8. from pylint.config.arguments_manager import _ArgumentsManager
  9. from pylint.typing import OptionDict, Options
  10. class _ArgumentsProvider:
  11. """Base class for classes that provide arguments."""
  12. name: str
  13. """Name of the provider."""
  14. options: Options = ()
  15. """Options provided by this provider."""
  16. option_groups_descs: dict[str, str] = {}
  17. """Option groups of this provider and their descriptions."""
  18. def __init__(self, arguments_manager: _ArgumentsManager) -> None:
  19. self._arguments_manager = arguments_manager
  20. """The manager that will parse and register any options provided."""
  21. self._arguments_manager._register_options_provider(self)
  22. def _option_value(self, opt: str) -> Any:
  23. """Get the current value for the given option."""
  24. return getattr(self._arguments_manager.config, opt.replace("-", "_"), None)
  25. def _options_by_section(
  26. self,
  27. ) -> Iterator[
  28. tuple[str, list[tuple[str, OptionDict, Any]]]
  29. | tuple[None, dict[str, list[tuple[str, OptionDict, Any]]]]
  30. ]:
  31. """Return an iterator on options grouped by section.
  32. (section, [list of (optname, optdict, optvalue)])
  33. """
  34. sections: dict[str, list[tuple[str, OptionDict, Any]]] = {}
  35. for optname, optdict in self.options:
  36. sections.setdefault(optdict.get("group"), []).append( # type: ignore[arg-type]
  37. (optname, optdict, self._option_value(optname))
  38. )
  39. if None in sections:
  40. yield None, sections.pop(None) # type: ignore[call-overload]
  41. for section, options in sorted(sections.items()):
  42. yield section.upper(), options
  43. def _options_and_values(
  44. self, options: Options | None = None
  45. ) -> Iterator[tuple[str, OptionDict, Any]]:
  46. """DEPRECATED."""
  47. if options is None:
  48. options = self.options
  49. for optname, optdict in options:
  50. yield optname, optdict, self._option_value(optname)