From 9264b635e2e4bcf73b3d62d86c34ef0eef858bd9 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Mon, 1 Dec 2003 23:47:37 +0000 Subject: [PATCH] Added most command. --- ChangeLog | 3 +++ plugins/Karma.py | 42 +++++++++++++++++++++++++++++++++++++++--- test/test_Karma.py | 21 +++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 402f812fe..75fb8a3e0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ + * Added Karma.most for determining various "mosts" in the Karma + database. + * Changed Lookup so added lookups are added in the Lookup plugin itself so there's no dependency on Alias, and so loaded lookups can be seen via 'list Lookup'. diff --git a/plugins/Karma.py b/plugins/Karma.py index 2b5efffb2..8ebcefb0a 100644 --- a/plugins/Karma.py +++ b/plugins/Karma.py @@ -30,9 +30,11 @@ ### """ -Plugin for handling basic Karma stuff for a channel. +Plugin for handling Karma stuff for a channel. """ +__revision__ = "$Id$" + import os import sets from itertools import imap @@ -40,7 +42,6 @@ from itertools import imap import sqlite import utils -__revision__ = "$Id$" import plugins import privmsgs @@ -169,7 +170,42 @@ class Karma(callbacks.PrivmsgCommandAndRegexp, s = 'Highest karma: %s. Lowest karma: %s.' % \ (utils.commaAndify(highest), utils.commaAndify(lowest)) irc.reply(msg, s) - + + _mostAbbrev = utils.abbrev(['increased', 'decreased', 'active']) + def most(self, irc, msg, args): + """[] {increased,decreased,active} + + Returns the most increased, the most decreased, or the most active + (the sum of increased and decreased) karma things. is only + necessary if the message isn't sent in the channel itself. + """ + channel = privmsgs.getChannel(msg, args) + kind = privmsgs.getArgs(args) + try: + kind = self._mostAbbrev[kind] + if kind == 'increased': + orderby = 'added' + elif kind == 'decreased': + orderby = 'subtracted' + elif kind == 'active': + orderby = 'added+subtracted' + else: + self.log.error('Impossible condition in most: kind=%s' % kind) + irc.error(msg, conf.replyPossibleBug) + return + sql = "SELECT name, %s FROM karma ORDER BY %s DESC LIMIT 50" % \ + (orderby, orderby) + db = self.getDb(channel) + cursor = db.cursor() + cursor.execute(sql) + L = ['%s: %s' % (name, i) for (name, i) in cursor.fetchall()] + if L: + irc.reply(msg, utils.commaAndify(L)) + else: + irc.error(msg, 'I have no karma for this channel.') + except KeyError: + raise callbacks.ArgumentError + def increaseKarma(self, irc, msg, match): r"^(\S+)\+\+(|\s+)$" name = match.group(1) diff --git a/test/test_Karma.py b/test/test_Karma.py index a32975a34..93e28cfb5 100644 --- a/test/test_Karma.py +++ b/test/test_Karma.py @@ -68,6 +68,27 @@ if sqlite is not None: self.assertRegexp('karma MoO', 'Karma for \'MoO\'.*increased 1.*total.*1') + def testMost(self): + self.assertError('most increased') + self.assertError('most decreased') + self.assertError('most active') + self.assertHelp('most aldsfkj') + self.assertNoResponse('foo++', 1) + self.assertNoResponse('foo++', 1) + self.assertNoResponse('bar++', 1) + self.assertNoResponse('bar--', 1) + self.assertNoResponse('bar--', 1) + self.assertRegexp('karma most active', 'bar.*foo') + self.assertRegexp('karma most increased', 'foo.*bar') + self.assertRegexp('karma most decreased', 'bar.*foo') + self.assertNoResponse('foo--', 1) + self.assertNoResponse('foo--', 1) + self.assertNoResponse('foo--', 1) + self.assertNoResponse('foo--', 1) + self.assertRegexp('karma most active', 'foo.*bar') + self.assertRegexp('karma most increased', 'foo.*bar') + self.assertRegexp('karma most decreased', 'foo.*bar') + def testSimpleOutput(self): self.assertNotError('karma config simple-output on') self.assertNoResponse('foo++', 2)