Files
zhenxun_bot/zhenxun/configs/utils/__init__.py
T

464 lines
13 KiB
Python
Raw Normal View History

2022-12-04 16:46:33 +08:00
import copy
2024-08-29 22:01:34 +08:00
from typing import Any
2021-11-04 16:11:50 +08:00
from pathlib import Path
2024-08-29 22:01:34 +08:00
from collections.abc import Callable
2023-02-26 22:17:26 +08:00
import cattrs
from ruamel.yaml import YAML
2024-08-29 22:01:34 +08:00
from pydantic import BaseModel
2022-05-26 22:49:48 +08:00
from ruamel.yaml.scanner import ScannerError
2021-11-04 16:11:50 +08:00
2024-02-04 04:18:54 +08:00
from zhenxun.services.log import logger
2024-08-29 22:01:34 +08:00
from zhenxun.configs.path_config import DATA_PATH
from zhenxun.utils.enum import BlockType, PluginType, LimitWatchType, PluginLimitType
2023-02-26 22:17:26 +08:00
2024-02-04 04:18:54 +08:00
_yaml = YAML(pure=True)
_yaml.indent = 2
_yaml.allow_unicode = True
2023-02-26 22:17:26 +08:00
2024-02-04 04:18:54 +08:00
class RegisterConfig(BaseModel):
"""
注册配置项
"""
key: str
"""配置项键"""
value: Any
"""配置项值"""
module: str | None = None
"""模块名"""
help: str | None
"""配置注解"""
default_value: Any | None = None
"""默认值"""
type: Any = None
"""参数类型"""
arg_parser: Callable | None = None
"""参数解析"""
class ConfigModel(BaseModel):
2023-02-26 22:17:26 +08:00
"""
配置项
"""
value: Any
"""配置项值"""
2024-02-04 04:18:54 +08:00
help: str | None
2023-02-26 22:17:26 +08:00
"""配置注解"""
2024-02-04 04:18:54 +08:00
default_value: Any | None = None
2023-02-26 22:17:26 +08:00
"""默认值"""
type: Any = None
"""参数类型"""
2024-02-04 04:18:54 +08:00
arg_parser: Callable | None = None
2023-02-26 22:17:26 +08:00
"""参数解析"""
class ConfigGroup(BaseModel):
"""
配置组
"""
module: str
"""模块名"""
2024-02-04 04:18:54 +08:00
name: str | None = None
"""插件名"""
2024-08-29 22:01:34 +08:00
configs: dict[str, ConfigModel] = {}
2023-02-26 22:17:26 +08:00
"""配置项列表"""
2024-02-04 04:18:54 +08:00
def get(self, c: str, default: Any = None) -> Any:
cfg = self.configs.get(c.upper())
2024-02-04 04:18:54 +08:00
if cfg is not None:
2024-07-21 19:06:50 +08:00
if cfg.value is not None:
return cfg.value
if cfg.default_value is not None:
return cfg.default_value
2024-02-04 04:18:54 +08:00
return default
class BaseBlock(BaseModel):
"""
插件阻断基本类(插件阻断限制)
"""
status: bool = True
"""限制状态"""
check_type: BlockType = BlockType.ALL
"""检查类型"""
watch_type: LimitWatchType = LimitWatchType.USER
"""监听对象"""
result: str | None = None
"""阻断时回复内容"""
_type: PluginLimitType = PluginLimitType.BLOCK
"""类型"""
class PluginCdBlock(BaseBlock):
"""
插件cd限制
"""
cd: int = 5
"""cd"""
_type: PluginLimitType = PluginLimitType.CD
"""类型"""
class PluginCountBlock(BaseBlock):
"""
插件次数限制
"""
max_count: int
"""最大调用次数"""
_type: PluginLimitType = PluginLimitType.COUNT
"""类型"""
class PluginSetting(BaseModel):
"""
插件基本配置
"""
level: int = 5
"""群权限等级"""
default_status: bool = True
"""进群默认开关状态"""
limit_superuser: bool = False
"""是否限制超级用户"""
cost_gold: int = 0
"""调用插件花费金币"""
2024-02-25 03:18:34 +08:00
class Task(BaseBlock):
module: str
"""被动技能模块名"""
name: str
"""被动技能名称"""
status: bool = True
"""全局开关状态"""
2024-08-22 19:10:47 +08:00
create_status: bool = False
2024-08-21 23:36:19 +08:00
"""初次加载默认开关状态"""
2024-08-29 22:01:34 +08:00
default_status: bool = True
"""进群时默认状态"""
2024-02-25 03:18:34 +08:00
run_time: str | None = None
"""运行时间"""
2024-02-04 04:18:54 +08:00
class PluginExtraData(BaseModel):
"""
插件扩展信息
"""
author: str | None = None
"""作者"""
version: str | None = None
"""版本"""
2024-02-25 03:18:34 +08:00
plugin_type: PluginType = PluginType.NORMAL
2024-02-04 04:18:54 +08:00
"""插件类型"""
menu_type: str = "功能"
"""菜单类型"""
admin_level: int | None = None
"""管理员插件所需权限等级"""
2024-02-25 03:18:34 +08:00
configs: list[RegisterConfig] | None = None
2024-02-04 04:18:54 +08:00
"""插件配置"""
setting: PluginSetting | None = None
"""插件基本配置"""
2024-02-25 03:18:34 +08:00
limits: list[BaseBlock | PluginCdBlock | PluginCountBlock] | None = None
2024-02-04 04:18:54 +08:00
"""插件限制"""
2024-02-25 03:18:34 +08:00
tasks: list[Task] | None = None
"""技能被动"""
2024-05-04 13:48:12 +08:00
superuser_help: str | None = None
"""超级用户帮助"""
2024-08-29 22:01:34 +08:00
aliases: set[str] = set()
2024-05-23 13:58:53 +08:00
"""额外名称"""
sql_list: list[str] | None = None
"""常用sql"""
2024-02-04 04:18:54 +08:00
2023-02-26 22:17:26 +08:00
class NoSuchConfig(Exception):
pass
2021-11-04 16:11:50 +08:00
class ConfigsManager:
"""
插件配置 与 资源 管理器
"""
def __init__(self, file: Path):
2024-08-29 22:01:34 +08:00
self._data: dict[str, ConfigGroup] = {}
2022-04-10 22:19:50 +08:00
self._simple_data: dict = {}
2024-02-04 04:18:54 +08:00
self._simple_file = DATA_PATH / "config.yaml"
2023-02-26 22:17:26 +08:00
_yaml = YAML()
2021-11-04 16:11:50 +08:00
if file:
file.parent.mkdir(exist_ok=True, parents=True)
self.file = file
2023-02-26 22:17:26 +08:00
self.load_data()
if self._simple_file.exists():
try:
2024-08-29 22:01:34 +08:00
with self._simple_file.open(encoding="utf8") as f:
2023-02-26 22:17:26 +08:00
self._simple_data = _yaml.load(f)
except ScannerError as e:
raise ScannerError(
f"{e}\n**********************************************\n"
f"****** 可能为config.yaml配置文件填写不规范 ******\n"
f"**********************************************"
2024-08-29 22:01:34 +08:00
) from e
2021-11-04 16:11:50 +08:00
2024-05-15 23:24:35 +08:00
def set_name(self, module: str, name: str):
"""设置插件配置中文名出
参数:
module: 模块名
name: 中文名称
异常:
ValueError: module不能为为空
"""
if not module:
raise ValueError("set_name: module不能为为空")
if data := self._data.get(module):
data.name = name
2021-11-04 16:11:50 +08:00
def add_plugin_config(
self,
module: str,
key: str,
2024-02-04 04:18:54 +08:00
value: Any,
2021-11-04 16:11:50 +08:00
*,
2024-02-04 04:18:54 +08:00
help: str | None = None,
default_value: Any = None,
2024-08-29 22:01:34 +08:00
type: type | None = None,
2024-02-04 04:18:54 +08:00
arg_parser: Callable | None = None,
2021-11-29 13:09:47 +08:00
_override: bool = False,
2021-11-04 16:11:50 +08:00
):
2024-02-04 04:18:54 +08:00
"""为插件添加一个配置,不会被覆盖,只有第一个生效
参数:
module: 模块
key: 键
value: 值
help: 配置注解.
default_value: 默认值.
type: 值类型.
arg_parser: 值解析器,一般与webui配合使用.
_override: 强制覆盖值.
异常:
2024-05-15 23:24:35 +08:00
ValueError: module和key不能为为空
ValueError: 填写错误
2021-11-04 16:11:50 +08:00
"""
2024-02-04 04:18:54 +08:00
2023-02-26 22:17:26 +08:00
if not module or not key:
raise ValueError("add_plugin_config: module和key不能为为空")
if module in self._data and (config := self._data[module].configs.get(key)):
2024-02-04 04:18:54 +08:00
config.help = help
2023-02-26 22:17:26 +08:00
config.arg_parser = arg_parser
config.type = type
if _override:
config.value = value
config.default_value = default_value
else:
2021-11-04 16:11:50 +08:00
_module = None
if ":" in module:
2023-02-26 22:17:26 +08:00
module_split = module.split(":")
if len(module_split) < 2:
raise ValueError(f"module: {module} 填写错误")
_module = module_split[-1]
module = module_split[0]
2021-11-04 16:11:50 +08:00
key = key.upper()
2023-02-26 22:17:26 +08:00
if not self._data.get(module):
self._data[module] = ConfigGroup(module=module)
2024-02-04 04:18:54 +08:00
self._data[module].configs[key] = ConfigModel(
2023-02-26 22:17:26 +08:00
value=value,
2024-02-04 04:18:54 +08:00
help=help,
2023-02-26 22:17:26 +08:00
default_value=default_value,
type=type,
)
2021-11-04 16:11:50 +08:00
2023-02-26 22:17:26 +08:00
def set_config(
self,
module: str,
key: str,
value: Any,
auto_save: bool = False,
save_simple_data: bool = True,
):
2024-02-04 04:18:54 +08:00
"""设置配置值
参数:
module: 模块名
key: 配置名称
value: 值
auto_save: 自动保存.
save_simple_data: 保存至config.yaml.
2021-11-04 16:11:50 +08:00
"""
2023-02-26 22:17:26 +08:00
if module in self._data:
2024-02-04 04:18:54 +08:00
data = self._data[module].configs.get(key)
if data and data != value:
2023-02-26 22:17:26 +08:00
self._data[module].configs[key].value = value
2022-04-10 22:19:50 +08:00
self._simple_data[module][key] = value
if auto_save:
self.save(save_simple_data=save_simple_data)
2021-11-04 16:11:50 +08:00
2024-02-04 04:18:54 +08:00
def get_config(self, module: str, key: str, default: Any = None) -> Any:
"""获取指定配置值
参数:
module: 模块名
key: 配置键
default: 没有key值内容的默认返回值.
异常:
NoSuchConfig: 未查询到配置
返回:
Any: 配置值
2021-11-04 16:11:50 +08:00
"""
2023-02-26 22:17:26 +08:00
logger.debug(
2024-08-29 22:01:34 +08:00
f"尝试获取配置MODULE: [<u><y>{module}</y></u>] | KEY: [<u><y>{key}</y></u>]"
2023-02-26 22:17:26 +08:00
)
2021-11-04 16:11:50 +08:00
key = key.upper()
2023-02-26 22:17:26 +08:00
value = None
if module in self._data.keys():
2024-08-29 22:01:34 +08:00
config = self._data[module].configs.get(key) or self._data[
module
].configs.get(key)
2023-02-26 22:17:26 +08:00
if not config:
2024-02-25 03:18:34 +08:00
raise NoSuchConfig(
f"未查询到配置项 MODULE: [ {module} ] | KEY: [ {key} ]"
)
2024-08-29 22:01:34 +08:00
try:
if config.arg_parser:
value = config.arg_parser(value or config.default_value)
elif config.value is not None:
# try:
value = (
cattrs.structure(config.value, config.type)
if config.type
else config.value
)
elif config.default_value is not None:
value = (
cattrs.structure(config.default_value, config.type)
if config.type
else config.default_value
2023-02-26 22:17:26 +08:00
)
2024-08-29 22:01:34 +08:00
except Exception as e:
logger.warning(
f"配置项类型转换 MODULE: [<u><y>{module}</y></u>]"
" | KEY: [<u><y>{key}</y></u>]",
e=e,
)
value = config.value or config.default_value
2023-03-02 18:17:07 +08:00
if value is None:
2023-02-26 22:17:26 +08:00
value = default
logger.debug(
2024-08-29 22:01:34 +08:00
f"获取配置 MODULE: [<u><y>{module}</y></u>] | "
2024-08-30 23:50:45 +08:00
f" KEY: [<u><y>{key}</y></u>] -> [<u><c>{value}</c></u>]"
2023-02-26 22:17:26 +08:00
)
return value
2021-11-04 16:11:50 +08:00
2024-02-04 04:18:54 +08:00
def get(self, key: str) -> ConfigGroup:
"""获取插件配置数据
2021-11-29 13:09:47 +08:00
2024-02-04 04:18:54 +08:00
参数:
key: 键,一般为模块名
2021-11-04 16:11:50 +08:00
2024-02-04 04:18:54 +08:00
返回:
ConfigGroup: ConfigGroup
2021-11-04 16:11:50 +08:00
"""
2024-02-04 04:18:54 +08:00
return self._data.get(key) or ConfigGroup(module="")
def save(self, path: str | Path | None = None, save_simple_data: bool = False):
"""保存数据
参数:
path: 路径.
save_simple_data: 同时保存至config.yaml.
2021-11-04 16:11:50 +08:00
"""
2022-04-10 22:19:50 +08:00
if save_simple_data:
with open(self._simple_file, "w", encoding="utf8") as f:
2024-02-04 04:18:54 +08:00
# yaml.dump(
# self._simple_data,
# f,
# indent=2,
# Dumper=yaml.RoundTripDumper,
# allow_unicode=True,
# )
_yaml.dump(self._simple_data, f)
2023-02-26 22:17:26 +08:00
path = path or self.file
data = {}
for module in self._data:
data[module] = {}
for config in self._data[module].configs:
value = self._data[module].configs[config].dict()
del value["type"]
2024-02-04 04:18:54 +08:00
del value["arg_parser"]
2023-02-26 22:17:26 +08:00
data[module][config] = value
2021-11-04 16:11:50 +08:00
with open(path, "w", encoding="utf8") as f:
2024-02-04 04:18:54 +08:00
# yaml.dump(
# data, f, indent=2, Dumper=yaml.RoundTripDumper, allow_unicode=True
# )
_yaml.dump(data, f)
2021-11-04 16:11:50 +08:00
2022-02-19 18:20:19 +08:00
def reload(self):
2024-02-04 04:18:54 +08:00
"""重新加载配置文件"""
2022-02-19 18:20:19 +08:00
_yaml = YAML()
if self._simple_file.exists():
2024-08-29 22:01:34 +08:00
with open(self._simple_file, encoding="utf8") as f:
self._simple_data = _yaml.load(f)
for key in self._simple_data.keys():
for k in self._simple_data[key].keys():
2023-02-26 22:17:26 +08:00
self._data[key].configs[k].value = self._simple_data[key][k]
2022-02-19 18:20:19 +08:00
self.save()
2023-02-26 22:17:26 +08:00
def load_data(self):
2024-02-04 04:18:54 +08:00
"""加载数据
2023-02-26 22:17:26 +08:00
2024-02-04 04:18:54 +08:00
异常:
ValueError: 配置文件为空!
2023-02-26 22:17:26 +08:00
"""
2024-08-29 22:01:34 +08:00
if not self.file.exists():
return
with open(self.file, encoding="utf8") as f:
temp_data = _yaml.load(f)
if not temp_data:
self.file.unlink()
raise ValueError(
"配置文件为空!\n"
"***********************************************************\n"
"****** 配置文件 plugins2config.yaml 为空,已删除,请重启 ******\n"
"***********************************************************"
2023-02-26 22:17:26 +08:00
)
2024-08-29 22:01:34 +08:00
count = 0
for module in temp_data:
config_group = ConfigGroup(module=module)
for config in temp_data[module]:
config_group.configs[config] = ConfigModel(**temp_data[module][config])
count += 1
self._data[module] = config_group
logger.info(
f"加载配置完成,共加载 <u><y>{len(temp_data)}</y></u> 个配置组及对应"
" <u><y>{count}</y></u> 个配置项"
)
2023-02-26 22:17:26 +08:00
2024-08-29 22:01:34 +08:00
def get_data(self) -> dict[str, ConfigGroup]:
2022-12-04 16:46:33 +08:00
return copy.deepcopy(self._data)
2021-11-04 16:11:50 +08:00
def is_empty(self) -> bool:
return not bool(self._data)
def keys(self):
return self._data.keys()
def __str__(self):
return str(self._data)
def __setitem__(self, key, value):
self._data[key] = value
def __getitem__(self, key):
return self._data[key]