niming_backend/utils/setting_loader.py
2024-12-12 03:30:51 +00:00

32 lines
878 B
Python

import json
from utils.platform_consts import PLATFORM_SETTING_MODEL
def loadset():
with open("./settings.json", "r", encoding = "utf-8") as f:
d = json.load(f)
return d
def typechecker(name:str, value):
# 型別驗證
if not(name in PLATFORM_SETTING_MODEL.keys()) or not(isinstance(value, PLATFORM_SETTING_MODEL.get(name)[0])):
return 0
# 如果是list: 檢查內容
if PLATFORM_SETTING_MODEL.get(name)[0] == list:
for v in value:
if not(isinstance(v, PLATFORM_SETTING_MODEL.get(name)[1])): return 0
return 1
def writeset(name:str, value):
# 驗證寫入資料型別
if not typechecker(name, value): return 0
# 寫入
d:dict = loadset()
d[name] = value
with open("./settings.json", "w", encoding = "utf-8") as f:
json.dump(d, f, ensure_ascii=False)
return d