quizzer: Add methods to upgrade data from v0.1 to v0.2
authorW. Trevor King <wking@tremily.us>
Thu, 7 Mar 2013 22:12:07 +0000 (17:12 -0500)
committerW. Trevor King <wking@tremily.us>
Thu, 7 Mar 2013 22:13:44 +0000 (17:13 -0500)
These are both no-ops that only bump the version number, since the
stored format didn't change between the two versions.

quizzer/answerdb.py
quizzer/quiz.py

index c8c802f9bc3606d012823cd3a7a8d53e469a7f7e..a47fe223e735fe2d091ea22329da8000ed52dc07 100644 (file)
@@ -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
index 4a36f86c9856f4995a104d2fe1c9d6a9c1def514..ae1702eed427ff7e46d5738f4d2c903629f433d0 100644 (file)
@@ -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