Added learn-separator and show-factoid-if-only-one-match configurables.

This commit is contained in:
Jeremy Fincher 2004-01-01 19:07:38 +00:00
parent c331bcfe00
commit 8070401018
2 changed files with 41 additions and 4 deletions

View File

@ -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 <http://pysqlite.sf.net/>'
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 <key> as <value>. 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; '

View File

@ -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: