Skip to content

Commit

Permalink
feat: move loading config method to base class
Browse files Browse the repository at this point in the history
  • Loading branch information
zhayujie committed Jul 20, 2023
1 parent aae9b64 commit 9ef8e1b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 33 deletions.
11 changes: 5 additions & 6 deletions plugins/banwords/banwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 1 addition & 7 deletions plugins/bdunit/bdunit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
File renamed without changes.
7 changes: 1 addition & 6 deletions plugins/godcmd/godcmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions plugins/plugin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import json
from config import pconf
from common.log import logger


class Plugin:
def __init__(self):
self.handlers = {}
Expand All @@ -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 "暂无帮助信息"
13 changes: 2 additions & 11 deletions plugins/tool/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 9ef8e1b

Please sign in to comment.