Skip to content

Commit e9d4e70

Browse files
committed
Remove direct references between plugins
1 parent 292aa34 commit e9d4e70

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

cloudbot/plugin.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from itertools import chain
1212
from operator import attrgetter
1313
from pathlib import Path
14+
from weakref import WeakValueDictionary
1415

1516
import sqlalchemy
1617

@@ -90,6 +91,7 @@ def __init__(self, bot):
9091
self.bot = bot
9192

9293
self.plugins = {}
94+
self._plugin_name_map = WeakValueDictionary()
9395
self.commands = {}
9496
self.raw_triggers = {}
9597
self.catch_all_triggers = []
@@ -103,6 +105,14 @@ def __init__(self, bot):
103105
self.perm_hooks = defaultdict(list)
104106
self._hook_waiting_queues = {}
105107

108+
def find_plugin(self, title):
109+
"""
110+
Finds a loaded plugin and returns its Plugin object
111+
:param title: the title of the plugin to find
112+
:return: The Plugin object if it exists, otherwise None
113+
"""
114+
return self._plugin_name_map.get(title)
115+
106116
@asyncio.coroutine
107117
def load_all(self, plugin_dir):
108118
"""
@@ -187,6 +197,7 @@ def load_plugin(self, path):
187197
return
188198

189199
self.plugins[plugin.file_path] = plugin
200+
self._plugin_name_map[plugin.title] = plugin
190201

191202
for on_cap_available_hook in plugin.hooks["on_cap_available"]:
192203
for cap in on_cap_available_hook.caps:
@@ -610,6 +621,8 @@ def __init__(self, filepath, filename, title, code):
610621
# we need to find tables for each plugin so that they can be unloaded from the global metadata when the
611622
# plugin is reloaded
612623
self.tables = find_tables(code)
624+
# Keep a reference to this in case another plugin needs to access it
625+
self.code = code
613626

614627
@asyncio.coroutine
615628
def create_tables(self, bot):

plugins/herald.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import re
22
import time
33
from cloudbot import hook
4-
from plugins import grab
54

65
import random
76

@@ -62,7 +61,7 @@ def deleteherald(text, chan, db, conn):
6261
return "{} does not have a herald".format(text.lower())
6362

6463
@hook.irc_raw("JOIN", singlethread=True)
65-
def welcome(nick, action, message, chan, event, db, conn):
64+
def welcome(nick, message, event, db, bot):
6665
# For some reason chan isn't passed correctly. The below hack is sloppy and may need to be adjusted for different networks.
6766
# If someone knows how to get the channel a better way please fix this.
6867
# freenode uncomment then next line
@@ -73,6 +72,8 @@ def welcome(nick, action, message, chan, event, db, conn):
7372
bino_re = re.compile('b+i+n+o+', re.IGNORECASE)
7473
offensive_re = re.compile('卐')
7574

75+
grab = bot.plugin_manager.find_plugin("grab")
76+
7677
try:
7778
chan = event.irc_raw.split(':')[2].lower()
7879
except:
@@ -97,7 +98,11 @@ def welcome(nick, action, message, chan, event, db, conn):
9798
if len(greet.split(' ')) >= 2:
9899
candidates = greet.lower().split(' ')[1:]
99100
text = random.choice(candidates)
100-
out = grab.grabrandom(text, chan, message)
101+
if grab is not None:
102+
out = grab.grabrandom(text, chan, message)
103+
else:
104+
out = "grab.py not loaded, original herald: {}".format(greet)
105+
101106
message(out, chan)
102107
elif decoy.search(colors_re.sub("", greet.replace('\u200b', '').replace(' ', '').replace('\u202f','').replace('\x02', ''))):
103108
message("DECOY DUCK --> {}".format(greet), chan)

0 commit comments

Comments
 (0)