From 5b1026e1f92a1ee65a0eacba941b9013b36714c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Kochen?= Date: Tue, 21 Oct 2003 13:23:26 +0000 Subject: [PATCH] Added --id= to praise and lart (RFE #827075) --- plugins/FunDB.py | 67 +++++++++++++++++++++++++++++++++------------- test/test_FunDB.py | 35 +++++++++++++++++------- 2 files changed, 75 insertions(+), 27 deletions(-) diff --git a/plugins/FunDB.py b/plugins/FunDB.py index 1b8869b4a..46d95afb4 100755 --- a/plugins/FunDB.py +++ b/plugins/FunDB.py @@ -42,6 +42,7 @@ import string import os.path import sqlite +import getopt import conf import debug @@ -376,22 +377,37 @@ class FunDB(callbacks.Privmsg): irc.reply(msg, reply) def lart(self, irc, msg, args): - """ [for ] + """[--id=] [for ] - Uses a lart on (giving the reason, if offered). + Uses a lart on (giving the reason, if offered). Will use lart + number from the database when is given. """ - nick = privmsgs.getArgs(args) + (optlist, rest) = getopt.getopt(args, '', ['id=']) try: (nick, reason) = map(' '.join, - utils.itersplit('for'.__eq__, nick.split(), 1)) + utils.itersplit('for'.__eq__, rest, 1)) except ValueError: - nick = ' '.join(args) + nick = ' '.join(rest) reason = '' cursor = self.db.cursor() - cursor.execute("""SELECT id, lart FROM larts - WHERE lart NOTNULL - ORDER BY random() - LIMIT 1""") + for (option, argument) in optlist: + if option == '--id': + try: + argument = int(argument) + except ValueError: + irc.error(msg, 'The 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: irc.error(msg, 'There are currently no available larts.') else: @@ -408,22 +424,37 @@ class FunDB(callbacks.Privmsg): irc.reply(msg, s, action=True) def praise(self, irc, msg, args): - """ [for ] + """[--id=] [for ] - Uses a praise on (giving the reason, if offered). + Uses a praise on (giving the reason, if offered). Will use + praise number from the database when is given. """ - nick = privmsgs.getArgs(args) + (optlist, rest) = getopt.getopt(args, '', ['id=']) try: (nick, reason) = map(' '.join, - utils.itersplit('for'.__eq__, nick.split(), 1)) + utils.itersplit('for'.__eq__, rest, 1)) except ValueError: - nick = ' '.join(args) + nick = ' '.join(rest) reason = '' cursor = self.db.cursor() - cursor.execute("""SELECT id, praise FROM praises - WHERE praise NOTNULL - ORDER BY random() - LIMIT 1""") + for (option, argument) in optlist: + if option == '--id': + try: + argument = int(argument) + except ValueError: + irc.error(msg, 'The 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: irc.error(msg, 'There are currently no available praises.') else: diff --git a/test/test_FunDB.py b/test/test_FunDB.py index 0b04704de..7443eaa4d 100644 --- a/test/test_FunDB.py +++ b/test/test_FunDB.py @@ -50,13 +50,22 @@ if sqlite is not None: def testLart(self): self.assertNotError('dbadd lart jabs $who') - self.assertResponse('lart jemfinch for being dumb', '\x01ACTION'\ - ' jabs jemfinch for being dumb (#1)\x01') - self.assertResponse('lart jemfinch', '\x01ACTION jabs jemfinch'\ - ' (#1)\x01') + self.assertResponse('lart jemfinch for being dumb', + '\x01ACTION jabs jemfinch for being dumb (#1)\x01') + self.assertResponse('lart jemfinch', + '\x01ACTION jabs jemfinch (#1)\x01') 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('dbnum lart') + self.assertResponse('lart jemfinch', + '\x01ACTION shoots jemfinch (#2)\x01') + self.assertNotError('dbremove lart 2') + self.assertNotError('dbnum lart') self.assertError('lart jemfinch') def testExcuse(self): @@ -78,14 +87,22 @@ if sqlite is not None: def testPraise(self): self.assertNotError('dbadd praise pets $who') - self.assertNotError('praise jemfinch') - self.assertResponse('praise jemfinch for being him', '\x01ACTION'\ - ' pets jemfinch for being him (#1)\x01') - self.assertResponse('praise jemfinch', '\x01ACTION pets jemfinch'\ - ' (#1)\x01') + self.assertResponse('praise jemfinch for being him', + '\x01ACTION pets jemfinch for being him (#1)\x01') + self.assertResponse('praise jemfinch', + '\x01ACTION pets jemfinch (#1)\x01') 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('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') def testDbInfo(self):