karma data store moved to sqlite
This commit is contained in:
parent
6a67795b18
commit
98f07105bc
@ -308,7 +308,8 @@ class Module(object):
|
||||
version = None
|
||||
try:
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT version FROM drbotzo_modules WHERE module = ?", modulename)
|
||||
cur.execute("SELECT version FROM drbotzo_modules WHERE module = :name",
|
||||
{'name': modulename})
|
||||
version = cur.fetchone()
|
||||
if (version != None):
|
||||
version = version[0]
|
||||
|
@ -15,7 +15,6 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import re
|
||||
import shelve
|
||||
|
||||
from Module import Module
|
||||
|
||||
@ -31,18 +30,41 @@ class Karma(Module):
|
||||
|
||||
Module.__init__(self, config, server, modlist)
|
||||
|
||||
filename = self.config.get(self.__class__.__name__, 'karmafile')
|
||||
self.karmafile = filename + "_karma.dat"
|
||||
self.trendfile = filename + "_trends.dat"
|
||||
|
||||
pattern = "([a-zA-Z0-9_]+)"
|
||||
|
||||
karmapattern = '^' + pattern + '(\+\+|--)'
|
||||
querypattern = '^!rank\s+' + pattern
|
||||
|
||||
self.karmare = re.compile(karmapattern)
|
||||
self.queryre = re.compile(querypattern)
|
||||
|
||||
# need to init the database if karma tables don't already exist
|
||||
version = self.db_module_registered(self.__class__.__name__)
|
||||
if (version == None):
|
||||
# have to create the database tables
|
||||
conn = self.get_db()
|
||||
try:
|
||||
conn.execute('''
|
||||
CREATE TABLE karma_log (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
key TEXT NOT NULL,
|
||||
delta INTEGER NOT NULL,
|
||||
who TEXT NOT NULL,
|
||||
userhost TEXT NOT NULL,
|
||||
karmatime TEXT DEFAULT CURRENT_TIMESTAMP
|
||||
)''')
|
||||
conn.execute('CREATE INDEX karma_log_key_ix ON karma_log (key)')
|
||||
conn.execute('CREATE INDEX karma_log_who_ix ON karma_log (who)')
|
||||
conn.execute('''
|
||||
CREATE VIEW karma_values AS
|
||||
SELECT key, SUM(delta) AS value
|
||||
FROM karma_log
|
||||
GROUP BY key''')
|
||||
|
||||
sql = 'INSERT INTO drbotzo_modules VALUES (?,?)'
|
||||
conn.execute(sql, (self.__class__.__name__, 1))
|
||||
conn.commit()
|
||||
finally: conn.close()
|
||||
|
||||
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
||||
"""look for karma strings at the start of messages"""
|
||||
|
||||
@ -64,35 +86,15 @@ class Karma(Module):
|
||||
else:
|
||||
value = -1;
|
||||
|
||||
# do karma recording
|
||||
karma = shelve.open(self.karmafile)
|
||||
conn = self.get_db()
|
||||
try:
|
||||
oldvalue = 0;
|
||||
if karma.has_key(key):
|
||||
oldvalue = karma[key]
|
||||
newvalue = oldvalue + value;
|
||||
karma[key] = newvalue;
|
||||
finally:
|
||||
karma.close()
|
||||
|
||||
trend = shelve.open(self.trendfile)
|
||||
try:
|
||||
nickpos = nick + "_pos"
|
||||
nickneg = nick + "_neg"
|
||||
trend_pos = 0;
|
||||
trend_neg = 0;
|
||||
if trend.has_key(nickpos):
|
||||
trend_pos = trend[nickpos]
|
||||
if trend.has_key(nickneg):
|
||||
trend_neg = trend[nickneg]
|
||||
if value > 0:
|
||||
trend_pos = trend_pos + 1
|
||||
else:
|
||||
trend_neg = trend_neg + 1
|
||||
trend[nickpos] = trend_pos;
|
||||
trend[nickneg] = trend_neg;
|
||||
finally:
|
||||
trend.close();
|
||||
sql = '''
|
||||
INSERT INTO karma_log (key, delta, who, userhost)
|
||||
VALUES (?, ?, ?, ?)
|
||||
'''
|
||||
conn.execute(sql, (key, value, nick, userhost))
|
||||
conn.commit()
|
||||
finally: conn.close()
|
||||
|
||||
reply = "karma change for '" + key + "' (" + str(value) + ") by " + nick
|
||||
self.reply(connection, replypath, reply)
|
||||
@ -101,14 +103,19 @@ class Karma(Module):
|
||||
match = self.queryre.match(what)
|
||||
key = match.group(1)
|
||||
|
||||
karma = shelve.open(self.karmafile, "r")
|
||||
reply = key + ' has no karma'
|
||||
conn = self.get_db()
|
||||
reply = '{nick}: {key} has no karma'.format(nick=nick, key=key)
|
||||
try:
|
||||
if karma.has_key(key):
|
||||
value = karma[key]
|
||||
reply = key + ' has ' + str(value) + ' points of karma'
|
||||
finally:
|
||||
karma.close()
|
||||
query = '''
|
||||
SELECT value
|
||||
FROM karma_values
|
||||
WHERE key = :key
|
||||
'''
|
||||
value = conn.execute(query, {'key': key}).fetchone()
|
||||
if (value != None):
|
||||
reply = '{nick}: {key} has {value[0]!s} points of karma'.format(nick=nick, key=key, value=value)
|
||||
|
||||
finally: conn.close()
|
||||
|
||||
self.reply(connection, replypath, reply)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user