1 # Copyright (C) 2013 W. Trevor King <wking@tremily.us>
3 # This file is part of quizzer.
5 # quizzer is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 3 of the License, or (at your option) any later
10 # quizzer is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # quizzer. If not, see <http://www.gnu.org/licenses/>.
17 import importlib as _importlib
19 from .. import answerdb as _answerdb
25 class UserInterface (object):
26 "Give a quiz over a generic user interface"
27 def __init__(self, quiz=None, answers=None, stack=None):
30 answers = _answerdb.AnswerDatabase()
31 self.answers = answers
33 stack = self.answers.get_never_correctly_answered(
34 questions=quiz.leaf_questions())
38 raise NotImplementedError()
40 def get_question(self):
42 return self.stack.pop(0)
44 def process_answer(self, question, answer, **kwargs):
45 correct,details = question.check(answer=answer, **kwargs)
46 self.answers.add(question=question, answer=answer, correct=correct)
48 self.stack.insert(0, question)
49 for qid in reversed(question.dependencies):
50 self.stack.insert(0, self.quiz.get(id=qid))
51 return (correct, details)
55 """Get the UserInterface subclass from a UI submodule
58 <class 'quizzer.ui.cli.CommandLineInterface'>
60 module = _importlib.import_module('{}.{}'.format(__name__, name))
61 for name in dir(module):
62 obj = getattr(module, name)
64 subclass = issubclass(obj, UserInterface)
65 except TypeError as e: # obj is not a class
69 raise ValueError(name)