_manager.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. from __future__ import annotations
  2. import inspect
  3. import sys
  4. import types
  5. import warnings
  6. from typing import Any
  7. from typing import Callable
  8. from typing import cast
  9. from typing import Iterable
  10. from typing import Mapping
  11. from typing import Sequence
  12. from typing import TYPE_CHECKING
  13. from . import _tracing
  14. from ._callers import _multicall
  15. from ._hooks import _HookCaller
  16. from ._hooks import _HookImplFunction
  17. from ._hooks import _HookRelay
  18. from ._hooks import _Namespace
  19. from ._hooks import _Plugin
  20. from ._hooks import _SubsetHookCaller
  21. from ._hooks import HookImpl
  22. from ._hooks import HookSpec
  23. from ._hooks import normalize_hookimpl_opts
  24. from ._result import _Result
  25. if sys.version_info >= (3, 8):
  26. from importlib import metadata as importlib_metadata
  27. else:
  28. import importlib_metadata
  29. if TYPE_CHECKING:
  30. from typing_extensions import Final
  31. from ._hooks import _HookImplOpts, _HookSpecOpts
  32. _BeforeTrace = Callable[[str, Sequence[HookImpl], Mapping[str, Any]], None]
  33. _AfterTrace = Callable[[_Result[Any], str, Sequence[HookImpl], Mapping[str, Any]], None]
  34. def _warn_for_function(warning: Warning, function: Callable[..., object]) -> None:
  35. func = cast(types.FunctionType, function)
  36. warnings.warn_explicit(
  37. warning,
  38. type(warning),
  39. lineno=func.__code__.co_firstlineno,
  40. filename=func.__code__.co_filename,
  41. )
  42. class PluginValidationError(Exception):
  43. """Plugin failed validation.
  44. :param plugin: The plugin which failed validation.
  45. """
  46. def __init__(self, plugin: _Plugin, message: str) -> None:
  47. super().__init__(message)
  48. self.plugin = plugin
  49. class DistFacade:
  50. """Emulate a pkg_resources Distribution"""
  51. def __init__(self, dist: importlib_metadata.Distribution) -> None:
  52. self._dist = dist
  53. @property
  54. def project_name(self) -> str:
  55. name: str = self.metadata["name"]
  56. return name
  57. def __getattr__(self, attr: str, default=None):
  58. return getattr(self._dist, attr, default)
  59. def __dir__(self) -> list[str]:
  60. return sorted(dir(self._dist) + ["_dist", "project_name"])
  61. class PluginManager:
  62. """Core class which manages registration of plugin objects and 1:N hook
  63. calling.
  64. You can register new hooks by calling :meth:`add_hookspecs(module_or_class)
  65. <PluginManager.add_hookspecs>`.
  66. You can register plugin objects (which contain hook implementations) by
  67. calling :meth:`register(plugin) <PluginManager.register>`.
  68. For debugging purposes you can call :meth:`PluginManager.enable_tracing`
  69. which will subsequently send debug information to the trace helper.
  70. """
  71. def __init__(self, project_name: str) -> None:
  72. self.project_name: Final = project_name
  73. self._name2plugin: Final[dict[str, _Plugin]] = {}
  74. self._plugin_distinfo: Final[list[tuple[_Plugin, DistFacade]]] = []
  75. self.trace: Final = _tracing.TagTracer().get("pluginmanage")
  76. self.hook: Final = _HookRelay()
  77. self._inner_hookexec = _multicall
  78. def _hookexec(
  79. self,
  80. hook_name: str,
  81. methods: Sequence[HookImpl],
  82. kwargs: Mapping[str, object],
  83. firstresult: bool,
  84. ) -> object | list[object]:
  85. # called from all hookcaller instances.
  86. # enable_tracing will set its own wrapping function at self._inner_hookexec
  87. return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  88. def register(self, plugin: _Plugin, name: str | None = None) -> str | None:
  89. """Register a plugin and return its name.
  90. If a name is not specified, a name is generated using
  91. :func:`get_canonical_name`.
  92. If the name is blocked from registering, returns ``None``.
  93. If the plugin is already registered, raises a :class:`ValueError`.
  94. """
  95. plugin_name = name or self.get_canonical_name(plugin)
  96. if plugin_name in self._name2plugin:
  97. if self._name2plugin.get(plugin_name, -1) is None:
  98. return None # blocked plugin, return None to indicate no registration
  99. raise ValueError(
  100. "Plugin name already registered: %s=%s\n%s"
  101. % (plugin_name, plugin, self._name2plugin)
  102. )
  103. if plugin in self._name2plugin.values():
  104. raise ValueError(
  105. "Plugin already registered under a different name: %s=%s\n%s"
  106. % (plugin_name, plugin, self._name2plugin)
  107. )
  108. # XXX if an error happens we should make sure no state has been
  109. # changed at point of return
  110. self._name2plugin[plugin_name] = plugin
  111. # register matching hook implementations of the plugin
  112. for name in dir(plugin):
  113. hookimpl_opts = self.parse_hookimpl_opts(plugin, name)
  114. if hookimpl_opts is not None:
  115. normalize_hookimpl_opts(hookimpl_opts)
  116. method: _HookImplFunction[object] = getattr(plugin, name)
  117. hookimpl = HookImpl(plugin, plugin_name, method, hookimpl_opts)
  118. name = hookimpl_opts.get("specname") or name
  119. hook: _HookCaller | None = getattr(self.hook, name, None)
  120. if hook is None:
  121. hook = _HookCaller(name, self._hookexec)
  122. setattr(self.hook, name, hook)
  123. elif hook.has_spec():
  124. self._verify_hook(hook, hookimpl)
  125. hook._maybe_apply_history(hookimpl)
  126. hook._add_hookimpl(hookimpl)
  127. return plugin_name
  128. def parse_hookimpl_opts(self, plugin: _Plugin, name: str) -> _HookImplOpts | None:
  129. method: object = getattr(plugin, name)
  130. if not inspect.isroutine(method):
  131. return None
  132. try:
  133. res: _HookImplOpts | None = getattr(
  134. method, self.project_name + "_impl", None
  135. )
  136. except Exception:
  137. res = {} # type: ignore[assignment]
  138. if res is not None and not isinstance(res, dict):
  139. # false positive
  140. res = None # type:ignore[unreachable]
  141. return res
  142. def unregister(
  143. self, plugin: _Plugin | None = None, name: str | None = None
  144. ) -> _Plugin:
  145. """Unregister a plugin and all of its hook implementations.
  146. The plugin can be specified either by the plugin object or the plugin
  147. name. If both are specified, they must agree.
  148. """
  149. if name is None:
  150. assert plugin is not None, "one of name or plugin needs to be specified"
  151. name = self.get_name(plugin)
  152. assert name is not None, "plugin is not registered"
  153. if plugin is None:
  154. plugin = self.get_plugin(name)
  155. hookcallers = self.get_hookcallers(plugin)
  156. if hookcallers:
  157. for hookcaller in hookcallers:
  158. hookcaller._remove_plugin(plugin)
  159. # if self._name2plugin[name] == None registration was blocked: ignore
  160. if self._name2plugin.get(name):
  161. assert name is not None
  162. del self._name2plugin[name]
  163. return plugin
  164. def set_blocked(self, name: str) -> None:
  165. """Block registrations of the given name, unregister if already registered."""
  166. self.unregister(name=name)
  167. self._name2plugin[name] = None
  168. def is_blocked(self, name: str) -> bool:
  169. """Return whether the given plugin name is blocked."""
  170. return name in self._name2plugin and self._name2plugin[name] is None
  171. def add_hookspecs(self, module_or_class: _Namespace) -> None:
  172. """Add new hook specifications defined in the given ``module_or_class``.
  173. Functions are recognized as hook specifications if they have been
  174. decorated with a matching :class:`HookspecMarker`.
  175. """
  176. names = []
  177. for name in dir(module_or_class):
  178. spec_opts = self.parse_hookspec_opts(module_or_class, name)
  179. if spec_opts is not None:
  180. hc: _HookCaller | None = getattr(self.hook, name, None)
  181. if hc is None:
  182. hc = _HookCaller(name, self._hookexec, module_or_class, spec_opts)
  183. setattr(self.hook, name, hc)
  184. else:
  185. # Plugins registered this hook without knowing the spec.
  186. hc.set_specification(module_or_class, spec_opts)
  187. for hookfunction in hc.get_hookimpls():
  188. self._verify_hook(hc, hookfunction)
  189. names.append(name)
  190. if not names:
  191. raise ValueError(
  192. f"did not find any {self.project_name!r} hooks in {module_or_class!r}"
  193. )
  194. def parse_hookspec_opts(
  195. self, module_or_class: _Namespace, name: str
  196. ) -> _HookSpecOpts | None:
  197. method: HookSpec = getattr(module_or_class, name)
  198. opts: _HookSpecOpts | None = getattr(method, self.project_name + "_spec", None)
  199. return opts
  200. def get_plugins(self) -> set[Any]:
  201. """Return a set of all registered plugin objects."""
  202. return set(self._name2plugin.values())
  203. def is_registered(self, plugin: _Plugin) -> bool:
  204. """Return whether the plugin is already registered."""
  205. return any(plugin == val for val in self._name2plugin.values())
  206. def get_canonical_name(self, plugin: _Plugin) -> str:
  207. """Return a canonical name for a plugin object.
  208. Note that a plugin may be registered under a different name
  209. specified by the caller of :meth:`register(plugin, name) <register>`.
  210. To obtain the name of n registered plugin use :meth:`get_name(plugin)
  211. <get_name>` instead.
  212. """
  213. name: str | None = getattr(plugin, "__name__", None)
  214. return name or str(id(plugin))
  215. def get_plugin(self, name: str) -> Any | None:
  216. """Return the plugin registered under the given name, if any."""
  217. return self._name2plugin.get(name)
  218. def has_plugin(self, name: str) -> bool:
  219. """Return whether a plugin with the given name is registered."""
  220. return self.get_plugin(name) is not None
  221. def get_name(self, plugin: _Plugin) -> str | None:
  222. """Return the name the plugin is registered under, or ``None`` if
  223. is isn't."""
  224. for name, val in self._name2plugin.items():
  225. if plugin == val:
  226. return name
  227. return None
  228. def _verify_hook(self, hook: _HookCaller, hookimpl: HookImpl) -> None:
  229. if hook.is_historic() and (hookimpl.hookwrapper or hookimpl.wrapper):
  230. raise PluginValidationError(
  231. hookimpl.plugin,
  232. "Plugin %r\nhook %r\nhistoric incompatible with yield/wrapper/hookwrapper"
  233. % (hookimpl.plugin_name, hook.name),
  234. )
  235. assert hook.spec is not None
  236. if hook.spec.warn_on_impl:
  237. _warn_for_function(hook.spec.warn_on_impl, hookimpl.function)
  238. # positional arg checking
  239. notinspec = set(hookimpl.argnames) - set(hook.spec.argnames)
  240. if notinspec:
  241. raise PluginValidationError(
  242. hookimpl.plugin,
  243. "Plugin %r for hook %r\nhookimpl definition: %s\n"
  244. "Argument(s) %s are declared in the hookimpl but "
  245. "can not be found in the hookspec"
  246. % (
  247. hookimpl.plugin_name,
  248. hook.name,
  249. _formatdef(hookimpl.function),
  250. notinspec,
  251. ),
  252. )
  253. if (
  254. hookimpl.wrapper or hookimpl.hookwrapper
  255. ) and not inspect.isgeneratorfunction(hookimpl.function):
  256. raise PluginValidationError(
  257. hookimpl.plugin,
  258. "Plugin %r for hook %r\nhookimpl definition: %s\n"
  259. "Declared as wrapper=True or hookwrapper=True "
  260. "but function is not a generator function"
  261. % (hookimpl.plugin_name, hook.name, _formatdef(hookimpl.function)),
  262. )
  263. if hookimpl.wrapper and hookimpl.hookwrapper:
  264. raise PluginValidationError(
  265. hookimpl.plugin,
  266. "Plugin %r for hook %r\nhookimpl definition: %s\n"
  267. "The wrapper=True and hookwrapper=True options are mutually exclusive"
  268. % (hookimpl.plugin_name, hook.name, _formatdef(hookimpl.function)),
  269. )
  270. def check_pending(self) -> None:
  271. """Verify that all hooks which have not been verified against a
  272. hook specification are optional, otherwise raise
  273. :class:`PluginValidationError`."""
  274. for name in self.hook.__dict__:
  275. if name[0] != "_":
  276. hook: _HookCaller = getattr(self.hook, name)
  277. if not hook.has_spec():
  278. for hookimpl in hook.get_hookimpls():
  279. if not hookimpl.optionalhook:
  280. raise PluginValidationError(
  281. hookimpl.plugin,
  282. "unknown hook %r in plugin %r"
  283. % (name, hookimpl.plugin),
  284. )
  285. def load_setuptools_entrypoints(self, group: str, name: str | None = None) -> int:
  286. """Load modules from querying the specified setuptools ``group``.
  287. :param str group: Entry point group to load plugins.
  288. :param str name: If given, loads only plugins with the given ``name``.
  289. :rtype: int
  290. :return: The number of plugins loaded by this call.
  291. """
  292. count = 0
  293. for dist in list(importlib_metadata.distributions()):
  294. for ep in dist.entry_points:
  295. if (
  296. ep.group != group
  297. or (name is not None and ep.name != name)
  298. # already registered
  299. or self.get_plugin(ep.name)
  300. or self.is_blocked(ep.name)
  301. ):
  302. continue
  303. plugin = ep.load()
  304. self.register(plugin, name=ep.name)
  305. self._plugin_distinfo.append((plugin, DistFacade(dist)))
  306. count += 1
  307. return count
  308. def list_plugin_distinfo(self) -> list[tuple[_Plugin, DistFacade]]:
  309. """Return a list of (plugin, distinfo) pairs for all
  310. setuptools-registered plugins."""
  311. return list(self._plugin_distinfo)
  312. def list_name_plugin(self) -> list[tuple[str, _Plugin]]:
  313. """Return a list of (name, plugin) pairs for all registered plugins."""
  314. return list(self._name2plugin.items())
  315. def get_hookcallers(self, plugin: _Plugin) -> list[_HookCaller] | None:
  316. """Get all hook callers for the specified plugin."""
  317. if self.get_name(plugin) is None:
  318. return None
  319. hookcallers = []
  320. for hookcaller in self.hook.__dict__.values():
  321. for hookimpl in hookcaller.get_hookimpls():
  322. if hookimpl.plugin is plugin:
  323. hookcallers.append(hookcaller)
  324. return hookcallers
  325. def add_hookcall_monitoring(
  326. self, before: _BeforeTrace, after: _AfterTrace
  327. ) -> Callable[[], None]:
  328. """Add before/after tracing functions for all hooks.
  329. Returns an undo function which, when called, removes the added tracers.
  330. ``before(hook_name, hook_impls, kwargs)`` will be called ahead
  331. of all hook calls and receive a hookcaller instance, a list
  332. of HookImpl instances and the keyword arguments for the hook call.
  333. ``after(outcome, hook_name, hook_impls, kwargs)`` receives the
  334. same arguments as ``before`` but also a :class:`_Result` object
  335. which represents the result of the overall hook call.
  336. """
  337. oldcall = self._inner_hookexec
  338. def traced_hookexec(
  339. hook_name: str,
  340. hook_impls: Sequence[HookImpl],
  341. caller_kwargs: Mapping[str, object],
  342. firstresult: bool,
  343. ) -> object | list[object]:
  344. before(hook_name, hook_impls, caller_kwargs)
  345. outcome = _Result.from_call(
  346. lambda: oldcall(hook_name, hook_impls, caller_kwargs, firstresult)
  347. )
  348. after(outcome, hook_name, hook_impls, caller_kwargs)
  349. return outcome.get_result()
  350. self._inner_hookexec = traced_hookexec
  351. def undo() -> None:
  352. self._inner_hookexec = oldcall
  353. return undo
  354. def enable_tracing(self) -> Callable[[], None]:
  355. """Enable tracing of hook calls.
  356. Returns an undo function which, when called, removes the added tracing.
  357. """
  358. hooktrace = self.trace.root.get("hook")
  359. def before(
  360. hook_name: str, methods: Sequence[HookImpl], kwargs: Mapping[str, object]
  361. ) -> None:
  362. hooktrace.root.indent += 1
  363. hooktrace(hook_name, kwargs)
  364. def after(
  365. outcome: _Result[object],
  366. hook_name: str,
  367. methods: Sequence[HookImpl],
  368. kwargs: Mapping[str, object],
  369. ) -> None:
  370. if outcome.exception is None:
  371. hooktrace("finish", hook_name, "-->", outcome.get_result())
  372. hooktrace.root.indent -= 1
  373. return self.add_hookcall_monitoring(before, after)
  374. def subset_hook_caller(
  375. self, name: str, remove_plugins: Iterable[_Plugin]
  376. ) -> _HookCaller:
  377. """Return a proxy :py:class:`._hooks._HookCaller` instance for the named
  378. method which manages calls to all registered plugins except the ones
  379. from remove_plugins."""
  380. orig: _HookCaller = getattr(self.hook, name)
  381. plugins_to_remove = {plug for plug in remove_plugins if hasattr(plug, name)}
  382. if plugins_to_remove:
  383. return _SubsetHookCaller(orig, plugins_to_remove)
  384. return orig
  385. def _formatdef(func: Callable[..., object]) -> str:
  386. return f"{func.__name__}{inspect.signature(func)}"