From: W. Trevor King Date: Tue, 5 Feb 2013 17:18:21 +0000 (-0500) Subject: Initial ScriptQuestion framework X-Git-Tag: v0.1~59 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=ddef1ad7d8775c114a0b379e3075ef0a33563add;p=quizzer.git Initial ScriptQuestion framework --- 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