U
    1e)                     @   sL   d Z ddlZddlmZ G dd deZG dd deZG dd	 d	eZdS )
zIO capturing utilities.    N)StringIOc                   @   sh   e Zd ZdddZdd Zdd Zdd	d
Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )
RichOutputNFc                 C   s(   |pi | _ |pi | _|pi | _|| _d S Ndatametadata	transientupdate)selfr   r   r   r	    r   9/tmp/pip-unpacked-wheel-3hxk2k58/IPython/utils/capture.py__init__   s    


zRichOutput.__init__c                 C   s(   ddl m} || j| j| j| jd d S )Nr   )publish_display_datar   )ZIPython.displayr   r   r   r   r	   )r
   r   r   r   r   display   s
    
 zRichOutput.displayc                 C   s8   || j krd S | j | }|| jkr0|| j| fS |S d S r   r   r   )r
   mimer   r   r   r   _repr_mime_   s    


zRichOutput._repr_mime_c                 C   s   | j | jfS r   r   )r
   includeexcluder   r   r   _repr_mimebundle_%   s    zRichOutput._repr_mimebundle_c                 C   s
   |  dS )Nz	text/htmlr   r
   r   r   r   _repr_html_(   s    zRichOutput._repr_html_c                 C   s
   |  dS )Nz
text/latexr   r   r   r   r   _repr_latex_+   s    zRichOutput._repr_latex_c                 C   s
   |  dS )Nzapplication/jsonr   r   r   r   r   _repr_json_.   s    zRichOutput._repr_json_c                 C   s
   |  dS )Nzapplication/javascriptr   r   r   r   r   _repr_javascript_1   s    zRichOutput._repr_javascript_c                 C   s
   |  dS )Nz	image/pngr   r   r   r   r   
_repr_png_4   s    zRichOutput._repr_png_c                 C   s
   |  dS )Nz
image/jpegr   r   r   r   r   _repr_jpeg_7   s    zRichOutput._repr_jpeg_c                 C   s
   |  dS )Nzimage/svg+xmlr   r   r   r   r   
_repr_svg_:   s    zRichOutput._repr_svg_)NNNF)NN)__name__
__module____qualname__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s   
	
r   c                   @   sR   e Zd ZdZdddZdd Zedd Zed	d
 Zedd Z	dd Z
e
ZdS )
CapturedIOa  Simple object for containing captured stdout/err and rich display StringIO objects

    Each instance `c` has three attributes:

    - ``c.stdout`` : standard output as a string
    - ``c.stderr`` : standard error as a string
    - ``c.outputs``: a list of rich display outputs

    Additionally, there's a ``c.show()`` method which will print all of the
    above in the same order, and can be invoked simply via ``c()``.
    Nc                 C   s"   || _ || _|d krg }|| _d S r   )_stdout_stderr_outputs)r
   stdoutstderroutputsr   r   r   r   K   s
    zCapturedIO.__init__c                 C   s   | j S r   )r&   r   r   r   r   __str__R   s    zCapturedIO.__str__c                 C   s   | j s
dS | j  S )zCaptured standard output )r#   getvaluer   r   r   r   r&   U   s    zCapturedIO.stdoutc                 C   s   | j s
dS | j  S )zCaptured standard errorr*   )r$   r+   r   r   r   r   r'   \   s    zCapturedIO.stderrc                 C   s   dd | j D S )a  A list of the captured rich display outputs, if any.

        If you have a CapturedIO object ``c``, these can be displayed in IPython
        using::

            from IPython.display import display
            for o in c.outputs:
                display(o)
        c                 S   s   g | ]}t f |qS r   )r   ).0kargsr   r   r   
<listcomp>n   s     z&CapturedIO.outputs.<locals>.<listcomp>)r%   r   r   r   r   r(   c   s    zCapturedIO.outputsc                 C   sN   t j| j t j| j t j  t j  | jD ]}tf |  q6dS )z0write my output to sys.stdout/err as appropriateN)sysr&   writer'   flushr%   r   r   )r
   r-   r   r   r   showp   s    


zCapturedIO.show)N)r   r    r!   __doc__r   r)   propertyr&   r'   r(   r2   __call__r   r   r   r   r"   >   s   



	r"   c                   @   s6   e Zd ZdZdZdZdZd
ddZdd Zdd Z	d	S )capture_outputz(context manager for capturing stdout/errTc                 C   s   || _ || _|| _d | _d S r   )r&   r'   r   shell)r
   r&   r'   r   r   r   r   r      s    zcapture_output.__init__c                 C   s   ddl m} ddlm} ddlm} tj| _tj	| _
| jrX| | _| jd krXd | _d| _d  } }}| jrvt  }t_| j	rt  }t_	| jr| jj| _| | j_| jjj}tj| _|| j|dt_t|||S )Nr   )get_ipython)CapturingDisplayPublisher)CapturingDisplayHookF)r7   r(   )ZIPython.core.getipythonr8   ZIPython.core.displaypubr9   ZIPython.core.displayhookr:   r/   r&   
sys_stdoutr'   
sys_stderrr   r7   save_display_pubr   display_pubr(   displayhooksave_display_hookr"   )r
   r8   r9   r:   r&   r'   r(   r   r   r   	__enter__   s0    



zcapture_output.__enter__c                 C   s2   | j t_| jt_| jr.| jr.| j| j_| j	t_
d S r   )r;   r/   r&   r<   r'   r   r7   r=   r>   r@   r?   )r
   exc_type	exc_value	tracebackr   r   r   __exit__   s
    
zcapture_output.__exit__N)TTT)
r   r    r!   r3   r&   r'   r   r   rA   rE   r   r   r   r   r6   |   s   
r6   )r3   r/   ior   objectr   r"   r6   r   r   r   r   <module>   s
   .>