Skip to content

Commit

Permalink
chore: 适配遗器模块
Browse files Browse the repository at this point in the history
  • Loading branch information
weiduhuo committed Oct 11, 2023
1 parent 208c92d commit edd18fb
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import inspect
import os
import sys
import jsonschema
from pathlib import Path
from typing import Any, Union, get_type_hints

Expand All @@ -12,6 +13,9 @@
from .log import log

CONFIG_FILE_NAME = "config.json"
RELIC_FILE_NAME = "relics_set.json"
LOADOUT_FILE_NAME = "relics_loadout.json"
TEAM_FILE_NAME = "relics_team.json"

def normalize_file_path(filename):
# 尝试在当前目录下读取文件
Expand All @@ -30,34 +34,59 @@ def normalize_file_path(filename):
return None


def read_json_file(filename: str, path=False) -> dict:
def read_json_file(filename: str, path=False, schema:dict=None) -> dict:
"""
说明:
读取文件
参数:
:param filename: 文件名称
:param path: 是否返回路径
:param schema: json格式规范
"""
# 找到文件的绝对路径
file_path = normalize_file_path(filename)
if file_path:
with open(file_path, "rb") as f:
data = orjson.loads(f.read())
if schema:
try:
jsonschema.validate(data, schema)
except jsonschema.exceptions.ValidationError as e:
log.error(_(f"JSON 数据不符合格式规范: {e}"))
if path:
return data, file_path
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, value):
def modify_json_file(filename: str, key: str, value) -> dict:
"""
说明:
写入文件
写入文件,并返回文件字典
参数:
:param filename: 文件名称
:param key: key
Expand All @@ -72,7 +101,8 @@ def modify_json_file(filename: str, key, value):
except PermissionError as e:
import time
time.sleep(1)
modify_json_file(filename, key, value)
return modify_json_file(filename, key, value)
return data


def get_file(path, exclude=[], exclude_file=None, get_path=False, only_place=False) -> list[str]:
Expand Down

0 comments on commit edd18fb

Please sign in to comment.