def buildSession(msg, categ = None, nbQuest = 10, channel = False): if Question.QUESTIONS is None: Question.QUESTIONS = xmlparser.parse_file(CONF.index["main"]["url"]) Question.QUESTIONS.setIndex("xml:id") Course.COURSES = xmlparser.parse_file(CONF.index["courses"]["url"]) Course.COURSES.setIndex("xml:id") User.USERS = xmlparser.parse_file(CONF.index["users"]["url"]) User.USERS.setIndex("xml:id") #Remove no validated questions keys = list() for k in Question.QUESTIONS.index.keys(): keys.append(k) for ques in keys: if Question.QUESTIONS.index[ques]["validated"] != "1" or Question.QUESTIONS.index[ques]["reported"] == "1": del Question.QUESTIONS.index[ques] #Apply filter QS = list() if categ is not None and len(categ) > 0: #Find course id corresponding to categ courses = list() for c in Course.COURSES.childs: if c["code"] in categ: courses.append(c["xml:id"]) #Keep only questions matching course or branch for q in Question.QUESTIONS.index.keys(): if (Question.QUESTIONS.index[q]["branch"] is not None and Question.QUESTIONS.index[q]["branch"].find(categ)) or Question.QUESTIONS.index[q]["course"] in courses: QS.append(q) else: for q in Question.QUESTIONS.index.keys(): QS.append(q) nbQuest = min(nbQuest, len(QS)) if channel: sess = Session.Session(msg.srv, msg.channel, msg.channel) else: sess = Session.Session(msg.srv, msg.channel, msg.sender) maxQuest = len(QS) - 1 for i in range(0, nbQuest): while True: q = QS[random.randint(0, maxQuest)] if sess.addQuestion(q): break if channel: Session.SESSIONS[msg.channel] = sess else: Session.SESSIONS[msg.sender] = sess
def __init__(self, filename): self.questions = xmlparser.parse_file(filename) self.questions.setIndex("xml:id")