Initial ScriptQuestion framework
authorW. Trevor King <wking@tremily.us>
Tue, 5 Feb 2013 17:18:21 +0000 (12:18 -0500)
committerW. Trevor King <wking@tremily.us>
Tue, 5 Feb 2013 17:18:21 +0000 (12:18 -0500)
quiz.json
quizzer/question.py

index 894105e627cea9702fc02b14d0d47a9b385baafa..9f1c9020ea3ff2ac2608946ddecca84500fe70f6 100644 (file)
--- a/quiz.json
+++ b/quiz.json
                        "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"
                }
        ]
 }
index 7cebd3013c6e92722879f7bef22e3b3864f3a3f6..1783d9c9234c6401210ba821083ffa0ee6c7920a 100644 (file)
@@ -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