From f1517a7accbb0d24b80bb8dc3fab923d7a5cc4c7 Mon Sep 17 00:00:00 2001 From: Daniel Folkinshteyn Date: Sun, 25 Apr 2010 02:58:43 -0400 Subject: [PATCH] some enhancements Factoids.rank: output options: plain key output, and alpha sorting for plain output. allow an optional argument for how many ranked facts to show. --- plugins/Factoids/plugin.py | 41 +++++++++++++++++++++++++++++--------- plugins/Factoids/test.py | 5 +++++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/plugins/Factoids/plugin.py b/plugins/Factoids/plugin.py index f5193a003..02dd18f27 100644 --- a/plugins/Factoids/plugin.py +++ b/plugins/Factoids/plugin.py @@ -380,26 +380,49 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler): alias = wrap(alias, ['channel', 'something', 'something', optional('int')]) - def rank(self, irc, msg, args, channel): - """[] + def rank(self, irc, msg, args, channel, optlist, number): + """[] [--plain] [--alpha] [] Returns a list of top-ranked factoid keys, sorted by usage count - (rank). The number of factoid keys returned is set by the - rankListLength registry value. is only necessary if the - message isn't sent in the channel itself. + (rank). If is not provided, the default number of factoid keys + returned is set by the rankListLength registry value. + + If --plain option is given, rank numbers and usage counts are not + included in output. + + If --alpha option is given in addition to --plain, keys are sorted + alphabetically, instead of by rank. + + is only necessary if the message isn't sent in the channel + itself. """ - numfacts = self.registryValue('rankListLength', channel) + if not number: + number = self.registryValue('rankListLength', channel) db = self.getDb(channel) cursor = db.cursor() cursor.execute("""SELECT keys.key, relations.usage_count FROM keys, relations WHERE relations.key_id=keys.id ORDER BY relations.usage_count DESC - LIMIT ?""", (numfacts,)) + LIMIT ?""", (number,)) factkeys = cursor.fetchall() - s = [ "#%d %s (%d)" % (i+1, key[0], key[1]) for i, key in enumerate(factkeys) ] + plain=False + alpha=False + for (option, arg) in optlist: + if option == 'plain': + plain = True + elif option =='alpha': + alpha = True + if plain: + s = [ "%s" % (key[0],) for i, key in enumerate(factkeys) ] + if alpha: + s.sort() + else: + s = [ "#%d %s (%d)" % (i+1, key[0], key[1]) for i, key in enumerate(factkeys) ] irc.reply(", ".join(s)) - rank = wrap(rank, ['channel']) + rank = wrap(rank, ['channel', + getopts({'plain': '', 'alpha': '',}), + optional('int')]) def lock(self, irc, msg, args, channel, key): """[] diff --git a/plugins/Factoids/test.py b/plugins/Factoids/test.py index 22595c329..46782c239 100644 --- a/plugins/Factoids/test.py +++ b/plugins/Factoids/test.py @@ -189,6 +189,11 @@ class FactoidsTestCase(ChannelPluginTestCase): self.assertRegexp('factoids rank', '#1 foo \(0\), #2 moo \(0\)') self.assertRegexp('whatis moo', '.*cow.*') self.assertRegexp('factoids rank', '#1 moo \(1\), #2 foo \(0\)') + self.assertRegexp('factoids rank 1', '#1 moo \(1\)') + self.assertNotRegexp('factoids rank 1', 'foo') + self.assertRegexp('factoids rank --plain', 'moo, foo') + self.assertRegexp('factoids rank --plain --alpha', 'foo, moo') + self.assertResponse('factoids rank --plain 1', 'moo') def testQuoteHandling(self): self.assertNotError('learn foo as "\\"bar\\""')