Show More
@@ -1,34 +1,19 b'' | |||||
1 | """Read and write notebooks in JSON format. |
|
1 | """Read and write notebooks in JSON format.""" | |
2 |
|
2 | |||
3 | Authors: |
|
3 | # Copyright (c) IPython Development Team. | |
4 |
|
4 | # Distributed under the terms of the Modified BSD License. | ||
5 | * Brian Granger |
|
|||
6 | """ |
|
|||
7 |
|
||||
8 | #----------------------------------------------------------------------------- |
|
|||
9 | # Copyright (C) 2008-2011 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 |
|
5 | |||
19 | import copy |
|
6 | import copy | |
20 | import json |
|
7 | import json | |
21 |
|
8 | |||
22 | from .nbbase import from_dict |
|
9 | from .nbbase import from_dict | |
23 | from .rwbase import ( |
|
10 | from .rwbase import ( | |
24 | NotebookReader, NotebookWriter, restore_bytes, rejoin_lines, split_lines |
|
11 | NotebookReader, NotebookWriter, restore_bytes, rejoin_lines, split_lines, | |
|
12 | strip_transient, | |||
25 | ) |
|
13 | ) | |
26 |
|
14 | |||
27 | from IPython.utils import py3compat |
|
15 | from IPython.utils import py3compat | |
28 |
|
16 | |||
29 | #----------------------------------------------------------------------------- |
|
|||
30 | # Code |
|
|||
31 | #----------------------------------------------------------------------------- |
|
|||
32 |
|
17 | |||
33 | class BytesEncoder(json.JSONEncoder): |
|
18 | class BytesEncoder(json.JSONEncoder): | |
34 | """A JSON encoder that accepts b64 (and other *ascii*) bytestrings.""" |
|
19 | """A JSON encoder that accepts b64 (and other *ascii*) bytestrings.""" | |
@@ -43,6 +28,7 b' class JSONReader(NotebookReader):' | |||||
43 | def reads(self, s, **kwargs): |
|
28 | def reads(self, s, **kwargs): | |
44 | nb = json.loads(s, **kwargs) |
|
29 | nb = json.loads(s, **kwargs) | |
45 | nb = self.to_notebook(nb, **kwargs) |
|
30 | nb = self.to_notebook(nb, **kwargs) | |
|
31 | nb = strip_transient(nb) | |||
46 | return nb |
|
32 | return nb | |
47 |
|
33 | |||
48 | def to_notebook(self, d, **kwargs): |
|
34 | def to_notebook(self, d, **kwargs): | |
@@ -56,8 +42,10 b' class JSONWriter(NotebookWriter):' | |||||
56 | kwargs['indent'] = 1 |
|
42 | kwargs['indent'] = 1 | |
57 | kwargs['sort_keys'] = True |
|
43 | kwargs['sort_keys'] = True | |
58 | kwargs['separators'] = (',',': ') |
|
44 | kwargs['separators'] = (',',': ') | |
|
45 | nb = copy.deepcopy(nb) | |||
|
46 | nb = strip_transient(nb) | |||
59 | if kwargs.pop('split_lines', True): |
|
47 | if kwargs.pop('split_lines', True): | |
60 |
nb = split_lines( |
|
48 | nb = split_lines(nb) | |
61 | return py3compat.str_to_unicode(json.dumps(nb, **kwargs), 'utf-8') |
|
49 | return py3compat.str_to_unicode(json.dumps(nb, **kwargs), 'utf-8') | |
62 |
|
50 | |||
63 |
|
51 |
@@ -1,30 +1,13 b'' | |||||
1 | """Base classes and utilities for readers and writers. |
|
1 | """Base classes and utilities for readers and writers.""" | |
2 |
|
2 | |||
3 | Authors: |
|
3 | # Copyright (c) IPython Development Team. | |
4 |
|
4 | # Distributed under the terms of the Modified BSD License. | ||
5 | * Brian Granger |
|
|||
6 | """ |
|
|||
7 |
|
||||
8 | #----------------------------------------------------------------------------- |
|
|||
9 | # Copyright (C) 2008-2011 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 |
|
5 | |||
19 | from base64 import encodestring, decodestring |
|
6 | from base64 import encodestring, decodestring | |
20 | import pprint |
|
|||
21 |
|
7 | |||
22 | from IPython.utils import py3compat |
|
8 | from IPython.utils import py3compat | |
23 | from IPython.utils.py3compat import str_to_bytes, unicode_type, string_types |
|
9 | from IPython.utils.py3compat import str_to_bytes, unicode_type, string_types | |
24 |
|
10 | |||
25 | #----------------------------------------------------------------------------- |
|
|||
26 | # Code |
|
|||
27 | #----------------------------------------------------------------------------- |
|
|||
28 |
|
11 | |||
29 | def restore_bytes(nb): |
|
12 | def restore_bytes(nb): | |
30 | """Restore bytes of image data from unicode-only formats. |
|
13 | """Restore bytes of image data from unicode-only formats. | |
@@ -157,6 +140,19 b' def base64_encode(nb):' | |||||
157 | return nb |
|
140 | return nb | |
158 |
|
141 | |||
159 |
|
142 | |||
|
143 | def strip_transient(nb): | |||
|
144 | """Strip transient values that shouldn't be stored in files. | |||
|
145 | ||||
|
146 | This should be called in *both* read and write. | |||
|
147 | """ | |||
|
148 | nb.pop('orig_nbformat', None) | |||
|
149 | nb.pop('orig_nbformat_minor', None) | |||
|
150 | for ws in nb['worksheets']: | |||
|
151 | for cell in ws['cells']: | |||
|
152 | cell.get('metadata', {}).pop('trusted', None) | |||
|
153 | return nb | |||
|
154 | ||||
|
155 | ||||
160 | class NotebookReader(object): |
|
156 | class NotebookReader(object): | |
161 | """A class for reading notebooks.""" |
|
157 | """A class for reading notebooks.""" | |
162 |
|
158 |
@@ -1,9 +1,11 b'' | |||||
1 |
import |
|
1 | import copy | |
|
2 | import json | |||
2 | from base64 import decodestring |
|
3 | from base64 import decodestring | |
3 | from unittest import TestCase |
|
4 | from unittest import TestCase | |
4 |
|
5 | |||
5 | from IPython.utils.py3compat import unicode_type |
|
6 | from IPython.utils.py3compat import unicode_type | |
6 | from ..nbjson import reads, writes |
|
7 | from ..nbjson import reads, writes | |
|
8 | from ..nbbase import from_dict | |||
7 | from .. import nbjson |
|
9 | from .. import nbjson | |
8 | from .nbexamples import nb0 |
|
10 | from .nbexamples import nb0 | |
9 |
|
11 | |||
@@ -31,6 +33,35 b' class TestJSON(formattest.NBFormatTest, TestCase):' | |||||
31 | s = writes(nb0, split_lines=True) |
|
33 | s = writes(nb0, split_lines=True) | |
32 | self.assertEqual(nbjson.reads(s),nb0) |
|
34 | self.assertEqual(nbjson.reads(s),nb0) | |
33 |
|
35 | |||
|
36 | def test_strip_transient(self): | |||
|
37 | """transient values aren't written to files""" | |||
|
38 | nb = copy.deepcopy(nb0) | |||
|
39 | nb.orig_nbformat = 2 | |||
|
40 | nb.orig_nbformat_minor = 3 | |||
|
41 | nb.worksheets[0].cells[0].metadata.trusted = False | |||
|
42 | nbs = nbjson.writes(nb) | |||
|
43 | ||||
|
44 | nb2 = from_dict(json.loads(nbs)) | |||
|
45 | self.assertNotIn('orig_nbformat', nb2) | |||
|
46 | self.assertNotIn('orig_nbformat_minor', nb2) | |||
|
47 | for cell in nb2.worksheets[0].cells: | |||
|
48 | self.assertNotIn('trusted', cell.metadata) | |||
|
49 | ||||
|
50 | def test_to_json(self): | |||
|
51 | """to_notebook_json doesn't strip transient""" | |||
|
52 | nb = copy.deepcopy(nb0) | |||
|
53 | nb.orig_nbformat = 2 | |||
|
54 | nb.orig_nbformat_minor = 3 | |||
|
55 | nb.worksheets[0].cells[0].metadata.trusted = False | |||
|
56 | nbs = json.dumps(nb) | |||
|
57 | nb2 = nbjson.to_notebook(json.loads(nbs)) | |||
|
58 | ||||
|
59 | nb2 = from_dict(json.loads(nbs)) | |||
|
60 | self.assertIn('orig_nbformat', nb2) | |||
|
61 | self.assertIn('orig_nbformat_minor', nb2) | |||
|
62 | cell = nb2.worksheets[0].cells[0] | |||
|
63 | self.assertIn('trusted', cell.metadata) | |||
|
64 | ||||
34 | def test_read_png(self): |
|
65 | def test_read_png(self): | |
35 | """PNG output data is b64 unicode""" |
|
66 | """PNG output data is b64 unicode""" | |
36 | s = writes(nb0) |
|
67 | s = writes(nb0) |
General Comments 0
You need to be logged in to leave comments.
Login now