##// END OF EJS Templates
fill missing metadata when upgrading v2->v3
MinRK -
Show More
@@ -1,88 +1,91 b''
1 """Code for converting notebooks to and from the v2 format."""
1 """Code for converting notebooks to and from the v2 format."""
2
2
3 # Copyright (c) IPython Development Team.
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 from .nbbase import (
6 from .nbbase import (
7 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
7 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
8 nbformat, nbformat_minor
8 nbformat, nbformat_minor
9 )
9 )
10
10
11 from IPython.nbformat import v2
11 from IPython.nbformat import v2
12
12
13 def _unbytes(obj):
13 def _unbytes(obj):
14 """There should be no bytes objects in a notebook
14 """There should be no bytes objects in a notebook
15
15
16 v2 stores png/jpeg as b64 ascii bytes
16 v2 stores png/jpeg as b64 ascii bytes
17 """
17 """
18 if isinstance(obj, dict):
18 if isinstance(obj, dict):
19 for k,v in obj.items():
19 for k,v in obj.items():
20 obj[k] = _unbytes(v)
20 obj[k] = _unbytes(v)
21 elif isinstance(obj, list):
21 elif isinstance(obj, list):
22 for i,v in enumerate(obj):
22 for i,v in enumerate(obj):
23 obj[i] = _unbytes(v)
23 obj[i] = _unbytes(v)
24 elif isinstance(obj, bytes):
24 elif isinstance(obj, bytes):
25 # only valid bytes are b64-encoded ascii
25 # only valid bytes are b64-encoded ascii
26 obj = obj.decode('ascii')
26 obj = obj.decode('ascii')
27 return obj
27 return obj
28
28
29 def upgrade(nb, from_version=2, from_minor=0):
29 def upgrade(nb, from_version=2, from_minor=0):
30 """Convert a notebook to v3.
30 """Convert a notebook to v3.
31
31
32 Parameters
32 Parameters
33 ----------
33 ----------
34 nb : NotebookNode
34 nb : NotebookNode
35 The Python representation of the notebook to convert.
35 The Python representation of the notebook to convert.
36 from_version : int
36 from_version : int
37 The original version of the notebook to convert.
37 The original version of the notebook to convert.
38 from_minor : int
38 from_minor : int
39 The original minor version of the notebook to convert (only relevant for v >= 3).
39 The original minor version of the notebook to convert (only relevant for v >= 3).
40 """
40 """
41 if from_version == 2:
41 if from_version == 2:
42 # Mark the original nbformat so consumers know it has been converted.
42 # Mark the original nbformat so consumers know it has been converted.
43 nb.nbformat = nbformat
43 nb.nbformat = nbformat
44 nb.nbformat_minor = nbformat_minor
44 nb.nbformat_minor = nbformat_minor
45
45
46 nb.orig_nbformat = 2
46 nb.orig_nbformat = 2
47 nb = _unbytes(nb)
47 nb = _unbytes(nb)
48 for ws in nb['worksheets']:
49 for cell in ws['cells']:
50 cell.setdefault('metadata', {})
48 return nb
51 return nb
49 elif from_version == 3:
52 elif from_version == 3:
50 if from_minor != nbformat_minor:
53 if from_minor != nbformat_minor:
51 nb.orig_nbformat_minor = from_minor
54 nb.orig_nbformat_minor = from_minor
52 nb.nbformat_minor = nbformat_minor
55 nb.nbformat_minor = nbformat_minor
53 return nb
56 return nb
54 else:
57 else:
55 raise ValueError('Cannot convert a notebook directly from v%s to v3. ' \
58 raise ValueError('Cannot convert a notebook directly from v%s to v3. ' \
56 'Try using the IPython.nbformat.convert module.' % from_version)
59 'Try using the IPython.nbformat.convert module.' % from_version)
57
60
58
61
59 def heading_to_md(cell):
62 def heading_to_md(cell):
60 """turn heading cell into corresponding markdown"""
63 """turn heading cell into corresponding markdown"""
61 cell.cell_type = "markdown"
64 cell.cell_type = "markdown"
62 level = cell.pop('level', 1)
65 level = cell.pop('level', 1)
63 cell.source = '#'*level + ' ' + cell.source
66 cell.source = '#'*level + ' ' + cell.source
64
67
65
68
66 def raw_to_md(cell):
69 def raw_to_md(cell):
67 """let raw passthrough as markdown"""
70 """let raw passthrough as markdown"""
68 cell.cell_type = "markdown"
71 cell.cell_type = "markdown"
69
72
70
73
71 def downgrade(nb):
74 def downgrade(nb):
72 """Convert a v3 notebook to v2.
75 """Convert a v3 notebook to v2.
73
76
74 Parameters
77 Parameters
75 ----------
78 ----------
76 nb : NotebookNode
79 nb : NotebookNode
77 The Python representation of the notebook to convert.
80 The Python representation of the notebook to convert.
78 """
81 """
79 if nb.nbformat != 3:
82 if nb.nbformat != 3:
80 return nb
83 return nb
81 nb.nbformat = 2
84 nb.nbformat = 2
82 for ws in nb.worksheets:
85 for ws in nb.worksheets:
83 for cell in ws.cells:
86 for cell in ws.cells:
84 if cell.cell_type == 'heading':
87 if cell.cell_type == 'heading':
85 heading_to_md(cell)
88 heading_to_md(cell)
86 elif cell.cell_type == 'raw':
89 elif cell.cell_type == 'raw':
87 raw_to_md(cell)
90 raw_to_md(cell)
88 return nb No newline at end of file
91 return nb
General Comments 0
You need to be logged in to leave comments. Login now