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 ircutils
import privmsgs import privmsgs
import callbacks import callbacks
import configurable
try: try:
import sqlite import sqlite
@ -57,10 +58,28 @@ except ImportError:
raise callbacks.Error, 'You need to have PySQLite installed to use this ' \ raise callbacks.Error, 'You need to have PySQLite installed to use this ' \
'plugin. Download it at <http://pysqlite.sf.net/>' '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): def __init__(self):
plugins.ChannelDBHandler.__init__(self)
callbacks.Privmsg.__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): def makeDb(self, filename):
if os.path.exists(filename): if os.path.exists(filename):
@ -96,7 +115,8 @@ class Factoids(plugins.ChannelDBHandler, callbacks.Privmsg):
""" """
channel = privmsgs.getChannel(msg, args) channel = privmsgs.getChannel(msg, args)
try: try:
i = args.index('as') separator = self.configurables.get('learn-separator', channel)
i = args.index(separator)
except ValueError: except ValueError:
raise callbacks.ArgumentError raise callbacks.ArgumentError
args.pop(i) args.pop(i)
@ -396,7 +416,8 @@ class Factoids(plugins.ChannelDBHandler, callbacks.Privmsg):
cursor.execute(sql, formats) cursor.execute(sql, formats)
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.reply(msg, 'No keys matched that query.') 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]]) self.whatis(irc, msg, [cursor.fetchone()[0]])
elif cursor.rowcount > 100: elif cursor.rowcount > 100:
irc.reply(msg, 'More than 100 keys matched that query; ' 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.assertNotError('learn foo as bar')
self.assertNotRegexp('info foo', '2 factoids') 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: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: