Commit 21c28235 authored by Guillaume Binet's avatar Guillaume Binet

removed circular dependency between pm and errbot

parent 899eea71
......@@ -129,25 +129,24 @@ def setup_bot(backend_name: str, logger, config, restore=None) -> ErrBot:
if isinstance(plugin_indexes, str):
plugin_indexes = (plugin_indexes, )
repo_manager = BotRepoManager(storage_plugin,
botplugins_dir,
plugin_indexes)
botpm = BotPluginManager(storage_plugin,
repo_manager,
config.BOT_EXTRA_PLUGIN_DIR,
config.AUTOINSTALL_DEPS,
getattr(config, 'CORE_PLUGINS', None),
getattr(config, 'PLUGINS_CALLBACK_ORDER', (None, )))
# init the backend manager & the bot
backendpm = BackendPluginManager(config, 'errbot.backends', backend_name,
ErrBot, CORE_BACKENDS, getattr(config, 'BOT_EXTRA_BACKEND_DIR', []))
log.info(f'Found Backend plugin: {backendpm.plugin_info.name}')
repo_manager = BotRepoManager(storage_plugin,
botplugins_dir,
plugin_indexes)
try:
bot = backendpm.load_plugin()
botpm = BotPluginManager(storage_plugin,
repo_manager,
config.BOT_EXTRA_PLUGIN_DIR,
config.AUTOINSTALL_DEPS,
getattr(config, 'CORE_PLUGINS', None),
lambda name, clazz: clazz(bot, name),
getattr(config, 'PLUGINS_CALLBACK_ORDER', (None, )))
bot.attach_storage_plugin(storage_plugin)
bot.attach_repo_manager(repo_manager)
bot.attach_plugin_manager(botpm)
......
......@@ -71,7 +71,6 @@ class ErrBot(Backend, StoreMixin):
def attach_plugin_manager(self, plugin_manager):
self.plugin_manager = plugin_manager
plugin_manager.attach_bot(self)
def attach_storage_plugin(self, storage_plugin):
# the storage_plugin is needed by the plugins
......
......@@ -8,7 +8,7 @@ import sys
import traceback
from pathlib import Path
from typing import Tuple, Dict, Any, Type, Set, List, Optional
from typing import Tuple, Dict, Any, Type, Set, List, Optional, Callable
from errbot.flow import BotFlow, Flow
from errbot.repo_manager import BotRepoManager, check_dependencies
......@@ -135,11 +135,22 @@ class BotPluginManager(StoreMixin):
extra_plugin_dir: Optional[str],
autoinstall_deps: bool,
core_plugins: Tuple[str, ...],
plugin_instance_callback: Callable[[str, Type[BotPlugin]], BotPlugin],
plugins_callback_order: Tuple[Optional[str], ...]):
"""
Creates a Plugin manager
:param storage_plugin: the plugin used to store to config for this manager
:param repo_manager: the repo manager
:param extra_plugin_dir: an extra directory to search for plugins
:param autoinstall_deps: if True, will install also the plugin deps from requirements.txt
:param core_plugins: the list of core plugin that will be started
:param plugin_instance_callback: the callback to instantiate a plugin (to inject the dependency on the bot)
:param plugins_callback_order: the order on which the plugins will be callbacked
"""
super().__init__()
self.bot = None
self.autoinstall_deps = autoinstall_deps
self._extra_plugin_dir = extra_plugin_dir
self._plugin_instance_callback = plugin_instance_callback
self.core_plugins = core_plugins
# Make sure there is a 'None' entry in the callback order, to include
# any plugin not explicitly ordered.
......@@ -156,9 +167,6 @@ class BotPluginManager(StoreMixin):
if CONFIGS not in self:
self[CONFIGS] = {}
def attach_bot(self, bot):
self.bot = bot
def get_plugin_obj_by_name(self, name: str) -> BotPlugin:
return self.plugins.get(name, None)
......@@ -233,7 +241,7 @@ class BotPluginManager(StoreMixin):
# instantiate the plugin object.
_, clazz = plugin_classes[0]
dest_dict[name] = clazz(self.bot, name)
dest_dict[name] = self._plugin_instance_callback(name, clazz)
except Exception:
feedback[path] = traceback.format_exc()
......
......@@ -110,6 +110,7 @@ class DummyBackend(ErrBot):
config.BOT_EXTRA_PLUGIN_DIR,
config.AUTOINSTALL_DEPS,
getattr(config, 'CORE_PLUGINS', None),
lambda name, clazz: clazz(self, name),
getattr(config, 'PLUGINS_CALLBACK_ORDER', (None, ))))
self.inject_commands_from(self)
self.inject_command_filters_from(ACLS(self))
......
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