diff --git a/plugins/FunDB.py b/plugins/FunDB.py index 74664bb0f..f5ec4f13b 100755 --- a/plugins/FunDB.py +++ b/plugins/FunDB.py @@ -377,33 +377,35 @@ class FunDB(callbacks.Privmsg): irc.reply(msg, reply) def lart(self, irc, msg, args): - """[--id=] [for ] + """[] [for ] Uses a lart on (giving the reason, if offered). Will use lart number from the database when is given. """ - (optlist, rest) = getopt.getopt(args, '', ['id=']) - privmsgs.getArgs(rest) + (id, nick) = privmsgs.getArgs(args, optional=1) + try: + id = int(id) + if id < 1: + irc.error(msg, 'There is no such lart.') + return + except ValueError: + nick = ' '.join([id, nick]).strip() + id = 0 + if not nick: + raise callbacks.ArgumentError + try: (nick, reason) = map(' '.join, - utils.itersplit('for'.__eq__, rest, 1)) + utils.itersplit('for'.__eq__, nick.split(), 1)) except ValueError: - nick = ' '.join(rest) reason = '' + cursor = self.db.cursor() - 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 + if id: + cursor.execute("""SELECT id, lart FROM larts WHERE id=%s""", id) + if cursor.rowcount == 0: + irc.error(msg, 'There is no such lart.') + return else: cursor.execute("""SELECT id, lart FROM larts WHERE lart NOTNULL @@ -425,38 +427,41 @@ class FunDB(callbacks.Privmsg): irc.reply(msg, s, action=True) def praise(self, irc, msg, args): - """[--id=] [for ] + """[] [for ] Uses a praise on (giving the reason, if offered). Will use praise number from the database when is given. """ - (optlist, rest) = getopt.getopt(args, '', ['id=']) - privmsgs.getArgs(rest) + (id, nick) = privmsgs.getArgs(args, optional=1) + try: + id = int(id) + if id < 1: + irc.error(msg, 'There is no such praise.') + return + except ValueError: + nick = ' '.join([id, nick]).strip() + id = 0 + if not nick: + raise callbacks.ArgumentError + try: (nick, reason) = map(' '.join, - utils.itersplit('for'.__eq__, rest, 1)) + utils.itersplit('for'.__eq__, nick.split(), 1)) except ValueError: - nick = ' '.join(rest) reason = '' + cursor = self.db.cursor() - 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 + if id: + cursor.execute("""SELECT id, praise FROM praises WHERE id=%s""", id) + if cursor.rowcount == 0: + irc.error(msg, 'There is no such praise.') + return 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 faa97dcae..6121af2f1 100644 --- a/test/test_FunDB.py +++ b/test/test_FunDB.py @@ -50,17 +50,17 @@ if sqlite is not None: def testLart(self): self.assertNotError('dbadd lart jabs $who') - self.assertRegexp('lart', '^lart [--id=]') + self.assertRegexp('lart', '^lart \[\]') 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.assertRegexp('lart --id=1', '^lart [--id=]') - self.assertResponse('lart --id=1 jemfinch', + self.assertRegexp('lart 1', '^lart \[\]') + self.assertResponse('lart 1 jemfinch', '\x01ACTION jabs jemfinch (#1)\x01') - self.assertResponse('lart --id=2 jemfinch for being dumb', + self.assertResponse('lart 2 jemfinch for being dumb', '\x01ACTION shoots jemfinch for being dumb (#2)\x01') self.assertNotError('dbremove lart 1') self.assertNotError('dbnum lart') @@ -89,17 +89,17 @@ if sqlite is not None: def testPraise(self): self.assertNotError('dbadd praise pets $who') - self.assertRegexp('praise', '^praise [--id=]') + self.assertRegexp('praise', '^praise \[\]') 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.assertRegexp('praise --id=1', '^praise [--id=]') - self.assertResponse('praise --id=1 jemfinch', + self.assertRegexp('praise 1', '^praise \[\]') + self.assertResponse('praise 1 jemfinch', '\x01ACTION pets jemfinch (#1)\x01') - self.assertResponse('praise --id=2 jemfinch for being him', + self.assertResponse('praise 2 jemfinch for being him', '\x01ACTION gives jemfinch a cookie for being him (#2)\x01') self.assertNotError('dbremove praise 1') self.assertNotError('dbnum praise')