Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复'config.json'文件未成功初始化 #746

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading