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, introduction=None, questions=None, path=None,
27 self.introduction = introduction
30 super(Quiz, self).__init__(questions)
32 self.encoding = encoding
34 def _open(self, mode='r', path=None, encoding=None):
38 self.encoding = encoding
39 return _codecs.open(self.path, mode, self.encoding)
41 def load(self, **kwargs):
42 with self._open(mode='r', **kwargs) as 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]
56 def save(self, **kwargs):
59 state = question.__getstate__()
60 state['class'] = type(question).__name__
62 'version': __version__,
63 'introduction': self.introduction,
64 'questions': questions,
66 with self._open(mode='w', **kwargs) as f:
68 data, f, indent=2, separators=(',', ': '), sort_keys=True)
71 def leaf_questions(self):
72 "Questions that are not dependencies of other question"
75 dependents.update(question.dependencies)
76 return [q for q in self if q.id not in dependents]
78 def get(self, id=None):
79 matches = [q for q in self if q.id == id]
82 elif len(matches) == 0:
84 raise NotImplementedError(
85 'multiple questions with one ID: {}'.format(matches))