Przeglądaj źródła

feat: move loading config method to base class

master
zhayujie 1 rok temu
rodzic
commit
9ef8e1be3f
6 zmienionych plików z 21 dodań i 33 usunięć
  1. +5
    -6
      plugins/banwords/banwords.py
  2. +1
    -7
      plugins/bdunit/bdunit.py
  3. +0
    -0
      plugins/config.json.template
  4. +1
    -6
      plugins/godcmd/godcmd.py
  5. +12
    -3
      plugins/plugin.py
  6. +2
    -11
      plugins/tool/tool.py

+ 5
- 6
plugins/banwords/banwords.py Wyświetl plik

@@ -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")


+ 1
- 7
plugins/bdunit/bdunit.py Wyświetl plik

@@ -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"]


plugins/config-template.json → plugins/config.json.template Wyświetl plik


+ 1
- 6
plugins/godcmd/godcmd.py Wyświetl plik

@@ -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)


+ 12
- 3
plugins/plugin.py Wyświetl plik

@@ -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 @@ class Plugin:
加载当前插件配置
: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 "暂无帮助信息"

+ 2
- 11
plugins/tool/tool.py Wyświetl plik

@@ -10,7 +10,6 @@ from bridge.bridge import Bridge
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 @@ class Tool(Plugin):
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")


Ładowanie…
Anuluj
Zapisz