From cfb64c0e3c4bfb53190597d3862964da91e01693 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 13 Mar 2013 17:23:26 -0400 Subject: [PATCH] cli: Teach quizzer.cli.main() the --ui option This uses the new quizzer.ui.INTERFACES and quizzer.ui.get_ui() to select from among the available interfaces (currently just the command line interface). --- quizzer/cli.py | 8 ++++++-- quizzer/ui/__init__.py | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/quizzer/cli.py b/quizzer/cli.py index e0d0ced..0115f4b 100644 --- a/quizzer/cli.py +++ b/quizzer/cli.py @@ -21,7 +21,7 @@ from . import __doc__ as _module_doc 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(): @@ -51,6 +51,9 @@ 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') @@ -85,7 +88,8 @@ def main(): 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: diff --git a/quizzer/ui/__init__.py b/quizzer/ui/__init__.py index 0db903c..333c6c0 100644 --- a/quizzer/ui/__init__.py +++ b/quizzer/ui/__init__.py @@ -14,9 +14,14 @@ # You should have received a copy of the GNU General Public License along with # quizzer. If not, see . +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): @@ -44,3 +49,21 @@ class UserInterface (object): 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') + + """ + 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) -- 2.26.2