Show More
@@ -1,75 +1,75 | |||||
1 | """Module that allows latex output notebooks to be conditioned before |
|
1 | """Module that allows latex output notebooks to be conditioned before | |
2 | they are converted. Exposes a decorator (@cell_preprocessor) in |
|
2 | they are converted. Exposes a decorator (@cell_preprocessor) in | |
3 | addition to the coalesce_streams pre-proccessor. |
|
3 | addition to the coalesce_streams pre-proccessor. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Functions |
|
14 | # Functions | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | def cell_preprocessor(function): |
|
17 | def cell_preprocessor(function): | |
18 | """ |
|
18 | """ | |
19 | Wrap a function to be executed on all cells of a notebook |
|
19 | Wrap a function to be executed on all cells of a notebook | |
20 |
|
20 | |||
21 | Wrapped Parameters |
|
21 | Wrapped Parameters | |
22 | ---------- |
|
22 | ---------- | |
23 | cell : NotebookNode cell |
|
23 | cell : NotebookNode cell | |
24 | Notebook cell being processed |
|
24 | Notebook cell being processed | |
25 | resources : dictionary |
|
25 | resources : dictionary | |
26 | Additional resources used in the conversion process. Allows |
|
26 | Additional resources used in the conversion process. Allows | |
27 | preprocessors to pass variables into the Jinja engine. |
|
27 | preprocessors to pass variables into the Jinja engine. | |
28 | index : int |
|
28 | index : int | |
29 | Index of the cell being processed |
|
29 | Index of the cell being processed | |
30 | """ |
|
30 | """ | |
31 |
|
31 | |||
32 | def wrappedfunc(nb, resources): |
|
32 | def wrappedfunc(nb, resources): | |
33 | for worksheet in nb.worksheets : |
|
33 | for worksheet in nb.worksheets : | |
34 | for index, cell in enumerate(worksheet.cells): |
|
34 | for index, cell in enumerate(worksheet.cells): | |
35 | worksheet.cells[index], resources = function(cell, resources, index) |
|
35 | worksheet.cells[index], resources = function(cell, resources, index) | |
36 | return nb, resources |
|
36 | return nb, resources | |
37 | return wrappedfunc |
|
37 | return wrappedfunc | |
38 |
|
38 | |||
39 |
|
39 | |||
40 | @cell_preprocessor |
|
40 | @cell_preprocessor | |
41 | def coalesce_streams(cell, resources, index): |
|
41 | def coalesce_streams(cell, resources, index): | |
42 | """ |
|
42 | """ | |
43 | Merge consecutive sequences of stream output into single stream |
|
43 | Merge consecutive sequences of stream output into single stream | |
44 | to prevent extra newlines inserted at flush calls |
|
44 | to prevent extra newlines inserted at flush calls | |
45 |
|
45 | |||
46 | Parameters |
|
46 | Parameters | |
47 | ---------- |
|
47 | ---------- | |
48 | cell : NotebookNode cell |
|
48 | cell : NotebookNode cell | |
49 | Notebook cell being processed |
|
49 | Notebook cell being processed | |
50 | resources : dictionary |
|
50 | resources : dictionary | |
51 | Additional resources used in the conversion process. Allows |
|
51 | Additional resources used in the conversion process. Allows | |
52 | transformers to pass variables into the Jinja engine. |
|
52 | transformers to pass variables into the Jinja engine. | |
53 | index : int |
|
53 | index : int | |
54 | Index of the cell being processed |
|
54 | Index of the cell being processed | |
55 | """ |
|
55 | """ | |
56 |
|
56 | |||
57 | outputs = cell.get('outputs', []) |
|
57 | outputs = cell.get('outputs', []) | |
58 | if not outputs: |
|
58 | if not outputs: | |
59 | return cell, resources |
|
59 | return cell, resources | |
60 |
|
60 | |||
61 | last = outputs[0] |
|
61 | last = outputs[0] | |
62 | new_outputs = [last] |
|
62 | new_outputs = [last] | |
63 |
|
63 | |||
64 | for output in outputs[1:]: |
|
64 | for output in outputs[1:]: | |
65 | if (output.output_type == 'stream' and |
|
65 | if (output.output_type == 'stream' and | |
66 | last.output_type == 'stream' and |
|
66 | last.output_type == 'stream' and | |
67 | last.stream == output.stream |
|
67 | last.stream == output.stream | |
68 | ): |
|
68 | ): | |
69 | last.text += output.text |
|
69 | last.text += output.text | |
70 | else: |
|
70 | else: | |
71 | new_outputs.append(output) |
|
71 | new_outputs.append(output) | |
72 | last = output |
|
72 | last = output | |
73 |
|
73 | |||
74 | cell.outputs = new_outputs |
|
74 | cell.outputs = new_outputs | |
75 | return cell, resources |
|
75 | return cell, resources |
@@ -1,38 +1,60 | |||||
1 | """ |
|
1 | """ | |
2 | Module with tests for the coalescestreams preprocessor |
|
2 | Module with tests for the coalescestreams preprocessor | |
3 | """ |
|
3 | """ | |
4 |
|
4 | |||
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
|
17 | from IPython.nbformat import current as nbformat | |||
|
18 | ||||
17 | from .base import PreprocessorTestsBase |
|
19 | from .base import PreprocessorTestsBase | |
18 | from ..coalescestreams import coalesce_streams |
|
20 | from ..coalescestreams import coalesce_streams | |
19 |
|
21 | |||
20 |
|
22 | |||
21 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
22 | # Class |
|
24 | # Class | |
23 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
24 |
|
26 | |||
25 | class TestCoalesceStreams(PreprocessorTestsBase): |
|
27 | class TestCoalesceStreams(PreprocessorTestsBase): | |
26 | """Contains test functions for coalescestreams.py""" |
|
28 | """Contains test functions for coalescestreams.py""" | |
27 |
|
29 | |||
28 | def test_coalesce_streams(self): |
|
30 | def test_coalesce_streams(self): | |
29 | """coalesce_streams preprocessor output test""" |
|
31 | """coalesce_streams preprocessor output test""" | |
30 | nb = self.build_notebook() |
|
32 | nb = self.build_notebook() | |
31 | res = self.build_resources() |
|
33 | res = self.build_resources() | |
32 | nb, res = coalesce_streams(nb, res) |
|
34 | nb, res = coalesce_streams(nb, res) | |
33 | outputs = nb.worksheets[0].cells[0].outputs |
|
35 | outputs = nb.worksheets[0].cells[0].outputs | |
34 | self.assertEqual(outputs[0].text, "a") |
|
36 | self.assertEqual(outputs[0].text, "a") | |
35 | self.assertEqual(outputs[1].output_type, "text") |
|
37 | self.assertEqual(outputs[1].output_type, "text") | |
36 | self.assertEqual(outputs[2].text, "cd") |
|
38 | self.assertEqual(outputs[2].text, "cd") | |
37 | self.assertEqual(outputs[3].text, "ef") |
|
39 | self.assertEqual(outputs[3].text, "ef") | |
38 |
|
40 | |||
|
41 | ||||
|
42 | def test_coalesce_sequenced_streams(self): | |||
|
43 | """Can the coalesce streams preprocessor merge a sequence of streams?""" | |||
|
44 | ||||
|
45 | outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="0"), | |||
|
46 | nbformat.new_output(output_type="stream", stream="stdout", output_text="1"), | |||
|
47 | nbformat.new_output(output_type="stream", stream="stdout", output_text="2"), | |||
|
48 | nbformat.new_output(output_type="stream", stream="stdout", output_text="3"), | |||
|
49 | nbformat.new_output(output_type="stream", stream="stdout", output_text="4"), | |||
|
50 | nbformat.new_output(output_type="stream", stream="stdout", output_text="5"), | |||
|
51 | nbformat.new_output(output_type="stream", stream="stdout", output_text="6"), | |||
|
52 | nbformat.new_output(output_type="stream", stream="stdout", output_text="7")] | |||
|
53 | cells=[nbformat.new_code_cell(input="# None", prompt_number=1,outputs=outputs)] | |||
|
54 | worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)] | |||
|
55 | ||||
|
56 | nb = nbformat.new_notebook(name="notebook1", worksheets=worksheets) | |||
|
57 | res = self.build_resources() | |||
|
58 | nb, res = coalesce_streams(nb, res) | |||
|
59 | outputs = nb.worksheets[0].cells[0].outputs | |||
|
60 | self.assertEqual(outputs[0].text, u'01234567') |
General Comments 0
You need to be logged in to leave comments.
Login now