B
    KZ                 @   sB   d Z ddlZddlZddlmZ dd ZedZedd ZdS )	zHPreprocessor for merging consecutive stream outputs for easier handling.    N)
get_loggerc                s   t   fdd}|S )a  
    Wrap a function to be executed on all cells of a notebook
    
    The wrapped function should have these parameters:
    
    cell : NotebookNode cell
        Notebook cell being processed
    resources : dictionary
        Additional resources used in the conversion process.  Allows
        preprocessors to pass variables into the Jinja engine.
    index : int
        Index of the cell being processed
    c                sF   t  d j x,t| jD ]\}} |||\| j|< }qW | |fS )NzApplying preprocessor: %s)r   debug__name__	enumerateZcells)Znb	resourcesindexcell)function Flib/python3.7/site-packages/nbconvert/preprocessors/coalescestreams.pywrappedfunc   s
    
z&cell_preprocessor.<locals>.wrappedfunc)	functoolswraps)r	   r   r
   )r	   r   cell_preprocessor
   s    r   z.*\r(?=[^\n])c             C   s   |  dg }|s| |fS |d }|g}xT|dd D ]D}|jdkrj|jdkrj|j|jkrj| j|j7  _q4|| |}q4W x0|D ](}|jdkrd|jkrtd|j|_qW || _| |fS )a  
    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
    outputsr      Nstream )getZoutput_typenametextappendcr_patsubr   )r   r   r   r   ZlastZnew_outputsoutputr
   r
   r   coalesce_streams$   s"    



r   )	__doc__rer   Ztraitlets.logr   r   compiler   r   r
   r
   r
   r   <module>   s   
