a
    ø£BeÑ  ã                   @   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                    sB   t ƒ  dˆ j¡ t| jƒD ]\}}ˆ |||ƒ\| j|< }q| |fS )NzApplying preprocessor: %s)r   ÚdebugÚ__name__Ú	enumerateÚcells)ÚnbÚ	resourcesÚindexÚcell©Úfunction© új/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/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}|dd… D ]D}|jdkrh|jdkrh|j|jkrh| j|j7  _q2| |¡ |}q2|D ](}|jdkr|d|jv r|t d|j¡|_q||| _| |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úÚ )ÚgetÚoutput_typeÚnameÚtextÚappendÚcr_patÚsubr   )r
   r   r	   r   ÚlastZnew_outputsÚoutputr   r   r   Úcoalesce_streams(   s(    ÿþ
ý
r!   )	Ú__doc__r   ÚreZtraitlets.logr   r   Úcompiler   r!   r   r   r   r   Ú<module>   s   
