Moved seconds command to Misc from Scheduler.

This commit is contained in:
Jeremy Fincher 2004-01-04 15:19:41 +00:00
parent d9e4cef025
commit f1a68ff16f
4 changed files with 45 additions and 47 deletions

View File

@ -55,39 +55,6 @@ def configure(onStart, afterConnect, advanced):
class Scheduler(callbacks.Privmsg): class Scheduler(callbacks.Privmsg):
pass
def seconds(self, irc, msg, args):
"""[<days>d] [<hours>h] [<minutes>m] [<seconds>s]
Returns the number of seconds in the number of <days>, <hours>,
<minutes>, and <seconds> given. An example usage is
"seconds 2h 30m", which would return 9000, which is 3600*2 + 30*60.
Useful for scheduling events at a given number of seconds in the
future.
"""
if not args:
raise callbacks.ArgumentError
seconds = 0
for arg in args:
if not arg or arg[-1] not in 'dhms':
raise callbacks.ArgumentError
(s, kind) = arg[:-1], arg[-1]
try:
i = int(s)
except ValueError:
irc.error(msg, 'Invalid argument: %s' % arg)
return
if kind == 'd':
seconds += i*86400
elif kind == 'h':
seconds += i*3600
elif kind == 'm':
seconds += i*60
elif kind == 's':
seconds += i
irc.reply(msg, str(seconds))
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)

View File

@ -389,6 +389,37 @@ class Misc(callbacks.Privmsg):
irc.error(msg, 'I couldn\'t find a message matching that criteria in ' irc.error(msg, 'I couldn\'t find a message matching that criteria in '
'my history of %s messages.' % len(irc.state.history)) 'my history of %s messages.' % len(irc.state.history))
def seconds(self, irc, msg, args):
"""[<days>d] [<hours>h] [<minutes>m] [<seconds>s]
Returns the number of seconds in the number of <days>, <hours>,
<minutes>, and <seconds> given. An example usage is
"seconds 2h 30m", which would return 9000, which is 3600*2 + 30*60.
Useful for scheduling events at a given number of seconds in the
future.
"""
if not args:
raise callbacks.ArgumentError
seconds = 0
for arg in args:
if not arg or arg[-1] not in 'dhms':
raise callbacks.ArgumentError
(s, kind) = arg[:-1], arg[-1]
try:
i = int(s)
except ValueError:
irc.error(msg, 'Invalid argument: %s' % arg)
return
if kind == 'd':
seconds += i*86400
elif kind == 'h':
seconds += i*3600
elif kind == 'm':
seconds += i*60
elif kind == 's':
seconds += i
irc.reply(msg, str(seconds))
def tell(self, irc, msg, args): def tell(self, irc, msg, args):
"""<nick|channel> <text> """<nick|channel> <text>

View File

@ -188,6 +188,20 @@ class MiscTestCase(ChannelPluginTestCase, PluginDocumentation):
def testRevisionIsCaseInsensitive(self): def testRevisionIsCaseInsensitive(self):
self.assertNotError('revision misc') self.assertNotError('revision misc')
def testSeconds(self):
self.assertResponse('seconds 1s', '1')
self.assertResponse('seconds 10s', '10')
self.assertResponse('seconds 1m', '60')
self.assertResponse('seconds 1m 1s', '61')
self.assertResponse('seconds 1h', '3600')
self.assertResponse('seconds 1h 1s', '3601')
self.assertResponse('seconds 1d', '86400')
self.assertResponse('seconds 1d 1s', '86401')
self.assertResponse('seconds 2s', '2')
self.assertResponse('seconds 2m', '120')
self.assertResponse('seconds 2d 2h 2m 2s', '180122')
self.assertResponse('seconds 1s', '1')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -33,20 +33,6 @@ from testsupport import *
class MiscTestCase(ChannelPluginTestCase, PluginDocumentation): class MiscTestCase(ChannelPluginTestCase, PluginDocumentation):
plugins = ('Scheduler', 'Utilities') plugins = ('Scheduler', 'Utilities')
def testSeconds(self):
self.assertResponse('seconds 1s', '1')
self.assertResponse('seconds 10s', '10')
self.assertResponse('seconds 1m', '60')
self.assertResponse('seconds 1m 1s', '61')
self.assertResponse('seconds 1h', '3600')
self.assertResponse('seconds 1h 1s', '3601')
self.assertResponse('seconds 1d', '86400')
self.assertResponse('seconds 1d 1s', '86401')
self.assertResponse('seconds 2s', '2')
self.assertResponse('seconds 2m', '120')
self.assertResponse('seconds 2d 2h 2m 2s', '180122')
self.assertResponse('seconds 1s', '1')
def testAddRemove(self): def testAddRemove(self):
self.assertNotError('scheduler add [seconds 5s] echo foo bar baz') self.assertNotError('scheduler add [seconds 5s] echo foo bar baz')
self.assertNoResponse(' ', 4) self.assertNoResponse(' ', 4)