From 7888ce1314beafb3d56a80a55b86080c99969a90 Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Sat, 13 Mar 2010 07:24:19 -0700 Subject: [PATCH] rename hook.thread to hook.singlethread, make singlethread plugins cache db connections --- core/main.py | 11 ++++++++++- plugins/log.py | 3 +-- plugins/seen.py | 19 +++++++++---------- plugins/tell.py | 7 +++---- plugins/urlhistory.py | 8 +++----- plugins/util/hook.py | 4 ++-- 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/core/main.py b/core/main.py index ce60bb1..ba73985 100644 --- a/core/main.py +++ b/core/main.py @@ -42,7 +42,7 @@ def run(func, input): input.inp = input.paraml if args: - if 'db' in args: + if 'db' in args and 'db' not in input: input.db = get_db_connection(input.conn) if 'input' in args: input.input = input @@ -74,12 +74,21 @@ class Handler(object): thread.start_new_thread(self.start, ()) def start(self): + uses_db = 'db' in self.func._args + db_conns = {} while True: input = self.input_queue.get() if input == StopIteration: break + if uses_db: + db = db_conns.get(input.conn) + if db is None: + db = bot.get_db_connection(input.conn) + db_conns[input.conn] = db + input.db = db + run(self.func, input) def stop(self): diff --git a/plugins/log.py b/plugins/log.py index cc6b62e..cbdf33b 100644 --- a/plugins/log.py +++ b/plugins/log.py @@ -3,7 +3,6 @@ log.py: written by Scaevolus 2009 """ import os -import thread import codecs import time import re @@ -82,7 +81,7 @@ def get_log_fd(dir, server, chan): return fd -@hook.thread +@hook.singlethread @hook.event('*') def log(paraml, input=None, bot=None): timestamp = gmtime(timestamp_format) diff --git a/plugins/seen.py b/plugins/seen.py index 3b27d26..8cb0328 100644 --- a/plugins/seen.py +++ b/plugins/seen.py @@ -5,10 +5,16 @@ import time from util import hook, timesince -@hook.thread +def db_init(db): + "check to see that our db has the the seen table and return a connection." + db.execute("create table if not exists seen(name, time, quote, chan, " + "primary key(name, chan))") + db.commit() + + +@hook.singlethread @hook.event('PRIVMSG') -def seeninput(paraml, input=None, bot=None): - db = bot.get_db_connection(input.conn) +def seeninput(paraml, input=None, db=None, bot=None): db_init(db) db.execute("insert or replace into seen(name, time, quote, chan)" "values(?,?,?,?)", (input.nick.lower(), time.time(), input.msg, @@ -39,10 +45,3 @@ def seen(inp, nick='', chan='', db=None): (inp, reltime, last_seen[2]) else: return "I've never seen %s" % inp - - -def db_init(db): - "check to see that our db has the the seen table and return a connection." - db.execute("create table if not exists seen(name, time, quote, chan, " - "primary key(name, chan))") - db.commit() diff --git a/plugins/tell.py b/plugins/tell.py index 350347b..8f6404c 100644 --- a/plugins/tell.py +++ b/plugins/tell.py @@ -12,14 +12,13 @@ def get_tells(db, user_to, chan): (user_to.lower(), chan)).fetchall() -@hook.thread +@hook.singlethread @hook.event('PRIVMSG') -def tellinput(paraml, input=None, bot=None): +def tellinput(paraml, input=None, db=None, bot=None): if 'showtells' in input.msg.lower(): return - db = bot.get_db_connection(input.conn) - db = db_init(db) + db_init(db) tells = get_tells(db, input.nick, input.chan) diff --git a/plugins/urlhistory.py b/plugins/urlhistory.py index 6d6ee6c..396bdb2 100644 --- a/plugins/urlhistory.py +++ b/plugins/urlhistory.py @@ -11,12 +11,10 @@ expiration_period = 60 * 60 * 24 # 1 day ignored_urls = [urlnorm.normalize("http://google.com")] -def db_connect(bot, conn): - db = bot.get_db_connection(conn) +def db_init(db): db.execute("create table if not exists urlhistory" "(chan, url, nick, time)") db.commit() - return db def insert_history(db, chan, url, nick): @@ -67,8 +65,8 @@ def format_reply(history): @hook.regex(url_re) -def urlinput(match, nick='', chan='', conn=None, bot=None): - db = db_connect(bot, conn) +def urlinput(match, nick='', chan='', db=None, bot=None): + db_init(db) url = urlnorm.normalize(match.group().encode('utf-8')) if url not in ignored_urls: history = get_history(db, chan, url) diff --git a/plugins/util/hook.py b/plugins/util/hook.py index 4baeb1f..da62689 100644 --- a/plugins/util/hook.py +++ b/plugins/util/hook.py @@ -34,7 +34,7 @@ def _hook_add(func, add, name=''): args.append(0) # means kwargs present func._args = args - if not hasattr(func, '_skybot_thread'): # does function run in its own thread? + if not hasattr(func, '_thread'): # does function run in its own thread? func._thread = False def sieve(func): @@ -79,7 +79,7 @@ def event(arg=None, **kwargs): return event_wrapper -def thread(func): +def singlethread(func): func._thread = True return func