niming_backend/utils/setting_loader.py

35 lines
888 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
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