2009-04-19 11:42:48 +00:00
|
|
|
import glob
|
|
|
|
import collections
|
2009-04-23 02:49:06 +00:00
|
|
|
import traceback
|
2009-04-19 11:42:48 +00:00
|
|
|
|
2009-04-23 02:49:06 +00:00
|
|
|
if 'mtimes' not in globals():
|
2009-04-19 11:18:27 +00:00
|
|
|
mtimes = {}
|
|
|
|
|
2009-04-23 02:49:06 +00:00
|
|
|
if 'lastfiles' not in globals():
|
|
|
|
lastfiles = set()
|
|
|
|
|
2009-10-17 01:36:02 +00:00
|
|
|
def reload(init=False):
|
|
|
|
if init:
|
2009-04-19 11:18:27 +00:00
|
|
|
bot.plugs = collections.defaultdict(lambda: [])
|
|
|
|
|
|
|
|
for filename in glob.glob("core/*.py"):
|
|
|
|
mtime = os.stat(filename).st_mtime
|
|
|
|
if mtime != mtimes.get(filename):
|
2009-04-23 02:49:06 +00:00
|
|
|
mtimes[filename] = mtime
|
2009-04-19 11:18:27 +00:00
|
|
|
try:
|
|
|
|
eval(compile(open(filename, 'U').read(), filename, 'exec'),
|
|
|
|
globals())
|
2009-04-23 02:49:06 +00:00
|
|
|
except Exception:
|
|
|
|
traceback.print_exc(Exception)
|
2009-04-19 11:18:27 +00:00
|
|
|
continue
|
|
|
|
|
2009-04-23 02:49:06 +00:00
|
|
|
if filename == 'core/reload.py':
|
2009-10-17 01:36:02 +00:00
|
|
|
reload(init=init)
|
2009-04-23 02:49:06 +00:00
|
|
|
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:
|
2009-04-19 11:18:27 +00:00
|
|
|
mtime = os.stat(filename).st_mtime
|
|
|
|
if mtime != mtimes.get(filename):
|
2009-04-23 02:49:06 +00:00
|
|
|
mtimes[filename] = mtime
|
2009-04-19 11:18:27 +00:00
|
|
|
try:
|
|
|
|
code = compile(open(filename, 'U').read(), filename, 'exec')
|
|
|
|
namespace = {}
|
|
|
|
eval(code, namespace)
|
2009-04-23 02:49:06 +00:00
|
|
|
except Exception:
|
|
|
|
traceback.print_exc(Exception)
|
2009-04-19 11:18:27 +00:00
|
|
|
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-10-17 01:36:02 +00:00
|
|
|
|
|
|
|
if type == 'init':
|
|
|
|
try:
|
|
|
|
obj(bot)
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc(Exception)
|
2009-04-19 11:18:27 +00:00
|
|
|
|
|
|
|
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])
|
|
|
|
print out,
|
|
|
|
if len(plug) == 3 and 'hook' in plug[2]:
|
|
|
|
print '%s%s' % (' ' * (40 - len(out)), plug[2]['hook'])
|
|
|
|
else:
|
|
|
|
print
|
|
|
|
print
|