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 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, ())

View File

@ -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

View File

@ -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:

View File

@ -80,7 +80,6 @@ def get_log_fd(dir, network, chan):
@hook.event(ignorebots=False)
def log(bot, input):
".remember <word> <data> -- 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')

View File

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