diff --git a/plugins/FunDB.py b/plugins/FunDB.py index 24c839c5a..7b5327dce 100755 --- a/plugins/FunDB.py +++ b/plugins/FunDB.py @@ -109,6 +109,10 @@ class DbiFunDBDB(object): def size(self, channel, type): 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): + """[] + + Searches the database specified for , 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): """[] diff --git a/test/test_FunDB.py b/test/test_FunDB.py index 39700b11e..9b302a642 100644 --- a/test/test_FunDB.py +++ b/test/test_FunDB.py @@ -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')