h/core/reload.py

78 lines
2.6 KiB
Python
Raw Normal View History

2009-04-19 11:42:48 +00:00
import glob
import collections
import traceback
2009-04-19 11:42:48 +00:00
if 'mtimes' not in globals():
mtimes = {}
if 'lastfiles' not in globals():
lastfiles = set()
2009-11-07 18:37:38 +00:00
def format_plug(plug, lpad=0, width=40):
out = ' ' * lpad + '%s:%s:%s' % (plug[0])
if len(plug) == 3 and 'hook' in plug[2]:
out += '%s%s' % (' ' * (width - len(out)), plug[2]['hook'])
return out
def reload(init=False):
if init:
bot.plugs = collections.defaultdict(lambda: [])
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())
except Exception:
traceback.print_exc(Exception)
continue
if filename == 'core/reload.py':
reload(init=init)
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:
traceback.print_exc(Exception)
continue
# remove plugins already loaded from this filename
for name, data in bot.plugs.iteritems():
bot.plugs[name] = filter(lambda x: x[0][0] != filename, data)
for obj in namespace.itervalues():
if hasattr(obj, '_skybot_hook'): #check for magic
for type, data in obj._skybot_hook:
bot.plugs[type] += [data]
2009-11-07 18:37:38 +00:00
if not init:
print '### new plugin (type: %s) loaded:' % type, format_plug(data)
2009-11-07 18:37:38 +00:00
if type == 'init': # run-once functions
try:
2009-11-07 18:37:38 +00:00
obj(bot) # not thread-safe!
except Exception:
traceback.print_exc(Exception)
if init:
print ' plugin listing:'
for type, plugs in sorted(bot.plugs.iteritems()):
print ' %s:' % type
for plug in plugs:
out = ' %s:%s:%s' % (plug[0])
2009-11-07 18:37:38 +00:00
print format_plug(plug, lpad=6)
print