plugin_handler.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:MusicBot 作者: BjoernPetersen 项目源码 文件源码
def load_plugins(self):
        logger = logging.getLogger(__name__)
        if not os.path.isdir(plugin_dir):
            return
        possible_plugins = os.listdir(plugin_dir)
        for filename in possible_plugins:
            if not os.path.isfile(os.path.join(plugin_dir, filename)):
                continue
            split = filename.split('.')
            if not (len(split) > 1 and split[-1] == "py"):
                continue
            name = "".join(split[:-1])
            module_info = imp.find_module(name, [plugin_dir])
            try:
                module = imp.load_module(name, *module_info)
                dir_list = dir(module)
                if ("get_label" in dir_list) and ("run_command" in dir_list):
                    self.plugins.append(module)
                else:
                    logger.error(Fore.RED + "'%s' is not a valid plugin!" + Fore.RESET, filename)
            except Exception as e:
                logger.error(Fore.RED + "%s error while loading plugin '%s': \n %s" + Fore.RESET,
                             type(e).__name__, filename, e)
            finally:
                module_info[0].close()

    # Return a list of loaded plugins
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号