From 8070401018a6517e7c2f672e2df5f77ee9bd9a85 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Thu, 1 Jan 2004 19:07:38 +0000 Subject: [PATCH] Added learn-separator and show-factoid-if-only-one-match configurables. --- plugins/Factoids.py | 29 +++++++++++++++++++++++++---- test/test_Factoids.py | 16 ++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/plugins/Factoids.py b/plugins/Factoids.py index a474c178c..d96b5aab3 100644 --- a/plugins/Factoids.py +++ b/plugins/Factoids.py @@ -50,6 +50,7 @@ import ircdb import ircutils import privmsgs import callbacks +import configurable try: import sqlite @@ -57,10 +58,28 @@ except ImportError: raise callbacks.Error, 'You need to have PySQLite installed to use this ' \ 'plugin. Download it at ' -class Factoids(plugins.ChannelDBHandler, callbacks.Privmsg): +class Factoids(plugins.ChannelDBHandler, + callbacks.Privmsg, + configurable.Mixin): + configurables = configurable.Dictionary( + [('learn-separator', configurable.NoSpacesStrType, 'as', + """Determines what separator must be used in the learn command. + Defaults to 'as' -- learn as . Users might feel more + comfortable with 'is' or something else, so it's configurable."""), + ('show-factoid-if-only-one-match', configurable.BoolType, True, + """Determines whether the bot will reply with the single matching + factoid if only one factoid matches when using the search command. + """),] + ) def __init__(self): - plugins.ChannelDBHandler.__init__(self) callbacks.Privmsg.__init__(self) + configurable.Mixin.__init__(self) + plugins.ChannelDBHandler.__init__(self) + + def die(self): + callbacks.Privmsg.die(self) + configurable.Mixin.die(self) + plugins.ChannelDBHandler.die(self) def makeDb(self, filename): if os.path.exists(filename): @@ -96,7 +115,8 @@ class Factoids(plugins.ChannelDBHandler, callbacks.Privmsg): """ channel = privmsgs.getChannel(msg, args) try: - i = args.index('as') + separator = self.configurables.get('learn-separator', channel) + i = args.index(separator) except ValueError: raise callbacks.ArgumentError args.pop(i) @@ -396,7 +416,8 @@ class Factoids(plugins.ChannelDBHandler, callbacks.Privmsg): cursor.execute(sql, formats) if cursor.rowcount == 0: irc.reply(msg, 'No keys matched that query.') - elif cursor.rowcount == 1: + elif cursor.rowcount == 1 and \ + self.configurables.get('show-factoid-if-only-one-match',channel): self.whatis(irc, msg, [cursor.fetchone()[0]]) elif cursor.rowcount > 100: irc.reply(msg, 'More than 100 keys matched that query; ' diff --git a/test/test_Factoids.py b/test/test_Factoids.py index 1f1d6cfe0..dbd3e7465 100644 --- a/test/test_Factoids.py +++ b/test/test_Factoids.py @@ -121,6 +121,22 @@ if sqlite is not None: self.assertNotError('learn foo as bar') self.assertNotRegexp('info foo', '2 factoids') + def testConfigurable(self): + self.assertError('learn foo is bar') + self.assertNotError('learn foo as bar') + self.assertRegexp('whatis foo', 'bar') + self.assertNotError('factoids config learn-separator is') + self.assertError('learn bar as baz') + self.assertNotError('learn bar is baz') + self.assertRegexp('whatis bar', 'baz') + + # show-factoid-if-only-one-match + m1 = self.assertNotError('factoids search m/foo|bar/') + q = 'factoids config show-factoid-if-only-one-match Off' + self.assertNotError(q) + m2 = self.assertNotError('factoids search m/foo/') + self.failUnless(m1.args[1].startswith(m2.args[1])) + # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: