##// END OF EJS Templates
add `nbformat.writes(version=X)` for downgrade...
MinRK -
Show More
@@ -101,7 +101,7 b' def writes_py(nb, **kwargs):'
101 101 # High level API
102 102
103 103
104 def reads(s, format, **kwargs):
104 def reads(s, format='DEPRECATED', version=current_nbformat, **kwargs):
105 105 """Read a notebook from a string and return the NotebookNode object.
106 106
107 107 This function properly handles notebooks of any version. The notebook
@@ -111,24 +111,22 b' def reads(s, format, **kwargs):'
111 111 ----------
112 112 s : unicode
113 113 The raw unicode string to read the notebook from.
114 format : (u'json', u'ipynb', u'py')
115 The format that the string is in.
116 114
117 115 Returns
118 116 -------
119 117 nb : NotebookNode
120 118 The notebook that was read.
121 119 """
122 format = unicode_type(format)
123 if format == u'json' or format == u'ipynb':
124 return reads_json(s, **kwargs)
125 elif format == u'py':
126 return reads_py(s, **kwargs)
127 else:
128 raise NBFormatError('Unsupported format: %s' % format)
120 nb = versions[version].reads_json(s, **kwargs)
121 nb = convert(nb, version)
122 try:
123 validate(nb)
124 except ValidationError as e:
125 get_logger().error("Notebook JSON is invalid: %s", e)
126 return nb
129 127
130 128
131 def writes(nb, format, **kwargs):
129 def writes(nb, format='DEPRECATED', version=current_nbformat, **kwargs):
132 130 """Write a notebook to a string in a given format in the current nbformat version.
133 131
134 132 This function always writes the notebook in the current nbformat version.
@@ -137,24 +135,24 b' def writes(nb, format, **kwargs):'
137 135 ----------
138 136 nb : NotebookNode
139 137 The notebook to write.
140 format : (u'json', u'ipynb', u'py')
141 The format to write the notebook in.
138 version : int
139 The nbformat version to write.
140 Used for downgrading notebooks.
142 141
143 142 Returns
144 143 -------
145 144 s : unicode
146 145 The notebook string.
147 146 """
148 format = unicode_type(format)
149 if format == u'json' or format == u'ipynb':
150 return writes_json(nb, **kwargs)
151 elif format == u'py':
152 return writes_py(nb, **kwargs)
153 else:
154 raise NBFormatError('Unsupported format: %s' % format)
147 try:
148 validate(nb)
149 except ValidationError as e:
150 get_logger().error("Notebook JSON is invalid: %s", e)
151 nb = convert(nb, version)
152 return versions[version].writes_json(nb, **kwargs)
155 153
156 154
157 def read(fp, format, **kwargs):
155 def read(fp, format='DEPRECATED', **kwargs):
158 156 """Read a notebook from a file and return the NotebookNode object.
159 157
160 158 This function properly handles notebooks of any version. The notebook
@@ -164,18 +162,16 b' def read(fp, format, **kwargs):'
164 162 ----------
165 163 fp : file
166 164 Any file-like object with a read method.
167 format : (u'json', u'ipynb', u'py')
168 The format that the string is in.
169 165
170 166 Returns
171 167 -------
172 168 nb : NotebookNode
173 169 The notebook that was read.
174 170 """
175 return reads(fp.read(), format, **kwargs)
171 return reads(fp.read(), **kwargs)
176 172
177 173
178 def write(nb, fp, format, **kwargs):
174 def write(nb, fp, format='DEPRECATED', **kwargs):
179 175 """Write a notebook to a file in a given format in the current nbformat version.
180 176
181 177 This function always writes the notebook in the current nbformat version.
@@ -194,7 +190,7 b' def write(nb, fp, format, **kwargs):'
194 190 s : unicode
195 191 The notebook string.
196 192 """
197 return fp.write(writes(nb, format, **kwargs))
193 return fp.write(writes(nb, **kwargs))
198 194
199 195 def _convert_to_metadata():
200 196 """Convert to a notebook having notebook metadata."""
@@ -1,25 +1,19 b''
1 1 """
2 2 Contains tests class for current.py
3 3 """
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2013 The IPython Development Team
6 #
7 # Distributed under the terms of the BSD License. The full license is in
8 # the file COPYING, distributed as part of this software.
9 #-----------------------------------------------------------------------------
10 4
11 #-----------------------------------------------------------------------------
12 # Imports
13 #-----------------------------------------------------------------------------
5 # Copyright (c) IPython Development Team.
6 # Distributed under the terms of the Modified BSD License.
7
8 import io
9 import json
10 import tempfile
14 11
15 12 from .base import TestsBase
16 13
17 14 from ..reader import get_version
18 from ..current import read, current_nbformat
15 from ..current import read, current_nbformat, validate, writes
19 16
20 #-----------------------------------------------------------------------------
21 # Classes and functions
22 #-----------------------------------------------------------------------------
23 17
24 18 class TestCurrent(TestsBase):
25 19
@@ -29,8 +23,19 b' class TestCurrent(TestsBase):'
29 23
30 24 # Open a version 2 notebook.
31 25 with self.fopen(u'test2.ipynb', u'r') as f:
32 nb = read(f, u'json')
26 nb = read(f)
33 27
34 28 # Check that the notebook was upgraded to the latest version automatically.
35 29 (major, minor) = get_version(nb)
36 30 self.assertEqual(major, current_nbformat)
31
32 def test_write_downgrade_2(self):
33 """dowgrade a v3 notebook to v2"""
34 # Open a version 3 notebook.
35 with self.fopen(u'test3.ipynb', 'r') as f:
36 nb = read(f, u'json')
37
38 jsons = writes(nb, version=2)
39 nb2 = json.loads(jsons)
40 (major, minor) = get_version(nb2)
41 self.assertEqual(major, 2)
General Comments 0
You need to be logged in to leave comments. Login now