mirror of
https://github.com/Matthww/TuneBot.git
synced 2026-09-21 22:47:51 +00:00
43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
import importlib
|
|
from typing import Any
|
|
from typing import TYPE_CHECKING
|
|
|
|
from tunebot.plugins import PluginInstance
|
|
from tunebot.plugins.exceptions import PluginInitFailed
|
|
|
|
if TYPE_CHECKING:
|
|
from tunebot import BasePluginInstance
|
|
from tunebot import ServiceBase
|
|
from bot import TuneBot
|
|
|
|
AnyDict = dict[Any, Any]
|
|
|
|
|
|
class FileSystemPluginLoader:
|
|
def __init__(self, bot: "TuneBot") -> None:
|
|
self.bot = bot
|
|
|
|
def load_plugin(self, plug_conf: AnyDict) -> "BasePluginInstance":
|
|
"""
|
|
Loads a plugin from configuration and returns a sequence of Services
|
|
|
|
Raises:
|
|
PluginInitFailed: [description]
|
|
|
|
Returns:
|
|
tuple[list["ServiceBase"], list[str]]: [description]
|
|
"""
|
|
services: list["ServiceBase"] = []
|
|
for service_location in plug_conf["services"]:
|
|
module = importlib.import_module(service_location)
|
|
|
|
if not hasattr(module, "setup"):
|
|
raise PluginInitFailed('Failed to find setup() for "{location}"')
|
|
|
|
services.append(module.setup(self.bot, plug_conf))
|
|
|
|
return PluginInstance(plug_conf["config"], services, plug_conf["cogs"])
|
|
|
|
|
|
__all__ = ("FileSystemPluginLoader",)
|