mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-12 05:02:32 +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)
|
db = self._getDb(channel, type)
|
||||||
return itertools.ilen(db)
|
return itertools.ilen(db)
|
||||||
|
|
||||||
|
def search(self, channel, type, p):
|
||||||
|
db = self._getDb(channel, type)
|
||||||
|
return db.select(p)
|
||||||
|
|
||||||
FunDBDB = plugins.DB('FunDB',
|
FunDBDB = plugins.DB('FunDB',
|
||||||
{'flat': DbiFunDBDB})
|
{'flat': DbiFunDBDB})
|
||||||
|
|
||||||
@ -125,12 +129,13 @@ class FunDB(callbacks.Privmsg):
|
|||||||
"""
|
"""
|
||||||
_types = ('insult', 'lart', 'praise')
|
_types = ('insult', 'lart', 'praise')
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(FunDB, self).__init__()
|
self.__parent = super(FunDB, self)
|
||||||
|
self.__parent.__init__()
|
||||||
self.db = FunDBDB()
|
self.db = FunDBDB()
|
||||||
|
|
||||||
def die(self):
|
def die(self):
|
||||||
self.db.close()
|
self.db.close()
|
||||||
super(FunDB, self).die()
|
self.__parent.die()
|
||||||
|
|
||||||
def _getBy(self, by):
|
def _getBy(self, by):
|
||||||
try:
|
try:
|
||||||
@ -139,7 +144,8 @@ class FunDB(callbacks.Privmsg):
|
|||||||
return by
|
return by
|
||||||
|
|
||||||
def _validType(self, irc, type, error=True):
|
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:
|
if error:
|
||||||
irc.error('%r is not a valid type. Valid types include %s.' %
|
irc.error('%r is not a valid type. Valid types include %s.' %
|
||||||
(type, utils.commaAndify(self._types)))
|
(type, utils.commaAndify(self._types)))
|
||||||
@ -210,6 +216,26 @@ class FunDB(callbacks.Privmsg):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
irc.error('There is no %s with that id.' % type)
|
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):
|
def change(self, irc, msg, args):
|
||||||
"""[<channel>] <lart|insult|praise> <id> <regexp>
|
"""[<channel>] <lart|insult|praise> <id> <regexp>
|
||||||
|
|
||||||
|
@ -55,6 +55,13 @@ class TestFunDB(ChannelPluginTestCase, PluginDocumentation):
|
|||||||
self.assertError('remove l4rt foo')
|
self.assertError('remove l4rt foo')
|
||||||
self.assertError('remove lart 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):
|
def testLart(self):
|
||||||
self.assertNotError('add lart jabs $who')
|
self.assertNotError('add lart jabs $who')
|
||||||
self.assertHelp('lart')
|
self.assertHelp('lart')
|
||||||
|
Loading…
Reference in New Issue
Block a user