From bc46ef50e6a58f4b6ed58f9a48a6fc018501c88d Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Wed, 22 Apr 2009 20:49:06 -0600 Subject: [PATCH] better error reporting, move printing into log module --- core/main.py | 8 +++----- core/reload.py | 30 ++++++++++++++++++++---------- plugins/hook.py | 5 +---- plugins/log.py | 7 +++++-- plugins/urbandictionary.py | 3 ++- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/core/main.py b/core/main.py index c15e525..643fdad 100644 --- a/core/main.py +++ b/core/main.py @@ -1,4 +1,5 @@ import thread +import traceback class Input(object): @@ -53,20 +54,17 @@ class FakeBot(object): self.say(unicode(out)) def main(out): - printed = False for csig, func, args in (bot.plugs['command'] + bot.plugs['event']): input = Input(*out) for fsig, sieve in bot.plugs['sieve']: try: input = sieve(bot, input, func, args) except Exception, e: - print 'sieve error:', e + print 'sieve error', + traceback.print_exc(Exception) input = None if input == None: break if input == None: continue - if not printed: - print '<<<', input.raw - printed = True thread.start_new_thread(FakeBot(bot, input, func).run, ()) diff --git a/core/reload.py b/core/reload.py index a468546..68df8ee 100644 --- a/core/reload.py +++ b/core/reload.py @@ -1,9 +1,13 @@ import glob import collections +import traceback -if 'plugin_mtimes' not in globals(): +if 'mtimes' not in globals(): mtimes = {} +if 'lastfiles' not in globals(): + lastfiles = set() + def reload(): init = False if not hasattr(bot, 'plugs'): @@ -13,23 +17,32 @@ def reload(): for filename in glob.glob("core/*.py"): mtime = os.stat(filename).st_mtime if mtime != mtimes.get(filename): + mtimes[filename] = mtime try: eval(compile(open(filename, 'U').read(), filename, 'exec'), globals()) - mtimes[filename] = mtime - except Exception, e: - print ' core error:', e + except Exception: + traceback.print_exc(Exception) 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 if mtime != mtimes.get(filename): + mtimes[filename] = mtime try: code = compile(open(filename, 'U').read(), filename, 'exec') namespace = {} eval(code, namespace) - except Exception, e: - print ' error:', e + except Exception: + traceback.print_exc(Exception) continue # remove plugins already loaded from this filename @@ -41,8 +54,6 @@ def reload(): for type, data in obj._skybot_hook: bot.plugs[type] += [data] - mtimes[filename] = mtime - if init: print ' plugin listing:' for type, plugs in sorted(bot.plugs.iteritems()): @@ -55,4 +66,3 @@ def reload(): else: print print - diff --git a/plugins/hook.py b/plugins/hook.py index 9b3d4ae..e27bc23 100644 --- a/plugins/hook.py +++ b/plugins/hook.py @@ -46,10 +46,7 @@ def command(func=None, hook=None, **kwargs): if func is not None: args['name'] = func if hook is not None: - if isinstance(hook, list): - args['hook'] = '(?:' + '|'.join(hook) + ')' - else: - args['hook'] = hook + args['hook'] = hook args.update(kwargs) return command_wrapper else: diff --git a/plugins/log.py b/plugins/log.py index 1aa5ed4..ae1ee49 100644 --- a/plugins/log.py +++ b/plugins/log.py @@ -80,7 +80,6 @@ def get_log_fd(dir, network, chan): @hook.event(ignorebots=False) def log(bot, input): - ".remember -- maps word to data in the memory" with lock: timestamp = gmtime(timestamp_format) @@ -90,6 +89,10 @@ def log(bot, input): if input.command == 'QUIT': input.chan = 'quit' + beau = beautify(input) + + print '%s %s %s' % (timestamp, input.chan, beau) + if input.chan: fd = get_log_fd(bot.persist_dir, bot.network, input.chan) - fd.write(timestamp + ' ' + beautify(input) + '\n') + fd.write(timestamp + ' ' + beau + '\n') diff --git a/plugins/urbandictionary.py b/plugins/urbandictionary.py index 5d12bd7..18abd83 100644 --- a/plugins/urbandictionary.py +++ b/plugins/urbandictionary.py @@ -4,7 +4,8 @@ import urllib import hook -@hook.command(['u', 'urban']) +@hook.command('u') +@hook.command def urban(inp): '''.u/.urban -- looks up on urbandictionary.com''' if not inp.strip():