quiz: Add Quiz.introduction for an optional intro message
[quizzer.git] / quizzer / quiz.py
1 # Copyright (C) 2013 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of quizzer.
4 #
5 # quizzer is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # quizzer is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # quizzer.  If not, see <http://www.gnu.org/licenses/>.
16
17 import codecs as _codecs
18 import json as _json
19
20 from . import __version__
21 from . import question as _question
22
23
24 class Quiz (list):
25     def __init__(self, introduction=None, questions=None, path=None,
26                  encoding=None):
27         self.introduction = introduction
28         if questions is None:
29             questions = []
30         super(Quiz, self).__init__(questions)
31         self.path = path
32         self.encoding = encoding
33
34     def _open(self, mode='r', path=None, encoding=None):
35         if path:
36             self.path = path
37         if encoding:
38             self.encoding = encoding
39         return _codecs.open(self.path, mode, self.encoding)
40
41     def load(self, **kwargs):
42         with self._open(mode='r', **kwargs) as f:
43             data = _json.load(f)
44         version = data.get('version', None)
45         if version != __version__:
46             raise NotImplementedError('upgrade from {} to {}'.format(
47                     version, __version__))
48         self.introduction = data.get('introduction', None)
49         for state in data['questions']:
50             question_class_name = state.pop('class', 'Question')
51             question_class = _question.QUESTION_CLASS[question_class_name]
52             q = question_class()
53             q.__setstate__(state)
54             self.append(q)
55
56     def save(self, **kwargs):
57         questions = []
58         for question in self:
59             state = question.__getstate__()
60             state['class'] = type(question).__name__
61         data = {
62             'version': __version__,
63             'introduction': self.introduction,
64             'questions': questions,
65             }
66         with self._open(mode='w', **kwargs) as f:
67             _json.dump(
68                 data, f, indent=2, separators=(',', ': '), sort_keys=True)
69             f.write('\n')
70
71     def leaf_questions(self):
72         "Questions that are not dependencies of other question"
73         dependents = set()
74         for question in self:
75             dependents.update(question.dependencies)
76         return [q for q in self if q.id not in dependents]
77
78     def get(self, id=None):
79         matches = [q for q in self if q.id == id]
80         if len(matches) == 1:
81             return matches[0]
82         elif len(matches) == 0:
83             raise KeyError(id)
84         raise NotImplementedError(
85             'multiple questions with one ID: {}'.format(matches))