mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-07 12:50:03 +08:00
Config提供type字段确定配置项类型
This commit is contained in:
+184
-98
@@ -1,11 +1,53 @@
|
||||
import copy
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional, Union
|
||||
from typing import Any, Callable, Dict, Optional, Type, Union
|
||||
|
||||
import cattrs
|
||||
from pydantic import BaseModel
|
||||
from ruamel import yaml
|
||||
from ruamel.yaml import YAML
|
||||
from ruamel.yaml.scanner import ScannerError
|
||||
|
||||
from services.log import logger
|
||||
|
||||
|
||||
class Config(BaseModel):
|
||||
|
||||
"""
|
||||
配置项
|
||||
"""
|
||||
|
||||
value: Any
|
||||
"""配置项值"""
|
||||
name: Optional[str]
|
||||
"""插件名称"""
|
||||
help: Optional[str]
|
||||
"""配置注解"""
|
||||
default_value: Optional[Any] = None
|
||||
"""默认值"""
|
||||
level_module: Optional[str]
|
||||
"""受权限模块"""
|
||||
type: Any = None
|
||||
"""参数类型"""
|
||||
arg_parser: Optional[Callable] = None
|
||||
"""参数解析"""
|
||||
|
||||
|
||||
class ConfigGroup(BaseModel):
|
||||
|
||||
"""
|
||||
配置组
|
||||
"""
|
||||
|
||||
module: str
|
||||
"""模块名"""
|
||||
configs: Dict[str, Config] = {}
|
||||
"""配置项列表"""
|
||||
|
||||
|
||||
class NoSuchConfig(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ConfigsManager:
|
||||
"""
|
||||
@@ -13,35 +55,25 @@ class ConfigsManager:
|
||||
"""
|
||||
|
||||
def __init__(self, file: Path):
|
||||
self._data: dict = {}
|
||||
self._data: Dict[str, ConfigGroup] = {}
|
||||
self._simple_data: dict = {}
|
||||
self._admin_level_data = []
|
||||
self._simple_file = Path() / "configs" / "config.yaml"
|
||||
_yaml = YAML()
|
||||
if file:
|
||||
file.parent.mkdir(exist_ok=True, parents=True)
|
||||
self.file = file
|
||||
_yaml = YAML()
|
||||
if file.exists():
|
||||
with open(file, "r", encoding="utf8") as f:
|
||||
self._data = _yaml.load(f)
|
||||
if not self._data:
|
||||
self.file.unlink()
|
||||
raise ValueError(
|
||||
"配置文件为空!\n"
|
||||
"***********************************************************\n"
|
||||
"****** 配置文件 plugins2config.yaml 为空,已删除,请重启 ******\n"
|
||||
"***********************************************************"
|
||||
)
|
||||
if self._simple_file.exists():
|
||||
try:
|
||||
with open(self._simple_file, "r", encoding="utf8") as f:
|
||||
self._simple_data = _yaml.load(f)
|
||||
except ScannerError as e:
|
||||
raise ScannerError(
|
||||
f"{e}\n**********************************************\n"
|
||||
f"****** 可能为config.yaml配置文件填写不规范 ******\n"
|
||||
f"**********************************************"
|
||||
)
|
||||
self.load_data()
|
||||
if self._simple_file.exists():
|
||||
try:
|
||||
with open(self._simple_file, "r", encoding="utf8") as f:
|
||||
self._simple_data = _yaml.load(f)
|
||||
except ScannerError as e:
|
||||
raise ScannerError(
|
||||
f"{e}\n**********************************************\n"
|
||||
f"****** 可能为config.yaml配置文件填写不规范 ******\n"
|
||||
f"**********************************************"
|
||||
)
|
||||
|
||||
def add_plugin_config(
|
||||
self,
|
||||
@@ -52,6 +84,8 @@ class ConfigsManager:
|
||||
name: Optional[str] = None,
|
||||
help_: Optional[str] = None,
|
||||
default_value: Optional[Any] = None,
|
||||
type: Optional[Type] = str,
|
||||
arg_parser: Optional[Callable] = None,
|
||||
_override: bool = False,
|
||||
):
|
||||
"""
|
||||
@@ -62,41 +96,49 @@ class ConfigsManager:
|
||||
:param name: 插件名称
|
||||
:param help_: 配置注解
|
||||
:param default_value: 默认值
|
||||
:param _override: 覆盖前值
|
||||
:param _override: 强制覆盖值
|
||||
"""
|
||||
if (
|
||||
not (module in self._data.keys() and self._data[module].get(key))
|
||||
or _override
|
||||
):
|
||||
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)):
|
||||
config.help = help_
|
||||
config.arg_parser = arg_parser
|
||||
config.type = type
|
||||
if _override:
|
||||
config.value = value
|
||||
config.name = name
|
||||
config.default_value = default_value
|
||||
else:
|
||||
_module = None
|
||||
if ":" in module:
|
||||
module = module.split(":")
|
||||
_module = module[-1]
|
||||
module = module[0]
|
||||
module_split = module.split(":")
|
||||
if len(module_split) < 2:
|
||||
raise ValueError(f"module: {module} 填写错误")
|
||||
_module = module_split[-1]
|
||||
module = module_split[0]
|
||||
if "[LEVEL]" in key and _module:
|
||||
key = key.replace("[LEVEL]", "").strip()
|
||||
self._admin_level_data.append((_module, value))
|
||||
if self._data.get(module) is None:
|
||||
self._data[module] = {}
|
||||
key = key.upper()
|
||||
self._data[module][key] = {
|
||||
"value": value,
|
||||
"name": name.strip() if isinstance(name, str) else name,
|
||||
"help": help_.strip() if isinstance(help_, str) else help_,
|
||||
"default_value": default_value,
|
||||
"level_module": _module,
|
||||
}
|
||||
if not self._data.get(module):
|
||||
self._data[module] = ConfigGroup(module=module)
|
||||
self._data[module].configs[key] = Config(
|
||||
value=value,
|
||||
name=name,
|
||||
help=help_,
|
||||
default_value=default_value,
|
||||
level_module=_module,
|
||||
type=type,
|
||||
)
|
||||
|
||||
def remove_plugin_config(self, module: str):
|
||||
"""
|
||||
为插件删除一个配置
|
||||
:param module: 模块名
|
||||
"""
|
||||
if module in self._data.keys():
|
||||
del self._data[module]
|
||||
self.save()
|
||||
|
||||
def set_config(self, module: str, key: str, value: Any, auto_save: bool = False, save_simple_data: bool = True):
|
||||
def set_config(
|
||||
self,
|
||||
module: str,
|
||||
key: str,
|
||||
value: Any,
|
||||
auto_save: bool = False,
|
||||
save_simple_data: bool = True,
|
||||
):
|
||||
"""
|
||||
设置配置值
|
||||
:param module: 模块名
|
||||
@@ -105,40 +147,16 @@ class ConfigsManager:
|
||||
:param auto_save: 自动保存
|
||||
:param save_simple_data: 保存至config.yaml
|
||||
"""
|
||||
if module in self._data.keys():
|
||||
if module in self._data:
|
||||
if (
|
||||
self._data[module].get(key) is not None
|
||||
and self._data[module][key] != value
|
||||
self._data[module].configs.get(key)
|
||||
and self._data[module].configs[key] != value
|
||||
):
|
||||
self._data[module][key]["value"] = value
|
||||
self._data[module].configs[key].value = value
|
||||
self._simple_data[module][key] = value
|
||||
if auto_save:
|
||||
self.save(save_simple_data=save_simple_data)
|
||||
|
||||
def set_help(self, module: str, key: str, help_: str):
|
||||
"""
|
||||
设置配置注释
|
||||
:param module: 模块名
|
||||
:param key: 配置名称
|
||||
:param help_: 注释文本
|
||||
"""
|
||||
if module in self._data.keys():
|
||||
if self._data[module].get(key) is not None:
|
||||
self._data[module][key]["help"] = help_
|
||||
self.save()
|
||||
|
||||
def set_default_value(self, module: str, key: str, value: Any):
|
||||
"""
|
||||
设置配置默认值
|
||||
:param module: 模块名
|
||||
:param key: 配置名称
|
||||
:param value: 值
|
||||
"""
|
||||
if module in self._data.keys():
|
||||
if self._data[module].get(key) is not None:
|
||||
self._data[module][key]["default_value"] = value
|
||||
self.save()
|
||||
|
||||
def get_config(
|
||||
self, module: str, key: str, default: Optional[Any] = None
|
||||
) -> Optional[Any]:
|
||||
@@ -148,37 +166,68 @@ class ConfigsManager:
|
||||
:param key: 配置名称
|
||||
:param default: 没有key值内容的默认返回值
|
||||
"""
|
||||
logger.debug(
|
||||
f"尝试获取配置 MODULE: [<u><y>{module}</y></u>] | KEY: [<u><y>{key}</y></u>]"
|
||||
)
|
||||
key = key.upper()
|
||||
value = None
|
||||
if module in self._data.keys():
|
||||
for key in [key, f"{key} [LEVEL]"]:
|
||||
if self._data[module].get(key) is not None:
|
||||
if self._data[module][key]["value"] is None:
|
||||
return self._data[module][key]["default_value"]
|
||||
return self._data[module][key]["value"]
|
||||
if default is not None:
|
||||
return default
|
||||
return None
|
||||
config = self._data[module].configs.get(key)
|
||||
if not config:
|
||||
config = self._data[module].configs.get(f"{key} [LEVEL]")
|
||||
if not config:
|
||||
raise NoSuchConfig(f"未查询到配置项 MODULE: [ {module} ] | KEY: [ {key} ]")
|
||||
if config.arg_parser:
|
||||
value = config.arg_parser(value or config.default_value)
|
||||
else:
|
||||
try:
|
||||
if config.value is not None:
|
||||
value = (
|
||||
cattrs.structure(config.value, config.type)
|
||||
if config.type
|
||||
else config.value
|
||||
)
|
||||
else:
|
||||
if config.default_value is not None:
|
||||
value = (
|
||||
cattrs.structure(config.default_value, config.type)
|
||||
if config.type
|
||||
else config.default_value
|
||||
)
|
||||
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
|
||||
if not value:
|
||||
value = default
|
||||
logger.debug(
|
||||
f"获取配置 MODULE: [<u><y>{module}</y></u>] | KEY: [<u><y>{key}</y></u>] -> [<u><c>{value}</c></u>]"
|
||||
)
|
||||
return value
|
||||
|
||||
def get_level2module(self, module: str, key: str) -> Optional[str]:
|
||||
"""
|
||||
获取指定key所绑定的module,一般为权限等级
|
||||
获取指定key所绑定的module,一般为权限等级
|
||||
:param module: 模块名
|
||||
:param key: 配置名称
|
||||
:return:
|
||||
"""
|
||||
if self._data.get(module) is not None:
|
||||
if self._data[module].get(key) is not None:
|
||||
return self._data[module][key].get("level_module")
|
||||
if config := self._data[module].configs.get(key):
|
||||
return config.level_module
|
||||
|
||||
def get(self, key: str):
|
||||
def get(self, key: str) -> Optional[ConfigGroup]:
|
||||
"""
|
||||
获取插件配置数据
|
||||
:param key: 名称
|
||||
"""
|
||||
if key in self._data.keys():
|
||||
return self._data[key]
|
||||
return self._data.get(key)
|
||||
|
||||
def save(self, path: Union[str, Path] = None, save_simple_data: bool = False):
|
||||
def save(
|
||||
self, path: Optional[Union[str, Path]] = None, save_simple_data: bool = False
|
||||
):
|
||||
"""
|
||||
保存数据
|
||||
:param path: 路径
|
||||
@@ -193,10 +242,17 @@ class ConfigsManager:
|
||||
Dumper=yaml.RoundTripDumper,
|
||||
allow_unicode=True,
|
||||
)
|
||||
path = path if path else self.file
|
||||
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"]
|
||||
data[module][config] = value
|
||||
with open(path, "w", encoding="utf8") as f:
|
||||
yaml.dump(
|
||||
self._data, f, indent=2, Dumper=yaml.RoundTripDumper, allow_unicode=True
|
||||
data, f, indent=2, Dumper=yaml.RoundTripDumper, allow_unicode=True
|
||||
)
|
||||
|
||||
def reload(self):
|
||||
@@ -209,16 +265,46 @@ class ConfigsManager:
|
||||
self._simple_data = _yaml.load(f)
|
||||
for key in self._simple_data.keys():
|
||||
for k in self._simple_data[key].keys():
|
||||
self._data[key][k]["value"] = self._simple_data[key][k]
|
||||
self._data[key].configs[k].value = self._simple_data[key][k]
|
||||
self.save()
|
||||
|
||||
def load_data(self):
|
||||
"""
|
||||
加载数据
|
||||
|
||||
Raises:
|
||||
ValueError: _description_
|
||||
"""
|
||||
if self.file.exists():
|
||||
_yaml = YAML()
|
||||
with open(self.file, "r", encoding="utf8") as f:
|
||||
temp_data = _yaml.load(f)
|
||||
if not temp_data:
|
||||
self.file.unlink()
|
||||
raise ValueError(
|
||||
"配置文件为空!\n"
|
||||
"***********************************************************\n"
|
||||
"****** 配置文件 plugins2config.yaml 为空,已删除,请重启 ******\n"
|
||||
"***********************************************************"
|
||||
)
|
||||
count = 0
|
||||
for module in temp_data:
|
||||
config_group = ConfigGroup(module=module)
|
||||
for config in temp_data[module]:
|
||||
config_group.configs[config] = Config(**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> 个配置项"
|
||||
)
|
||||
|
||||
def get_admin_level_data(self):
|
||||
"""
|
||||
获取管理插件等级
|
||||
"""
|
||||
return self._admin_level_data
|
||||
|
||||
def get_data(self):
|
||||
def get_data(self) -> Dict[str, ConfigGroup]:
|
||||
return copy.deepcopy(self._data)
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
|
||||
Reference in New Issue
Block a user