Skip to content

Commit

Permalink
plugins: cleanup and rename type to group
Browse files Browse the repository at this point in the history
  • Loading branch information
abn committed Apr 4, 2022
1 parent a84df8d commit 6ad9785
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 13 deletions.
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]]:
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

0 comments on commit 6ad9785

Please sign in to comment.