mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-08 03:59:23 +01:00
update Karma plugin and test code to use sqlite3.
This involved also updating src/conf.py to look for sqlite3 and add it to supybot.conf.databases list, since karma uses the plugins.DB() constructor for its database, which checks the available databases list.
This commit is contained in:
parent
4890e2e80d
commit
b5058cc5c2
@ -39,6 +39,11 @@ import supybot.ircmsgs as ircmsgs
|
||||
import supybot.ircutils as ircutils
|
||||
import supybot.callbacks as callbacks
|
||||
|
||||
try:
|
||||
import sqlite3
|
||||
except ImportError:
|
||||
from pysqlite2 import dbapi2 as sqlite3 # for python2.4
|
||||
|
||||
class SqliteKarmaDB(object):
|
||||
def __init__(self, filename):
|
||||
self.dbs = ircutils.IrcDict()
|
||||
@ -49,19 +54,16 @@ class SqliteKarmaDB(object):
|
||||
db.close()
|
||||
|
||||
def _getDb(self, channel):
|
||||
try:
|
||||
import sqlite
|
||||
except ImportError:
|
||||
raise callbacks.Error, 'You need to have PySQLite installed to ' \
|
||||
'use Karma. Download it at ' \
|
||||
'<http://code.google.com/p/pysqlite/>'
|
||||
filename = plugins.makeChannelFilename(self.filename, channel)
|
||||
if filename in self.dbs:
|
||||
return self.dbs[filename]
|
||||
if os.path.exists(filename):
|
||||
self.dbs[filename] = sqlite.connect(filename)
|
||||
return self.dbs[filename]
|
||||
db = sqlite.connect(filename)
|
||||
db = sqlite3.connect(filename)
|
||||
db.text_factory = str
|
||||
self.dbs[filename] = db
|
||||
return db
|
||||
db = sqlite3.connect(filename)
|
||||
db.text_factory = str
|
||||
self.dbs[filename] = db
|
||||
cursor = db.cursor()
|
||||
cursor.execute("""CREATE TABLE karma (
|
||||
@ -82,20 +84,21 @@ class SqliteKarmaDB(object):
|
||||
thing = thing.lower()
|
||||
cursor = db.cursor()
|
||||
cursor.execute("""SELECT added, subtracted FROM karma
|
||||
WHERE normalized=%s""", thing)
|
||||
if cursor.rowcount == 0:
|
||||
WHERE normalized=?""", (thing,))
|
||||
results = cursor.fetchall()
|
||||
if len(results) == 0:
|
||||
return None
|
||||
else:
|
||||
return map(int, cursor.fetchone())
|
||||
return map(int, results[0])
|
||||
|
||||
def gets(self, channel, things):
|
||||
db = self._getDb(channel)
|
||||
cursor = db.cursor()
|
||||
normalizedThings = dict(zip(map(lambda s: s.lower(), things), things))
|
||||
criteria = ' OR '.join(['normalized=%s'] * len(normalizedThings))
|
||||
criteria = ' OR '.join(['normalized=?'] * len(normalizedThings))
|
||||
sql = """SELECT name, added-subtracted FROM karma
|
||||
WHERE %s ORDER BY added-subtracted DESC""" % criteria
|
||||
cursor.execute(sql, *normalizedThings)
|
||||
cursor.execute(sql, normalizedThings.keys())
|
||||
L = [(name, int(karma)) for (name, karma) in cursor.fetchall()]
|
||||
for (name, _) in L:
|
||||
del normalizedThings[name.lower()]
|
||||
@ -107,26 +110,27 @@ class SqliteKarmaDB(object):
|
||||
db = self._getDb(channel)
|
||||
cursor = db.cursor()
|
||||
cursor.execute("""SELECT name, added-subtracted FROM karma
|
||||
ORDER BY added-subtracted DESC LIMIT %s""", limit)
|
||||
ORDER BY added-subtracted DESC LIMIT ?""", (limit,))
|
||||
return [(t[0], int(t[1])) for t in cursor.fetchall()]
|
||||
|
||||
def bottom(self, channel, limit):
|
||||
db = self._getDb(channel)
|
||||
cursor = db.cursor()
|
||||
cursor.execute("""SELECT name, added-subtracted FROM karma
|
||||
ORDER BY added-subtracted ASC LIMIT %s""", limit)
|
||||
ORDER BY added-subtracted ASC LIMIT ?""", (limit,))
|
||||
return [(t[0], int(t[1])) for t in cursor.fetchall()]
|
||||
|
||||
def rank(self, channel, thing):
|
||||
db = self._getDb(channel)
|
||||
cursor = db.cursor()
|
||||
cursor.execute("""SELECT added-subtracted FROM karma
|
||||
WHERE name=%s""", thing)
|
||||
if cursor.rowcount == 0:
|
||||
WHERE name=?""", (thing,))
|
||||
results = cursor.fetchall()
|
||||
if len(results) == 0:
|
||||
return None
|
||||
karma = int(cursor.fetchone()[0])
|
||||
karma = int(results[0][0])
|
||||
cursor.execute("""SELECT COUNT(*) FROM karma
|
||||
WHERE added-subtracted > %s""", karma)
|
||||
WHERE added-subtracted > ?""", (karma,))
|
||||
rank = int(cursor.fetchone()[0])
|
||||
return rank+1
|
||||
|
||||
@ -140,20 +144,20 @@ class SqliteKarmaDB(object):
|
||||
db = self._getDb(channel)
|
||||
cursor = db.cursor()
|
||||
normalized = name.lower()
|
||||
cursor.execute("""INSERT INTO karma VALUES (NULL, %s, %s, 0, 0)""",
|
||||
name, normalized)
|
||||
cursor.execute("""INSERT INTO karma VALUES (NULL, ?, ?, 0, 0)""",
|
||||
(name, normalized,))
|
||||
cursor.execute("""UPDATE karma SET added=added+1
|
||||
WHERE normalized=%s""", normalized)
|
||||
WHERE normalized=?""", (normalized,))
|
||||
db.commit()
|
||||
|
||||
def decrement(self, channel, name):
|
||||
db = self._getDb(channel)
|
||||
cursor = db.cursor()
|
||||
normalized = name.lower()
|
||||
cursor.execute("""INSERT INTO karma VALUES (NULL, %s, %s, 0, 0)""",
|
||||
name, normalized)
|
||||
cursor.execute("""INSERT INTO karma VALUES (NULL, ?, ?, 0, 0)""",
|
||||
(name, normalized,))
|
||||
cursor.execute("""UPDATE karma SET subtracted=subtracted+1
|
||||
WHERE normalized=%s""", normalized)
|
||||
WHERE normalized=?""", (normalized,))
|
||||
db.commit()
|
||||
|
||||
def most(self, channel, kind, limit):
|
||||
@ -177,7 +181,7 @@ class SqliteKarmaDB(object):
|
||||
cursor = db.cursor()
|
||||
normalized = name.lower()
|
||||
cursor.execute("""UPDATE karma SET subtracted=0, added=0
|
||||
WHERE normalized=%s""", normalized)
|
||||
WHERE normalized=?""", (normalized,))
|
||||
db.commit()
|
||||
|
||||
def dump(self, channel, filename):
|
||||
@ -201,13 +205,13 @@ class SqliteKarmaDB(object):
|
||||
for (name, added, subtracted) in reader:
|
||||
normalized = name.lower()
|
||||
cursor.execute("""INSERT INTO karma
|
||||
VALUES (NULL, %s, %s, %s, %s)""",
|
||||
name, normalized, added, subtracted)
|
||||
VALUES (NULL, ?, ?, ?, ?)""",
|
||||
(name, normalized, added, subtracted,))
|
||||
db.commit()
|
||||
fd.close()
|
||||
|
||||
KarmaDB = plugins.DB('Karma',
|
||||
{'sqlite': SqliteKarmaDB})
|
||||
{'sqlite3': SqliteKarmaDB})
|
||||
|
||||
class Karma(callbacks.Plugin):
|
||||
callBefore = ('Factoids', 'MoobotFactoids', 'Infobot')
|
||||
|
@ -30,12 +30,11 @@
|
||||
from supybot.test import *
|
||||
|
||||
try:
|
||||
import sqlite
|
||||
import sqlite3
|
||||
except ImportError:
|
||||
sqlite = None
|
||||
from pysqlite2 import dbapi2 as sqlite3 # for python2.4
|
||||
|
||||
if sqlite is not None:
|
||||
class KarmaTestCase(ChannelPluginTestCase):
|
||||
class KarmaTestCase(ChannelPluginTestCase):
|
||||
plugins = ('Karma',)
|
||||
def testKarma(self):
|
||||
self.assertError('karma')
|
||||
|
@ -765,6 +765,10 @@ class Databases(registry.SpaceSeparatedListOfStrings):
|
||||
v = ['anydbm', 'cdb', 'flat', 'pickle']
|
||||
if 'sqlite' in sys.modules:
|
||||
v.insert(0, 'sqlite')
|
||||
if 'sqlite3' in sys.modules:
|
||||
v.insert(0, 'sqlite3')
|
||||
if 'pysqlite2' in sys.modules: # for python 2.4
|
||||
v.insert(0, 'sqlite3')
|
||||
return v
|
||||
|
||||
def serialize(self):
|
||||
|
Loading…
Reference in New Issue
Block a user