##// END OF EJS Templates
Merge pull request #5732 from jdfreder/quickfix...
Thomas Kluyver -
r16459:29cdaa72 merge
parent child Browse files
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." % (version, step_version))
67
67
68 # Recursively 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))
General Comments 0
You need to be logged in to leave comments. Login now