Fix broken Quotes.info and improve Quotes.search by passing the predicate

to QuotesDB.search instead of the regexp.
This commit is contained in:
James Vega 2004-08-16 16:36:18 +00:00
parent 7b2cbd950f
commit 57c2f263f9

View File

@ -52,9 +52,12 @@ import supybot.callbacks as callbacks
try: try:
import sqlite import sqlite
except ImportError: 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 QuotesError(Exception):
pass
class QuoteRecord(object): class QuoteRecord(object):
__metaclass__ = dbi.Record __metaclass__ = dbi.Record
__fields__ = [ __fields__ = [
@ -65,7 +68,7 @@ class QuoteRecord(object):
def __str__(self): def __str__(self):
format = conf.supybot.humanTimestampFormat() format = conf.supybot.humanTimestampFormat()
return 'Quote %r added by %s at %s.' % \ return 'Quote %r added by %s at %s.' % \
(self.text, ircdb.users.getUser(self.by).name, (self.text, self.by,
time.strftime(format, time.localtime(float(self.at)))) time.strftime(format, time.localtime(float(self.at))))
class SqliteQuotesDB(object): class SqliteQuotesDB(object):
@ -125,9 +128,7 @@ class SqliteQuotesDB(object):
for v in kwargs['by']: for v in kwargs['by']:
criteria.append('added_by=%s') criteria.append('added_by=%s')
formats.append(arg) formats.append(arg)
for v in kwargs['predicate']: for p in kwargs['predicate']:
def p(s):
return int(bool(v.search(s)))
predicateName += 'p' predicateName += 'p'
db.create_function(predicateName, 1, p) db.create_function(predicateName, 1, p)
criteria.append('%s(quote)' % predicateName) criteria.append('%s(quote)' % predicateName)
@ -160,7 +161,7 @@ class SqliteQuotesDB(object):
cursor.execute("""SELECT added_by, added_at, quote FROM quotes cursor.execute("""SELECT added_by, added_at, quote FROM quotes
WHERE id=%s""", id) WHERE id=%s""", id)
if cursor.rowcount == 0: if cursor.rowcount == 0:
raise KeyError, id raise QuotesDBError, id
(by, at, text) = cursor.fetchone() (by, at, text) = cursor.fetchone()
return QuoteRecord(id, by=by, at=int(at), text=text) return QuoteRecord(id, by=by, at=int(at), text=text)
@ -169,12 +170,12 @@ class SqliteQuotesDB(object):
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""DELETE FROM quotes WHERE id=%s""", id) cursor.execute("""DELETE FROM quotes WHERE id=%s""", id)
if cursor.rowcount == 0: if cursor.rowcount == 0:
raise KeyError, id raise QuotesDBError, id
db.commit() db.commit()
def QuotesDB(): def QuotesDB():
return SqliteQuotesDB() return SqliteQuotesDB()
class Quotes(callbacks.Privmsg): class Quotes(callbacks.Privmsg):
def __init__(self): def __init__(self):
self.db = QuotesDB() self.db = QuotesDB()
@ -250,7 +251,9 @@ class Quotes(callbacks.Privmsg):
except re.error, e: except re.error, e:
irc.error(str(e)) irc.error(str(e))
return return
kwargs['predicate'].append(r) def p(s):
return int(bool(r.search(s)))
kwargs['predicate'].append(p)
quote = self.db.search(channel, **kwargs) quote = self.db.search(channel, **kwargs)
if quote is None: if quote is None:
irc.reply('No quotes matched that criteria.') irc.reply('No quotes matched that criteria.')
@ -288,7 +291,7 @@ class Quotes(callbacks.Privmsg):
try: try:
quote = self.db.get(channel, id) quote = self.db.get(channel, id)
irc.reply(str(quote)) irc.reply(str(quote))
except KeyError: except QuotesDBError, e:
irc.error('There isn\'t a quote with that id.') irc.error('There isn\'t a quote with that id.')
def remove(self, irc, msg, args): def remove(self, irc, msg, args):