##// END OF EJS Templates
Merge pull request #4498 from takluyver/daemon-streamcapturer...
Merge pull request #4498 from takluyver/daemon-streamcapturer Daemon StreamCapturer The StreamCapturer should die if the main thread crashes. On Shiningpanda, a failure in another nose plugin has been causing the tests to hang, because the main thread exits, but the StreamCapturer thread is still alive. Under normal conditions, the thread will still be shut down cleanly - it will only die a messy death if the main thread does.

File last commit:

r12553:43ed80e5
r13524:b1976c99 merge
Show More
coalescestreams.py
76 lines | 2.4 KiB | text/x-python | PythonLexer
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """Module that allows latex output notebooks to be conditioned before
they are converted. Exposes a decorator (@cell_preprocessor) in
addition to the coalesce_streams pre-proccessor.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
Jonathan Frederic
Post code-review, extended refactor.
r10485
Jonathan Frederic
Cleanup and refactor, transformers
r10674 #-----------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------
Jonathan Frederic
Post code-review, extended refactor.
r10485
Jonathan Frederic
Cleanup and refactor, transformers
r10674 def cell_preprocessor(function):
"""
Wrap a function to be executed on all cells of a notebook
Wrapped Parameters
Thomas Kluyver
Improvements to docs formatting.
r12553 ------------------
Jonathan Frederic
Cleanup and refactor, transformers
r10674 cell : NotebookNode cell
Notebook cell being processed
resources : dictionary
Additional resources used in the conversion process. Allows
Paul Ivanov
replace 'transformer' with 'preprocessor'
r12219 preprocessors to pass variables into the Jinja engine.
Jonathan Frederic
Cleanup and refactor, transformers
r10674 index : int
Index of the cell being processed
"""
def wrappedfunc(nb, resources):
Jonathan Frederic
Post code-review, extended refactor.
r10485 for worksheet in nb.worksheets :
for index, cell in enumerate(worksheet.cells):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 worksheet.cells[index], resources = function(cell, resources, index)
return nb, resources
Jonathan Frederic
Post code-review, extended refactor.
r10485 return wrappedfunc
@cell_preprocessor
Jonathan Frederic
Cleanup and refactor, transformers
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
Post code-review, extended refactor.
r10485 outputs = cell.get('outputs', [])
if not outputs:
Jonathan Frederic
Cleanup and refactor, transformers
r10674 return cell, resources
Jonathan Frederic
Post code-review, extended refactor.
r10485 last = outputs[0]
new_outputs = [last]
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Post code-review, extended refactor.
r10485 for output in outputs[1:]:
if (output.output_type == 'stream' and
last.output_type == 'stream' and
last.stream == output.stream
):
last.text += output.text
else:
new_outputs.append(output)
Jonathan Frederic
FIX, coalescestreams incorrect nesting.
r12268 last = output
Jonathan Frederic
Post code-review, extended refactor.
r10485
cell.outputs = new_outputs
Jonathan Frederic
Cleanup and refactor, transformers
r10674 return cell, resources