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