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
replies = []
event._target = None
for handler in h.get("all_events", []) + h.get(event.eventtype(), []):
ret = handler[1](connection, event)
if ret:
@ -237,20 +236,6 @@ class DrBotIRC(irclib.IRC):
# if we got here, there are no matching aliases, so return what we got
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):
"""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
newevent = copy.deepcopy(event)
newevent.arguments()[0] = subcmd
newevent._recursing = True
self.try_recursion(connection, newevent)
# recursion over, check for aliases

View File

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

View File

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

View File

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