Show More
@@ -1,189 +1,191 b'' | |||||
1 | """The basic dict based notebook format. |
|
1 | """The basic dict based notebook format. | |
2 |
|
2 | |||
3 | The Python representation of a notebook is a nested structure of |
|
3 | The Python representation of a notebook is a nested structure of | |
4 | dictionary subclasses that support attribute access |
|
4 | dictionary subclasses that support attribute access | |
5 | (IPython.utils.ipstruct.Struct). The functions in this module are merely |
|
5 | (IPython.utils.ipstruct.Struct). The functions in this module are merely | |
6 | helpers to build the structs in the right form. |
|
6 | helpers to build the structs in the right form. | |
7 |
|
7 | |||
8 | Authors: |
|
8 | Authors: | |
9 |
|
9 | |||
10 | * Brian Granger |
|
10 | * Brian Granger | |
11 | """ |
|
11 | """ | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Copyright (C) 2008-2011 The IPython Development Team |
|
14 | # Copyright (C) 2008-2011 The IPython Development Team | |
15 | # |
|
15 | # | |
16 | # Distributed under the terms of the BSD License. The full license is in |
|
16 | # Distributed under the terms of the BSD License. The full license is in | |
17 | # the file COPYING, distributed as part of this software. |
|
17 | # the file COPYING, distributed as part of this software. | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | # Imports |
|
21 | # Imports | |
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 |
|
23 | |||
24 | import pprint |
|
24 | import pprint | |
25 | import uuid |
|
25 | import uuid | |
26 |
|
26 | |||
27 | from IPython.utils.ipstruct import Struct |
|
27 | from IPython.utils.ipstruct import Struct | |
28 |
|
28 | |||
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 | # Code |
|
30 | # Code | |
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 |
|
32 | |||
33 | class NotebookNode(Struct): |
|
33 | class NotebookNode(Struct): | |
34 | pass |
|
34 | pass | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | def from_dict(d): |
|
37 | def from_dict(d): | |
38 | if isinstance(d, dict): |
|
38 | if isinstance(d, dict): | |
39 | newd = NotebookNode() |
|
39 | newd = NotebookNode() | |
40 | for k,v in d.items(): |
|
40 | for k,v in d.items(): | |
41 | newd[k] = from_dict(v) |
|
41 | newd[k] = from_dict(v) | |
42 | return newd |
|
42 | return newd | |
43 | elif isinstance(d, (tuple, list)): |
|
43 | elif isinstance(d, (tuple, list)): | |
44 | return [from_dict(i) for i in d] |
|
44 | return [from_dict(i) for i in d] | |
45 | else: |
|
45 | else: | |
46 | return d |
|
46 | return d | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | def new_output(output_type=None, output_text=None, output_png=None, |
|
49 | def new_output(output_type=None, output_text=None, output_png=None, | |
50 | output_html=None, output_svg=None, output_latex=None, output_json=None, |
|
50 | output_html=None, output_svg=None, output_latex=None, output_json=None, | |
51 | output_javascript=None, output_jpeg=None, prompt_number=None, |
|
51 | output_javascript=None, output_jpeg=None, prompt_number=None, | |
52 | etype=None, evalue=None, traceback=None): |
|
52 | etype=None, evalue=None, traceback=None): | |
53 | """Create a new code cell with input and output""" |
|
53 | """Create a new code cell with input and output""" | |
54 | output = NotebookNode() |
|
54 | output = NotebookNode() | |
55 | if output_type is not None: |
|
55 | if output_type is not None: | |
56 | output.output_type = unicode(output_type) |
|
56 | output.output_type = unicode(output_type) | |
57 |
|
57 | |||
58 | if output_type != 'pyerr': |
|
58 | if output_type != 'pyerr': | |
59 | if output_text is not None: |
|
59 | if output_text is not None: | |
60 | output.text = unicode(output_text) |
|
60 | output.text = unicode(output_text) | |
61 | if output_png is not None: |
|
61 | if output_png is not None: | |
62 | output.png = bytes(output_png) |
|
62 | output.png = bytes(output_png) | |
63 | if output_jpeg is not None: |
|
63 | if output_jpeg is not None: | |
64 | output.jpeg = bytes(output_jpeg) |
|
64 | output.jpeg = bytes(output_jpeg) | |
65 | if output_html is not None: |
|
65 | if output_html is not None: | |
66 | output.html = unicode(output_html) |
|
66 | output.html = unicode(output_html) | |
67 | if output_svg is not None: |
|
67 | if output_svg is not None: | |
68 | output.svg = unicode(output_svg) |
|
68 | output.svg = unicode(output_svg) | |
69 | if output_latex is not None: |
|
69 | if output_latex is not None: | |
70 | output.latex = unicode(output_latex) |
|
70 | output.latex = unicode(output_latex) | |
71 | if output_json is not None: |
|
71 | if output_json is not None: | |
72 | output.json = unicode(output_json) |
|
72 | output.json = unicode(output_json) | |
73 | if output_javascript is not None: |
|
73 | if output_javascript is not None: | |
74 | output.javascript = unicode(output_javascript) |
|
74 | output.javascript = unicode(output_javascript) | |
75 |
|
75 | |||
76 | if output_type == u'pyout': |
|
76 | if output_type == u'pyout': | |
77 | if prompt_number is not None: |
|
77 | if prompt_number is not None: | |
78 | output.prompt_number = int(prompt_number) |
|
78 | output.prompt_number = int(prompt_number) | |
79 |
|
79 | |||
80 | if output_type == u'pyerr': |
|
80 | if output_type == u'pyerr': | |
81 | if etype is not None: |
|
81 | if etype is not None: | |
82 | output.etype = unicode(etype) |
|
82 | output.etype = unicode(etype) | |
83 | if evalue is not None: |
|
83 | if evalue is not None: | |
84 | output.evalue = unicode(evalue) |
|
84 | output.evalue = unicode(evalue) | |
85 | if traceback is not None: |
|
85 | if traceback is not None: | |
86 | output.traceback = [unicode(frame) for frame in list(traceback)] |
|
86 | output.traceback = [unicode(frame) for frame in list(traceback)] | |
87 |
|
87 | |||
88 | return output |
|
88 | return output | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | def new_code_cell(input=None, prompt_number=None, outputs=None, |
|
91 | def new_code_cell(input=None, prompt_number=None, outputs=None, | |
92 | language=u'python', collapsed=False): |
|
92 | language=u'python', collapsed=False): | |
93 | """Create a new code cell with input and output""" |
|
93 | """Create a new code cell with input and output""" | |
94 | cell = NotebookNode() |
|
94 | cell = NotebookNode() | |
95 | cell.cell_type = u'code' |
|
95 | cell.cell_type = u'code' | |
96 | if language is not None: |
|
96 | if language is not None: | |
97 | cell.language = unicode(language) |
|
97 | cell.language = unicode(language) | |
98 | if input is not None: |
|
98 | if input is not None: | |
99 | cell.input = unicode(input) |
|
99 | cell.input = unicode(input) | |
100 | if prompt_number is not None: |
|
100 | if prompt_number is not None: | |
101 | cell.prompt_number = int(prompt_number) |
|
101 | cell.prompt_number = int(prompt_number) | |
102 | if outputs is None: |
|
102 | if outputs is None: | |
103 | cell.outputs = [] |
|
103 | cell.outputs = [] | |
104 | else: |
|
104 | else: | |
105 | cell.outputs = outputs |
|
105 | cell.outputs = outputs | |
106 | if collapsed is not None: |
|
106 | if collapsed is not None: | |
107 | cell.collapsed = bool(collapsed) |
|
107 | cell.collapsed = bool(collapsed) | |
108 |
|
108 | |||
109 | return cell |
|
109 | return cell | |
110 |
|
110 | |||
111 | def new_text_cell(cell_type, source=None, rendered=None): |
|
111 | def new_text_cell(cell_type, source=None, rendered=None): | |
112 | """Create a new text cell.""" |
|
112 | """Create a new text cell.""" | |
113 | cell = NotebookNode() |
|
113 | cell = NotebookNode() | |
114 | if source is not None: |
|
114 | if source is not None: | |
115 | cell.source = unicode(source) |
|
115 | cell.source = unicode(source) | |
116 | if rendered is not None: |
|
116 | if rendered is not None: | |
117 | cell.rendered = unicode(rendered) |
|
117 | cell.rendered = unicode(rendered) | |
118 | cell.cell_type = cell_type |
|
118 | cell.cell_type = cell_type | |
119 | return cell |
|
119 | return cell | |
120 |
|
120 | |||
121 |
|
121 | |||
122 | def new_heading_cell(source=None, level=1): |
|
122 | def new_heading_cell(source=None, rendered=None, level=1): | |
123 | """Create a new section cell with a given integer level.""" |
|
123 | """Create a new section cell with a given integer level.""" | |
124 | cell = NotebookNode() |
|
124 | cell = NotebookNode() | |
125 | cell.cell_type = u'heading' |
|
125 | cell.cell_type = u'heading' | |
126 | if source is not None: |
|
126 | if source is not None: | |
127 | cell.source = unicode(source) |
|
127 | cell.source = unicode(source) | |
|
128 | if rendered is not None: | |||
|
129 | cell.rendered = unicode(rendered) | |||
128 | cell.level = int(level) |
|
130 | cell.level = int(level) | |
129 | return cell |
|
131 | return cell | |
130 |
|
132 | |||
131 |
|
133 | |||
132 | def new_worksheet(name=None, cells=None): |
|
134 | def new_worksheet(name=None, cells=None): | |
133 | """Create a worksheet by name with with a list of cells.""" |
|
135 | """Create a worksheet by name with with a list of cells.""" | |
134 | ws = NotebookNode() |
|
136 | ws = NotebookNode() | |
135 | if name is not None: |
|
137 | if name is not None: | |
136 | ws.name = unicode(name) |
|
138 | ws.name = unicode(name) | |
137 | if cells is None: |
|
139 | if cells is None: | |
138 | ws.cells = [] |
|
140 | ws.cells = [] | |
139 | else: |
|
141 | else: | |
140 | ws.cells = list(cells) |
|
142 | ws.cells = list(cells) | |
141 | return ws |
|
143 | return ws | |
142 |
|
144 | |||
143 |
|
145 | |||
144 | def new_notebook(metadata=None, worksheets=None): |
|
146 | def new_notebook(metadata=None, worksheets=None): | |
145 | """Create a notebook by name, id and a list of worksheets.""" |
|
147 | """Create a notebook by name, id and a list of worksheets.""" | |
146 | nb = NotebookNode() |
|
148 | nb = NotebookNode() | |
147 | nb.nbformat = 2 |
|
149 | nb.nbformat = 2 | |
148 | if worksheets is None: |
|
150 | if worksheets is None: | |
149 | nb.worksheets = [] |
|
151 | nb.worksheets = [] | |
150 | else: |
|
152 | else: | |
151 | nb.worksheets = list(worksheets) |
|
153 | nb.worksheets = list(worksheets) | |
152 | if metadata is None: |
|
154 | if metadata is None: | |
153 | nb.metadata = new_metadata() |
|
155 | nb.metadata = new_metadata() | |
154 | else: |
|
156 | else: | |
155 | nb.metadata = NotebookNode(metadata) |
|
157 | nb.metadata = NotebookNode(metadata) | |
156 | return nb |
|
158 | return nb | |
157 |
|
159 | |||
158 |
|
160 | |||
159 | def new_metadata(name=None, authors=None, license=None, created=None, |
|
161 | def new_metadata(name=None, authors=None, license=None, created=None, | |
160 | modified=None, gistid=None): |
|
162 | modified=None, gistid=None): | |
161 | """Create a new metadata node.""" |
|
163 | """Create a new metadata node.""" | |
162 | metadata = NotebookNode() |
|
164 | metadata = NotebookNode() | |
163 | if name is not None: |
|
165 | if name is not None: | |
164 | metadata.name = unicode(name) |
|
166 | metadata.name = unicode(name) | |
165 | if authors is not None: |
|
167 | if authors is not None: | |
166 | metadata.authors = list(authors) |
|
168 | metadata.authors = list(authors) | |
167 | if created is not None: |
|
169 | if created is not None: | |
168 | metadata.created = unicode(created) |
|
170 | metadata.created = unicode(created) | |
169 | if modified is not None: |
|
171 | if modified is not None: | |
170 | metadata.modified = unicode(modified) |
|
172 | metadata.modified = unicode(modified) | |
171 | if license is not None: |
|
173 | if license is not None: | |
172 | metadata.license = unicode(license) |
|
174 | metadata.license = unicode(license) | |
173 | if gistid is not None: |
|
175 | if gistid is not None: | |
174 | metadata.gistid = unicode(gistid) |
|
176 | metadata.gistid = unicode(gistid) | |
175 | return metadata |
|
177 | return metadata | |
176 |
|
178 | |||
177 | def new_author(name=None, email=None, affiliation=None, url=None): |
|
179 | def new_author(name=None, email=None, affiliation=None, url=None): | |
178 | """Create a new author.""" |
|
180 | """Create a new author.""" | |
179 | author = NotebookNode() |
|
181 | author = NotebookNode() | |
180 | if name is not None: |
|
182 | if name is not None: | |
181 | author.name = unicode(name) |
|
183 | author.name = unicode(name) | |
182 | if email is not None: |
|
184 | if email is not None: | |
183 | author.email = unicode(email) |
|
185 | author.email = unicode(email) | |
184 | if affiliation is not None: |
|
186 | if affiliation is not None: | |
185 | author.affiliation = unicode(affiliation) |
|
187 | author.affiliation = unicode(affiliation) | |
186 | if url is not None: |
|
188 | if url is not None: | |
187 | author.url = unicode(url) |
|
189 | author.url = unicode(url) | |
188 | return author |
|
190 | return author | |
189 |
|
191 |
@@ -1,169 +1,165 b'' | |||||
1 | """Base classes and utilities for readers and writers. |
|
1 | """Base classes and utilities for readers and writers. | |
2 |
|
2 | |||
3 | Authors: |
|
3 | Authors: | |
4 |
|
4 | |||
5 | * Brian Granger |
|
5 | * Brian Granger | |
6 | """ |
|
6 | """ | |
7 |
|
7 | |||
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 | # Copyright (C) 2008-2011 The IPython Development Team |
|
9 | # Copyright (C) 2008-2011 The IPython Development Team | |
10 | # |
|
10 | # | |
11 | # Distributed under the terms of the BSD License. The full license is in |
|
11 | # Distributed under the terms of the BSD License. The full license is in | |
12 | # the file COPYING, distributed as part of this software. |
|
12 | # the file COPYING, distributed as part of this software. | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | from base64 import encodestring, decodestring |
|
19 | from base64 import encodestring, decodestring | |
20 | import pprint |
|
20 | import pprint | |
21 |
|
21 | |||
22 | from IPython.utils.py3compat import str_to_bytes |
|
22 | from IPython.utils.py3compat import str_to_bytes | |
23 |
|
23 | |||
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 | # Code |
|
25 | # Code | |
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 |
|
27 | |||
28 | def restore_bytes(nb): |
|
28 | def restore_bytes(nb): | |
29 | """Restore bytes of image data from unicode-only formats. |
|
29 | """Restore bytes of image data from unicode-only formats. | |
30 |
|
30 | |||
31 | Base64 encoding is handled elsewhere. Bytes objects in the notebook are |
|
31 | Base64 encoding is handled elsewhere. Bytes objects in the notebook are | |
32 | always b64-encoded. We DO NOT encode/decode around file formats. |
|
32 | always b64-encoded. We DO NOT encode/decode around file formats. | |
33 | """ |
|
33 | """ | |
34 | for ws in nb.worksheets: |
|
34 | for ws in nb.worksheets: | |
35 | for cell in ws.cells: |
|
35 | for cell in ws.cells: | |
36 | if cell.cell_type == 'code': |
|
36 | if cell.cell_type == 'code': | |
37 | for output in cell.outputs: |
|
37 | for output in cell.outputs: | |
38 | if 'png' in output: |
|
38 | if 'png' in output: | |
39 | output.png = str_to_bytes(output.png, 'ascii') |
|
39 | output.png = str_to_bytes(output.png, 'ascii') | |
40 | if 'jpeg' in output: |
|
40 | if 'jpeg' in output: | |
41 | output.jpeg = str_to_bytes(output.jpeg, 'ascii') |
|
41 | output.jpeg = str_to_bytes(output.jpeg, 'ascii') | |
42 | return nb |
|
42 | return nb | |
43 |
|
43 | |||
44 | # output keys that are likely to have multiline values |
|
44 | # output keys that are likely to have multiline values | |
45 | _multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] |
|
45 | _multiline_outputs = ['text', 'html', 'svg', 'latex', 'javascript', 'json'] | |
46 |
|
46 | |||
47 | def rejoin_lines(nb): |
|
47 | def rejoin_lines(nb): | |
48 | """rejoin multiline text into strings |
|
48 | """rejoin multiline text into strings | |
49 |
|
49 | |||
50 | For reversing effects of ``split_lines(nb)``. |
|
50 | For reversing effects of ``split_lines(nb)``. | |
51 |
|
51 | |||
52 | This only rejoins lines that have been split, so if text objects were not split |
|
52 | This only rejoins lines that have been split, so if text objects were not split | |
53 | they will pass through unchanged. |
|
53 | they will pass through unchanged. | |
54 |
|
54 | |||
55 | Used when reading JSON files that may have been passed through split_lines. |
|
55 | Used when reading JSON files that may have been passed through split_lines. | |
56 | """ |
|
56 | """ | |
57 | for ws in nb.worksheets: |
|
57 | for ws in nb.worksheets: | |
58 | for cell in ws.cells: |
|
58 | for cell in ws.cells: | |
59 | if cell.cell_type == 'code': |
|
59 | if cell.cell_type == 'code': | |
60 | if 'input' in cell and isinstance(cell.input, list): |
|
60 | if 'input' in cell and isinstance(cell.input, list): | |
61 | cell.input = u'\n'.join(cell.input) |
|
61 | cell.input = u'\n'.join(cell.input) | |
62 | for output in cell.outputs: |
|
62 | for output in cell.outputs: | |
63 | for key in _multiline_outputs: |
|
63 | for key in _multiline_outputs: | |
64 | item = output.get(key, None) |
|
64 | item = output.get(key, None) | |
65 | if isinstance(item, list): |
|
65 | if isinstance(item, list): | |
66 | output[key] = u'\n'.join(item) |
|
66 | output[key] = u'\n'.join(item) | |
67 |
el |
|
67 | else: # text, heading cell | |
68 | pass |
|
|||
69 | else: # text cell |
|
|||
70 | for key in ['source', 'rendered']: |
|
68 | for key in ['source', 'rendered']: | |
71 | item = cell.get(key, None) |
|
69 | item = cell.get(key, None) | |
72 | if isinstance(item, list): |
|
70 | if isinstance(item, list): | |
73 | cell[key] = u'\n'.join(item) |
|
71 | cell[key] = u'\n'.join(item) | |
74 | return nb |
|
72 | return nb | |
75 |
|
73 | |||
76 |
|
74 | |||
77 | def split_lines(nb): |
|
75 | def split_lines(nb): | |
78 | """split likely multiline text into lists of strings |
|
76 | """split likely multiline text into lists of strings | |
79 |
|
77 | |||
80 | For file output more friendly to line-based VCS. ``rejoin_lines(nb)`` will |
|
78 | For file output more friendly to line-based VCS. ``rejoin_lines(nb)`` will | |
81 | reverse the effects of ``split_lines(nb)``. |
|
79 | reverse the effects of ``split_lines(nb)``. | |
82 |
|
80 | |||
83 | Used when writing JSON files. |
|
81 | Used when writing JSON files. | |
84 | """ |
|
82 | """ | |
85 | for ws in nb.worksheets: |
|
83 | for ws in nb.worksheets: | |
86 | for cell in ws.cells: |
|
84 | for cell in ws.cells: | |
87 | if cell.cell_type == 'code': |
|
85 | if cell.cell_type == 'code': | |
88 | if 'input' in cell and isinstance(cell.input, basestring): |
|
86 | if 'input' in cell and isinstance(cell.input, basestring): | |
89 | cell.input = cell.input.splitlines() |
|
87 | cell.input = cell.input.splitlines() | |
90 | for output in cell.outputs: |
|
88 | for output in cell.outputs: | |
91 | for key in _multiline_outputs: |
|
89 | for key in _multiline_outputs: | |
92 | item = output.get(key, None) |
|
90 | item = output.get(key, None) | |
93 | if isinstance(item, basestring): |
|
91 | if isinstance(item, basestring): | |
94 | output[key] = item.splitlines() |
|
92 | output[key] = item.splitlines() | |
95 |
el |
|
93 | else: # text, heading cell | |
96 | pass |
|
|||
97 | else: # text cell |
|
|||
98 | for key in ['source', 'rendered']: |
|
94 | for key in ['source', 'rendered']: | |
99 | item = cell.get(key, None) |
|
95 | item = cell.get(key, None) | |
100 | if isinstance(item, basestring): |
|
96 | if isinstance(item, basestring): | |
101 | cell[key] = item.splitlines() |
|
97 | cell[key] = item.splitlines() | |
102 | return nb |
|
98 | return nb | |
103 |
|
99 | |||
104 | # b64 encode/decode are never actually used, because all bytes objects in |
|
100 | # b64 encode/decode are never actually used, because all bytes objects in | |
105 | # the notebook are already b64-encoded, and we don't need/want to double-encode |
|
101 | # the notebook are already b64-encoded, and we don't need/want to double-encode | |
106 |
|
102 | |||
107 | def base64_decode(nb): |
|
103 | def base64_decode(nb): | |
108 | """Restore all bytes objects in the notebook from base64-encoded strings. |
|
104 | """Restore all bytes objects in the notebook from base64-encoded strings. | |
109 |
|
105 | |||
110 | Note: This is never used |
|
106 | Note: This is never used | |
111 | """ |
|
107 | """ | |
112 | for ws in nb.worksheets: |
|
108 | for ws in nb.worksheets: | |
113 | for cell in ws.cells: |
|
109 | for cell in ws.cells: | |
114 | if cell.cell_type == 'code': |
|
110 | if cell.cell_type == 'code': | |
115 | for output in cell.outputs: |
|
111 | for output in cell.outputs: | |
116 | if 'png' in output: |
|
112 | if 'png' in output: | |
117 | if isinstance(output.png, unicode): |
|
113 | if isinstance(output.png, unicode): | |
118 | output.png = output.png.encode('ascii') |
|
114 | output.png = output.png.encode('ascii') | |
119 | output.png = decodestring(output.png) |
|
115 | output.png = decodestring(output.png) | |
120 | if 'jpeg' in output: |
|
116 | if 'jpeg' in output: | |
121 | if isinstance(output.jpeg, unicode): |
|
117 | if isinstance(output.jpeg, unicode): | |
122 | output.jpeg = output.jpeg.encode('ascii') |
|
118 | output.jpeg = output.jpeg.encode('ascii') | |
123 | output.jpeg = decodestring(output.jpeg) |
|
119 | output.jpeg = decodestring(output.jpeg) | |
124 | return nb |
|
120 | return nb | |
125 |
|
121 | |||
126 |
|
122 | |||
127 | def base64_encode(nb): |
|
123 | def base64_encode(nb): | |
128 | """Base64 encode all bytes objects in the notebook. |
|
124 | """Base64 encode all bytes objects in the notebook. | |
129 |
|
125 | |||
130 | These will be b64-encoded unicode strings |
|
126 | These will be b64-encoded unicode strings | |
131 |
|
127 | |||
132 | Note: This is never used |
|
128 | Note: This is never used | |
133 | """ |
|
129 | """ | |
134 | for ws in nb.worksheets: |
|
130 | for ws in nb.worksheets: | |
135 | for cell in ws.cells: |
|
131 | for cell in ws.cells: | |
136 | if cell.cell_type == 'code': |
|
132 | if cell.cell_type == 'code': | |
137 | for output in cell.outputs: |
|
133 | for output in cell.outputs: | |
138 | if 'png' in output: |
|
134 | if 'png' in output: | |
139 | output.png = encodestring(output.png).decode('ascii') |
|
135 | output.png = encodestring(output.png).decode('ascii') | |
140 | if 'jpeg' in output: |
|
136 | if 'jpeg' in output: | |
141 | output.jpeg = encodestring(output.jpeg).decode('ascii') |
|
137 | output.jpeg = encodestring(output.jpeg).decode('ascii') | |
142 | return nb |
|
138 | return nb | |
143 |
|
139 | |||
144 |
|
140 | |||
145 | class NotebookReader(object): |
|
141 | class NotebookReader(object): | |
146 | """A class for reading notebooks.""" |
|
142 | """A class for reading notebooks.""" | |
147 |
|
143 | |||
148 | def reads(self, s, **kwargs): |
|
144 | def reads(self, s, **kwargs): | |
149 | """Read a notebook from a string.""" |
|
145 | """Read a notebook from a string.""" | |
150 | raise NotImplementedError("loads must be implemented in a subclass") |
|
146 | raise NotImplementedError("loads must be implemented in a subclass") | |
151 |
|
147 | |||
152 | def read(self, fp, **kwargs): |
|
148 | def read(self, fp, **kwargs): | |
153 | """Read a notebook from a file like object""" |
|
149 | """Read a notebook from a file like object""" | |
154 | return self.read(fp.read(), **kwargs) |
|
150 | return self.read(fp.read(), **kwargs) | |
155 |
|
151 | |||
156 |
|
152 | |||
157 | class NotebookWriter(object): |
|
153 | class NotebookWriter(object): | |
158 | """A class for writing notebooks.""" |
|
154 | """A class for writing notebooks.""" | |
159 |
|
155 | |||
160 | def writes(self, nb, **kwargs): |
|
156 | def writes(self, nb, **kwargs): | |
161 | """Write a notebook to a string.""" |
|
157 | """Write a notebook to a string.""" | |
162 | raise NotImplementedError("loads must be implemented in a subclass") |
|
158 | raise NotImplementedError("loads must be implemented in a subclass") | |
163 |
|
159 | |||
164 | def write(self, nb, fp, **kwargs): |
|
160 | def write(self, nb, fp, **kwargs): | |
165 | """Write a notebook to a file like object""" |
|
161 | """Write a notebook to a file like object""" | |
166 | return fp.write(self.writes(nb,**kwargs)) |
|
162 | return fp.write(self.writes(nb,**kwargs)) | |
167 |
|
163 | |||
168 |
|
164 | |||
169 |
|
165 |
@@ -1,134 +1,136 b'' | |||||
1 | from unittest import TestCase |
|
1 | from unittest import TestCase | |
2 |
|
2 | |||
3 | from ..nbbase import ( |
|
3 | from ..nbbase import ( | |
4 | NotebookNode, |
|
4 | NotebookNode, | |
5 | new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output, |
|
5 | new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output, | |
6 | new_author, new_metadata, new_heading_cell |
|
6 | new_author, new_metadata, new_heading_cell | |
7 | ) |
|
7 | ) | |
8 |
|
8 | |||
9 | class TestCell(TestCase): |
|
9 | class TestCell(TestCase): | |
10 |
|
10 | |||
11 | def test_empty_code_cell(self): |
|
11 | def test_empty_code_cell(self): | |
12 | cc = new_code_cell() |
|
12 | cc = new_code_cell() | |
13 | self.assertEquals(cc.cell_type,u'code') |
|
13 | self.assertEquals(cc.cell_type,u'code') | |
14 | self.assertEquals(u'input' not in cc, True) |
|
14 | self.assertEquals(u'input' not in cc, True) | |
15 | self.assertEquals(u'prompt_number' not in cc, True) |
|
15 | self.assertEquals(u'prompt_number' not in cc, True) | |
16 | self.assertEquals(cc.outputs, []) |
|
16 | self.assertEquals(cc.outputs, []) | |
17 | self.assertEquals(cc.collapsed, False) |
|
17 | self.assertEquals(cc.collapsed, False) | |
18 |
|
18 | |||
19 | def test_code_cell(self): |
|
19 | def test_code_cell(self): | |
20 | cc = new_code_cell(input='a=10', prompt_number=0, collapsed=True) |
|
20 | cc = new_code_cell(input='a=10', prompt_number=0, collapsed=True) | |
21 | cc.outputs = [new_output(output_type=u'pyout', |
|
21 | cc.outputs = [new_output(output_type=u'pyout', | |
22 | output_svg=u'foo',output_text=u'10',prompt_number=0)] |
|
22 | output_svg=u'foo',output_text=u'10',prompt_number=0)] | |
23 | self.assertEquals(cc.input, u'a=10') |
|
23 | self.assertEquals(cc.input, u'a=10') | |
24 | self.assertEquals(cc.prompt_number, 0) |
|
24 | self.assertEquals(cc.prompt_number, 0) | |
25 | self.assertEquals(cc.language, u'python') |
|
25 | self.assertEquals(cc.language, u'python') | |
26 | self.assertEquals(cc.outputs[0].svg, u'foo') |
|
26 | self.assertEquals(cc.outputs[0].svg, u'foo') | |
27 | self.assertEquals(cc.outputs[0].text, u'10') |
|
27 | self.assertEquals(cc.outputs[0].text, u'10') | |
28 | self.assertEquals(cc.outputs[0].prompt_number, 0) |
|
28 | self.assertEquals(cc.outputs[0].prompt_number, 0) | |
29 | self.assertEquals(cc.collapsed, True) |
|
29 | self.assertEquals(cc.collapsed, True) | |
30 |
|
30 | |||
31 | def test_pyerr(self): |
|
31 | def test_pyerr(self): | |
32 | o = new_output(output_type=u'pyerr', etype=u'NameError', |
|
32 | o = new_output(output_type=u'pyerr', etype=u'NameError', | |
33 | evalue=u'Name not found', traceback=[u'frame 0', u'frame 1', u'frame 2'] |
|
33 | evalue=u'Name not found', traceback=[u'frame 0', u'frame 1', u'frame 2'] | |
34 | ) |
|
34 | ) | |
35 | self.assertEquals(o.output_type, u'pyerr') |
|
35 | self.assertEquals(o.output_type, u'pyerr') | |
36 | self.assertEquals(o.etype, u'NameError') |
|
36 | self.assertEquals(o.etype, u'NameError') | |
37 | self.assertEquals(o.evalue, u'Name not found') |
|
37 | self.assertEquals(o.evalue, u'Name not found') | |
38 | self.assertEquals(o.traceback, [u'frame 0', u'frame 1', u'frame 2']) |
|
38 | self.assertEquals(o.traceback, [u'frame 0', u'frame 1', u'frame 2']) | |
39 |
|
39 | |||
40 | def test_empty_html_cell(self): |
|
40 | def test_empty_html_cell(self): | |
41 | tc = new_text_cell(u'html') |
|
41 | tc = new_text_cell(u'html') | |
42 | self.assertEquals(tc.cell_type, u'html') |
|
42 | self.assertEquals(tc.cell_type, u'html') | |
43 | self.assertEquals(u'source' not in tc, True) |
|
43 | self.assertEquals(u'source' not in tc, True) | |
44 | self.assertEquals(u'rendered' not in tc, True) |
|
44 | self.assertEquals(u'rendered' not in tc, True) | |
45 |
|
45 | |||
46 | def test_html_cell(self): |
|
46 | def test_html_cell(self): | |
47 | tc = new_text_cell(u'html', 'hi', 'hi') |
|
47 | tc = new_text_cell(u'html', 'hi', 'hi') | |
48 | self.assertEquals(tc.source, u'hi') |
|
48 | self.assertEquals(tc.source, u'hi') | |
49 | self.assertEquals(tc.rendered, u'hi') |
|
49 | self.assertEquals(tc.rendered, u'hi') | |
50 |
|
50 | |||
51 | def test_empty_markdown_cell(self): |
|
51 | def test_empty_markdown_cell(self): | |
52 | tc = new_text_cell(u'markdown') |
|
52 | tc = new_text_cell(u'markdown') | |
53 | self.assertEquals(tc.cell_type, u'markdown') |
|
53 | self.assertEquals(tc.cell_type, u'markdown') | |
54 | self.assertEquals(u'source' not in tc, True) |
|
54 | self.assertEquals(u'source' not in tc, True) | |
55 | self.assertEquals(u'rendered' not in tc, True) |
|
55 | self.assertEquals(u'rendered' not in tc, True) | |
56 |
|
56 | |||
57 | def test_markdown_cell(self): |
|
57 | def test_markdown_cell(self): | |
58 | tc = new_text_cell(u'markdown', 'hi', 'hi') |
|
58 | tc = new_text_cell(u'markdown', 'hi', 'hi') | |
59 | self.assertEquals(tc.source, u'hi') |
|
59 | self.assertEquals(tc.source, u'hi') | |
60 | self.assertEquals(tc.rendered, u'hi') |
|
60 | self.assertEquals(tc.rendered, u'hi') | |
61 |
|
61 | |||
62 | def test_empty_rst_cell(self): |
|
62 | def test_empty_rst_cell(self): | |
63 | tc = new_text_cell(u'rst') |
|
63 | tc = new_text_cell(u'rst') | |
64 | self.assertEquals(tc.cell_type, u'rst') |
|
64 | self.assertEquals(tc.cell_type, u'rst') | |
65 | self.assertEquals(u'source' not in tc, True) |
|
65 | self.assertEquals(u'source' not in tc, True) | |
66 | self.assertEquals(u'rendered' not in tc, True) |
|
66 | self.assertEquals(u'rendered' not in tc, True) | |
67 |
|
67 | |||
68 | def test_rst_cell(self): |
|
68 | def test_rst_cell(self): | |
69 | tc = new_text_cell(u'rst', 'hi', 'hi') |
|
69 | tc = new_text_cell(u'rst', 'hi', 'hi') | |
70 | self.assertEquals(tc.source, u'hi') |
|
70 | self.assertEquals(tc.source, u'hi') | |
71 | self.assertEquals(tc.rendered, u'hi') |
|
71 | self.assertEquals(tc.rendered, u'hi') | |
72 |
|
72 | |||
73 | def test_empty_heading_cell(self): |
|
73 | def test_empty_heading_cell(self): | |
74 | tc = new_heading_cell() |
|
74 | tc = new_heading_cell() | |
75 | self.assertEquals(tc.cell_type, u'heading') |
|
75 | self.assertEquals(tc.cell_type, u'heading') | |
76 | self.assertEquals(u'source' not in tc, True) |
|
76 | self.assertEquals(u'source' not in tc, True) | |
|
77 | self.assertEquals(u'rendered' not in tc, True) | |||
77 |
|
78 | |||
78 | def test_heading_cell(self): |
|
79 | def test_heading_cell(self): | |
79 |
tc = new_heading_cell(u' |
|
80 | tc = new_heading_cell(u'hi', u'hi', level=2) | |
80 |
self.assertEquals(tc.source, u' |
|
81 | self.assertEquals(tc.source, u'hi') | |
|
82 | self.assertEquals(tc.rendered, u'hi') | |||
81 | self.assertEquals(tc.level, 2) |
|
83 | self.assertEquals(tc.level, 2) | |
82 |
|
84 | |||
83 |
|
85 | |||
84 | class TestWorksheet(TestCase): |
|
86 | class TestWorksheet(TestCase): | |
85 |
|
87 | |||
86 | def test_empty_worksheet(self): |
|
88 | def test_empty_worksheet(self): | |
87 | ws = new_worksheet() |
|
89 | ws = new_worksheet() | |
88 | self.assertEquals(ws.cells,[]) |
|
90 | self.assertEquals(ws.cells,[]) | |
89 | self.assertEquals(u'name' not in ws, True) |
|
91 | self.assertEquals(u'name' not in ws, True) | |
90 |
|
92 | |||
91 | def test_worksheet(self): |
|
93 | def test_worksheet(self): | |
92 | cells = [new_code_cell(), new_text_cell(u'html')] |
|
94 | cells = [new_code_cell(), new_text_cell(u'html')] | |
93 | ws = new_worksheet(cells=cells,name=u'foo') |
|
95 | ws = new_worksheet(cells=cells,name=u'foo') | |
94 | self.assertEquals(ws.cells,cells) |
|
96 | self.assertEquals(ws.cells,cells) | |
95 | self.assertEquals(ws.name,u'foo') |
|
97 | self.assertEquals(ws.name,u'foo') | |
96 |
|
98 | |||
97 | class TestNotebook(TestCase): |
|
99 | class TestNotebook(TestCase): | |
98 |
|
100 | |||
99 | def test_empty_notebook(self): |
|
101 | def test_empty_notebook(self): | |
100 | nb = new_notebook() |
|
102 | nb = new_notebook() | |
101 | self.assertEquals(nb.worksheets, []) |
|
103 | self.assertEquals(nb.worksheets, []) | |
102 | self.assertEquals(nb.metadata, NotebookNode()) |
|
104 | self.assertEquals(nb.metadata, NotebookNode()) | |
103 | self.assertEquals(nb.nbformat,2) |
|
105 | self.assertEquals(nb.nbformat,2) | |
104 |
|
106 | |||
105 | def test_notebook(self): |
|
107 | def test_notebook(self): | |
106 | worksheets = [new_worksheet(),new_worksheet()] |
|
108 | worksheets = [new_worksheet(),new_worksheet()] | |
107 | metadata = new_metadata(name=u'foo') |
|
109 | metadata = new_metadata(name=u'foo') | |
108 | nb = new_notebook(metadata=metadata,worksheets=worksheets) |
|
110 | nb = new_notebook(metadata=metadata,worksheets=worksheets) | |
109 | self.assertEquals(nb.metadata.name,u'foo') |
|
111 | self.assertEquals(nb.metadata.name,u'foo') | |
110 | self.assertEquals(nb.worksheets,worksheets) |
|
112 | self.assertEquals(nb.worksheets,worksheets) | |
111 | self.assertEquals(nb.nbformat,2) |
|
113 | self.assertEquals(nb.nbformat,2) | |
112 |
|
114 | |||
113 | class TestMetadata(TestCase): |
|
115 | class TestMetadata(TestCase): | |
114 |
|
116 | |||
115 | def test_empty_metadata(self): |
|
117 | def test_empty_metadata(self): | |
116 | md = new_metadata() |
|
118 | md = new_metadata() | |
117 | self.assertEquals(u'name' not in md, True) |
|
119 | self.assertEquals(u'name' not in md, True) | |
118 | self.assertEquals(u'authors' not in md, True) |
|
120 | self.assertEquals(u'authors' not in md, True) | |
119 | self.assertEquals(u'license' not in md, True) |
|
121 | self.assertEquals(u'license' not in md, True) | |
120 | self.assertEquals(u'saved' not in md, True) |
|
122 | self.assertEquals(u'saved' not in md, True) | |
121 | self.assertEquals(u'modified' not in md, True) |
|
123 | self.assertEquals(u'modified' not in md, True) | |
122 | self.assertEquals(u'gistid' not in md, True) |
|
124 | self.assertEquals(u'gistid' not in md, True) | |
123 |
|
125 | |||
124 | def test_metadata(self): |
|
126 | def test_metadata(self): | |
125 | authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com')] |
|
127 | authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com')] | |
126 | md = new_metadata(name=u'foo',license=u'BSD',created=u'today', |
|
128 | md = new_metadata(name=u'foo',license=u'BSD',created=u'today', | |
127 | modified=u'now',gistid=u'21341231',authors=authors) |
|
129 | modified=u'now',gistid=u'21341231',authors=authors) | |
128 | self.assertEquals(md.name, u'foo') |
|
130 | self.assertEquals(md.name, u'foo') | |
129 | self.assertEquals(md.license, u'BSD') |
|
131 | self.assertEquals(md.license, u'BSD') | |
130 | self.assertEquals(md.created, u'today') |
|
132 | self.assertEquals(md.created, u'today') | |
131 | self.assertEquals(md.modified, u'now') |
|
133 | self.assertEquals(md.modified, u'now') | |
132 | self.assertEquals(md.gistid, u'21341231') |
|
134 | self.assertEquals(md.gistid, u'21341231') | |
133 | self.assertEquals(md.authors, authors) |
|
135 | self.assertEquals(md.authors, authors) | |
134 |
|
136 |
General Comments 0
You need to be logged in to leave comments.
Login now