Commit 748b1c60 by Arun Babu Neelicattu Committed by GitHub

plugins: cleanup and rename type to group (#5412)

parent cdbb70e7
...@@ -313,9 +313,10 @@ class Application(BaseApplication): ...@@ -313,9 +313,10 @@ class Application(BaseApplication):
self._disable_plugins = io.input.has_parameter_option("--no-plugins") self._disable_plugins = io.input.has_parameter_option("--no-plugins")
if not self._disable_plugins: if not self._disable_plugins:
from poetry.plugins.application_plugin import ApplicationPlugin
from poetry.plugins.plugin_manager import PluginManager from poetry.plugins.plugin_manager import PluginManager
manager = PluginManager("application.plugin") manager = PluginManager(ApplicationPlugin.group)
manager.load_plugins() manager.load_plugins()
manager.activate(self) manager.activate(self)
......
...@@ -19,6 +19,7 @@ class PluginShowCommand(Command): ...@@ -19,6 +19,7 @@ class PluginShowCommand(Command):
def handle(self) -> int: def handle(self) -> int:
from poetry.plugins.application_plugin import ApplicationPlugin from poetry.plugins.application_plugin import ApplicationPlugin
from poetry.plugins.plugin import Plugin
from poetry.plugins.plugin_manager import PluginManager from poetry.plugins.plugin_manager import PluginManager
from poetry.repositories.installed_repository import InstalledRepository from poetry.repositories.installed_repository import InstalledRepository
from poetry.utils.env import EnvManager from poetry.utils.env import EnvManager
...@@ -34,8 +35,8 @@ class PluginShowCommand(Command): ...@@ -34,8 +35,8 @@ class PluginShowCommand(Command):
) )
entry_points = ( entry_points = (
PluginManager("application.plugin").get_plugin_entry_points() PluginManager(ApplicationPlugin.group).get_plugin_entry_points()
+ PluginManager("plugin").get_plugin_entry_points() + PluginManager(Plugin.group).get_plugin_entry_points()
) )
system_env = EnvManager.get_system_env(naive=True) system_env = EnvManager.get_system_env(naive=True)
......
...@@ -12,6 +12,7 @@ from poetry.config.file_config_source import FileConfigSource ...@@ -12,6 +12,7 @@ from poetry.config.file_config_source import FileConfigSource
from poetry.locations import CONFIG_DIR from poetry.locations import CONFIG_DIR
from poetry.packages.locker import Locker from poetry.packages.locker import Locker
from poetry.packages.project_package import ProjectPackage from poetry.packages.project_package import ProjectPackage
from poetry.plugins.plugin import Plugin
from poetry.plugins.plugin_manager import PluginManager from poetry.plugins.plugin_manager import PluginManager
from poetry.poetry import Poetry from poetry.poetry import Poetry
...@@ -77,7 +78,7 @@ class Factory(BaseFactory): ...@@ -77,7 +78,7 @@ class Factory(BaseFactory):
poetry, poetry.local_config.get("source", []), config, io 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() plugin_manager.load_plugins()
poetry.set_plugin_manager(plugin_manager) poetry.set_plugin_manager(plugin_manager)
plugin_manager.activate(poetry, io) plugin_manager.activate(poetry, io)
......
...@@ -7,14 +7,20 @@ from poetry.plugins.base_plugin import BasePlugin ...@@ -7,14 +7,20 @@ from poetry.plugins.base_plugin import BasePlugin
if TYPE_CHECKING: if TYPE_CHECKING:
from poetry.console.application import Application from poetry.console.application import Application
from poetry.console.commands.command import Command
class ApplicationPlugin(BasePlugin): 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]]:
return []
def activate(self, application: Application) -> None: def activate(self, application: Application) -> None:
raise NotImplementedError() for command in self.commands:
application.command_loader.register_factory(command.name, lambda: command())
from __future__ import annotations from __future__ import annotations
from abc import abstractmethod
class BasePlugin: class BasePlugin:
""" """
Base class for all plugin types Base class for all plugin types
The `activate()` method must be implemented and receives the Poetry instance.
""" """
PLUGIN_API_VERSION = "1.0.0" PLUGIN_API_VERSION = "1.0.0"
@property
@abstractmethod
def group(self) -> str:
"""
Name of entrypoint group the plugin belongs to.
"""
raise NotImplementedError()
from __future__ import annotations from __future__ import annotations
from abc import abstractmethod
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from poetry.plugins.base_plugin import BasePlugin from poetry.plugins.base_plugin import BasePlugin
...@@ -14,11 +15,10 @@ if TYPE_CHECKING: ...@@ -14,11 +15,10 @@ if TYPE_CHECKING:
class Plugin(BasePlugin): class Plugin(BasePlugin):
""" """
Generic plugin not related to the console application. 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: def activate(self, poetry: Poetry, io: IO) -> None:
raise NotImplementedError() raise NotImplementedError()
...@@ -18,8 +18,8 @@ class PluginManager: ...@@ -18,8 +18,8 @@ class PluginManager:
This class registers and activates plugins. This class registers and activates plugins.
""" """
def __init__(self, type: str, disable_plugins: bool = False) -> None: def __init__(self, group: str, disable_plugins: bool = False) -> None:
self._type = type self._group = group
self._disable_plugins = disable_plugins self._disable_plugins = disable_plugins
self._plugins: list[Plugin] = [] self._plugins: list[Plugin] = []
...@@ -33,7 +33,7 @@ class PluginManager: ...@@ -33,7 +33,7 @@ class PluginManager:
self._load_plugin_entrypoint(entrypoint) self._load_plugin_entrypoint(entrypoint)
def get_plugin_entry_points(self) -> list[entrypoints.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: def add_plugin(self, plugin: Plugin) -> None:
if not isinstance(plugin, (Plugin, ApplicationPlugin)): if not isinstance(plugin, (Plugin, ApplicationPlugin)):
......
...@@ -28,8 +28,7 @@ class FooCommand(Command): ...@@ -28,8 +28,7 @@ class FooCommand(Command):
class AddCommandPlugin(ApplicationPlugin): class AddCommandPlugin(ApplicationPlugin):
def activate(self, application: Application) -> None: commands = [FooCommand]
application.command_loader.register_factory("foo", lambda: FooCommand())
def test_application_with_plugins(mocker: MockerFixture): def test_application_with_plugins(mocker: MockerFixture):
......
...@@ -26,7 +26,7 @@ CWD = Path(__file__).parent.parent / "fixtures" / "simple_project" ...@@ -26,7 +26,7 @@ CWD = Path(__file__).parent.parent / "fixtures" / "simple_project"
class ManagerFactory(Protocol): class ManagerFactory(Protocol):
def __call__(self, type: str = "plugin") -> PluginManager: def __call__(self, group: str = Plugin.group) -> PluginManager:
... ...
...@@ -37,9 +37,7 @@ class MyPlugin(Plugin): ...@@ -37,9 +37,7 @@ class MyPlugin(Plugin):
class MyCommandPlugin(ApplicationPlugin): class MyCommandPlugin(ApplicationPlugin):
@property commands = []
def commands(self) -> list[str]:
return []
class InvalidPlugin: class InvalidPlugin:
...@@ -68,15 +66,15 @@ def io() -> BufferedIO: ...@@ -68,15 +66,15 @@ def io() -> BufferedIO:
@pytest.fixture() @pytest.fixture()
def manager_factory(poetry: Poetry, io: BufferedIO) -> ManagerFactory: def manager_factory(poetry: Poetry, io: BufferedIO) -> ManagerFactory:
def _manager(type: str = "plugin") -> PluginManager: def _manager(group: str = Plugin.group) -> PluginManager:
return PluginManager(type) return PluginManager(group)
return _manager return _manager
@pytest.fixture() @pytest.fixture()
def no_plugin_manager(poetry: Poetry, io: BufferedIO) -> PluginManager: 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( def test_load_plugins_and_activate(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment