From ddef1ad7d8775c114a0b379e3075ef0a33563add Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 5 Feb 2013 12:18:21 -0500 Subject: [PATCH] Initial ScriptQuestion framework --- quiz.json | 10 ++++++++++ quizzer/question.py | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/quiz.json b/quiz.json index 894105e..9f1c902 100644 --- a/quiz.json +++ b/quiz.json @@ -30,6 +30,16 @@ "dependencies": [ "What is your quest?" ] + }, + { + "class": "ScriptQuestion", + "interpreter": "sh", + "prompt": "list all the files in the current directory", + "answer": "ls", + "setup": [ + "touch file-1 file-2 file-3" + ], + "help": "http://pubs.opengroup.org/onlinepubs/009696699/idx/utilities.html" } ] } diff --git a/quizzer/question.py b/quizzer/question.py index 7cebd30..1783d9c 100644 --- a/quizzer/question.py +++ b/quizzer/question.py @@ -50,6 +50,26 @@ class NormalizedStringQuestion (Question): return self.normalize(answer) == self.normalize(self.answer) +class ScriptQuestion (Question): + _state_attributes = Question._state_attributes + [ + 'interpreter', + 'setup', + 'teardown', + ] + + def __setstate__(self, state): + if 'interpreter' not in state: + state['interpreter'] = 'sh' # POSIX-compatible shell + for key in ['setup', 'teardown']: + if key not in state: + state[key] = [] + super(ScriptQuestion, self).__setstate__(state) + + def check(self, answer): + script = '\n'.join(self.setup + [answer] + self.teardown) + raise ValueError(script) + + for name,obj in list(locals().items()): if name.startswith('_'): continue -- 2.26.2