Added --id=<id> to praise and lart (RFE #827075)

This commit is contained in:
Stéphan Kochen 2003-10-21 13:23:26 +00:00
parent 71c79417e1
commit 5b1026e1f9
2 changed files with 75 additions and 27 deletions

View File

@ -42,6 +42,7 @@ import string
import os.path import os.path
import sqlite import sqlite
import getopt
import conf import conf
import debug import debug
@ -376,22 +377,37 @@ class FunDB(callbacks.Privmsg):
irc.reply(msg, reply) irc.reply(msg, reply)
def lart(self, irc, msg, args): def lart(self, irc, msg, args):
"""<text> [for <reason>] """[--id=<id>] <text> [for <reason>]
Uses a lart on <text> (giving the reason, if offered). Uses a lart on <text> (giving the reason, if offered). Will use lart
number <id> from the database when <id> is given.
""" """
nick = privmsgs.getArgs(args) (optlist, rest) = getopt.getopt(args, '', ['id='])
try: try:
(nick, reason) = map(' '.join, (nick, reason) = map(' '.join,
utils.itersplit('for'.__eq__, nick.split(), 1)) utils.itersplit('for'.__eq__, rest, 1))
except ValueError: except ValueError:
nick = ' '.join(args) nick = ' '.join(rest)
reason = '' reason = ''
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute("""SELECT id, lart FROM larts for (option, argument) in optlist:
WHERE lart NOTNULL if option == '--id':
ORDER BY random() try:
LIMIT 1""") argument = int(argument)
except ValueError:
irc.error(msg, 'The <id> argument must be an integer.')
return
cursor.execute("""SELECT id, lart FROM larts WHERE id=%s""",
argument)
if cursor.rowcount == 0:
irc.error(msg, 'There is no such lart.')
return
break
else:
cursor.execute("""SELECT id, lart FROM larts
WHERE lart NOTNULL
ORDER BY random()
LIMIT 1""")
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.error(msg, 'There are currently no available larts.') irc.error(msg, 'There are currently no available larts.')
else: else:
@ -408,22 +424,37 @@ class FunDB(callbacks.Privmsg):
irc.reply(msg, s, action=True) irc.reply(msg, s, action=True)
def praise(self, irc, msg, args): def praise(self, irc, msg, args):
"""<text> [for <reason>] """[--id=<id>] <text> [for <reason>]
Uses a praise on <text> (giving the reason, if offered). Uses a praise on <text> (giving the reason, if offered). Will use
praise number <id> from the database when <id> is given.
""" """
nick = privmsgs.getArgs(args) (optlist, rest) = getopt.getopt(args, '', ['id='])
try: try:
(nick, reason) = map(' '.join, (nick, reason) = map(' '.join,
utils.itersplit('for'.__eq__, nick.split(), 1)) utils.itersplit('for'.__eq__, rest, 1))
except ValueError: except ValueError:
nick = ' '.join(args) nick = ' '.join(rest)
reason = '' reason = ''
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute("""SELECT id, praise FROM praises for (option, argument) in optlist:
WHERE praise NOTNULL if option == '--id':
ORDER BY random() try:
LIMIT 1""") argument = int(argument)
except ValueError:
irc.error(msg, 'The <id> argument must be an integer.')
return
cursor.execute("""SELECT id, praise FROM praises WHERE id=%s""",
argument)
if cursor.rowcount == 0:
irc.error(msg, 'There is no such praise.')
return
break
else:
cursor.execute("""SELECT id, praise FROM praises
WHERE praise NOTNULL
ORDER BY random()
LIMIT 1""")
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.error(msg, 'There are currently no available praises.') irc.error(msg, 'There are currently no available praises.')
else: else:

View File

@ -50,13 +50,22 @@ if sqlite is not None:
def testLart(self): def testLart(self):
self.assertNotError('dbadd lart jabs $who') self.assertNotError('dbadd lart jabs $who')
self.assertResponse('lart jemfinch for being dumb', '\x01ACTION'\ self.assertResponse('lart jemfinch for being dumb',
' jabs jemfinch for being dumb (#1)\x01') '\x01ACTION jabs jemfinch for being dumb (#1)\x01')
self.assertResponse('lart jemfinch', '\x01ACTION jabs jemfinch'\ self.assertResponse('lart jemfinch',
' (#1)\x01') '\x01ACTION jabs jemfinch (#1)\x01')
self.assertNotError('dbnum lart') self.assertNotError('dbnum lart')
self.assertNotError('dbadd lart shoots $who')
self.assertResponse('lart --id=1 jemfinch',
'\x01ACTION jabs jemfinch (#1)\x01')
self.assertResponse('lart --id=2 jemfinch for being dumb',
'\x01ACTION shoots jemfinch for being dumb (#2)\x01')
self.assertNotError('dbremove lart 1') self.assertNotError('dbremove lart 1')
self.assertNotError('dbnum lart') self.assertNotError('dbnum lart')
self.assertResponse('lart jemfinch',
'\x01ACTION shoots jemfinch (#2)\x01')
self.assertNotError('dbremove lart 2')
self.assertNotError('dbnum lart')
self.assertError('lart jemfinch') self.assertError('lart jemfinch')
def testExcuse(self): def testExcuse(self):
@ -78,14 +87,22 @@ if sqlite is not None:
def testPraise(self): def testPraise(self):
self.assertNotError('dbadd praise pets $who') self.assertNotError('dbadd praise pets $who')
self.assertNotError('praise jemfinch') self.assertResponse('praise jemfinch for being him',
self.assertResponse('praise jemfinch for being him', '\x01ACTION'\ '\x01ACTION pets jemfinch for being him (#1)\x01')
' pets jemfinch for being him (#1)\x01') self.assertResponse('praise jemfinch',
self.assertResponse('praise jemfinch', '\x01ACTION pets jemfinch'\ '\x01ACTION pets jemfinch (#1)\x01')
' (#1)\x01')
self.assertNotError('dbnum praise') self.assertNotError('dbnum praise')
self.assertNotError('dbadd praise gives $who a cookie')
self.assertResponse('praise --id=1 jemfinch',
'\x01ACTION pets jemfinch (#1)\x01')
self.assertResponse('praise --id=2 jemfinch for being him',
'\x01ACTION gives jemfinch a cookie for being him (#2)\x01')
self.assertNotError('dbremove praise 1') self.assertNotError('dbremove praise 1')
self.assertNotError('dbnum praise') self.assertNotError('dbnum praise')
self.assertResponse('praise jemfinch',
'\x01ACTION gives jemfinch a cookie (#2)\x01')
self.assertNotError('dbremove praise 2')
self.assertNotError('dbnum praise')
self.assertError('praise jemfinch') self.assertError('praise jemfinch')
def testDbInfo(self): def testDbInfo(self):