2024-01-27 00:06:38 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-11-26 22:46:27 +08:00
|
|
|
from . import model as file_model
|
2024-08-24 20:54:36 +08:00
|
|
|
from .impls import pymodule, json as json_file, yaml as yaml_file
|
2023-11-26 22:46:27 +08:00
|
|
|
|
|
|
|
|
2024-02-06 21:26:03 +08:00
|
|
|
managers: ConfigManager = []
|
|
|
|
|
|
|
|
|
2023-11-26 22:46:27 +08:00
|
|
|
class ConfigManager:
|
|
|
|
"""配置文件管理器"""
|
|
|
|
|
|
|
|
file: file_model.ConfigFile = None
|
|
|
|
"""配置文件实例"""
|
|
|
|
|
|
|
|
data: dict = None
|
|
|
|
"""配置数据"""
|
|
|
|
|
|
|
|
def __init__(self, cfg_file: file_model.ConfigFile) -> None:
|
|
|
|
self.file = cfg_file
|
|
|
|
self.data = {}
|
|
|
|
|
2024-05-15 21:40:18 +08:00
|
|
|
async def load_config(self, completion: bool=True):
|
|
|
|
self.data = await self.file.load(completion=completion)
|
2023-11-26 22:46:27 +08:00
|
|
|
|
|
|
|
async def dump_config(self):
|
|
|
|
await self.file.save(self.data)
|
2024-01-25 15:28:23 +08:00
|
|
|
|
2024-03-14 16:00:22 +08:00
|
|
|
def dump_config_sync(self):
|
|
|
|
self.file.save_sync(self.data)
|
|
|
|
|
2024-01-25 15:28:23 +08:00
|
|
|
|
2024-05-15 21:40:18 +08:00
|
|
|
async def load_python_module_config(config_name: str, template_name: str, completion: bool=True) -> ConfigManager:
|
2024-08-24 20:54:36 +08:00
|
|
|
"""加载Python模块配置文件
|
|
|
|
|
|
|
|
Args:
|
|
|
|
config_name (str): 配置文件名
|
|
|
|
template_name (str): 模板文件名
|
|
|
|
completion (bool): 是否自动补全内存中的配置文件
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
ConfigManager: 配置文件管理器
|
|
|
|
"""
|
2024-01-25 15:28:23 +08:00
|
|
|
cfg_inst = pymodule.PythonModuleConfigFile(
|
|
|
|
config_name,
|
|
|
|
template_name
|
|
|
|
)
|
|
|
|
|
|
|
|
cfg_mgr = ConfigManager(cfg_inst)
|
2024-05-15 21:40:18 +08:00
|
|
|
await cfg_mgr.load_config(completion=completion)
|
2024-01-25 15:28:23 +08:00
|
|
|
|
|
|
|
return cfg_mgr
|
|
|
|
|
|
|
|
|
2024-05-15 21:40:18 +08:00
|
|
|
async def load_json_config(config_name: str, template_name: str=None, template_data: dict=None, completion: bool=True) -> ConfigManager:
|
2024-08-24 20:54:36 +08:00
|
|
|
"""加载JSON配置文件
|
|
|
|
|
|
|
|
Args:
|
|
|
|
config_name (str): 配置文件名
|
|
|
|
template_name (str): 模板文件名
|
|
|
|
template_data (dict): 模板数据
|
|
|
|
completion (bool): 是否自动补全内存中的配置文件
|
|
|
|
"""
|
2024-01-25 15:28:23 +08:00
|
|
|
cfg_inst = json_file.JSONConfigFile(
|
|
|
|
config_name,
|
2024-03-20 14:20:56 +08:00
|
|
|
template_name,
|
|
|
|
template_data
|
2024-01-25 15:28:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
cfg_mgr = ConfigManager(cfg_inst)
|
2024-05-15 21:40:18 +08:00
|
|
|
await cfg_mgr.load_config(completion=completion)
|
2024-01-25 15:28:23 +08:00
|
|
|
|
2024-08-24 20:54:36 +08:00
|
|
|
return cfg_mgr
|
|
|
|
|
|
|
|
|
|
|
|
async def load_yaml_config(config_name: str, template_name: str=None, template_data: dict=None, completion: bool=True) -> ConfigManager:
|
|
|
|
"""加载YAML配置文件
|
|
|
|
|
|
|
|
Args:
|
|
|
|
config_name (str): 配置文件名
|
|
|
|
template_name (str): 模板文件名
|
|
|
|
template_data (dict): 模板数据
|
|
|
|
completion (bool): 是否自动补全内存中的配置文件
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
ConfigManager: 配置文件管理器
|
|
|
|
"""
|
|
|
|
cfg_inst = yaml_file.YAMLConfigFile(
|
|
|
|
config_name,
|
|
|
|
template_name,
|
|
|
|
template_data
|
|
|
|
)
|
|
|
|
|
|
|
|
cfg_mgr = ConfigManager(cfg_inst)
|
|
|
|
await cfg_mgr.load_config(completion=completion)
|
|
|
|
|
|
|
|
return cfg_mgr
|