niming_backend/utils/setting_loader.py

35 lines
888 B
Python
Raw Normal View History

2024-11-14 13:03:00 +08:00
import json
2024-11-18 02:47:25 +08:00
from utils.platform_consts import platform_setting_model
2024-11-14 13:03:00 +08:00
def loadset():
with open("./settings.json", "r", encoding = "utf-8") as f:
d = json.load(f)
2024-11-18 02:47:25 +08:00
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)
2024-11-14 13:03:00 +08:00
return d