logging now properly ordered
This commit is contained in:
parent
8f045f626c
commit
985f7c5d51
|
@ -6,6 +6,7 @@ class Input(object):
|
||||||
def __init__(self, conn, raw, prefix, command,
|
def __init__(self, conn, raw, prefix, command,
|
||||||
params, nick, user, host, paraml, msg):
|
params, nick, user, host, paraml, msg):
|
||||||
self.conn = conn
|
self.conn = conn
|
||||||
|
self.server = conn.server
|
||||||
self.raw = raw
|
self.raw = raw
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
self.command = command
|
self.command = command
|
||||||
|
@ -28,7 +29,6 @@ class FakeBot(object):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.conn = conn
|
self.conn = conn
|
||||||
self.persist_dir = bot.persist_dir
|
self.persist_dir = bot.persist_dir
|
||||||
self.server = conn.server
|
|
||||||
self.input = input
|
self.input = input
|
||||||
self.msg = conn.msg
|
self.msg = conn.msg
|
||||||
self.cmd = conn.cmd
|
self.cmd = conn.cmd
|
||||||
|
@ -56,6 +56,9 @@ class FakeBot(object):
|
||||||
self.say(unicode(out))
|
self.say(unicode(out))
|
||||||
|
|
||||||
def main(conn, out):
|
def main(conn, out):
|
||||||
|
for csig, func, args in bot.plugs['tee']:
|
||||||
|
input = Input(conn, *out)
|
||||||
|
func._iqueue.put((bot, input))
|
||||||
for csig, func, args in (bot.plugs['command'] + bot.plugs['event']):
|
for csig, func, args in (bot.plugs['command'] + bot.plugs['event']):
|
||||||
input = Input(conn, *out)
|
input = Input(conn, *out)
|
||||||
for fsig, sieve in bot.plugs['sieve']:
|
for fsig, sieve in bot.plugs['sieve']:
|
||||||
|
|
|
@ -51,6 +51,12 @@ def reload(init=False):
|
||||||
|
|
||||||
# remove plugins already loaded from this filename
|
# remove plugins already loaded from this filename
|
||||||
for name, data in bot.plugs.iteritems():
|
for name, data in bot.plugs.iteritems():
|
||||||
|
|
||||||
|
if name == 'tee': # signal tee trampolines to stop
|
||||||
|
for csig, func, args in data:
|
||||||
|
if csig[0] == filename:
|
||||||
|
func._iqueue.put(StopIteration)
|
||||||
|
|
||||||
bot.plugs[name] = filter(lambda x: x[0][0] != filename, data)
|
bot.plugs[name] = filter(lambda x: x[0][0] != filename, data)
|
||||||
|
|
||||||
for obj in namespace.itervalues():
|
for obj in namespace.itervalues():
|
||||||
|
@ -62,12 +68,6 @@ def reload(init=False):
|
||||||
print '### new plugin (type: %s) loaded:' % \
|
print '### new plugin (type: %s) loaded:' % \
|
||||||
type, format_plug(data)
|
type, format_plug(data)
|
||||||
|
|
||||||
if type == 'init': # run-once functions
|
|
||||||
try:
|
|
||||||
obj(bot) # not thread-safe!
|
|
||||||
except Exception:
|
|
||||||
traceback.print_exc(Exception)
|
|
||||||
|
|
||||||
if init:
|
if init:
|
||||||
print ' plugin listing:'
|
print ' plugin listing:'
|
||||||
for type, plugs in sorted(bot.plugs.iteritems()):
|
for type, plugs in sorted(bot.plugs.iteritems()):
|
||||||
|
|
|
@ -83,12 +83,12 @@ def get_log_fd(dir, server, chan):
|
||||||
return fd
|
return fd
|
||||||
|
|
||||||
|
|
||||||
@hook.event(ignorebots=False)
|
@hook.tee
|
||||||
def log(bot, input):
|
def log(bot, input):
|
||||||
with lock:
|
with lock:
|
||||||
timestamp = gmtime(timestamp_format)
|
timestamp = gmtime(timestamp_format)
|
||||||
|
|
||||||
fd = get_log_fd(bot.persist_dir, bot.server, 'raw')
|
fd = get_log_fd(bot.persist_dir, input.server, 'raw')
|
||||||
fd.write(timestamp + ' ' + input.raw + '\n')
|
fd.write(timestamp + ' ' + input.raw + '\n')
|
||||||
|
|
||||||
if input.command == 'QUIT': # these are temporary fixes until proper
|
if input.command == 'QUIT': # these are temporary fixes until proper
|
||||||
|
@ -104,5 +104,5 @@ def log(bot, input):
|
||||||
print '%s %s %s' % (timestamp, input.chan, beau)
|
print '%s %s %s' % (timestamp, input.chan, beau)
|
||||||
|
|
||||||
if input.chan:
|
if input.chan:
|
||||||
fd = get_log_fd(bot.persist_dir, bot.server, input.chan)
|
fd = get_log_fd(bot.persist_dir, input.server, input.chan)
|
||||||
fd.write(timestamp + ' ' + beau + '\n')
|
fd.write(timestamp + ' ' + beau + '\n')
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
import Queue
|
import Queue
|
||||||
|
import thread
|
||||||
|
|
||||||
class queue(Queue.Queue):
|
|
||||||
|
|
||||||
def __init__(self, maxsize=0):
|
|
||||||
Queue.Queue.__init__(self, maxsize)
|
|
||||||
self._skybot_hook = [['queue', self]]
|
|
||||||
|
|
||||||
def _isfunc(x):
|
def _isfunc(x):
|
||||||
if type(x) == type(_isfunc):
|
if type(x) == type(_isfunc):
|
||||||
|
@ -30,14 +26,6 @@ def sieve(func):
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
||||||
def init(func):
|
|
||||||
if func.func_code.co_argcount != 1:
|
|
||||||
raise ValueError(
|
|
||||||
'initializers must take 1 argument: bot')
|
|
||||||
_hook_add(func, ['init', (_make_sig(func), func)])
|
|
||||||
return func
|
|
||||||
|
|
||||||
|
|
||||||
def command(func=None, hook=None, **kwargs):
|
def command(func=None, hook=None, **kwargs):
|
||||||
args = {}
|
args = {}
|
||||||
|
|
||||||
|
@ -79,3 +67,25 @@ def event(arg=None, **kwargs):
|
||||||
if arg is not None:
|
if arg is not None:
|
||||||
args['events'] = arg.split()
|
args['events'] = arg.split()
|
||||||
return event_wrapper
|
return event_wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def tee(func, **kwargs):
|
||||||
|
"passes _all_ input lines to function, in order (skips sieves)"
|
||||||
|
|
||||||
|
if func.func_code.co_argcount != 2:
|
||||||
|
raise ValueError('tees must take 2 arguments: (bot, input)')
|
||||||
|
|
||||||
|
_hook_add(func, ['tee', (_make_sig(func), func, kwargs)])
|
||||||
|
func._iqueue = Queue.Queue()
|
||||||
|
|
||||||
|
def trampoline(func):
|
||||||
|
input = None
|
||||||
|
while True:
|
||||||
|
input = func._iqueue.get()
|
||||||
|
if input == StopIteration:
|
||||||
|
return
|
||||||
|
func(*input)
|
||||||
|
|
||||||
|
thread.start_new_thread(trampoline, (func,))
|
||||||
|
|
||||||
|
return func
|
||||||
|
|
Loading…
Reference in New Issue