better error reporting, move printing into log module

This commit is contained in:
Ryan Hitchman 2009-04-22 20:49:06 -06:00
parent 322205eb92
commit bc46ef50e6
5 changed files with 31 additions and 22 deletions

View File

@ -1,4 +1,5 @@
import thread import thread
import traceback
class Input(object): class Input(object):
@ -53,20 +54,17 @@ class FakeBot(object):
self.say(unicode(out)) self.say(unicode(out))
def main(out): def main(out):
printed = False
for csig, func, args in (bot.plugs['command'] + bot.plugs['event']): for csig, func, args in (bot.plugs['command'] + bot.plugs['event']):
input = Input(*out) input = Input(*out)
for fsig, sieve in bot.plugs['sieve']: for fsig, sieve in bot.plugs['sieve']:
try: try:
input = sieve(bot, input, func, args) input = sieve(bot, input, func, args)
except Exception, e: except Exception, e:
print 'sieve error:', e print 'sieve error',
traceback.print_exc(Exception)
input = None input = None
if input == None: if input == None:
break break
if input == None: if input == None:
continue continue
if not printed:
print '<<<', input.raw
printed = True
thread.start_new_thread(FakeBot(bot, input, func).run, ()) thread.start_new_thread(FakeBot(bot, input, func).run, ())

View File

@ -1,9 +1,13 @@
import glob import glob
import collections import collections
import traceback
if 'plugin_mtimes' not in globals(): if 'mtimes' not in globals():
mtimes = {} mtimes = {}
if 'lastfiles' not in globals():
lastfiles = set()
def reload(): def reload():
init = False init = False
if not hasattr(bot, 'plugs'): if not hasattr(bot, 'plugs'):
@ -13,23 +17,32 @@ def reload():
for filename in glob.glob("core/*.py"): for filename in glob.glob("core/*.py"):
mtime = os.stat(filename).st_mtime mtime = os.stat(filename).st_mtime
if mtime != mtimes.get(filename): if mtime != mtimes.get(filename):
mtimes[filename] = mtime
try: try:
eval(compile(open(filename, 'U').read(), filename, 'exec'), eval(compile(open(filename, 'U').read(), filename, 'exec'),
globals()) globals())
mtimes[filename] = mtime except Exception:
except Exception, e: traceback.print_exc(Exception)
print ' core error:', e
continue continue
for filename in glob.glob("plugins/*.py"): if filename == 'core/reload.py':
reload()
return
fileset = set(glob.glob("plugins/*py"))
for name, data in bot.plugs.iteritems(): # remove deleted/moved plugins
bot.plugs[name] = filter(lambda x: x[0][0] in fileset, data)
for filename in fileset:
mtime = os.stat(filename).st_mtime mtime = os.stat(filename).st_mtime
if mtime != mtimes.get(filename): if mtime != mtimes.get(filename):
mtimes[filename] = mtime
try: try:
code = compile(open(filename, 'U').read(), filename, 'exec') code = compile(open(filename, 'U').read(), filename, 'exec')
namespace = {} namespace = {}
eval(code, namespace) eval(code, namespace)
except Exception, e: except Exception:
print ' error:', e traceback.print_exc(Exception)
continue continue
# remove plugins already loaded from this filename # remove plugins already loaded from this filename
@ -41,8 +54,6 @@ def reload():
for type, data in obj._skybot_hook: for type, data in obj._skybot_hook:
bot.plugs[type] += [data] bot.plugs[type] += [data]
mtimes[filename] = mtime
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()):
@ -55,4 +66,3 @@ def reload():
else: else:
print print
print print

View File

@ -46,10 +46,7 @@ def command(func=None, hook=None, **kwargs):
if func is not None: if func is not None:
args['name'] = func args['name'] = func
if hook is not None: if hook is not None:
if isinstance(hook, list): args['hook'] = hook
args['hook'] = '(?:' + '|'.join(hook) + ')'
else:
args['hook'] = hook
args.update(kwargs) args.update(kwargs)
return command_wrapper return command_wrapper
else: else:

View File

@ -80,7 +80,6 @@ def get_log_fd(dir, network, chan):
@hook.event(ignorebots=False) @hook.event(ignorebots=False)
def log(bot, input): def log(bot, input):
".remember <word> <data> -- maps word to data in the memory"
with lock: with lock:
timestamp = gmtime(timestamp_format) timestamp = gmtime(timestamp_format)
@ -90,6 +89,10 @@ def log(bot, input):
if input.command == 'QUIT': if input.command == 'QUIT':
input.chan = 'quit' input.chan = 'quit'
beau = beautify(input)
print '%s %s %s' % (timestamp, input.chan, beau)
if input.chan: if input.chan:
fd = get_log_fd(bot.persist_dir, bot.network, input.chan) fd = get_log_fd(bot.persist_dir, bot.network, input.chan)
fd.write(timestamp + ' ' + beautify(input) + '\n') fd.write(timestamp + ' ' + beau + '\n')

View File

@ -4,7 +4,8 @@ import urllib
import hook import hook
@hook.command(['u', 'urban']) @hook.command('u')
@hook.command
def urban(inp): def urban(inp):
'''.u/.urban <phrase> -- looks up <phrase> on urbandictionary.com''' '''.u/.urban <phrase> -- looks up <phrase> on urbandictionary.com'''
if not inp.strip(): if not inp.strip():