Show More
@@ -1,190 +1,192 b'' | |||
|
1 | 1 | """Base classes and utilities for readers and writers. |
|
2 | 2 | |
|
3 | 3 | Authors: |
|
4 | 4 | |
|
5 | 5 | * Brian Granger |
|
6 | 6 | """ |
|
7 | 7 | |
|
8 | 8 | #----------------------------------------------------------------------------- |
|
9 | 9 | # Copyright (C) 2008-2011 The IPython Development Team |
|
10 | 10 | # |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | 12 | # the file COPYING, distributed as part of this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | from base64 import encodestring, decodestring |
|
20 | 20 | import pprint |
|
21 | 21 | |
|
22 | 22 | from IPython.utils import py3compat |
|
23 | 23 | |
|
24 | 24 | str_to_bytes = py3compat.str_to_bytes |
|
25 | 25 | |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | # Code |
|
28 | 28 | #----------------------------------------------------------------------------- |
|
29 | 29 | |
|
30 | 30 | def restore_bytes(nb): |
|
31 | 31 | """Restore bytes of image data from unicode-only formats. |
|
32 | 32 | |
|
33 | 33 | Base64 encoding is handled elsewhere. Bytes objects in the notebook are |
|
34 | 34 | always b64-encoded. We DO NOT encode/decode around file formats. |
|
35 | ||
|
36 | Note: this is never used | |
|
35 | 37 | """ |
|
36 | 38 | for ws in nb.worksheets: |
|
37 | 39 | for cell in ws.cells: |
|
38 | 40 | if cell.cell_type == 'code': |
|
39 | 41 | for output in cell.outputs: |
|
40 | 42 | if 'png' in output: |
|
41 | 43 | output.png = str_to_bytes(output.png, 'ascii') |
|
42 | 44 | if 'jpeg' in output: |
|
43 | 45 | output.jpeg = str_to_bytes(output.jpeg, 'ascii') |
|
44 | 46 | return nb |
|
45 | 47 | |
|
46 | 48 | # output keys that are likely to have multiline values |
|
47 | 49 | _multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] |
|
48 | 50 | |
|
49 | 51 | |
|
50 | 52 | # FIXME: workaround for old splitlines() |
|
51 | 53 | def _join_lines(lines): |
|
52 | 54 | """join lines that have been written by splitlines() |
|
53 | 55 | |
|
54 | 56 | Has logic to protect against `splitlines()`, which |
|
55 | 57 | should have been `splitlines(True)` |
|
56 | 58 | """ |
|
57 | 59 | if lines and lines[0].endswith(('\n', '\r')): |
|
58 | 60 | # created by splitlines(True) |
|
59 | 61 | return u''.join(lines) |
|
60 | 62 | else: |
|
61 | 63 | # created by splitlines() |
|
62 | 64 | return u'\n'.join(lines) |
|
63 | 65 | |
|
64 | 66 | |
|
65 | 67 | def rejoin_lines(nb): |
|
66 | 68 | """rejoin multiline text into strings |
|
67 | 69 | |
|
68 | 70 | For reversing effects of ``split_lines(nb)``. |
|
69 | 71 | |
|
70 | 72 | This only rejoins lines that have been split, so if text objects were not split |
|
71 | 73 | they will pass through unchanged. |
|
72 | 74 | |
|
73 | 75 | Used when reading JSON files that may have been passed through split_lines. |
|
74 | 76 | """ |
|
75 | 77 | for ws in nb.worksheets: |
|
76 | 78 | for cell in ws.cells: |
|
77 | 79 | if cell.cell_type == 'code': |
|
78 | 80 | if 'input' in cell and isinstance(cell.input, list): |
|
79 | 81 | cell.input = _join_lines(cell.input) |
|
80 | 82 | for output in cell.outputs: |
|
81 | 83 | for key in _multiline_outputs: |
|
82 | 84 | item = output.get(key, None) |
|
83 | 85 | if isinstance(item, list): |
|
84 | 86 | output[key] = _join_lines(item) |
|
85 | 87 | else: # text, heading cell |
|
86 | 88 | for key in ['source', 'rendered']: |
|
87 | 89 | item = cell.get(key, None) |
|
88 | 90 | if isinstance(item, list): |
|
89 | 91 | cell[key] = _join_lines(item) |
|
90 | 92 | return nb |
|
91 | 93 | |
|
92 | 94 | |
|
93 | 95 | def split_lines(nb): |
|
94 | 96 | """split likely multiline text into lists of strings |
|
95 | 97 | |
|
96 | 98 | For file output more friendly to line-based VCS. ``rejoin_lines(nb)`` will |
|
97 | 99 | reverse the effects of ``split_lines(nb)``. |
|
98 | 100 | |
|
99 | 101 | Used when writing JSON files. |
|
100 | 102 | """ |
|
101 | 103 | for ws in nb.worksheets: |
|
102 | 104 | for cell in ws.cells: |
|
103 | 105 | if cell.cell_type == 'code': |
|
104 | 106 | if 'input' in cell and isinstance(cell.input, basestring): |
|
105 | 107 | cell.input = cell.input.splitlines(True) |
|
106 | 108 | for output in cell.outputs: |
|
107 | 109 | for key in _multiline_outputs: |
|
108 | 110 | item = output.get(key, None) |
|
109 | 111 | if isinstance(item, basestring): |
|
110 | 112 | output[key] = item.splitlines(True) |
|
111 | 113 | else: # text, heading cell |
|
112 | 114 | for key in ['source', 'rendered']: |
|
113 | 115 | item = cell.get(key, None) |
|
114 | 116 | if isinstance(item, basestring): |
|
115 | 117 | cell[key] = item.splitlines(True) |
|
116 | 118 | return nb |
|
117 | 119 | |
|
118 | 120 | # b64 encode/decode are never actually used, because all bytes objects in |
|
119 | 121 | # the notebook are already b64-encoded, and we don't need/want to double-encode |
|
120 | 122 | |
|
121 | 123 | def base64_decode(nb): |
|
122 | 124 | """Restore all bytes objects in the notebook from base64-encoded strings. |
|
123 | 125 | |
|
124 | 126 | Note: This is never used |
|
125 | 127 | """ |
|
126 | 128 | for ws in nb.worksheets: |
|
127 | 129 | for cell in ws.cells: |
|
128 | 130 | if cell.cell_type == 'code': |
|
129 | 131 | for output in cell.outputs: |
|
130 | 132 | if 'png' in output: |
|
131 | 133 | if isinstance(output.png, unicode): |
|
132 | 134 | output.png = output.png.encode('ascii') |
|
133 | 135 | output.png = decodestring(output.png) |
|
134 | 136 | if 'jpeg' in output: |
|
135 | 137 | if isinstance(output.jpeg, unicode): |
|
136 | 138 | output.jpeg = output.jpeg.encode('ascii') |
|
137 | 139 | output.jpeg = decodestring(output.jpeg) |
|
138 | 140 | return nb |
|
139 | 141 | |
|
140 | 142 | |
|
141 | 143 | def base64_encode(nb): |
|
142 | 144 | """Base64 encode all bytes objects in the notebook. |
|
143 | 145 | |
|
144 | 146 | These will be b64-encoded unicode strings |
|
145 | 147 | |
|
146 | 148 | Note: This is never used |
|
147 | 149 | """ |
|
148 | 150 | for ws in nb.worksheets: |
|
149 | 151 | for cell in ws.cells: |
|
150 | 152 | if cell.cell_type == 'code': |
|
151 | 153 | for output in cell.outputs: |
|
152 | 154 | if 'png' in output: |
|
153 | 155 | output.png = encodestring(output.png).decode('ascii') |
|
154 | 156 | if 'jpeg' in output: |
|
155 | 157 | output.jpeg = encodestring(output.jpeg).decode('ascii') |
|
156 | 158 | return nb |
|
157 | 159 | |
|
158 | 160 | |
|
159 | 161 | class NotebookReader(object): |
|
160 | 162 | """A class for reading notebooks.""" |
|
161 | 163 | |
|
162 | 164 | def reads(self, s, **kwargs): |
|
163 | 165 | """Read a notebook from a string.""" |
|
164 | 166 | raise NotImplementedError("loads must be implemented in a subclass") |
|
165 | 167 | |
|
166 | 168 | def read(self, fp, **kwargs): |
|
167 | 169 | """Read a notebook from a file like object""" |
|
168 | 170 | nbs = fp.read() |
|
169 | 171 | if not py3compat.PY3 and not isinstance(nbs, unicode): |
|
170 | 172 | nbs = py3compat.str_to_unicode(nbs) |
|
171 | 173 | return self.reads(nbs, **kwargs) |
|
172 | 174 | |
|
173 | 175 | |
|
174 | 176 | class NotebookWriter(object): |
|
175 | 177 | """A class for writing notebooks.""" |
|
176 | 178 | |
|
177 | 179 | def writes(self, nb, **kwargs): |
|
178 | 180 | """Write a notebook to a string.""" |
|
179 | 181 | raise NotImplementedError("loads must be implemented in a subclass") |
|
180 | 182 | |
|
181 | 183 | def write(self, nb, fp, **kwargs): |
|
182 | 184 | """Write a notebook to a file like object""" |
|
183 | 185 | nbs = self.writes(nb,**kwargs) |
|
184 | 186 | if not py3compat.PY3 and not isinstance(nbs, unicode): |
|
185 | 187 | # this branch is likely only taken for JSON on Python 2 |
|
186 | 188 | nbs = py3compat.str_to_unicode(nbs) |
|
187 | 189 | return fp.write(nbs) |
|
188 | 190 | |
|
189 | 191 | |
|
190 | 192 |
General Comments 0
You need to be logged in to leave comments.
Login now