Add dbi.NoRecordError and update Quotes to use it

This commit is contained in:
James Vega 2004-08-16 17:34:58 +00:00
parent eb1dac0d60
commit 46c71a7521
2 changed files with 9 additions and 9 deletions

View File

@ -55,9 +55,6 @@ 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__ = [
@ -161,7 +158,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 QuotesDBError, id raise dbi.NoRecordError, 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)
@ -170,7 +167,7 @@ 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 QuotesDBError, id raise dbi.NoRecordError, id
db.commit() db.commit()
def QuotesDB(): def QuotesDB():
@ -291,7 +288,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 QuotesDBError, e: except dbi.NoRecordError, 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):

View File

@ -48,6 +48,9 @@ import supybot.utils as utils
class Error(Exception): class Error(Exception):
"""General error for this module.""" """General error for this module."""
class NoRecordError(KeyError):
pass
class MappingInterface(object): class MappingInterface(object):
"""This is a class to represent the underlying representation of a map """This is a class to represent the underlying representation of a map
from integer keys to strings.""" from integer keys to strings."""
@ -56,7 +59,7 @@ class MappingInterface(object):
raise NotImplementedError raise NotImplementedError
def get(id): def get(id):
"""Gets the record matching id. Raises KeyError otherwise.""" """Gets the record matching id. Raises NoRecordError otherwise."""
raise NotImplementedError raise NotImplementedError
def set(id, s): def set(id, s):
@ -149,7 +152,7 @@ class FlatfileMapping(MappingInterface):
(lineId, s) = self._splitLine(line) (lineId, s) = self._splitLine(line)
if lineId == strId: if lineId == strId:
return s return s
raise KeyError, id raise NoRecordError, id
finally: finally:
fd.close() fd.close()