rename hook.thread to hook.singlethread, make singlethread plugins cache db connections
This commit is contained in:
parent
60be1e7165
commit
7888ce1314
11
core/main.py
11
core/main.py
|
@ -42,7 +42,7 @@ def run(func, input):
|
||||||
input.inp = input.paraml
|
input.inp = input.paraml
|
||||||
|
|
||||||
if args:
|
if args:
|
||||||
if 'db' in args:
|
if 'db' in args and 'db' not in input:
|
||||||
input.db = get_db_connection(input.conn)
|
input.db = get_db_connection(input.conn)
|
||||||
if 'input' in args:
|
if 'input' in args:
|
||||||
input.input = input
|
input.input = input
|
||||||
|
@ -74,12 +74,21 @@ class Handler(object):
|
||||||
thread.start_new_thread(self.start, ())
|
thread.start_new_thread(self.start, ())
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
|
uses_db = 'db' in self.func._args
|
||||||
|
db_conns = {}
|
||||||
while True:
|
while True:
|
||||||
input = self.input_queue.get()
|
input = self.input_queue.get()
|
||||||
|
|
||||||
if input == StopIteration:
|
if input == StopIteration:
|
||||||
break
|
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)
|
run(self.func, input)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
|
|
|
@ -3,7 +3,6 @@ log.py: written by Scaevolus 2009
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import thread
|
|
||||||
import codecs
|
import codecs
|
||||||
import time
|
import time
|
||||||
import re
|
import re
|
||||||
|
@ -82,7 +81,7 @@ def get_log_fd(dir, server, chan):
|
||||||
return fd
|
return fd
|
||||||
|
|
||||||
|
|
||||||
@hook.thread
|
@hook.singlethread
|
||||||
@hook.event('*')
|
@hook.event('*')
|
||||||
def log(paraml, input=None, bot=None):
|
def log(paraml, input=None, bot=None):
|
||||||
timestamp = gmtime(timestamp_format)
|
timestamp = gmtime(timestamp_format)
|
||||||
|
|
|
@ -5,10 +5,16 @@ import time
|
||||||
from util import hook, timesince
|
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')
|
@hook.event('PRIVMSG')
|
||||||
def seeninput(paraml, input=None, bot=None):
|
def seeninput(paraml, input=None, db=None, bot=None):
|
||||||
db = bot.get_db_connection(input.conn)
|
|
||||||
db_init(db)
|
db_init(db)
|
||||||
db.execute("insert or replace into seen(name, time, quote, chan)"
|
db.execute("insert or replace into seen(name, time, quote, chan)"
|
||||||
"values(?,?,?,?)", (input.nick.lower(), time.time(), input.msg,
|
"values(?,?,?,?)", (input.nick.lower(), time.time(), input.msg,
|
||||||
|
@ -39,10 +45,3 @@ def seen(inp, nick='', chan='', db=None):
|
||||||
(inp, reltime, last_seen[2])
|
(inp, reltime, last_seen[2])
|
||||||
else:
|
else:
|
||||||
return "I've never seen %s" % inp
|
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()
|
|
||||||
|
|
|
@ -12,14 +12,13 @@ def get_tells(db, user_to, chan):
|
||||||
(user_to.lower(), chan)).fetchall()
|
(user_to.lower(), chan)).fetchall()
|
||||||
|
|
||||||
|
|
||||||
@hook.thread
|
@hook.singlethread
|
||||||
@hook.event('PRIVMSG')
|
@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():
|
if 'showtells' in input.msg.lower():
|
||||||
return
|
return
|
||||||
|
|
||||||
db = bot.get_db_connection(input.conn)
|
db_init(db)
|
||||||
db = db_init(db)
|
|
||||||
|
|
||||||
tells = get_tells(db, input.nick, input.chan)
|
tells = get_tells(db, input.nick, input.chan)
|
||||||
|
|
||||||
|
|
|
@ -11,12 +11,10 @@ expiration_period = 60 * 60 * 24 # 1 day
|
||||||
ignored_urls = [urlnorm.normalize("http://google.com")]
|
ignored_urls = [urlnorm.normalize("http://google.com")]
|
||||||
|
|
||||||
|
|
||||||
def db_connect(bot, conn):
|
def db_init(db):
|
||||||
db = bot.get_db_connection(conn)
|
|
||||||
db.execute("create table if not exists urlhistory"
|
db.execute("create table if not exists urlhistory"
|
||||||
"(chan, url, nick, time)")
|
"(chan, url, nick, time)")
|
||||||
db.commit()
|
db.commit()
|
||||||
return db
|
|
||||||
|
|
||||||
|
|
||||||
def insert_history(db, chan, url, nick):
|
def insert_history(db, chan, url, nick):
|
||||||
|
@ -67,8 +65,8 @@ def format_reply(history):
|
||||||
|
|
||||||
|
|
||||||
@hook.regex(url_re)
|
@hook.regex(url_re)
|
||||||
def urlinput(match, nick='', chan='', conn=None, bot=None):
|
def urlinput(match, nick='', chan='', db=None, bot=None):
|
||||||
db = db_connect(bot, conn)
|
db_init(db)
|
||||||
url = urlnorm.normalize(match.group().encode('utf-8'))
|
url = urlnorm.normalize(match.group().encode('utf-8'))
|
||||||
if url not in ignored_urls:
|
if url not in ignored_urls:
|
||||||
history = get_history(db, chan, url)
|
history = get_history(db, chan, url)
|
||||||
|
|
|
@ -34,7 +34,7 @@ def _hook_add(func, add, name=''):
|
||||||
args.append(0) # means kwargs present
|
args.append(0) # means kwargs present
|
||||||
func._args = args
|
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
|
func._thread = False
|
||||||
|
|
||||||
def sieve(func):
|
def sieve(func):
|
||||||
|
@ -79,7 +79,7 @@ def event(arg=None, **kwargs):
|
||||||
return event_wrapper
|
return event_wrapper
|
||||||
|
|
||||||
|
|
||||||
def thread(func):
|
def singlethread(func):
|
||||||
func._thread = True
|
func._thread = True
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue