def f_load(bot, trigger): """Loads a module, for use by admins only.""" if not trigger.admin: return module_name = trigger.group(2) path = '' if module_name == bot.config.owner: return bot.reply('What?') if module_name in sys.modules: return bot.reply('Module already loaded, use reload') mods = bot.config.enumerate_modules() for name in mods: if name == trigger.group(2): path = mods[name] if not os.path.isfile(path): return bot.reply('Module %s not found' % module_name) module = imp.load_source(module_name, path) mtime = os.path.getmtime(module.__file__) modified = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(mtime)) if hasattr(module, 'setup'): module.setup(bot) bot.register(vars(module)) bot.bind_commands() bot.reply('%r (version: %s)' % (module, modified))
def f_reload(bot, trigger): """Reloads a module, for use by admins only.""" if not trigger.admin: return name = trigger.group(2) if name == bot.config.owner: return bot.reply('What?') if not name or name == '*': bot.callables = None bot.commands = None bot.setup() return bot.reply('Reloading all done!') if name not in sys.modules: return bot.reply('%s: not loaded, try the `load` command' % name) old_module = sys.modules[name] old_callables = {} for obj_name, obj in vars(old_module).items(): if bot.is_callable(obj) or bot.is_shutdown(obj): old_callables[obj_name] = obj bot.unregister(old_callables) # Also remove all references to willie callables from top level of the # module, so that they will not get loaded again if reloading the # module does not override them. for obj_name in list(old_callables.keys()): delattr(old_module, obj_name) # Also delete the setup function if hasattr(old_module, "setup"): delattr(old_module, "setup") # Thanks to moot for prodding me on this path = old_module.__file__ if path.endswith('.pyc') or path.endswith('.pyo'): path = path[:-1] if not os.path.isfile(path): return bot.reply('Found %s, but not the source file' % name) module = imp.load_source(name, path) sys.modules[name] = module if hasattr(module, 'setup'): module.setup(bot) mtime = os.path.getmtime(module.__file__) modified = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(mtime)) bot.register(vars(module)) bot.bind_commands() bot.reply('%r (version: %s)' % (module, modified))
def setup(self): stderr("\nWelcome to lpbot. Loading modules...\n\n") self.callables = set() self.shutdown_methods = set() filenames = self.config.enumerate_modules() # Coretasks is special. No custom user coretasks. this_dir = os.path.dirname(os.path.abspath(__file__)) filenames['coretasks'] = os.path.join(this_dir, 'coretasks.py') modules = [] error_count = 0 for name, filename in iteritems(filenames): try: module = imp.load_source(name, filename) except Exception as e: error_count = error_count + 1 filename, lineno = tools.get_raising_file_and_line() rel_path = os.path.relpath(filename, os.path.dirname(__file__)) raising_stmt = "%s:%d" % (rel_path, lineno) stderr("Error loading %s: %s (%s)" % (name, e, raising_stmt)) else: try: if hasattr(module, 'setup'): module.setup(self) self.register(vars(module)) modules.append(name) except Exception as e: error_count = error_count + 1 filename, lineno = tools.get_raising_file_and_line() rel_path = os.path.relpath( filename, os.path.dirname(__file__) ) raising_stmt = "%s:%d" % (rel_path, lineno) stderr("Error in %s setup procedure: %s (%s)" % (name, e, raising_stmt)) if modules: stderr('\n\nRegistered %d modules,' % (len(modules) - 1)) stderr('%d modules failed to load\n\n' % error_count) else: stderr("Warning: Couldn't find any modules") self.bind_commands()