diff --git a/docs/source/changes.md b/docs/source/changes.md index 7df3a5ca..c08c93e8 100644 --- a/docs/source/changes.md +++ b/docs/source/changes.md @@ -5,6 +5,10 @@ chronological order. Releases follow [semantic versioning](https://semver.org/) releases are available on [PyPI](https://pypi.org/project/pytask) and [Anaconda.org](https://anaconda.org/conda-forge/pytask). +## 0.4.2 - 2023-xx-xx + +- {pull}`449` simplifies the code building the plugin manager. + ## 0.4.1 - 2023-10-11 - {pull}`443` ensures that `PythonNode.name` is always unique by only handling it diff --git a/src/_pytask/build.py b/src/_pytask/build.py index 11c903f3..1e569970 100644 --- a/src/_pytask/build.py +++ b/src/_pytask/build.py @@ -40,7 +40,7 @@ def pytask_extend_command_line_interface(cli: click.Group) -> None: cli.add_command(build_command) -def build( # noqa: C901, PLR0912, PLR0913, PLR0915 +def build( # noqa: C901, PLR0912, PLR0913 *, capture: Literal["fd", "no", "sys", "tee-sys"] | CaptureMethod = CaptureMethod.NO, check_casing_of_paths: bool = True, @@ -154,10 +154,6 @@ def build( # noqa: C901, PLR0912, PLR0913, PLR0915 """ try: pm = get_plugin_manager() - from _pytask import cli - - pm.register(cli) - pm.hook.pytask_add_hooks(pm=pm) raw_config = { "capture": capture, diff --git a/src/_pytask/clean.py b/src/_pytask/clean.py index fb39e4cc..941f8793 100644 --- a/src/_pytask/clean.py +++ b/src/_pytask/clean.py @@ -95,17 +95,13 @@ def pytask_parse_config(config: dict[str, Any]) -> None: help="Do not print the names of the removed paths.", default=False, ) -def clean(**raw_config: Any) -> NoReturn: # noqa: C901, PLR0912, PLR0915 +def clean(**raw_config: Any) -> NoReturn: # noqa: C901, PLR0912 """Clean the provided paths by removing files unknown to pytask.""" raw_config["command"] = "clean" try: # Duplication of the same mechanism in :func:`pytask.build`. pm = get_plugin_manager() - from _pytask import cli - - pm.register(cli) - pm.hook.pytask_add_hooks(pm=pm) config = pm.hook.pytask_configure(pm=pm, raw_config=raw_config) session = Session.from_config(config) diff --git a/src/_pytask/cli.py b/src/_pytask/cli.py index 2fcef289..406f9a00 100644 --- a/src/_pytask/cli.py +++ b/src/_pytask/cli.py @@ -1,7 +1,6 @@ """Implements the command line interface.""" from __future__ import annotations -import sys from typing import Any from typing import TYPE_CHECKING @@ -29,20 +28,12 @@ def _extend_command_line_interface(cli: click.Group) -> click.Group: """Add parameters from plugins to the commandline interface.""" - pm = _prepare_plugin_manager() + pm = get_plugin_manager() pm.hook.pytask_extend_command_line_interface(cli=cli) _sort_options_for_each_command_alphabetically(cli) return cli -def _prepare_plugin_manager() -> pluggy.PluginManager: - """Prepare the plugin manager.""" - pm = get_plugin_manager() - pm.register(sys.modules[__name__]) - pm.hook.pytask_add_hooks(pm=pm) - return pm - - def _sort_options_for_each_command_alphabetically(cli: click.Group) -> None: """Sort command line options and arguments for each command alphabetically.""" for command in cli.commands: diff --git a/src/_pytask/collect_command.py b/src/_pytask/collect_command.py index ef65d35e..e5bf4d3f 100644 --- a/src/_pytask/collect_command.py +++ b/src/_pytask/collect_command.py @@ -57,13 +57,7 @@ def collect(**raw_config: Any | None) -> NoReturn: raw_config["command"] = "collect" try: - # Duplication of the same mechanism in :func:`pytask.build`. pm = get_plugin_manager() - from _pytask import cli - - pm.register(cli) - pm.hook.pytask_add_hooks(pm=pm) - config = pm.hook.pytask_configure(pm=pm, raw_config=raw_config) session = Session.from_config(config) diff --git a/src/_pytask/graph.py b/src/_pytask/graph.py index caeeab2d..8d15a6a4 100644 --- a/src/_pytask/graph.py +++ b/src/_pytask/graph.py @@ -86,13 +86,7 @@ def dag(**raw_config: Any) -> NoReturn: """Create a visualization of the project's directed acyclic graph.""" try: pm = get_plugin_manager() - from _pytask import cli - - pm.register(cli) - pm.hook.pytask_add_hooks(pm=pm) - config = pm.hook.pytask_configure(pm=pm, raw_config=raw_config) - session = Session.from_config(config) except (ConfigurationError, Exception): @@ -153,10 +147,6 @@ def build_dag(raw_config: dict[str, Any]) -> nx.DiGraph: """ try: pm = get_plugin_manager() - from _pytask import cli - - pm.register(cli) - pm.hook.pytask_add_hooks(pm=pm) # If someone called the programmatic interface, we need to do some parsing. if "command" not in raw_config: diff --git a/src/_pytask/mark/__init__.py b/src/_pytask/mark/__init__.py index 15144e6e..fb90eb15 100644 --- a/src/_pytask/mark/__init__.py +++ b/src/_pytask/mark/__init__.py @@ -50,13 +50,7 @@ def markers(**raw_config: Any) -> NoReturn: raw_config["command"] = "markers" try: - # Duplication of the same mechanism in :func:`pytask.build`. pm = get_plugin_manager() - from _pytask import cli - - pm.register(cli) - pm.hook.pytask_add_hooks(pm=pm) - config = pm.hook.pytask_configure(pm=pm, raw_config=raw_config) session = Session.from_config(config) diff --git a/src/_pytask/pluginmanager.py b/src/_pytask/pluginmanager.py index 390e2fca..aa3acac7 100644 --- a/src/_pytask/pluginmanager.py +++ b/src/_pytask/pluginmanager.py @@ -11,4 +11,9 @@ def get_plugin_manager() -> pluggy.PluginManager: pm.add_hookspecs(hookspecs) pm.load_setuptools_entrypoints("pytask") + from _pytask import cli + + pm.register(cli) + pm.hook.pytask_add_hooks(pm=pm) + return pm diff --git a/src/_pytask/profile.py b/src/_pytask/profile.py index 61759d87..836f78fc 100644 --- a/src/_pytask/profile.py +++ b/src/_pytask/profile.py @@ -114,13 +114,7 @@ def profile(**raw_config: Any) -> NoReturn: raw_config["command"] = "profile" try: - # Duplication of the same mechanism in :func:`pytask.build`. pm = get_plugin_manager() - from _pytask import cli - - pm.register(cli) - pm.hook.pytask_add_hooks(pm=pm) - config = pm.hook.pytask_configure(pm=pm, raw_config=raw_config) session = Session.from_config(config)