Place MessageTreater in context

This commit is contained in:
nemunaire 2015-09-04 22:47:02 +02:00
parent 4af108265b
commit 60f7c6eea7
4 changed files with 13 additions and 14 deletions

View File

@ -25,7 +25,6 @@ from nemubot import __version__
from nemubot.consumer import Consumer, EventConsumer, MessageConsumer
from nemubot import datastore
import nemubot.hooks
from nemubot.modulecontext import ModuleContext
logger = logging.getLogger("nemubot")
@ -70,19 +69,19 @@ class Bot(threading.Thread):
self.event_timer = None
# Own hooks
from nemubot.hooks.manager import HooksManager
self.hooks = HooksManager()
from nemubot.treatment import MessageTreater
self.treater = MessageTreater()
import re
def in_ping(msg):
if re.match("^ *(m[' ]?entends?[ -]+tu|h?ear me|do you copy|ping)", msg.message, re.I) is not None:
return msg.respond("pong")
self.hooks.add_hook(nemubot.hooks.Message(in_ping), "in", "DirectAsk")
self.treater.hm.add_hook(nemubot.hooks.Message(in_ping), "in", "DirectAsk")
def in_echo(msg):
from nemubot.message import Text
return Text(msg.nick + ": " + " ".join(msg.args), to=msg.to_response)
self.hooks.add_hook(nemubot.hooks.Message(in_echo, "echo"), "in", "Command")
self.treater.hm.add_hook(nemubot.hooks.Message(in_echo, "echo"), "in", "Command")
def _help_msg(msg):
"""Parse and response to help messages"""
@ -129,7 +128,7 @@ class Bot(threading.Thread):
" de tous les modules disponibles localement",
message=["\x03\x02%s\x03\x02 (%s)" % (im, self.modules[im].__doc__) for im in self.modules if self.modules[im].__doc__])
return res
self.hooks.add_hook(nemubot.hooks.Message(_help_msg, "help"), "in", "Command")
self.treater.hm.add_hook(nemubot.hooks.Message(_help_msg, "help"), "in", "Command")
from queue import Queue
# Messages to be treated
@ -400,6 +399,7 @@ class Bot(threading.Thread):
module.print = prnt
# Create module context
from nemubot.modulecontext import ModuleContext
module.__nemubot_context__ = ModuleContext(self, module)
if not hasattr(module, "logger"):

View File

@ -58,10 +58,8 @@ class MessageConsumer:
msg.frm_owner = (not hasattr(self.srv, "owner") or self.srv.owner == msg.frm)
# Treat the message
from nemubot.treatment import MessageTreater
mt = MessageTreater(context.hooks) # Should be in context, this is static
for msg in msgs:
for res in mt.treat_msg(msg):
for res in context.treater.treat_msg(msg):
# Identify the destination
to_server = None
if isinstance(res, str):

View File

@ -62,13 +62,13 @@ class ModuleContext:
def add_hook(store, hook):
store = convert_legacy_store(store)
self.hooks.append((store, hook))
return context.hooks.add_hook(hook, store)
return context.treater.hm.add_hook(hook, store)
def del_hook(store, hook):
store = convert_legacy_store(store)
self.hooks.remove((store, hook))
return context.hooks.del_hook(hook, store)
return context.treater.hm.del_hook(hook, store)
def call_hook(store, msg):
for h in context.hooks.get_hooks(store):
for h in context.treater.hm.get_hooks(store):
if h.match(msg):
res = h.run(msg)
if isinstance(res, list):

View File

@ -23,8 +23,9 @@ class MessageTreater:
"""Treat a message"""
def __init__(self, hm):
self.hm = hm # Pointer to the HookManager
def __init__(self):
from nemubot.hooks.manager import HooksManager
self.hm = HooksManager()
def treat_msg(self, msg):