Skip to content

Simplify building the plugin manager. #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/source/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/_pytask/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions src/_pytask/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 1 addition & 10 deletions src/_pytask/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Implements the command line interface."""
from __future__ import annotations

import sys
from typing import Any
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -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:
Expand Down
6 changes: 0 additions & 6 deletions src/_pytask/collect_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 0 additions & 10 deletions src/_pytask/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 0 additions & 6 deletions src/_pytask/mark/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions src/_pytask/pluginmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 0 additions & 6 deletions src/_pytask/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down