1 # Copyright (C) 2013 W. Trevor King <wking@tremily.us>
3 # This file is part of quizzer.
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
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.
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/>.
17 import codecs as _codecs
20 from . import __version__
21 from . import question as _question
25 def __init__(self, questions=None, path=None, encoding=None):
28 super(Quiz, self).__init__(questions)
30 self.encoding = encoding
32 def _open(self, mode='r', path=None, encoding=None):
36 self.encoding = encoding
37 return _codecs.open(self.path, mode, self.encoding)
39 def load(self, **kwargs):
40 with self._open(mode='r', **kwargs) as 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]
53 def save(self, **kwargs):
56 state = question.__getstate__()
57 state['class'] = type(question).__name__
59 'version': __version__,
60 'questions': questions,
62 with self._open(mode='w', **kwargs) as f:
64 data, f, indent=2, separators=(',', ': '), sort_keys=True)
67 def leaf_questions(self):
68 "Questions that are not dependencies of other question"
71 dependents.update(question.dependencies)
72 return [q for q in self if q.id not in dependents]
74 def get(self, id=None):
75 matches = [q for q in self if q.id == id]
78 elif len(matches) == 0:
80 raise NotImplementedError(
81 'multiple questions with one ID: {}'.format(matches))