I forget what I did.

This commit is contained in:
Jeremy Fincher 2004-01-08 00:44:35 +00:00
parent ed7748492f
commit 1132c63d38
1 changed files with 36 additions and 8 deletions

View File

@ -36,6 +36,7 @@ or repeatedly run at a particular interval.
import plugins import plugins
import sets
import time import time
import conf import conf
@ -55,6 +56,10 @@ def configure(onStart, afterConnect, advanced):
class Scheduler(callbacks.Privmsg): class Scheduler(callbacks.Privmsg):
def __init__(self):
callbacks.Privmsg.__init__(self)
self.events = {}
def _makeCommandFunction(self, irc, msg, command): def _makeCommandFunction(self, irc, msg, command):
"""Makes a function suitable for scheduling from command.""" """Makes a function suitable for scheduling from command."""
tokens = callbacks.tokenize(command) tokens = callbacks.tokenize(command)
@ -83,6 +88,7 @@ class Scheduler(callbacks.Privmsg):
return return
f = self._makeCommandFunction(irc, msg, command) f = self._makeCommandFunction(irc, msg, command)
id = schedule.addEvent(f, time.time() + seconds) id = schedule.addEvent(f, time.time() + seconds)
self.events[str(id)] = command
irc.reply(msg, '%s Event #%s added.' % (conf.replySuccess, id)) irc.reply(msg, '%s Event #%s added.' % (conf.replySuccess, id))
def remove(self, irc, msg, args): def remove(self, irc, msg, args):
@ -91,14 +97,19 @@ class Scheduler(callbacks.Privmsg):
Removes the event scheduled with id <id> from the schedule. Removes the event scheduled with id <id> from the schedule.
""" """
id = privmsgs.getArgs(args) id = privmsgs.getArgs(args)
try: id = id.lower()
id = int(id) if id in self.events:
except ValueError: del self.events[id]
pass try:
try: id = int(id)
schedule.removeEvent(id) except ValueError:
irc.replySuccess(msg) pass
except KeyError: try:
schedule.removeEvent(id)
irc.reply(msg, conf.replySuccess)
except KeyError:
irc.error(msg, 'Invalid event id.')
else:
irc.error(msg, 'Invalid event id.') irc.error(msg, 'Invalid event id.')
def repeat(self, irc, msg, args): def repeat(self, irc, msg, args):
@ -110,16 +121,33 @@ class Scheduler(callbacks.Privmsg):
unscheduled. unscheduled.
""" """
(name, seconds, command) = privmsgs.getArgs(args, required=3) (name, seconds, command) = privmsgs.getArgs(args, required=3)
name = name.lower()
try: try:
seconds = int(seconds) seconds = int(seconds)
except ValueError: except ValueError:
irc.error(msg, 'Invalid seconds: %r' % seconds) irc.error(msg, 'Invalid seconds: %r' % seconds)
return return
self.events[name] = command
f = self._makeCommandFunction(irc, msg, command) f = self._makeCommandFunction(irc, msg, command)
id = schedule.addPeriodicEvent(f, seconds, name) id = schedule.addPeriodicEvent(f, seconds, name)
assert id == name
# We don't reply because the command runs immediately. # We don't reply because the command runs immediately.
# irc.replySuccess(msg) # irc.replySuccess(msg)
def list(self, irc, msg, args):
"""takes no arguments.
Lists the currently scheduled events.
"""
L = self.events.items()
if L:
L.sort()
for (i, (name, command)) in enumerate(L):
L[i] = '%s: %s' % (name, utils.dqrepr(command))
irc.reply(msg, utils.commaAndify(L))
else:
irc.reply(msg, 'There are currently no scheduled commands.')
Class = Scheduler Class = Scheduler