mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-24 19:52:54 +01:00
Added fundb.search() and tests, AND fixed a bug with reloading FunDB
This commit is contained in:
parent
cd91e37bc6
commit
3e44335f28
@ -110,6 +110,10 @@ class DbiFunDBDB(object):
|
||||
db = self._getDb(channel, type)
|
||||
return itertools.ilen(db)
|
||||
|
||||
def search(self, channel, type, p):
|
||||
db = self._getDb(channel, type)
|
||||
return db.select(p)
|
||||
|
||||
FunDBDB = plugins.DB('FunDB',
|
||||
{'flat': DbiFunDBDB})
|
||||
|
||||
@ -125,12 +129,13 @@ class FunDB(callbacks.Privmsg):
|
||||
"""
|
||||
_types = ('insult', 'lart', 'praise')
|
||||
def __init__(self):
|
||||
super(FunDB, self).__init__()
|
||||
self.__parent = super(FunDB, self)
|
||||
self.__parent.__init__()
|
||||
self.db = FunDBDB()
|
||||
|
||||
def die(self):
|
||||
self.db.close()
|
||||
super(FunDB, self).die()
|
||||
self.__parent.die()
|
||||
|
||||
def _getBy(self, by):
|
||||
try:
|
||||
@ -139,7 +144,8 @@ class FunDB(callbacks.Privmsg):
|
||||
return by
|
||||
|
||||
def _validType(self, irc, type, error=True):
|
||||
if type not in self._types:
|
||||
lowerTypes = [t.lower() for t in self._types]
|
||||
if type.lower() not in lowerTypes:
|
||||
if error:
|
||||
irc.error('%r is not a valid type. Valid types include %s.' %
|
||||
(type, utils.commaAndify(self._types)))
|
||||
@ -210,6 +216,26 @@ class FunDB(callbacks.Privmsg):
|
||||
except KeyError:
|
||||
irc.error('There is no %s with that id.' % type)
|
||||
|
||||
def search(self, irc, msg, args):
|
||||
"""[<channel>] <lart|insult|praise> <text>
|
||||
|
||||
Searches the database specified for <text>, and returns the records
|
||||
with matching ids as well as a snippet of the text of each record.
|
||||
"""
|
||||
channel = privmsgs.getChannel(msg, args)
|
||||
(type, text) = privmsgs.getArgs(args, required=2)
|
||||
if not self._validType(irc, type):
|
||||
return
|
||||
def p(record):
|
||||
return text in record.text
|
||||
results = self.db.search(channel, type, p)
|
||||
L = ['#%s: %s' % (record.id, utils.ellipsisify(record.text, 50)) \
|
||||
for record in results]
|
||||
if not L:
|
||||
irc.error('There is no %s containing that text.' % type)
|
||||
else:
|
||||
irc.reply(utils.commaAndify(L))
|
||||
|
||||
def change(self, irc, msg, args):
|
||||
"""[<channel>] <lart|insult|praise> <id> <regexp>
|
||||
|
||||
|
@ -55,6 +55,13 @@ class TestFunDB(ChannelPluginTestCase, PluginDocumentation):
|
||||
self.assertError('remove l4rt foo')
|
||||
self.assertError('remove lart foo')
|
||||
|
||||
def testSearch(self):
|
||||
# 'fundb search' because otherwise it thinks it's 'config search'
|
||||
self.assertError('fundb search l4rt foo')
|
||||
self.assertError('fundb search lart foo')
|
||||
self.assertNotError('add lart foo $who')
|
||||
self.assertNotError('fundb search lart foo')
|
||||
|
||||
def testLart(self):
|
||||
self.assertNotError('add lart jabs $who')
|
||||
self.assertHelp('lart')
|
||||
|
Loading…
Reference in New Issue
Block a user