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