From 7b3d7e7bd69bbf58618278bb9717c335a76c9be3 Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Sat, 30 Mar 2024 19:01:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20json=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=9A=84=E5=8A=A0=E8=BD=BD=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/config/impls/json.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/config/impls/json.py b/pkg/config/impls/json.py index a9aab6a9..754bfa58 100644 --- a/pkg/config/impls/json.py +++ b/pkg/config/impls/json.py @@ -19,7 +19,13 @@ class JSONConfigFile(file_model.ConfigFile): return os.path.exists(self.config_file_name) async def create(self): - shutil.copyfile(self.template_file_name, self.config_file_name) + 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: + json.dump(self.template_data, f, indent=4, ensure_ascii=False) + else: + raise ValueError("template_file_name or template_data must be provided") async def load(self) -> dict: @@ -27,12 +33,11 @@ class JSONConfigFile(file_model.ConfigFile): await self.create() 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: + self.template_data = json.load(f) - # 从模板文件中进行补全 - with open(self.template_file_name, "r", encoding="utf-8") as f: - self.template_data = json.load(f) + with open(self.config_file_name, "r", encoding="utf-8") as f: + cfg = json.load(f) for key in self.template_data: if key not in cfg: