coalescestreams.py
75 lines
| 2.2 KiB
| text/x-python
|
PythonLexer
MinRK
|
r16207 | """Preprocessor for merging consecutive stream outputs for easier handling.""" | ||
# Copyright (c) IPython Development Team. | ||||
Jonathan Frederic
|
r10674 | # Distributed under the terms of the Modified BSD License. | ||
Jonathan Frederic
|
r10485 | |||
Jonathan Frederic
|
r15869 | import re | ||
MinRK
|
r18580 | from IPython.utils.log import get_logger | ||
Jonathan Frederic
|
r10485 | |||
Jonathan Frederic
|
r10674 | def cell_preprocessor(function): | ||
""" | ||||
Wrap a function to be executed on all cells of a notebook | ||||
Thomas Kluyver
|
r13587 | The wrapped function should have these parameters: | ||
Thomas Kluyver
|
r12553 | |||
Jonathan Frederic
|
r10674 | cell : NotebookNode cell | ||
Notebook cell being processed | ||||
resources : dictionary | ||||
Additional resources used in the conversion process. Allows | ||||
Paul Ivanov
|
r12219 | preprocessors to pass variables into the Jinja engine. | ||
Jonathan Frederic
|
r10674 | index : int | ||
Index of the cell being processed | ||||
""" | ||||
def wrappedfunc(nb, resources): | ||||
MinRK
|
r18580 | get_logger().debug( | ||
MinRK
|
r16207 | "Applying preprocessor: %s", function.__name__ | ||
) | ||||
MinRK
|
r18580 | for index, cell in enumerate(nb.cells): | ||
nb.cells[index], resources = function(cell, resources, index) | ||||
Jonathan Frederic
|
r10674 | return nb, resources | ||
Jonathan Frederic
|
r10485 | return wrappedfunc | ||
MinRK
|
r16206 | cr_pat = re.compile(r'.*\r(?=[^\n])') | ||
Jonathan Frederic
|
r10485 | |||
@cell_preprocessor | ||||
Jonathan Frederic
|
r10674 | def coalesce_streams(cell, resources, index): | ||
""" | ||||
Merge consecutive sequences of stream output into single stream | ||||
to prevent extra newlines inserted at flush calls | ||||
Parameters | ||||
---------- | ||||
cell : NotebookNode cell | ||||
Notebook cell being processed | ||||
resources : dictionary | ||||
Additional resources used in the conversion process. Allows | ||||
transformers to pass variables into the Jinja engine. | ||||
index : int | ||||
Index of the cell being processed | ||||
""" | ||||
Jonathan Frederic
|
r10485 | outputs = cell.get('outputs', []) | ||
if not outputs: | ||||
Jonathan Frederic
|
r10674 | return cell, resources | ||
Jonathan Frederic
|
r10485 | last = outputs[0] | ||
new_outputs = [last] | ||||
for output in outputs[1:]: | ||||
if (output.output_type == 'stream' and | ||||
last.output_type == 'stream' and | ||||
MinRK
|
r18580 | last.name == output.name | ||
Jonathan Frederic
|
r10485 | ): | ||
Jonathan Frederic
|
r15869 | last.text += output.text | ||
Jonathan Frederic
|
r10485 | else: | ||
new_outputs.append(output) | ||||
Jonathan Frederic
|
r12268 | last = output | ||
MinRK
|
r16206 | |||
# process \r characters | ||||
for output in new_outputs: | ||||
MinRK
|
r16249 | if output.output_type == 'stream' and '\r' in output.text: | ||
MinRK
|
r16206 | output.text = cr_pat.sub('', output.text) | ||
Jonathan Frederic
|
r10485 | |||
cell.outputs = new_outputs | ||||
Jonathan Frederic
|
r10674 | return cell, resources | ||