from . import __version__
from . import answerdb as _answerdb
from . import quiz as _quiz
-from .ui import cli as _cli
+from . import ui as _ui
def main():
'--questions', action='store_const', const=True, default=False,
help=('instead of running the quiz, '
'print a list of questions on the stack'))
+ parser.add_argument(
+ '-u', '--ui', choices=_ui.INTERFACES, default=_ui.INTERFACES[0],
+ help='select a user interface')
parser.add_argument(
'quiz', metavar='QUIZ',
help='path to a quiz file')
print(q.format_prompt())
print()
return
- ui = _cli.CommandLineInterface(quiz=quiz, answers=answers, stack=stack)
+ ui_class = _ui.get_ui(args.ui)
+ ui = ui_class(quiz=quiz, answers=answers, stack=stack)
try:
ui.run()
finally:
# You should have received a copy of the GNU General Public License along with
# quizzer. If not, see <http://www.gnu.org/licenses/>.
+import importlib as _importlib
+
from .. import answerdb as _answerdb
+INTERFACES = ['cli']
+
+
class UserInterface (object):
"Give a quiz over a generic user interface"
def __init__(self, quiz=None, answers=None, stack=None):
for qid in reversed(question.dependencies):
self.stack.insert(0, self.quiz.get(id=qid))
return correct
+
+
+def get_ui(name):
+ """Get the UserInterface subclass from a UI submodule
+
+ >>> get_ui('cli')
+ <class 'quizzer.ui.cli.CommandLineInterface'>
+ """
+ module = _importlib.import_module('{}.{}'.format(__name__, name))
+ for name in dir(module):
+ obj = getattr(module, name)
+ try:
+ subclass = issubclass(obj, UserInterface)
+ except TypeError as e: # obj is not a class
+ continue
+ if subclass:
+ return obj
+ raise ValueError(name)