2009-04-19 11:42:48 +00:00
|
|
|
import collections
|
2010-01-17 20:38:37 +00:00
|
|
|
import glob
|
|
|
|
import os
|
2010-03-11 23:34:54 +00:00
|
|
|
import re
|
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
|
|
|
|
2010-03-11 23:34:54 +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
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
def make_signature(f):
|
|
|
|
return f.func_code.co_filename, f.func_name, f.func_code.co_firstlineno
|
|
|
|
|
|
|
|
|
|
|
|
def format_plug(plug, kind='', lpad=0, width=40):
|
|
|
|
out = ' ' * lpad + '%s:%s:%s' % make_signature(plug[0])
|
|
|
|
if kind == 'command':
|
|
|
|
out += ' ' * (50 - len(out)) + plug[1]['name']
|
|
|
|
|
|
|
|
if kind == 'event':
|
|
|
|
out += ' ' * (50 - len(out)) + ', '.join(plug[1]['events'])
|
|
|
|
|
2010-03-12 00:19:36 +00:00
|
|
|
if kind == 'regex':
|
|
|
|
out += ' ' * (50 - len(out)) + plug[1]['regex']
|
|
|
|
|
2009-11-07 18:37:38 +00:00
|
|
|
return out
|
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2009-10-17 01:36:02 +00:00
|
|
|
def reload(init=False):
|
2010-03-11 23:34:54 +00:00
|
|
|
changed = False
|
|
|
|
|
2009-10-17 01:36:02 +00:00
|
|
|
if init:
|
2010-03-11 23:34:54 +00:00
|
|
|
bot.plugs = collections.defaultdict(list)
|
|
|
|
bot.threads = {}
|
2009-04-19 11:18:27 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
core_fileset = set(glob.glob(os.path.join("core", "*.py")))
|
2010-03-27 08:42:27 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
for filename in core_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
|
2010-03-27 08:42:27 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
changed = True
|
|
|
|
|
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'),
|
2014-01-14 21:12:37 +00:00
|
|
|
globals())
|
2009-04-23 02:49:06 +00:00
|
|
|
except Exception:
|
2010-03-18 21:11:20 +00:00
|
|
|
traceback.print_exc()
|
|
|
|
if init: # stop if there's an error (syntax?) 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-03-11 23:34:54 +00:00
|
|
|
fileset = set(glob.glob(os.path.join('plugins', '*.py')))
|
|
|
|
|
|
|
|
# remove deleted/moved plugins
|
|
|
|
for name, data in bot.plugs.iteritems():
|
|
|
|
bot.plugs[name] = [x for x in data if x[0]._filename in fileset]
|
|
|
|
|
|
|
|
for filename in list(mtimes):
|
|
|
|
if filename not in fileset and filename not in core_fileset:
|
|
|
|
mtimes.pop(filename)
|
2009-04-23 02:49:06 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
for func, handler in list(bot.threads.iteritems()):
|
|
|
|
if func._filename not in fileset:
|
|
|
|
handler.stop()
|
|
|
|
del bot.threads[func]
|
|
|
|
|
|
|
|
# compile new plugins
|
2009-04-23 02:49:06 +00:00
|
|
|
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
|
2010-03-27 08:42:27 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
changed = True
|
|
|
|
|
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:
|
2010-03-18 21:11:20 +00:00
|
|
|
traceback.print_exc()
|
2009-04-19 11:18:27 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# remove plugins already loaded from this filename
|
|
|
|
for name, data in bot.plugs.iteritems():
|
2010-03-27 08:42:27 +00:00
|
|
|
bot.plugs[name] = [x for x in data
|
2010-03-11 23:34:54 +00:00
|
|
|
if x[0]._filename != filename]
|
2009-11-19 00:39:27 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
for func, handler in list(bot.threads.iteritems()):
|
|
|
|
if func._filename == filename:
|
|
|
|
handler.stop()
|
|
|
|
del bot.threads[func]
|
2009-04-19 11:18:27 +00:00
|
|
|
|
|
|
|
for obj in namespace.itervalues():
|
2010-03-11 23:34:54 +00:00
|
|
|
if hasattr(obj, '_hook'): # check for magic
|
|
|
|
if obj._thread:
|
|
|
|
bot.threads[obj] = Handler(obj)
|
2010-03-27 08:42:27 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
for type, data in obj._hook:
|
2009-04-19 11:18:27 +00:00
|
|
|
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:' % \
|
2014-01-14 21:12:37 +00:00
|
|
|
type, format_plug(data)
|
2009-04-19 11:18:27 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
if changed:
|
|
|
|
bot.commands = {}
|
|
|
|
for plug in bot.plugs['command']:
|
|
|
|
name = plug[1]['name'].lower()
|
|
|
|
if not re.match(r'^\w+$', name):
|
|
|
|
print '### ERROR: invalid command name "%s" (%s)' % (name,
|
2014-01-14 21:12:37 +00:00
|
|
|
format_plug(plug))
|
2010-03-11 23:34:54 +00:00
|
|
|
continue
|
|
|
|
if name in bot.commands:
|
|
|
|
print "### ERROR: command '%s' already registered (%s, %s)" % \
|
|
|
|
(name, format_plug(bot.commands[name]),
|
|
|
|
format_plug(plug))
|
|
|
|
continue
|
|
|
|
bot.commands[name] = plug
|
|
|
|
|
|
|
|
bot.events = collections.defaultdict(list)
|
|
|
|
for func, args in bot.plugs['event']:
|
|
|
|
for event in args['events']:
|
|
|
|
bot.events[event].append((func, args))
|
|
|
|
|
2009-04-19 11:18:27 +00:00
|
|
|
if init:
|
|
|
|
print ' plugin listing:'
|
2010-03-11 23:34:54 +00:00
|
|
|
|
|
|
|
if bot.commands:
|
|
|
|
# hack to make commands with multiple aliases
|
|
|
|
# print nicely
|
2010-03-27 08:42:27 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
print ' command:'
|
|
|
|
commands = collections.defaultdict(list)
|
|
|
|
|
|
|
|
for name, (func, args) in bot.commands.iteritems():
|
|
|
|
commands[make_signature(func)].append(name)
|
|
|
|
|
|
|
|
for sig, names in sorted(commands.iteritems()):
|
2010-03-27 08:42:27 +00:00
|
|
|
names.sort(key=lambda x: (-len(x), x)) # long names first
|
2010-03-11 23:34:54 +00:00
|
|
|
out = ' ' * 6 + '%s:%s:%s' % sig
|
|
|
|
out += ' ' * (50 - len(out)) + ', '.join(names)
|
|
|
|
print out
|
2010-03-27 08:42:27 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
for kind, plugs in sorted(bot.plugs.iteritems()):
|
|
|
|
if kind == 'command':
|
|
|
|
continue
|
2010-03-12 00:19:36 +00:00
|
|
|
print ' %s:' % kind
|
2009-04-19 11:18:27 +00:00
|
|
|
for plug in plugs:
|
2010-03-11 23:34:54 +00:00
|
|
|
print format_plug(plug, kind=kind, lpad=6)
|
2009-04-19 11:18:27 +00:00
|
|
|
print
|