rename hook.thread to hook.singlethread, make singlethread plugins cache db connections

This commit is contained in:
Ryan Hitchman 2010-03-13 07:24:19 -07:00
parent 60be1e7165
commit 7888ce1314
6 changed files with 28 additions and 24 deletions

View File

@ -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):

View File

@ -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)

View File

@ -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()

View File

@ -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)

View File

@ -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)

View File

@ -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