indicate recursion better by adding _recursing to Event

for simplicity's sake, this was added to the extlib/irclib rather
than subclassing. because i'm lazy. anyway, check that flag instead
of doing the event._target = None hack, since that hack was breaking
Markov.

for an unrelated reason (what to learn and not learn), update Markov

also remove an unused method that was getting in my way while coding this
This commit is contained in:
Brian S. Stephan 2012-03-29 20:07:32 -05:00
parent 7d41564d02
commit 07744a0f66
4 changed files with 9 additions and 24 deletions

View File

@ -139,7 +139,6 @@ class DrBotIRC(irclib.IRC):
h = self.handlers h = self.handlers
replies = [] replies = []
event._target = None
for handler in h.get("all_events", []) + h.get(event.eventtype(), []): for handler in h.get("all_events", []) + h.get(event.eventtype(), []):
ret = handler[1](connection, event) ret = handler[1](connection, event)
if ret: if ret:
@ -237,20 +236,6 @@ class DrBotIRC(irclib.IRC):
# if we got here, there are no matching aliases, so return what we got # if we got here, there are no matching aliases, so return what we got
return event return event
def reply(self, connection, event, replystr, stop_responding=False):
"""Reply over IRC to replypath or return a string with the reply."""
replypath = event.target()
if replystr is not None:
if replypath is None:
return replystr
else:
replies = replystr.split('\n')
for reply in replies:
connection.privmsg(replypath, reply)
if stop_responding:
return "NO MORE"
def try_recursion(self, connection, event): def try_recursion(self, connection, event):
"""Scan message for subcommands to execute and use as part of this command. """Scan message for subcommands to execute and use as part of this command.
@ -277,6 +262,8 @@ class DrBotIRC(irclib.IRC):
# copy the event and see if IT has recursion to do # copy the event and see if IT has recursion to do
newevent = copy.deepcopy(event) newevent = copy.deepcopy(event)
newevent.arguments()[0] = subcmd newevent.arguments()[0] = subcmd
newevent._recursing = True
self.try_recursion(connection, newevent) self.try_recursion(connection, newevent)
# recursion over, check for aliases # recursion over, check for aliases

View File

@ -114,7 +114,7 @@ class Module(object):
replypath = irclib.nm_to_n(event.source()) replypath = irclib.nm_to_n(event.source())
if replystr is not None: if replystr is not None:
if replypath is None: if event._recursing:
return replystr return replystr
else: else:
replies = replystr.split('\n') replies = replystr.split('\n')

View File

@ -1132,6 +1132,7 @@ class Event:
self._eventtype = eventtype self._eventtype = eventtype
self._source = source self._source = source
self._target = target self._target = target
self._recursing = False
if arguments: if arguments:
self._arguments = arguments self._arguments = arguments
else: else:

View File

@ -130,8 +130,6 @@ class Markov(Module):
db.commit() db.commit()
db.close() db.close()
self.db_register_module_version(self.__class__.__name__, version) self.db_register_module_version(self.__class__.__name__, version)
self._learn_line('','')
except sqlite3.Error as e: except sqlite3.Error as e:
db.rollback() db.rollback()
db.close() db.close()
@ -171,7 +169,7 @@ class Markov(Module):
if self.learnre.search(what) or self.replyre.search(what): if self.learnre.search(what) or self.replyre.search(what):
return return
self._learn_line(what, target) self._learn_line(what, target, event)
def do(self, connection, event, nick, userhost, what, admin_unlocked): def do(self, connection, event, nick, userhost, what, admin_unlocked):
"""Handle commands and inputs.""" """Handle commands and inputs."""
@ -207,7 +205,7 @@ class Markov(Module):
match = self.learnre.search(what) match = self.learnre.search(what)
if match: if match:
line = match.group(1) line = match.group(1)
self._learn_line(line, target) self._learn_line(line, target, event)
# return what was learned, for weird chaining purposes # return what was learned, for weird chaining purposes
return line return line
@ -288,7 +286,7 @@ class Markov(Module):
self.sendmsg(self.connection, t['target'], self.sendmsg(self.connection, t['target'],
'shutting up for 30 seconds due to last 30 seconds of activity') 'shutting up for 30 seconds due to last 30 seconds of activity')
def _learn_line(self, line, target): def _learn_line(self, line, target, event):
"""Create Markov chains from the provided line.""" """Create Markov chains from the provided line."""
# set up the head of the chain # set up the head of the chain
@ -297,9 +295,8 @@ class Markov(Module):
context_id = self._get_context_id_for_target(target) context_id = self._get_context_id_for_target(target)
# if there's no target, this is probably a sub-command. don't learn it # don't learn recursion
if target: if not event._recursing:
words = line.split() words = line.split()
if len(words) <= 0: if len(words) <= 0:
return line return line