From d1a06f47301eaa0ae9d6030f9b1a955d0ef66bf0 Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Sat, 24 Aug 2024 20:54:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0yaml=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/config/impls/yaml.py | 59 ++++++++++++++++++++++++++++++++++++++++ pkg/config/manager.py | 48 +++++++++++++++++++++++++++++--- 2 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 pkg/config/impls/yaml.py diff --git a/pkg/config/impls/yaml.py b/pkg/config/impls/yaml.py new file mode 100644 index 0000000..f451800 --- /dev/null +++ b/pkg/config/impls/yaml.py @@ -0,0 +1,59 @@ +import os +import shutil +import yaml + +from .. import model as file_model + + +class YAMLConfigFile(file_model.ConfigFile): + """YAML配置文件""" + + def __init__( + self, config_file_name: str, template_file_name: str = None, template_data: dict = None + ) -> None: + self.config_file_name = config_file_name + self.template_file_name = template_file_name + self.template_data = template_data + + def exists(self) -> bool: + return os.path.exists(self.config_file_name) + + async def create(self): + if self.template_file_name is not None: + shutil.copyfile(self.template_file_name, self.config_file_name) + elif self.template_data is not None: + with open(self.config_file_name, "w", encoding="utf-8") as f: + yaml.dump(self.template_data, f, indent=4, allow_unicode=True) + else: + raise ValueError("template_file_name or template_data must be provided") + + async def load(self, completion: bool=True) -> dict: + + if not self.exists(): + await self.create() + + if self.template_file_name is not None: + with open(self.template_file_name, "r", encoding="utf-8") as f: + self.template_data = yaml.load(f, Loader=yaml.FullLoader) + + with open(self.config_file_name, "r", encoding="utf-8") as f: + try: + cfg = yaml.load(f, Loader=yaml.FullLoader) + except yaml.YAMLError as e: + raise Exception(f"配置文件 {self.config_file_name} 语法错误: {e}") + + if completion: + + for key in self.template_data: + if key not in cfg: + cfg[key] = self.template_data[key] + + return cfg + + async def save(self, cfg: dict): + with open(self.config_file_name, "w", encoding="utf-8") as f: + yaml.dump(cfg, f, indent=4, allow_unicode=True) + + def save_sync(self, cfg: dict): + with open(self.config_file_name, "w", encoding="utf-8") as f: + yaml.dump(cfg, f, indent=4, allow_unicode=True) \ No newline at end of file diff --git a/pkg/config/manager.py b/pkg/config/manager.py index 7983407..88ed652 100644 --- a/pkg/config/manager.py +++ b/pkg/config/manager.py @@ -1,7 +1,7 @@ from __future__ import annotations from . import model as file_model -from .impls import pymodule, json as json_file +from .impls import pymodule, json as json_file, yaml as yaml_file managers: ConfigManager = [] @@ -31,7 +31,16 @@ class ConfigManager: async def load_python_module_config(config_name: str, template_name: str, completion: bool=True) -> ConfigManager: - """加载Python模块配置文件""" + """加载Python模块配置文件 + + Args: + config_name (str): 配置文件名 + template_name (str): 模板文件名 + completion (bool): 是否自动补全内存中的配置文件 + + Returns: + ConfigManager: 配置文件管理器 + """ cfg_inst = pymodule.PythonModuleConfigFile( config_name, template_name @@ -44,7 +53,14 @@ async def load_python_module_config(config_name: str, template_name: str, comple async def load_json_config(config_name: str, template_name: str=None, template_data: dict=None, completion: bool=True) -> ConfigManager: - """加载JSON配置文件""" + """加载JSON配置文件 + + Args: + config_name (str): 配置文件名 + template_name (str): 模板文件名 + template_data (dict): 模板数据 + completion (bool): 是否自动补全内存中的配置文件 + """ cfg_inst = json_file.JSONConfigFile( config_name, template_name, @@ -54,4 +70,28 @@ async def load_json_config(config_name: str, template_name: str=None, template_d cfg_mgr = ConfigManager(cfg_inst) await cfg_mgr.load_config(completion=completion) - return cfg_mgr \ No newline at end of file + 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