From 9ef8e1be3ff8ce1475faec9ef18a638d9085c6a3 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Thu, 20 Jul 2023 16:08:19 +0800 Subject: [PATCH] feat: move loading config method to base class --- plugins/banwords/banwords.py | 11 +++++------ plugins/bdunit/bdunit.py | 8 +------- ...{config-template.json => config.json.template} | 0 plugins/godcmd/godcmd.py | 7 +------ plugins/plugin.py | 15 ++++++++++++--- plugins/tool/tool.py | 13 ++----------- 6 files changed, 21 insertions(+), 33 deletions(-) rename plugins/{config-template.json => config.json.template} (100%) diff --git a/plugins/banwords/banwords.py b/plugins/banwords/banwords.py index d8d4adb17..2a33a5aff 100644 --- a/plugins/banwords/banwords.py +++ b/plugins/banwords/banwords.py @@ -24,18 +24,17 @@ class Banwords(Plugin): def __init__(self): super().__init__() try: - curdir = os.path.dirname(__file__) - config_path = os.path.join(curdir, "config.json") - # loading config from global plugin config + # load config conf = super().load_config() + curdir = os.path.dirname(__file__) if not conf: + # 配置不存在则写入默认配置 + config_path = os.path.join(curdir, "config.json") if not os.path.exists(config_path): conf = {"action": "ignore"} with open(config_path, "w") as f: json.dump(conf, f, indent=4) - else: - with open(config_path, "r") as f: - conf = super().load_config() or json.load(f) + self.searchr = WordsSearch() self.action = conf["action"] banwords_path = os.path.join(curdir, "banwords.txt") diff --git a/plugins/bdunit/bdunit.py b/plugins/bdunit/bdunit.py index 212b4d711..33194e3a3 100644 --- a/plugins/bdunit/bdunit.py +++ b/plugins/bdunit/bdunit.py @@ -29,15 +29,9 @@ class BDunit(Plugin): def __init__(self): super().__init__() try: - curdir = os.path.dirname(__file__) - config_path = os.path.join(curdir, "config.json") conf = super().load_config() if not conf: - if not os.path.exists(config_path): - raise Exception("config.json not found") - else: - with open(config_path, "r") as f: - conf = json.load(f) + raise Exception("config.json not found") self.service_id = conf["service_id"] self.api_key = conf["api_key"] self.secret_key = conf["secret_key"] diff --git a/plugins/config-template.json b/plugins/config.json.template similarity index 100% rename from plugins/config-template.json rename to plugins/config.json.template diff --git a/plugins/godcmd/godcmd.py b/plugins/godcmd/godcmd.py index 11def3fb6..08bc09e4f 100644 --- a/plugins/godcmd/godcmd.py +++ b/plugins/godcmd/godcmd.py @@ -178,18 +178,13 @@ class Godcmd(Plugin): def __init__(self): super().__init__() - curdir = os.path.dirname(__file__) - config_path = os.path.join(curdir, "config.json") - # loading config from global plugin config + config_path = os.path.join(os.path.dirname(__file__), "config.json") gconf = super().load_config() if not gconf: if not os.path.exists(config_path): gconf = {"password": "", "admin_users": []} with open(config_path, "w") as f: json.dump(gconf, f, indent=4) - else: - with open(config_path, "r") as f: - gconf = json.load(f) if gconf["password"] == "": self.temp_password = "".join(random.sample(string.digits, 4)) logger.info("[Godcmd] 因未设置口令,本次的临时口令为%s。" % self.temp_password) diff --git a/plugins/plugin.py b/plugins/plugin.py index 2938b47a1..e7444d2ec 100644 --- a/plugins/plugin.py +++ b/plugins/plugin.py @@ -1,7 +1,9 @@ import os +import json from config import pconf from common.log import logger + class Plugin: def __init__(self): self.handlers = {} @@ -11,9 +13,16 @@ def load_config(self) -> dict: 加载当前插件配置 :return: 插件配置字典 """ - conf = pconf(self.name) - logger.info(f"loading from global plugin config, plugin_name={self.name}, conf={conf}") - return conf + # 优先获取 plugins/config.json 中的全局配置 + plugin_conf = pconf(self.name) + if not plugin_conf: + # 全局配置不存在,则获取插件目录下的配置 + plugin_config_path = os.path.join(self.path, "config.json") + if os.path.exists(plugin_config_path): + with open(plugin_config_path, "r") as f: + plugin_conf = json.load(f) + logger.debug(f"loading plugin config, plugin_name={self.name}, conf={plugin_conf}") + return plugin_conf def get_help_text(self, **kwargs): return "暂无帮助信息" diff --git a/plugins/tool/tool.py b/plugins/tool/tool.py index eadd4d94b..b99eabbf9 100644 --- a/plugins/tool/tool.py +++ b/plugins/tool/tool.py @@ -10,7 +10,6 @@ from bridge.context import ContextType from bridge.reply import Reply, ReplyType from common import const -from common.log import logger from config import conf from plugins import * @@ -119,16 +118,8 @@ def on_handle_context(self, e_context: EventContext): return def _read_json(self) -> dict: - curdir = os.path.dirname(__file__) - config_path = os.path.join(curdir, "config.json") - tool_config = super().load_config() - if not tool_config: - if not os.path.exists(config_path): - return {"tools": [], "kwargs": {}} - else: - with open(config_path, "r") as f: - tool_config = json.load(f) - return tool_config + default_config = {"tools": [], "kwargs": {}} + return super().load_config() or default_config def _build_tool_kwargs(self, kwargs: dict): tool_model_name = kwargs.get("model_name")