From: W. Trevor King Date: Thu, 7 Mar 2013 22:12:07 +0000 (-0500) Subject: quizzer: Add methods to upgrade data from v0.1 to v0.2 X-Git-Tag: v0.3~2 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=7cf160fde01776a7490cbefb2fc845813dc6f8e3;p=quizzer.git quizzer: Add methods to upgrade data from v0.1 to v0.2 These are both no-ops that only bump the version number, since the stored format didn't change between the two versions. --- diff --git a/quizzer/answerdb.py b/quizzer/answerdb.py index c8c802f..a47fe22 100644 --- a/quizzer/answerdb.py +++ b/quizzer/answerdb.py @@ -39,8 +39,13 @@ class AnswerDatabase (dict): data = _json.load(f) version = data.get('version', None) if version != __version__: - raise NotImplementedError('upgrade from {} to {}'.format( - version, __version__)) + try: + upgrader = getattr( + self, '_upgrade_from_{}'.format(version.replace('.', '_'))) + except AttributeError as e: + raise NotImplementedError('upgrade from {} to {}'.format( + version, __version__)) from e + data = upgrader(data) self.update(data['answers']) def save(self, **kwargs): @@ -77,3 +82,7 @@ class AnswerDatabase (dict): def get_never_correctly_answered(self, questions): return [q for q in questions if True not in [a['correct'] for a in self.get(q.id, [])]] + + def _upgrade_from_0_1(self, data): + data['version'] = __version__ + return data diff --git a/quizzer/quiz.py b/quizzer/quiz.py index 4a36f86..ae1702e 100644 --- a/quizzer/quiz.py +++ b/quizzer/quiz.py @@ -44,8 +44,13 @@ class Quiz (list): data = _json.load(f) version = data.get('version', None) if version != __version__: - raise NotImplementedError('upgrade from {} to {}'.format( - version, __version__)) + try: + upgrader = getattr( + self, '_upgrade_from_{}'.format(version.replace('.', '_'))) + except AttributeError as e: + raise NotImplementedError('upgrade from {} to {}'.format( + version, __version__)) from e + data = upgrader(data) self.copyright = data.get('copyright', None) self.introduction = data.get('introduction', None) for state in data['questions']: @@ -86,3 +91,7 @@ class Quiz (list): raise KeyError(id) raise NotImplementedError( 'multiple questions with one ID: {}'.format(matches)) + + def _upgrade_from_0_1(self, data): + data['version'] = __version__ + return data