diff --git a/utils/config.py b/utils/config.py index 105acfd7..30ce5346 100644 --- a/utils/config.py +++ b/utils/config.py @@ -59,25 +59,10 @@ def read_json_file(filename: str, path=False, schema:dict=None) -> dict: else: return data else: - if filename in [RELIC_FILE_NAME, LOADOUT_FILE_NAME, TEAM_FILE_NAME]: - init_json_file(filename) - return read_json_file(filename, path) if path: return {}, filename else: return {} - - -def init_json_file(filename: str): - """ - 说明: - 初始化json文件为空字典 - 参数: - :param filename: 文件名称 - """ - with open(filename, "wb") as f: - log.info(_(f"{filename} 文件初始化")) - f.write(orjson.dumps({}, option = orjson.OPT_PASSTHROUGH_DATETIME | orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_INDENT_2)) def modify_json_file(filename:str, key:str, value:Any) -> dict: @@ -92,9 +77,9 @@ def modify_json_file(filename:str, key:str, value:Any) -> dict: data: 修改后的json字典 """ # 先读,再写 - data, file_path = read_json_file(filename, path=True) + data = read_json_file(filename) data[key] = value - return rewrite_json_file(file_path, data) + return rewrite_json_file(filename, data) def rewrite_json_file(filename:str, data:dict) -> dict: @@ -108,6 +93,8 @@ def rewrite_json_file(filename:str, data:dict) -> dict: data: 修改后的json字典 """ file_path = normalize_file_path(filename) + if file_path is None: + file_path = filename # 原文件不存在,则新建 try: with open(file_path, "wb") as f: f.write(orjson.dumps(data, option=orjson.OPT_PASSTHROUGH_DATETIME | orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_INDENT_2))