Skip to content

Commit

Permalink
Merge pull request #746 from weiduhuo/debug
Browse files Browse the repository at this point in the history
fix: 修复'config.json'文件未成功初始化
  • Loading branch information
Night-stars-1 authored Nov 3, 2023
2 parents 8f0316d + bf8c21d commit 5c3d7e3
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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))
Expand Down

0 comments on commit 5c3d7e3

Please sign in to comment.