Show More
@@ -0,0 +1,68 b'' | |||||
|
1 | """API for converting notebooks between versions. | |||
|
2 | ||||
|
3 | Authors: | |||
|
4 | ||||
|
5 | * Jonathan Frederic | |||
|
6 | """ | |||
|
7 | ||||
|
8 | #----------------------------------------------------------------------------- | |||
|
9 | # Copyright (C) 2008-2013 The IPython Development Team | |||
|
10 | # | |||
|
11 | # Distributed under the terms of the BSD License. The full license is in | |||
|
12 | # the file COPYING, distributed as part of this software. | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | #----------------------------------------------------------------------------- | |||
|
16 | # Imports | |||
|
17 | #----------------------------------------------------------------------------- | |||
|
18 | ||||
|
19 | import re | |||
|
20 | ||||
|
21 | import IPython.nbformat as nbformat | |||
|
22 | ||||
|
23 | #----------------------------------------------------------------------------- | |||
|
24 | # Constants | |||
|
25 | #----------------------------------------------------------------------------- | |||
|
26 | ||||
|
27 | # Get the convert modules for each major revision. | |||
|
28 | VERSION_REGEX = re.compile("v[0-9]+") | |||
|
29 | version_modules = {} | |||
|
30 | for module in dir(nbformat): | |||
|
31 | if VERSION_REGEX.match(module): | |||
|
32 | version_modules[int(module[1:])] = eval('nbformat.' + module + '.convert') | |||
|
33 | ||||
|
34 | # Get the number of minor versions in each major version and create an ordered | |||
|
35 | # list. | |||
|
36 | versions = [] | |||
|
37 | for major in version_modules.keys().sort(): | |||
|
38 | current_minor = 0 | |||
|
39 | if hasattr(version_modules[major], 'nbformat_minor'): | |||
|
40 | current_minor = version_modules[major].nbformat_minor | |||
|
41 | ||||
|
42 | for minor in range(0, current_minor + 1): | |||
|
43 | versions.append((major, minor)) | |||
|
44 | ||||
|
45 | #----------------------------------------------------------------------------- | |||
|
46 | # Functions | |||
|
47 | #----------------------------------------------------------------------------- | |||
|
48 | ||||
|
49 | def convert(nb, to_major, to_minor=0): | |||
|
50 | """Convert a notebook node object to a specific version""" | |||
|
51 | ||||
|
52 | # Get input notebook version. | |||
|
53 | major = nb.get('nbformat', 1) | |||
|
54 | minor = nb.get('nbformat_minor', 0) # v3+ | |||
|
55 | ||||
|
56 | # Check if destination is current version, if so return contents | |||
|
57 | if to_major == major and to_minor == minor: | |||
|
58 | return nb | |||
|
59 | elif (to_major, to_minor) in versions: | |||
|
60 | index = versions.indexof((major, minor)) | |||
|
61 | ||||
|
62 | if to_major > major or (to_major == major and to_minor > minor): | |||
|
63 | to_index = index + 1 | |||
|
64 | else: | |||
|
65 | to_index = index - 1 | |||
|
66 | ||||
|
67 | else: | |||
|
68 | raise Exception("Cannot convert notebook to v%d.%d because that version doesn't exist" % (to_major, to_minor)) No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now