From fa823de6b000ae8fdf79e74b0877343a8d21426f Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Wed, 20 Mar 2024 14:20:56 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=88=9D=E5=A7=8B=E5=8C=96config?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E6=97=B6=E6=94=AF=E6=8C=81=E4=BC=A0=E9=80=92?= =?UTF-8?q?dict=E4=BD=9C=E4=B8=BA=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/config/impls/json.py | 32 +++++++++++++++----------------- pkg/config/manager.py | 5 +++-- pkg/config/model.py | 3 +++ 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/pkg/config/impls/json.py b/pkg/config/impls/json.py index aecf686..a9aab6a 100644 --- a/pkg/config/impls/json.py +++ b/pkg/config/impls/json.py @@ -8,15 +8,12 @@ from .. import model as file_model class JSONConfigFile(file_model.ConfigFile): """JSON配置文件""" - config_file_name: str = None - """配置文件名""" - - template_file_name: str = None - """模板文件名""" - - def __init__(self, config_file_name: str, template_file_name: str) -> None: + 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) @@ -29,23 +26,24 @@ class JSONConfigFile(file_model.ConfigFile): if not self.exists(): await self.create() - with open(self.config_file_name, 'r', encoding='utf-8') as f: - cfg = json.load(f) + if self.template_file_name is not None: + with open(self.config_file_name, "r", encoding="utf-8") as f: + cfg = json.load(f) # 从模板文件中进行补全 - with open(self.template_file_name, 'r', encoding='utf-8') as f: - template_cfg = json.load(f) + with open(self.template_file_name, "r", encoding="utf-8") as f: + self.template_data = json.load(f) - for key in template_cfg: + for key in self.template_data: if key not in cfg: - cfg[key] = template_cfg[key] + 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: + with open(self.config_file_name, "w", encoding="utf-8") as f: json.dump(cfg, f, indent=4, ensure_ascii=False) def save_sync(self, cfg: dict): - with open(self.config_file_name, 'w', encoding='utf-8') as f: - json.dump(cfg, f, indent=4, ensure_ascii=False) \ No newline at end of file + with open(self.config_file_name, "w", encoding="utf-8") as f: + json.dump(cfg, f, indent=4, ensure_ascii=False) diff --git a/pkg/config/manager.py b/pkg/config/manager.py index aae827e..f9e93c8 100644 --- a/pkg/config/manager.py +++ b/pkg/config/manager.py @@ -43,11 +43,12 @@ async def load_python_module_config(config_name: str, template_name: str) -> Con return cfg_mgr -async def load_json_config(config_name: str, template_name: str) -> ConfigManager: +async def load_json_config(config_name: str, template_name: str=None, template_data: dict=None) -> ConfigManager: """加载JSON配置文件""" cfg_inst = json_file.JSONConfigFile( config_name, - template_name + template_name, + template_data ) cfg_mgr = ConfigManager(cfg_inst) diff --git a/pkg/config/model.py b/pkg/config/model.py index 9be6f0f..d209093 100644 --- a/pkg/config/model.py +++ b/pkg/config/model.py @@ -10,6 +10,9 @@ class ConfigFile(metaclass=abc.ABCMeta): template_file_name: str = None """模板文件名""" + template_data: dict = None + """模板数据""" + @abc.abstractmethod def exists(self) -> bool: pass