make persistent db name include bot nick

This commit is contained in:
Ryan Hitchman 2010-03-13 07:02:05 -07:00
parent 892f858643
commit 30baa6c49c
6 changed files with 33 additions and 31 deletions

View File

@ -2,9 +2,13 @@ import os
import sqlite3 import sqlite3
def get_db_connection(server, name='skybot.%s.db'): def get_db_connection(conn, name=''):
"returns an sqlite3 connection to a persistent database" "returns an sqlite3 connection to a persistent database"
filename = os.path.join(bot.persist_dir, name % server)
if not name:
name = '%s.%s.db' % (conn.nick, conn.server)
filename = os.path.join(bot.persist_dir, name)
return sqlite3.connect(filename, timeout=10) return sqlite3.connect(filename, timeout=10)
bot.get_db_connection = get_db_connection bot.get_db_connection = get_db_connection

View File

@ -27,6 +27,7 @@ class Input(dict):
paraml=paraml, msg=msg, server=conn.server, chan=chan, paraml=paraml, msg=msg, server=conn.server, chan=chan,
say=say, reply=reply, pm=pm, bot=bot, lastparam=paraml[-1]) say=say, reply=reply, pm=pm, bot=bot, lastparam=paraml[-1])
# make dict keys accessible as attributes
def __getattr__(self, key): def __getattr__(self, key):
return self[key] return self[key]
@ -42,18 +43,18 @@ def run(func, input):
if args: if args:
if 'db' in args: if 'db' in args:
input['db'] = get_db_connection(input['server']) input.db = get_db_connection(input.conn)
if 'input' in args: if 'input' in args:
input['input'] = input input.input = input
if 0 in args: if 0 in args:
out = func(input['inp'], **input) out = func(input.inp, **input)
else: else:
kw = dict((key, input[key]) for key in args if key in input) kw = dict((key, input[key]) for key in args if key in input)
out = func(input['inp'], **kw) out = func(input.inp, **kw)
else: else:
out = func(input['inp']) out = func(input.inp)
if out is not None: if out is not None:
input['reply'](unicode(out)) input.reply(unicode(out))
def do_sieve(sieve, bot, input, func, type, args): def do_sieve(sieve, bot, input, func, type, args):

View File

@ -11,7 +11,6 @@ import re
from util import hook from util import hook
lock = thread.allocate_lock()
log_fds = {} # '%(net)s %(chan)s' : (filename, fd) log_fds = {} # '%(net)s %(chan)s' : (filename, fd)
timestamp_format = '%H:%M:%S' timestamp_format = '%H:%M:%S'
@ -86,7 +85,6 @@ def get_log_fd(dir, server, chan):
@hook.thread @hook.thread
@hook.event('*') @hook.event('*')
def log(paraml, input=None, bot=None): def log(paraml, input=None, bot=None):
with lock:
timestamp = gmtime(timestamp_format) timestamp = gmtime(timestamp_format)
fd = get_log_fd(bot.persist_dir, input.server, 'raw') fd = get_log_fd(bot.persist_dir, input.server, 'raw')

View File

@ -8,7 +8,7 @@ from util import hook, timesince
@hook.thread @hook.thread
@hook.event('PRIVMSG') @hook.event('PRIVMSG')
def seeninput(paraml, input=None, bot=None): def seeninput(paraml, input=None, bot=None):
db = bot.get_db_connection(input.server) 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,

View File

@ -18,7 +18,7 @@ def tellinput(paraml, input=None, bot=None):
if 'showtells' in input.msg.lower(): if 'showtells' in input.msg.lower():
return return
db = bot.get_db_connection(input.server) db = bot.get_db_connection(input.conn)
db = db_init(db) db = db_init(db)
tells = get_tells(db, input.nick, input.chan) tells = get_tells(db, input.nick, input.chan)

View File

@ -11,9 +11,8 @@ 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, server): def db_connect(bot, conn):
"check to see that our db has the the seen table and return a dbection." db = bot.get_db_connection(conn)
db = bot.get_db_connection(server)
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()
@ -68,8 +67,8 @@ def format_reply(history):
@hook.regex(url_re) @hook.regex(url_re)
def urlinput(match, nick='', chan='', server='', reply=None, bot=None): def urlinput(match, nick='', chan='', conn=None, bot=None):
db = db_connect(bot, server) db = db_connect(bot, conn)
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)