Show More
@@ -1,72 +1,72 b'' | |||
|
1 | 1 | """API for converting notebooks between versions. |
|
2 | 2 | |
|
3 | 3 | Authors: |
|
4 | 4 | |
|
5 | 5 | * Jonathan Frederic |
|
6 | 6 | """ |
|
7 | 7 | |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | # Copyright (C) 2013 The IPython Development Team |
|
10 | 10 | # |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | 12 | # the file COPYING, distributed as part of this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | import re |
|
20 | 20 | |
|
21 | 21 | from .reader import get_version, versions |
|
22 | 22 | |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | # Functions |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | |
|
27 | 27 | def convert(nb, to_version): |
|
28 | 28 | """Convert a notebook node object to a specific version. Assumes that |
|
29 | 29 | all the versions starting from 1 to the latest major X are implemented. |
|
30 | 30 | In other words, there should never be a case where v1 v2 v3 v5 exist without |
|
31 | 31 | a v4. Also assumes that all conversions can be made in one step increments |
|
32 | 32 | between major versions and ignores minor revisions. |
|
33 | 33 | |
|
34 | 34 | PARAMETERS: |
|
35 | 35 | ----------- |
|
36 | 36 | nb : NotebookNode |
|
37 | 37 | to_version : int |
|
38 | 38 | Major revision to convert the notebook to. Can either be an upgrade or |
|
39 | 39 | a downgrade. |
|
40 | 40 | """ |
|
41 | 41 | |
|
42 | 42 | # Get input notebook version. |
|
43 | 43 | (version, version_minor) = get_version(nb) |
|
44 | 44 | |
|
45 | 45 | # Check if destination is current version, if so return contents |
|
46 | 46 | if version == to_version: |
|
47 | 47 | return nb |
|
48 | 48 | |
|
49 | 49 | # If the version exist, try to convert to it one step at a time. |
|
50 | 50 | elif to_version in versions: |
|
51 | 51 | |
|
52 | 52 | # Get the the version that this recursion will convert to as a step |
|
53 | 53 | # closer to the final revision. Make sure the newer of the conversion |
|
54 | 54 | # functions is used to perform the conversion. |
|
55 | 55 | if to_version > version: |
|
56 | 56 | step_version = version + 1 |
|
57 | 57 | convert_function = versions[step_version].upgrade |
|
58 | 58 | else: |
|
59 | 59 | step_version = version - 1 |
|
60 | 60 | convert_function = versions[version].downgrade |
|
61 | 61 | |
|
62 | 62 | # Convert and make sure version changed during conversion. |
|
63 | 63 | converted = convert_function(nb) |
|
64 | 64 | if converted.get('nbformat', 1) == version: |
|
65 | 65 | raise Exception("Cannot convert notebook from v%d to v%d. Operation" \ |
|
66 | 66 | "failed silently." % (major, step_version)) |
|
67 | 67 | |
|
68 |
# Recur |
|
|
68 | # Recursively convert until target version is reached. | |
|
69 | 69 | return convert(converted, to_version) |
|
70 | 70 | else: |
|
71 | 71 | raise Exception("Cannot convert notebook to v%d because that " \ |
|
72 | 72 | "version doesn't exist" % (to_version)) |
@@ -1,69 +1,69 b'' | |||
|
1 | 1 | """ |
|
2 | 2 | Contains tests class for convert.py |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (C) 2013 The IPython Development Team |
|
6 | 6 | # |
|
7 | 7 | # Distributed under the terms of the BSD License. The full license is in |
|
8 | 8 | # the file COPYING, distributed as part of this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | from .base import TestsBase |
|
16 | 16 | |
|
17 | 17 | from ..convert import convert |
|
18 | 18 | from ..reader import read, get_version |
|
19 | 19 | from ..current import current_nbformat |
|
20 | 20 | |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | # Classes and functions |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 | 25 | class TestConvert(TestsBase): |
|
26 | 26 | |
|
27 | 27 | def test_downgrade(self): |
|
28 | 28 | """Do notebook downgrades work?""" |
|
29 | 29 | |
|
30 | 30 | # Open a version 3 notebook and attempt to downgrade it to version 2. |
|
31 | 31 | with self.fopen(u'test3.ipynb', u'r') as f: |
|
32 | 32 | nb = read(f) |
|
33 | 33 | nb = convert(nb, 2) |
|
34 | 34 | |
|
35 | 35 | # Check if downgrade was successful. |
|
36 | 36 | (major, minor) = get_version(nb) |
|
37 | 37 | self.assertEqual(major, 2) |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | def test_upgrade(self): |
|
41 | 41 | """Do notebook upgrades work?""" |
|
42 | 42 | |
|
43 | 43 | # Open a version 2 notebook and attempt to upgrade it to version 3. |
|
44 | 44 | with self.fopen(u'test2.ipynb', u'r') as f: |
|
45 | 45 | nb = read(f) |
|
46 | 46 | nb = convert(nb, 3) |
|
47 | 47 | |
|
48 | 48 | # Check if upgrade was successful. |
|
49 | 49 | (major, minor) = get_version(nb) |
|
50 | 50 | self.assertEqual(major, 3) |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | def test_open_current(self): |
|
54 |
"""Can an old notebook be opened and converted to the current verion |
|
|
55 | remembering the original version of the notebook?""" | |
|
54 | """Can an old notebook be opened and converted to the current version | |
|
55 | while remembering the original version of the notebook?""" | |
|
56 | 56 | |
|
57 | 57 | # Open a version 2 notebook and attempt to upgrade it to the current version |
|
58 | 58 | # while remembering it's version information. |
|
59 | 59 | with self.fopen(u'test2.ipynb', u'r') as f: |
|
60 | 60 | nb = read(f) |
|
61 | 61 | (original_major, original_minor) = get_version(nb) |
|
62 | 62 | nb = convert(nb, current_nbformat) |
|
63 | 63 | |
|
64 | 64 | # Check if upgrade was successful. |
|
65 | 65 | (major, minor) = get_version(nb) |
|
66 | 66 | self.assertEqual(major, current_nbformat) |
|
67 | 67 | |
|
68 | 68 | # Check if the original major revision was remembered. |
|
69 | 69 | self.assertEqual(original_major, 2) |
General Comments 0
You need to be logged in to leave comments.
Login now