Run update-copyright.py
[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, questions=None, path=None, encoding=None):
26         if questions is None:
27             questions = []
28         super(Quiz, self).__init__(questions)
29         self.path = path
30         self.encoding = encoding
31
32     def _open(self, mode='r', path=None, encoding=None):
33         if path:
34             self.path = path
35         if encoding:
36             self.encoding = encoding
37         return _codecs.open(self.path, mode, self.encoding)
38
39     def load(self, **kwargs):
40         with self._open(mode='r', **kwargs) as f:
41             data = _json.load(f)
42         version = data.get('version', None)
43         if version != __version__:
44             raise NotImplementedError('upgrade from {} to {}'.format(
45                     version, __version__))
46         for state in data['questions']:
47             question_class_name = state.pop('class', 'Question')
48             question_class = _question.QUESTION_CLASS[question_class_name]
49             q = question_class()
50             q.__setstate__(state)
51             self.append(q)
52
53     def save(self, **kwargs):
54         questions = []
55         for question in self:
56             state = question.__getstate__()
57             state['class'] = type(question).__name__
58         data = {
59             'version': __version__,
60             'questions': questions,
61             }
62         with self._open(mode='w', **kwargs) as f:
63             _json.dump(
64                 data, f, indent=2, separators=(',', ': '), sort_keys=True)
65             f.write('\n')
66
67     def leaf_questions(self):
68         "Questions that are not dependencies of other question"
69         dependents = set()
70         for question in self:
71             dependents.update(question.dependencies)
72         return [q for q in self if q.id not in dependents]
73
74     def get(self, id=None):
75         matches = [q for q in self if q.id == id]
76         if len(matches) == 1:
77             return matches[0]
78         elif len(matches) == 0:
79             raise KeyError(id)
80         raise NotImplementedError(
81             'multiple questions with one ID: {}'.format(matches))