Skip to content

Commit 21c2823

Browse files
committed
removed circular dependency between pm and errbot
1 parent 899eea7 commit 21c2823

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

errbot/bootstrap.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,24 @@ def setup_bot(backend_name: str, logger, config, restore=None) -> ErrBot:
129129
if isinstance(plugin_indexes, str):
130130
plugin_indexes = (plugin_indexes, )
131131

132-
repo_manager = BotRepoManager(storage_plugin,
133-
botplugins_dir,
134-
plugin_indexes)
135-
botpm = BotPluginManager(storage_plugin,
136-
repo_manager,
137-
config.BOT_EXTRA_PLUGIN_DIR,
138-
config.AUTOINSTALL_DEPS,
139-
getattr(config, 'CORE_PLUGINS', None),
140-
getattr(config, 'PLUGINS_CALLBACK_ORDER', (None, )))
141-
142-
# init the backend manager & the bot
143-
144132
backendpm = BackendPluginManager(config, 'errbot.backends', backend_name,
145133
ErrBot, CORE_BACKENDS, getattr(config, 'BOT_EXTRA_BACKEND_DIR', []))
146134

147135
log.info(f'Found Backend plugin: {backendpm.plugin_info.name}')
148136

137+
repo_manager = BotRepoManager(storage_plugin,
138+
botplugins_dir,
139+
plugin_indexes)
140+
149141
try:
150142
bot = backendpm.load_plugin()
143+
botpm = BotPluginManager(storage_plugin,
144+
repo_manager,
145+
config.BOT_EXTRA_PLUGIN_DIR,
146+
config.AUTOINSTALL_DEPS,
147+
getattr(config, 'CORE_PLUGINS', None),
148+
lambda name, clazz: clazz(bot, name),
149+
getattr(config, 'PLUGINS_CALLBACK_ORDER', (None, )))
151150
bot.attach_storage_plugin(storage_plugin)
152151
bot.attach_repo_manager(repo_manager)
153152
bot.attach_plugin_manager(botpm)

errbot/core.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ def attach_repo_manager(self, repo_manager):
7171

7272
def attach_plugin_manager(self, plugin_manager):
7373
self.plugin_manager = plugin_manager
74-
plugin_manager.attach_bot(self)
7574

7675
def attach_storage_plugin(self, storage_plugin):
7776
# the storage_plugin is needed by the plugins

errbot/plugin_manager.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import traceback
99
from pathlib import Path
1010

11-
from typing import Tuple, Dict, Any, Type, Set, List, Optional
11+
from typing import Tuple, Dict, Any, Type, Set, List, Optional, Callable
1212

1313
from errbot.flow import BotFlow, Flow
1414
from errbot.repo_manager import BotRepoManager, check_dependencies
@@ -135,11 +135,22 @@ def __init__(self,
135135
extra_plugin_dir: Optional[str],
136136
autoinstall_deps: bool,
137137
core_plugins: Tuple[str, ...],
138+
plugin_instance_callback: Callable[[str, Type[BotPlugin]], BotPlugin],
138139
plugins_callback_order: Tuple[Optional[str], ...]):
140+
"""
141+
Creates a Plugin manager
142+
:param storage_plugin: the plugin used to store to config for this manager
143+
:param repo_manager: the repo manager
144+
:param extra_plugin_dir: an extra directory to search for plugins
145+
:param autoinstall_deps: if True, will install also the plugin deps from requirements.txt
146+
:param core_plugins: the list of core plugin that will be started
147+
:param plugin_instance_callback: the callback to instantiate a plugin (to inject the dependency on the bot)
148+
:param plugins_callback_order: the order on which the plugins will be callbacked
149+
"""
139150
super().__init__()
140-
self.bot = None
141151
self.autoinstall_deps = autoinstall_deps
142152
self._extra_plugin_dir = extra_plugin_dir
153+
self._plugin_instance_callback = plugin_instance_callback
143154
self.core_plugins = core_plugins
144155
# Make sure there is a 'None' entry in the callback order, to include
145156
# any plugin not explicitly ordered.
@@ -156,9 +167,6 @@ def __init__(self,
156167
if CONFIGS not in self:
157168
self[CONFIGS] = {}
158169

159-
def attach_bot(self, bot):
160-
self.bot = bot
161-
162170
def get_plugin_obj_by_name(self, name: str) -> BotPlugin:
163171
return self.plugins.get(name, None)
164172

@@ -233,7 +241,7 @@ def _load_plugins_generic(self,
233241

234242
# instantiate the plugin object.
235243
_, clazz = plugin_classes[0]
236-
dest_dict[name] = clazz(self.bot, name)
244+
dest_dict[name] = self._plugin_instance_callback(name, clazz)
237245

238246
except Exception:
239247
feedback[path] = traceback.format_exc()

tests/base_backend_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def __init__(self, extra_config=None):
110110
config.BOT_EXTRA_PLUGIN_DIR,
111111
config.AUTOINSTALL_DEPS,
112112
getattr(config, 'CORE_PLUGINS', None),
113+
lambda name, clazz: clazz(self, name),
113114
getattr(config, 'PLUGINS_CALLBACK_ORDER', (None, ))))
114115
self.inject_commands_from(self)
115116
self.inject_command_filters_from(ACLS(self))

0 commit comments

Comments
 (0)