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