Show More
@@ -16,9 +16,8 b' Authors:' | |||
|
16 | 16 | # Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | from base64 import encodestring | |
|
20 | 19 | from .nbbase import from_dict |
|
21 |
from .rwbase import NotebookReader, NotebookWriter, |
|
|
20 | from .rwbase import NotebookReader, NotebookWriter, restore_bytes | |
|
22 | 21 | import json |
|
23 | 22 | |
|
24 | 23 | #----------------------------------------------------------------------------- |
@@ -26,9 +25,10 b' import json' | |||
|
26 | 25 | #----------------------------------------------------------------------------- |
|
27 | 26 | |
|
28 | 27 | class BytesEncoder(json.JSONEncoder): |
|
28 | """A JSON encoder that accepts b64 (and other *ascii*) bytestrings.""" | |
|
29 | 29 | def default(self, obj): |
|
30 | 30 | if isinstance(obj, bytes): |
|
31 |
return |
|
|
31 | return obj.decode('ascii') | |
|
32 | 32 | return json.JSONEncoder.default(self, obj) |
|
33 | 33 | |
|
34 | 34 | |
@@ -40,7 +40,7 b' class JSONReader(NotebookReader):' | |||
|
40 | 40 | return nb |
|
41 | 41 | |
|
42 | 42 | def to_notebook(self, d, **kwargs): |
|
43 |
return |
|
|
43 | return restore_bytes(from_dict(d)) | |
|
44 | 44 | |
|
45 | 45 | |
|
46 | 46 | class JSONWriter(NotebookWriter): |
@@ -19,31 +19,67 b' Authors:' | |||
|
19 | 19 | from base64 import encodestring, decodestring |
|
20 | 20 | import pprint |
|
21 | 21 | |
|
22 | from IPython.utils.py3compat import str_to_bytes | |
|
23 | ||
|
22 | 24 | #----------------------------------------------------------------------------- |
|
23 | 25 | # Code |
|
24 | 26 | #----------------------------------------------------------------------------- |
|
25 | 27 | |
|
28 | def restore_bytes(nb): | |
|
29 | """Restore bytes of image data from unicode-only formats. | |
|
30 | ||
|
31 | Base64 encoding is handled elsewhere. Bytes objects in the notebook are | |
|
32 | always b64-encoded. We DO NOT encode/decode around file formats. | |
|
33 | """ | |
|
34 | for ws in nb.worksheets: | |
|
35 | for cell in ws.cells: | |
|
36 | if cell.cell_type == 'code': | |
|
37 | for output in cell.outputs: | |
|
38 | if 'png' in output: | |
|
39 | output.png = str_to_bytes(output.png, 'ascii') | |
|
40 | if 'jpeg' in output: | |
|
41 | output.jpeg = str_to_bytes(output.jpeg, 'ascii') | |
|
42 | return nb | |
|
43 | ||
|
44 | ||
|
45 | # b64 encode/decode are never actually used, because all bytes objects in | |
|
46 | # the notebook are already b64-encoded, and we don't need/want to double-encode | |
|
47 | ||
|
26 | 48 | def base64_decode(nb): |
|
27 |
""" |
|
|
49 | """Restore all bytes objects in the notebook from base64-encoded strings. | |
|
50 | ||
|
51 | Note: This is never used | |
|
52 | """ | |
|
28 | 53 | for ws in nb.worksheets: |
|
29 | 54 | for cell in ws.cells: |
|
30 | 55 | if cell.cell_type == 'code': |
|
31 |
|
|
|
32 | cell.png = bytes(decodestring(cell.png)) | |
|
33 | if 'jpeg' in cell: | |
|
34 | cell.jpeg = bytes(decodestring(cell.jpeg)) | |
|
56 | for output in cell.outputs: | |
|
57 | if 'png' in output: | |
|
58 | if isinstance(output.png, unicode): | |
|
59 | output.png = output.png.encode('ascii') | |
|
60 | output.png = decodestring(output.png) | |
|
61 | if 'jpeg' in output: | |
|
62 | if isinstance(output.jpeg, unicode): | |
|
63 | output.jpeg = output.jpeg.encode('ascii') | |
|
64 | output.jpeg = decodestring(output.jpeg) | |
|
35 | 65 | return nb |
|
36 | 66 | |
|
37 | 67 | |
|
38 | 68 | def base64_encode(nb): |
|
39 |
"""Base64 |
|
|
69 | """Base64 encode all bytes objects in the notebook. | |
|
70 | ||
|
71 | These will be b64-encoded unicode strings | |
|
72 | ||
|
73 | Note: This is never used | |
|
74 | """ | |
|
40 | 75 | for ws in nb.worksheets: |
|
41 | 76 | for cell in ws.cells: |
|
42 | 77 | if cell.cell_type == 'code': |
|
43 |
|
|
|
44 | cell.png = unicode(encodestring(cell.png)) | |
|
45 | if 'jpeg' in cell: | |
|
46 | cell.jpeg = unicode(encodestring(cell.jpeg)) | |
|
78 | for output in cell.outputs: | |
|
79 | if 'png' in output: | |
|
80 | output.png = encodestring(output.png).decode('ascii') | |
|
81 | if 'jpeg' in output: | |
|
82 | output.jpeg = encodestring(output.jpeg).decode('ascii') | |
|
47 | 83 | return nb |
|
48 | 84 | |
|
49 | 85 |
@@ -1,10 +1,15 b'' | |||
|
1 | import os | |
|
2 | from base64 import encodestring | |
|
3 | ||
|
1 | 4 | from ..nbbase import ( |
|
2 | 5 | NotebookNode, |
|
3 | 6 | new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output, |
|
4 | 7 | new_metadata, new_author |
|
5 | 8 | ) |
|
6 | 9 | |
|
7 | ||
|
10 | # some random base64-encoded *bytes* | |
|
11 | png = encodestring(os.urandom(5)) | |
|
12 | jpeg = encodestring(os.urandom(6)) | |
|
8 | 13 | |
|
9 | 14 | ws = new_worksheet(name='worksheet1') |
|
10 | 15 | |
@@ -42,8 +47,8 b' ws.cells.append(new_code_cell(' | |||
|
42 | 47 | output_text=u'<array a>', |
|
43 | 48 | output_html=u'The HTML rep', |
|
44 | 49 | output_latex=u'$a$', |
|
45 |
output_png= |
|
|
46 |
output_jpeg= |
|
|
50 | output_png=png, | |
|
51 | output_jpeg=jpeg, | |
|
47 | 52 | output_svg=u'<svg>', |
|
48 | 53 | output_json=u'json data', |
|
49 | 54 | output_javascript=u'var i=0;', |
@@ -53,8 +58,8 b' ws.cells.append(new_code_cell(' | |||
|
53 | 58 | output_text=u'<array a>', |
|
54 | 59 | output_html=u'The HTML rep', |
|
55 | 60 | output_latex=u'$a$', |
|
56 |
output_png= |
|
|
57 |
output_jpeg= |
|
|
61 | output_png=png, | |
|
62 | output_jpeg=jpeg, | |
|
58 | 63 | output_svg=u'<svg>', |
|
59 | 64 | output_json=u'json data', |
|
60 | 65 | output_javascript=u'var i=0;' |
General Comments 0
You need to be logged in to leave comments.
Login now