def sync(bot, module_name): global williedir global modulelist if not module_name or module_name == bot.config.owner: return bot.reply("No such module, use !webload list for available modules.") format_url = "https://raw.githubusercontent.com/srg/willie-modules/master/" resp = requests.get(format_url + "modules/" + module_name + ".py", stream=True) if resp.status_code is not requests.codes.ok: bot.reply("Response code for '{0}' not OK: {1}".format(module_name, resp.status_code)) return with open("{0}/modules/{1}.py".format(williedir, module_name), "wb") as fh: for chunk in resp.iter_content(1024): if not chunk: break fh.write(chunk) if modulelist[module_name]["depends"]: depends = modulelist[module_name]["depends"] if isinstance(depends, basestring): getdepends(depends, format_url, bot) else: for depen in depends: getdepends(depen, format_url, bot) module = imp.load_source(module_name, "{0}/modules/{1}.py".format(williedir, module_name)) if hasattr(module, "setup"): module.setup(bot) bot.register(vars(module)) bot.bind_commands() bot.reply(module)
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_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 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(u'%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 == '*') or (name.upper() == 'ALL THE THINGS'): bot.callables = None bot.commands = None bot.setup() return bot.reply('done') if not name in sys.modules: return bot.reply('%s: no such module!' % name) old_module = sys.modules[name] old_callables = {} for obj_name, obj in iteritems(vars(old_module)): 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 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 load_module(bot, name, path, type_): module, mtime = willie.loader.load_module(name, path, type_) relevant_parts = willie.loader.clean_module(module, bot.config) bot.register(*relevant_parts) # TODO sys.modules[name] = module if hasattr(module, 'setup'): module.setup(bot) modified = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(mtime)) bot.reply('%r (version: %s)' % (module, modified))
def setup(self): stderr("\nWelcome to Willie. 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() try: rel_path = os.path.relpath( filename, os.path.dirname(__file__) ) except Exception as e2: rel_path = filename 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()
def setup(self): stderr("\nWelcome to Willie. 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()
def f_reload(bot, trigger): if not trigger.admin: if bot.config.lang == 'ca': bot.reply(u'No tens permisos d\'administrador') elif bot.config.lang == 'es': bot.reply(u"No tienes permisos de administrador") else: bot.reply(u"You aren't an admin") return name = trigger.group(2) if (not name) or (name == '*') or (name.upper() == 'ALL THE THINGS'): bot.callables = None bot.commands = None bot.setup() return bot.reply('done') if not name in sys.modules: if bot.config.lang == 'ca': bot.reply(u"No hi ha cap mòdul anomenat " + name) elif bot.config.lang == 'es': bot.reply(u"No hay ningún module nombrado " + name) else: bot.reply(name + ": no such module!") return old_module = sys.modules[name] old_callables = {} for obj_name, obj in vars(old_module).iteritems(): 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 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(u'%Y-%m-%d, %H:%M:%S', time.gmtime(mtime)) bot.register(vars(module)) bot.bind_commands() bot.reply(u'%r (version: %s)' % (module, modified))