implement priority in calling do().

store module.do instead of module, making this an awful lot like
an internal command bus. steal a trick from irclib and sort based
on priority, while we're at it. we added priority a while ago, and
this means that low prio (high value) items are called later, meaning
we can do things like

!join #botname

and have the IrcAdmin handler handle it before MegaHAL gets to it
This commit is contained in:
Brian S. Stephan 2011-01-08 00:32:46 -06:00
parent 07be2f3a85
commit 015fe87e1a
1 changed files with 6 additions and 2 deletions

View File

@ -16,6 +16,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import bisect
from ConfigParser import NoOptionError, NoSectionError
import re
import signal
@ -81,6 +82,7 @@ class DrBotIRC(irclib.IRC):
"""Implement a customized irclib IRC."""
modlist = []
internal_bus = []
config = None
server = None
@ -228,8 +230,8 @@ class DrBotIRC(irclib.IRC):
"""Walk the loaded modules, see if any reply to input text."""
# aliases resolved. run result against each module
for module in self.modlist:
ret = module.do(connection, event, nick, userhost, attempt, admin_unlocked)
for (priority, handler) in self.internal_bus:
ret = handler(connection, event, nick, userhost, attempt, admin_unlocked)
if ret is not None:
# a module had a result for us, post-alias, so return it
# TODO: scan all modules with compounding results
@ -268,6 +270,7 @@ class DrBotIRC(irclib.IRC):
module = sys.modules[modstr]
botmod = eval('module.' + modname + '(self, self.config, self.server)')
self.modlist.append(botmod)
bisect.insort(self.internal_bus, (botmod.priority(), botmod.do))
botmod.register_handlers(self.server)
# might as well add it to the list
@ -290,6 +293,7 @@ class DrBotIRC(irclib.IRC):
# remove module references
self.modlist.remove(module)
self.internal_bus.remove((module.priority(), module.do))
module.unregister_handlers()
# del it