From 4bab4299f28f0660cb4c76a5b97e20e78f5a90ff Mon Sep 17 00:00:00 2001 From: zhayujie Date: Thu, 20 Jul 2023 14:24:40 +0800 Subject: [PATCH] fix: global plugin config read --- plugins/banwords/banwords.py | 18 ++++++++++-------- plugins/bdunit/bdunit.py | 13 +++++++------ plugins/godcmd/godcmd.py | 18 ++++++++++-------- plugins/plugin.py | 5 ++++- plugins/tool/tool.py | 13 +++++++------ 5 files changed, 38 insertions(+), 29 deletions(-) diff --git a/plugins/banwords/banwords.py b/plugins/banwords/banwords.py index 51cfc89..d8d4adb 100644 --- a/plugins/banwords/banwords.py +++ b/plugins/banwords/banwords.py @@ -26,14 +26,16 @@ class Banwords(Plugin): try: curdir = os.path.dirname(__file__) config_path = os.path.join(curdir, "config.json") - conf = None - 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) + # loading config from global plugin config + conf = super().load_config() + if not conf: + 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 778d33d..212b4d7 100644 --- a/plugins/bdunit/bdunit.py +++ b/plugins/bdunit/bdunit.py @@ -31,12 +31,13 @@ class BDunit(Plugin): try: curdir = os.path.dirname(__file__) config_path = os.path.join(curdir, "config.json") - conf = None - if not os.path.exists(config_path): - raise Exception("config.json not found") - else: - with open(config_path, "r") as f: - conf = super().load_config() or json.load(f) + 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) self.service_id = conf["service_id"] self.api_key = conf["api_key"] self.secret_key = conf["secret_key"] diff --git a/plugins/godcmd/godcmd.py b/plugins/godcmd/godcmd.py index 7131af0..11def3f 100644 --- a/plugins/godcmd/godcmd.py +++ b/plugins/godcmd/godcmd.py @@ -180,14 +180,16 @@ class Godcmd(Plugin): curdir = os.path.dirname(__file__) config_path = os.path.join(curdir, "config.json") - gconf = None - 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 = super().load_config() or json.load(f) + # loading config from global plugin config + 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 bd432b9..2938b47 100644 --- a/plugins/plugin.py +++ b/plugins/plugin.py @@ -1,5 +1,6 @@ import os from config import pconf +from common.log import logger class Plugin: def __init__(self): @@ -10,7 +11,9 @@ class Plugin: 加载当前插件配置 :return: 插件配置字典 """ - return pconf(self.name) + conf = pconf(self.name) + logger.info(f"loading from global plugin config, plugin_name={self.name}, conf={conf}") + return conf def get_help_text(self, **kwargs): return "暂无帮助信息" diff --git a/plugins/tool/tool.py b/plugins/tool/tool.py index b84d475..eadd4d9 100644 --- a/plugins/tool/tool.py +++ b/plugins/tool/tool.py @@ -121,12 +121,13 @@ class Tool(Plugin): def _read_json(self) -> dict: curdir = os.path.dirname(__file__) config_path = os.path.join(curdir, "config.json") - tool_config = {"tools": [], "kwargs": {}} - if not os.path.exists(config_path): - return tool_config - else: - with open(config_path, "r") as f: - tool_config = super().load_config() or json.load(f) + 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 def _build_tool_kwargs(self, kwargs: dict):