diff --git a/bot.py b/bot.py index 1bcc46b..0fb66da 100755 --- a/bot.py +++ b/bot.py @@ -1,8 +1,8 @@ #!/usr/bin/python -network = "irc.synirc.net" +network = "localhost" nick = "skybot" -channel = "#cobol" +channel = "#test" import sys import os @@ -12,25 +12,25 @@ sys.path += ['plugins'] # so 'import hook' works without duplication sys.path += ['lib'] os.chdir(sys.path[0]) # do stuff relative to the installation directory -import irc - class Bot(object): pass + bot = Bot() print 'Loading plugins' # bootstrap the reloader eval(compile(open('core/reload.py', 'U').read(), 'core/reload.py', 'exec')) +reload(init=True) print 'Connecting to IRC' bot.nick = nick bot.channel = channel bot.network = network -bot.irc = irc.irc(network, nick) +bot.irc = irc(network, nick) bot.irc.join(channel) bot.persist_dir = os.path.abspath('persist') diff --git a/core/reload.py b/core/reload.py index 68df8ee..d6365aa 100644 --- a/core/reload.py +++ b/core/reload.py @@ -8,11 +8,9 @@ if 'mtimes' not in globals(): if 'lastfiles' not in globals(): lastfiles = set() -def reload(): - init = False - if not hasattr(bot, 'plugs'): +def reload(init=False): + if init: bot.plugs = collections.defaultdict(lambda: []) - init = True for filename in glob.glob("core/*.py"): mtime = os.stat(filename).st_mtime @@ -26,7 +24,7 @@ def reload(): continue if filename == 'core/reload.py': - reload() + reload(init=init) return fileset = set(glob.glob("plugins/*py")) @@ -53,6 +51,12 @@ def reload(): if hasattr(obj, '_skybot_hook'): #check for magic for type, data in obj._skybot_hook: bot.plugs[type] += [data] + + if type == 'init': + try: + obj(bot) + except Exception: + traceback.print_exc(Exception) if init: print ' plugin listing:' diff --git a/plugins/util/hook.py b/plugins/util/hook.py index e27bc23..767c52e 100644 --- a/plugins/util/hook.py +++ b/plugins/util/hook.py @@ -30,6 +30,14 @@ def sieve(func): return func +def init(func): + if func.func_code.co_argcount != 1: + raise ValueError( + 'initializers must take 1 argument: bot') + _hook_add(func, ['init', (_make_sig(func), func)]) + return func + + def command(func=None, hook=None, **kwargs): args = {}