Update to use the new-style DB abstraction.

This commit is contained in:
James Vega 2004-09-17 03:01:21 +00:00
parent d242c8ad81
commit bc8a6b94d9
2 changed files with 44 additions and 24 deletions

View File

@ -81,21 +81,38 @@ conf.registerChannelValue(conf.supybot.plugins.Karma, 'allowUnaddressedKarma',
increase/decrease karma without being addressed.""")) increase/decrease karma without being addressed."""))
class SqliteKarmaDB(object): class SqliteKarmaDB(object):
def __init__(self, filename):
self.dbs = ircutils.IrcDict()
self.filename = filename
def close(self):
for db in self.dbs.itervalues():
db.close()
def _getDb(self, channel): def _getDb(self, channel):
filename = plugins.makeChannelFilename('Karma.db', channel) try:
import sqlite
except ImportError:
raise callbacks.Error, 'You need to have PySQLite installed to ' \
'use this plugin. Download it at ' \
'<http://pysqlite.sf.net/>'
filename = plugins.makeChannelFilename(self.filename, channel)
if filename in self.dbs:
return self.dbs[filename]
if os.path.exists(filename): if os.path.exists(filename):
db = sqlite.connect(filename) self.dbs[filename] = sqlite.connect(filename)
else: return self.dbs[filename]
db = sqlite.connect(filename) db = sqlite.connect(filename)
cursor = db.cursor() self.dbs[filename] = db
cursor.execute("""CREATE TABLE karma ( cursor = db.cursor()
id INTEGER PRIMARY KEY, cursor.execute("""CREATE TABLE karma (
name TEXT, id INTEGER PRIMARY KEY,
normalized TEXT UNIQUE ON CONFLICT IGNORE, name TEXT,
added INTEGER, normalized TEXT UNIQUE ON CONFLICT IGNORE,
subtracted INTEGER added INTEGER,
)""") subtracted INTEGER
db.commit() )""")
db.commit()
def p(s1, s2): def p(s1, s2):
return int(ircutils.nickEqual(s1, s2)) return int(ircutils.nickEqual(s1, s2))
db.create_function('nickeq', 2, p) db.create_function('nickeq', 2, p)
@ -204,9 +221,8 @@ class SqliteKarmaDB(object):
WHERE normalized=%s""", normalized) WHERE normalized=%s""", normalized)
db.commit() db.commit()
KarmaDB = plugins.DB('Karma',
def KarmaDB(): {'sqlite': SqliteKarmaDB})
return SqliteKarmaDB()
class Karma(callbacks.Privmsg): class Karma(callbacks.Privmsg):
callBefore = ('Factoids', 'MoobotFactoids', 'Infobot') callBefore = ('Factoids', 'MoobotFactoids', 'Infobot')
@ -214,6 +230,9 @@ class Karma(callbacks.Privmsg):
self.db = KarmaDB() self.db = KarmaDB()
super(Karma, self).__init__() super(Karma, self).__init__()
def die(self):
self.db.close()
def _normalizeThing(self, thing): def _normalizeThing(self, thing):
assert thing assert thing
if thing[0] == '(' and thing[-1] == ')': if thing[0] == '(' and thing[-1] == ')':

View File

@ -79,8 +79,9 @@ class NewsRecord(object):
return s return s
class SqliteNewsDB(object): class SqliteNewsDB(object):
def __init__(self): def __init__(self, filename):
self.dbs = ircutils.IrcDict() self.dbs = ircutils.IrcDict()
self.filename = filename
def close(self): def close(self):
for db in self.dbs.itervalues(): for db in self.dbs.itervalues():
@ -93,7 +94,7 @@ class SqliteNewsDB(object):
raise callbacks.Error, 'You need to have PySQLite installed to ' \ raise callbacks.Error, 'You need to have PySQLite installed to ' \
'use this plugin. Download it at ' \ 'use this plugin. Download it at ' \
'<http://pysqlite.sf.net/>' '<http://pysqlite.sf.net/>'
filename = plugins.makeChannelFilename(channel, 'News.db') filename = plugins.makeChannelFilename(self.filename, channel)
if filename in self.dbs: if filename in self.dbs:
return self.dbs[filename] return self.dbs[filename]
if os.path.exists(filename): if os.path.exists(filename):
@ -170,8 +171,8 @@ class SqliteNewsDB(object):
cursor.execute("""UPDATE news SET subject=%s, item=%s WHERE id=%s""", cursor.execute("""UPDATE news SET subject=%s, item=%s WHERE id=%s""",
newSubject, newItem, id) newSubject, newItem, id)
def NewsDB(): NewsDB = plugins.DB('News',
return SqliteNewsDB() {'sqlite': SqliteNewsDB})
class News(callbacks.Privmsg): class News(callbacks.Privmsg):
def __init__(self): def __init__(self):