40 lines
1.0 KiB
Python
40 lines
1.0 KiB
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
|
|
|
|
# 確定是否是可迭代物件
|
|
iterable = False
|
|
try:
|
|
iter(PLATFORM_SETTING_MODEL.get(name)[0])
|
|
iterable = True
|
|
except:
|
|
iterable = False
|
|
|
|
# 如果可迭代,就把裡面每個元素都檢查型別
|
|
if iterable:
|
|
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 |