Skip to content
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
3 changes: 2 additions & 1 deletion src/poetry/console/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,10 @@ def _load_plugins(self, io: IO) -> None:
self._disable_plugins = io.input.has_parameter_option("--no-plugins")

if not self._disable_plugins:
from poetry.plugins.application_plugin import ApplicationPlugin
from poetry.plugins.plugin_manager import PluginManager

manager = PluginManager("application.plugin")
manager = PluginManager(ApplicationPlugin.group)
manager.load_plugins()
manager.activate(self)

Expand Down
5 changes: 3 additions & 2 deletions src/poetry/console/commands/plugin/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class PluginShowCommand(Command):

def handle(self) -> int:
from poetry.plugins.application_plugin import ApplicationPlugin
from poetry.plugins.plugin import Plugin
from poetry.plugins.plugin_manager import PluginManager
from poetry.repositories.installed_repository import InstalledRepository
from poetry.utils.env import EnvManager
Expand All @@ -34,8 +35,8 @@ def handle(self) -> int:
)

entry_points = (
PluginManager("application.plugin").get_plugin_entry_points()
+ PluginManager("plugin").get_plugin_entry_points()
PluginManager(ApplicationPlugin.group).get_plugin_entry_points()
+ PluginManager(Plugin.group).get_plugin_entry_points()
)

system_env = EnvManager.get_system_env(naive=True)
Expand Down
3 changes: 2 additions & 1 deletion src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from poetry.locations import CONFIG_DIR
from poetry.packages.locker import Locker
from poetry.packages.project_package import ProjectPackage
from poetry.plugins.plugin import Plugin
from poetry.plugins.plugin_manager import PluginManager
from poetry.poetry import Poetry

Expand Down Expand Up @@ -77,7 +78,7 @@ def create_poetry(
poetry, poetry.local_config.get("source", []), config, io
)

plugin_manager = PluginManager("plugin", disable_plugins=disable_plugins)
plugin_manager = PluginManager(Plugin.group, disable_plugins=disable_plugins)
plugin_manager.load_plugins()
poetry.set_plugin_manager(plugin_manager)
plugin_manager.activate(poetry, io)
Expand Down
12 changes: 9 additions & 3 deletions src/poetry/plugins/application_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@

if TYPE_CHECKING:
from poetry.console.application import Application
from poetry.console.commands.command import Command


class ApplicationPlugin(BasePlugin):
"""
Base class for plugins.
Base class for application plugins.
"""

type = "application.plugin"
group = "poetry.application.plugin"

@property
def commands(self) -> list[type[Command]]:
Comment thread
abn marked this conversation as resolved.
Resolved
return []

def activate(self, application: Application) -> None:
raise NotImplementedError()
for command in self.commands:
application.command_loader.register_factory(command.name, lambda: command())
12 changes: 12 additions & 0 deletions src/poetry/plugins/base_plugin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
from __future__ import annotations

from abc import abstractmethod


class BasePlugin:
"""
Base class for all plugin types

The `activate()` method must be implemented and receives the Poetry instance.
"""

PLUGIN_API_VERSION = "1.0.0"

@property
@abstractmethod
def group(self) -> str:
"""
Name of entrypoint group the plugin belongs to.
"""
raise NotImplementedError()
6 changes: 3 additions & 3 deletions src/poetry/plugins/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from abc import abstractmethod
from typing import TYPE_CHECKING

from poetry.plugins.base_plugin import BasePlugin
Expand All @@ -14,11 +15,10 @@
class Plugin(BasePlugin):
"""
Generic plugin not related to the console application.
The activate() method must be implemented and receives
the Poetry instance.
"""

type = "plugin"
group = "poetry.plugin"

@abstractmethod
def activate(self, poetry: Poetry, io: IO) -> None:
raise NotImplementedError()
6 changes: 3 additions & 3 deletions src/poetry/plugins/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class PluginManager:
This class registers and activates plugins.
"""

def __init__(self, type: str, disable_plugins: bool = False) -> None:
self._type = type
def __init__(self, group: str, disable_plugins: bool = False) -> None:
self._group = group
self._disable_plugins = disable_plugins
self._plugins: list[Plugin] = []

Expand All @@ -33,7 +33,7 @@ def load_plugins(self) -> None:
self._load_plugin_entrypoint(entrypoint)

def get_plugin_entry_points(self) -> list[entrypoints.EntryPoint]:
return entrypoints.get_group_all(f"poetry.{self._type}")
return entrypoints.get_group_all(self._group)

def add_plugin(self, plugin: Plugin) -> None:
if not isinstance(plugin, (Plugin, ApplicationPlugin)):
Expand Down
3 changes: 1 addition & 2 deletions tests/console/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def handle(self) -> int:


class AddCommandPlugin(ApplicationPlugin):
def activate(self, application: Application) -> None:
application.command_loader.register_factory("foo", lambda: FooCommand())
commands = [FooCommand]


def test_application_with_plugins(mocker: MockerFixture):
Expand Down
12 changes: 5 additions & 7 deletions tests/plugins/test_plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


class ManagerFactory(Protocol):
def __call__(self, type: str = "plugin") -> PluginManager:
def __call__(self, group: str = Plugin.group) -> PluginManager:
...


Expand All @@ -37,9 +37,7 @@ def activate(self, poetry: Poetry, io: BufferedIO) -> None:


class MyCommandPlugin(ApplicationPlugin):
@property
def commands(self) -> list[str]:
return []
commands = []


class InvalidPlugin:
Expand Down Expand Up @@ -68,15 +66,15 @@ def io() -> BufferedIO:

@pytest.fixture()
def manager_factory(poetry: Poetry, io: BufferedIO) -> ManagerFactory:
def _manager(type: str = "plugin") -> PluginManager:
return PluginManager(type)
def _manager(group: str = Plugin.group) -> PluginManager:
return PluginManager(group)

return _manager


@pytest.fixture()
def no_plugin_manager(poetry: Poetry, io: BufferedIO) -> PluginManager:
return PluginManager("plugin", disable_plugins=True)
return PluginManager(Plugin.group, disable_plugins=True)


def test_load_plugins_and_activate(
Expand Down