From ea024def787ecc475ba6153db76b0e5d8bca8747 Mon Sep 17 00:00:00 2001 From: Daniel DiPaolo Date: Thu, 16 Oct 2003 03:51:53 +0000 Subject: [PATCH] * Fixed some quoting issues in list*. Adopted the practice of repr()'ing all factoid keys before displaying them (idea stolen from searchfactoids in Factoids). Also fixed them in the tests as well as adding a test to insure that it's done right. * Added the listkeys and listvalues commands. --- plugins/MoobotFactoids.py | 67 +++++++++++++++++++++++++++++++++++++ test/test_MoobotFactoids.py | 37 +++++++++++--------- 2 files changed, 89 insertions(+), 15 deletions(-) diff --git a/plugins/MoobotFactoids.py b/plugins/MoobotFactoids.py index e90bc34e2..241e19ce1 100644 --- a/plugins/MoobotFactoids.py +++ b/plugins/MoobotFactoids.py @@ -397,6 +397,73 @@ class MoobotFactoids(callbacks.PrivmsgCommandAndRegexp): """ self._lock(irc, msg, args, False) + def listauth(self, irc, msg, args): + """ + + Lists the keys of the factoids with the given author. Note that if an + author has an integer name, you'll have to use that author's id to use + this function (so don't use integer usernames!). + """ + author = privmsgs.getArgs(args, needed=1) + try: + id = ircdb.users.getUserId(author) + except KeyError: + irc.error(msg, "No such user: %s" % author) + return + cursor = self.db.cursor() + cursor.execute("""SELECT key FROM factoids + WHERE created_by = %s + ORDER BY key""", id) + if cursor.rowcount == 0: + irc.reply(msg, "No factoids by %s found." % author) + return + keys = [repr(tup[0]) for tup in cursor.fetchall()] + s = "Author search for %s (%d found): %s" % \ + (author, len(keys), utils.commaAndify(keys)) + irc.reply(msg, s) + + _sqlTrans = string.maketrans('*?', '%_') + def listkeys(self, irc, msg, args): + """ + + Lists the keys of the factoids whose key matches the provided glob. + """ + glob = privmsgs.getArgs(args, needed=1) + cursor = self.db.cursor() + cursor.execute("""SELECT key FROM factoids + WHERE key LIKE %s + ORDER BY key""", + glob.translate(self._sqlTrans)) + if cursor.rowcount == 0: + irc.reply(msg, "No keys matching %r found." % glob) + return + keys = [repr(tup[0]) for tup in cursor.fetchall()] + s = "Key search for %r (%d found): %s" % \ + (glob, len(keys), utils.commaAndify(keys)) + irc.reply(msg, s) + + def listvalues(self, irc, msg, args): + """ + + + Lists the keys of the factoids whose value matches the provided glob. + """ + glob = privmsgs.getArgs(args, needed=1) + cursor = self.db.cursor() + cursor.execute("""SELECT key FROM factoids + WHERE fact LIKE %s + ORDER BY key""", + glob.translate(self._sqlTrans)) + if cursor.rowcount == 0: + irc.reply(msg, "No values matching %r found." % glob) + return + keys = [repr(tup[0]) for tup in cursor.fetchall()] + s = "Value search for %r (%d found): %s" % \ + (glob, len(keys), utils.commaAndify(keys)) + irc.reply(msg, s) + + + Class = MoobotFactoids # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: diff --git a/test/test_MoobotFactoids.py b/test/test_MoobotFactoids.py index e7f291a25..8edb3009a 100644 --- a/test/test_MoobotFactoids.py +++ b/test/test_MoobotFactoids.py @@ -121,33 +121,40 @@ if sqlite is not None: self.assertError('moo =~ s/moo/') def testListkeys(self): - self.assertResponse('listkeys *', 'No keys found matching \'*\'.') + self.assertResponse('listkeys *', 'No keys matching \'*\' found.') self.assertNotError('moo is moo') self.assertResponse('listkeys moo', 'Key search for \'moo\' ' - '(1 found): moo') - self.assertResponse('listkeys foo', 'No keys found matching ' - '\'foo\'.') + '(1 found): \'moo\'') + self.assertResponse('listkeys foo', 'No keys matching \'foo\' ' + 'found.') # Throw in a bunch more for i in range(10): self.assertNotError('moo%s is moo' % i) - self.assertRegexp('listkeys moo', '^Key search for \'moo\' ' - '(11 found): (moo\d*, )+ and moo9$') - self.assertRegexp('listkeys *', '^Key search for \'*\' ' - '(12 found): foo, (moo\d*, )+ and moo9$') + self.assertRegexp('listkeys moo*', '^Key search for \'moo\*\' ' + '\(11 found\): (\'moo\d*\', )+and \'moo9\'$') + self.assertNotError('foo is bar') + self.assertRegexp('listkeys *', '^Key search for \'\*\' ' + '\(12 found\): \'foo\', (\'moo\d*\', )+and ' + '\'moo9\'$') + # Check quoting + self.assertNotError('foo\' is bar') + self.assertResponse('listkeys foo*', 'Key search for \'foo*\' ' + '(2 found): \'foo\' and "foo\'"') def testListvalues(self): - self.assertNotError('moo is moo') + self.assertNotError('moo is moo') self.assertResponse('listvalues moo', 'Value search for \'moo\' ' - '(1 found): moo') + '(1 found): \'moo\'') def testListauth(self): self.assertNotError('moo is moo') self.assertResponse('listauth tester', 'Author search for tester ' - '(1 found): moo') + '(1 found): \'moo\'') + self.assertError('listauth moo') - class DunnoTestCase(PluginTestCase, PluginDocumentation): - plugins = ('MiscCommands', 'MoobotFactoids', 'UserCommands') - def testDunno(self): - self.assertNotError('apfasdfjoia') # Should say a dunno, no error +# class DunnoTestCase(PluginTestCase, PluginDocumentation): +# plugins = ('MiscCommands', 'MoobotFactoids', 'UserCommands') +# def testDunno(self): +# self.assertNotError('apfasdfjoia') # Should say a dunno, no error # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: