/bin/python /home/marco/so-un-bot/scripts/validate_questions.py

This commit is contained in:
Marco Realacci 2024-06-14 14:47:00 +02:00
parent 6988ff0549
commit 5b49781fa6

View file

@ -12,22 +12,25 @@ for filename in os.listdir("data/questions"):
data = json.loads(text) data = json.loads(text)
for q in data: for q in data:
if type(q["quest"]) is not str or type(q["image"]) is not str or type(q["answers"]) is not list or type(q["correct"]) is not int:
raise Exception(str(data.index(q)) + ": Some parameters are null, missing or their type is wrong.")
if q["quest"] == "" and q["image"] == "": if q["quest"] == "" and q["image"] == "":
raise Exception(str(data.index(q)) + ") Question's text and image cannot both be empty.") raise Exception(str(data.index(q)) + ") Question's text and image cannot both be empty.")
if q["quest"] is None or q["image"] is None or q["answers"] is None or q["correct"] is None:
raise Exception(str(data.index(q)) + ") Some parameters are null or missing.")
if len(q["answers"]) == 0: if len(q["answers"]) == 0:
raise Exception(str(data.index(q)) + ") Question has no answers.") raise Exception(str(data.index(q)) + ": Question has no answers.")
for a in q["answers"]: for a in q["answers"]:
if a["text"] == "" and a["image"] == "": if a["text"] == "" and a["image"] == "":
raise Exception(str(data.index(q)) + ") Answer's text and image cannot both be empty.") raise Exception(str(data.index(q)) + ": Answer's text and image cannot both be empty.")
if a["text"] is None or a["image"] is None: if type(a["text"]) is not str or type(a["image"]) is not str:
raise Exception(str(data.index(q)) + ") Some answer's parameters are null or missing.") raise Exception(str(data.index(q)) + ": Some answer's parameters are null, missing or their type is wrong.")
except Exception as e: except Exception as e:
logging.error(getattr(e, 'message', repr(e))) logging.error(getattr(e, 'message', repr(e)))
logging.fatal(filename + " is invalid. Aborting.") logging.fatal(filename + " is invalid. Aborting.")
exit(1)
logging.info("Parsing successful!") logging.info("Parsing successful!")
exit(0)