2009-04-19 11:42:48 +00:00
|
|
|
import collections
|
2010-01-17 20:38:37 +00:00
|
|
|
import glob
|
|
|
|
import os
|
2009-12-12 19:11:04 +00:00
|
|
|
import sys
|
2010-01-17 20:38:37 +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()
|
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
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
|
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
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: [])
|
|
|
|
|
2010-01-17 20:38:37 +00:00
|
|
|
for filename in glob.glob(os.path.join("core", "*.py")):
|
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:
|
2010-03-01 02:32:41 +00:00
|
|
|
eval(compile(open(filename, 'U').read(), filename, 'exec'),
|
2009-04-19 11:18:27 +00:00
|
|
|
globals())
|
2009-04-23 02:49:06 +00:00
|
|
|
except Exception:
|
|
|
|
traceback.print_exc(Exception)
|
2009-12-12 19:11:04 +00:00
|
|
|
if init: # stop if there's a syntax error in a core
|
2010-03-01 02:32:41 +00:00
|
|
|
sys.exit() # script on startup
|
2009-04-19 11:18:27 +00:00
|
|
|
continue
|
|
|
|
|
2010-01-17 20:38:37 +00:00
|
|
|
if filename == os.path.join('core', 'reload.py'):
|
2009-10-17 01:36:02 +00:00
|
|
|
reload(init=init)
|
2009-04-23 02:49:06 +00:00
|
|
|
return
|
|
|
|
|
2010-01-17 20:38:37 +00:00
|
|
|
fileset = set(glob.glob(os.path.join('plugins', '*py')))
|
2010-03-01 02:32:41 +00:00
|
|
|
for name, data in bot.plugs.iteritems(): # remove deleted/moved plugins
|
2009-04-23 02:49:06 +00:00
|
|
|
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():
|
2009-11-19 00:39:27 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
if name == 'tee': # signal tee trampolines to stop
|
2009-11-19 00:39:27 +00:00
|
|
|
for csig, func, args in data:
|
|
|
|
if csig[0] == filename:
|
|
|
|
func._iqueue.put(StopIteration)
|
|
|
|
|
2009-04-19 11:18:27 +00:00
|
|
|
bot.plugs[name] = filter(lambda x: x[0][0] != filename, data)
|
|
|
|
|
|
|
|
for obj in namespace.itervalues():
|
2010-03-01 02:32:41 +00:00
|
|
|
if hasattr(obj, '_skybot_hook'): # check for magic
|
2009-04-19 11:18:27 +00:00
|
|
|
for type, data in obj._skybot_hook:
|
|
|
|
bot.plugs[type] += [data]
|
2009-11-07 18:37:38 +00:00
|
|
|
|
|
|
|
if not init:
|
2009-11-18 00:38:48 +00:00
|
|
|
print '### new plugin (type: %s) loaded:' % \
|
|
|
|
type, format_plug(data)
|
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])
|
2009-11-07 18:37:38 +00:00
|
|
|
print format_plug(plug, lpad=6)
|
2009-04-19 11:18:27 +00:00
|
|
|
print
|