so-un-bot/scripts/validate_questions.py

36 lines
1.5 KiB
Python
Raw Normal View History

2024-06-14 14:33:43 +02:00
import os
import json
import logging
logging.basicConfig(level=logging.DEBUG)
for filename in os.listdir("data/questions"):
logging.info("Analyzing " + filename)
with open(os.path.join("data/questions", filename), 'r') as f:
text = f.read()
try:
data = json.loads(text)
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.")
2024-06-14 14:33:43 +02:00
if q["quest"] == "" and q["image"] == "":
raise Exception(str(data.index(q)) + ") Question's text and image cannot both be empty.")
if len(q["answers"]) == 0:
raise Exception(str(data.index(q)) + ": Question has no answers.")
2024-06-14 14:33:43 +02:00
for a in q["answers"]:
if a["text"] == "" and a["image"] == "":
raise Exception(str(data.index(q)) + ": Answer's text and image cannot both be empty.")
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, missing or their type is wrong.")
2024-06-14 14:33:43 +02:00
except Exception as e:
logging.error(getattr(e, 'message', repr(e)))
logging.fatal(filename + " is invalid. Aborting.")
exit(1)
2024-06-14 14:33:43 +02:00
logging.info("Parsing successful!")
exit(0)