perf: 初始化config对象时支持传递dict作为模板

This commit is contained in:
RockChinQ
2024-03-20 14:20:56 +08:00
parent f53070d8b6
commit fa823de6b0
3 changed files with 21 additions and 19 deletions
+13 -15
View File
@@ -8,15 +8,12 @@ from .. import model as file_model
class JSONConfigFile(file_model.ConfigFile): class JSONConfigFile(file_model.ConfigFile):
"""JSON配置文件""" """JSON配置文件"""
config_file_name: str = None def __init__(
"""配置文件名""" self, config_file_name: str, template_file_name: str = None, template_data: dict = None
) -> None:
template_file_name: str = None
"""模板文件名"""
def __init__(self, config_file_name: str, template_file_name: str) -> None:
self.config_file_name = config_file_name self.config_file_name = config_file_name
self.template_file_name = template_file_name self.template_file_name = template_file_name
self.template_data = template_data
def exists(self) -> bool: def exists(self) -> bool:
return os.path.exists(self.config_file_name) return os.path.exists(self.config_file_name)
@@ -29,23 +26,24 @@ class JSONConfigFile(file_model.ConfigFile):
if not self.exists(): if not self.exists():
await self.create() await self.create()
with open(self.config_file_name, 'r', encoding='utf-8') as f: if self.template_file_name is not None:
cfg = json.load(f) 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: with open(self.template_file_name, "r", encoding="utf-8") as f:
template_cfg = json.load(f) self.template_data = json.load(f)
for key in template_cfg: for key in self.template_data:
if key not in cfg: if key not in cfg:
cfg[key] = template_cfg[key] cfg[key] = self.template_data[key]
return cfg return cfg
async def save(self, cfg: dict): 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) json.dump(cfg, f, indent=4, ensure_ascii=False)
def save_sync(self, cfg: dict): def save_sync(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) json.dump(cfg, f, indent=4, ensure_ascii=False)
+3 -2
View File
@@ -43,11 +43,12 @@ async def load_python_module_config(config_name: str, template_name: str) -> Con
return cfg_mgr 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配置文件""" """加载JSON配置文件"""
cfg_inst = json_file.JSONConfigFile( cfg_inst = json_file.JSONConfigFile(
config_name, config_name,
template_name template_name,
template_data
) )
cfg_mgr = ConfigManager(cfg_inst) cfg_mgr = ConfigManager(cfg_inst)
+3
View File
@@ -10,6 +10,9 @@ class ConfigFile(metaclass=abc.ABCMeta):
template_file_name: str = None template_file_name: str = None
"""模板文件名""" """模板文件名"""
template_data: dict = None
"""模板数据"""
@abc.abstractmethod @abc.abstractmethod
def exists(self) -> bool: def exists(self) -> bool:
pass pass