From 240612fecf49db4e4aa563962fc3dca96eec2f90 Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Sun, 24 Oct 2010 09:46:41 -0500 Subject: [PATCH 1/6] database connection grabbing for Modules --- Module.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Module.py b/Module.py index aaec8f5..38e89cf 100644 --- a/Module.py +++ b/Module.py @@ -20,6 +20,7 @@ from ConfigParser import NoSectionError, NoOptionError import inspect import re import sys +import sqlite3 from extlib import irclib @@ -281,6 +282,20 @@ class Module(object): self.server._handle_event(event) + def get_db(self): + """ + Get a database connection to sqlite3. Once grabbed, it should be closed + when work is done. Modules that need a database connection should + test for and create (or, eventually, alter) required table structure + in their __init__ IF that structure does not already exist. Well-behaved + modules should use a prefix in their table names (eg "karma_log" rather + than "log") + """ + + dbfile = self.config.get('dr.botzo', 'database') + conn = sqlite3.connect(dbfile) + return conn + def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked): """ Do the primary thing this module was intended to do. From 48427ecd21cc4fbd538bc1736815f99434e2f99f Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Sun, 24 Oct 2010 11:46:49 -0500 Subject: [PATCH 2/6] create the database if it doesn't exist, on startup --- dr.botzo.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/dr.botzo.py b/dr.botzo.py index b436cab..f153b3c 100644 --- a/dr.botzo.py +++ b/dr.botzo.py @@ -22,6 +22,7 @@ import re import socket import sys import inspect +import sqlite3 from extlib import irclib @@ -103,6 +104,32 @@ except NoOptionError as e: # load additional options irclib.DEBUG = config.getboolean('dr.botzo', 'debug') +try: + # make sure we can initialize the database, if such a thing is + # called for in the config file + dbfile = config.get('dr.botzo', 'database') + conn = sqlite3.connect(dbfile) + try: + query = """ + SELECT COUNT(*) + FROM sqlite_master + WHERE type = 'table' AND name = 'drbotzo_modules' + """ + row = conn.execute(query).fetchone() + if row[0] == 0: + # need to create the drbotzo_modules table + query = """ + CREATE TABLE drbotzo_modules ( + module TEXT, + version INTEGER + ) + """ + conn.execute(query) + conn.commit() + finally: conn.close() +except NoOptionError: pass # if the config file has no db property, assume that +except NoSectionError: pass # the database doesn't need to exist + # start up the IRC bot # create IRC and server objects and connect From 56d2847285e6e21909852101ba1b2835318a67be Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Sun, 24 Oct 2010 11:49:05 -0500 Subject: [PATCH 3/6] ignore database files --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c78c5d6..fde6af8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ -karma* *.facts *.pyc *.swp *.urls *~ +dr.botzo.data dr.botzo.cfg nbproject From 6a67795b187e24e7957873ab5b3b4a811e44136c Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Sun, 24 Oct 2010 11:50:12 -0500 Subject: [PATCH 4/6] modules know how to ask for a version number from the database --- Module.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Module.py b/Module.py index 38e89cf..61ba095 100644 --- a/Module.py +++ b/Module.py @@ -17,6 +17,7 @@ along with this program. If not, see . """ from ConfigParser import NoSectionError, NoOptionError + import inspect import re import sys @@ -290,12 +291,30 @@ class Module(object): in their __init__ IF that structure does not already exist. Well-behaved modules should use a prefix in their table names (eg "karma_log" rather than "log") + + See also db_module_registered, below. """ dbfile = self.config.get('dr.botzo', 'database') conn = sqlite3.connect(dbfile) return conn + def db_module_registered(self, modulename): + """ + ask the database for a version number for a module. Return that version + number if the module has registered, or None if not + """ + conn = self.get_db() + version = None + try: + cur = conn.cursor() + cur.execute("SELECT version FROM drbotzo_modules WHERE module = ?", modulename) + version = cur.fetchone() + if (version != None): + version = version[0] + finally: conn.close() + return version + def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked): """ Do the primary thing this module was intended to do. From 98f07105bca003f41b3d75ed271aa420492d6cc3 Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Sun, 24 Oct 2010 13:05:16 -0500 Subject: [PATCH 5/6] karma data store moved to sqlite --- Module.py | 3 +- modules/Karma.py | 89 ++++++++++++++++++++++++++---------------------- 2 files changed, 50 insertions(+), 42 deletions(-) diff --git a/Module.py b/Module.py index 61ba095..90c701c 100644 --- a/Module.py +++ b/Module.py @@ -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] diff --git a/modules/Karma.py b/modules/Karma.py index 24472be..940f991 100755 --- a/modules/Karma.py +++ b/modules/Karma.py @@ -15,7 +15,6 @@ # along with this program. If not, see . 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) From 84ba3107f0115760f5bc30ae88c89f4580998277 Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Sun, 24 Oct 2010 14:48:26 -0500 Subject: [PATCH 6/6] include rank in karma output --- dr.botzo.cfg.example | 4 ++-- modules/Karma.py | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/dr.botzo.cfg.example b/dr.botzo.cfg.example index 7bf599d..efa4417 100644 --- a/dr.botzo.cfg.example +++ b/dr.botzo.cfg.example @@ -7,10 +7,10 @@ usermode = -x debug = true admin_userhost = bss@ayu.incorporeal.org module_list = IrcAdmin +database = dr.botzo.data [IrcAdmin] autojoin = #bss [Karma] -meta.pubmsg_needs_bot_prefix = false -karmafile = karma \ No newline at end of file +meta.pubmsg_needs_bot_prefix = false \ No newline at end of file diff --git a/modules/Karma.py b/modules/Karma.py index 940f991..6c1d4ea 100755 --- a/modules/Karma.py +++ b/modules/Karma.py @@ -96,9 +96,6 @@ class Karma(Module): conn.commit() finally: conn.close() - reply = "karma change for '" + key + "' (" + str(value) + ") by " + nick - self.reply(connection, replypath, reply) - def handle_karma_query(self, connection, nick, userhost, replypath, what): match = self.queryre.match(what) key = match.group(1) @@ -112,8 +109,16 @@ class Karma(Module): 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) + query = ''' + SELECT count(*) FROM karma_values WHERE value > :value + ''' + rank = conn.execute(query, {'value': value[0]}).fetchone() + rank = rank[0] + 1; + + reply = '{nick}: {key} has {value[0]!s} points of karma (rank {rank})'.format( + nick=nick, key=key, value=value, rank=rank) finally: conn.close()