a
    4ñ»j¦  ã                   @   sˆ   d Z ddlmZ ddlmZ ddlZddlmZmZ ddl	m
Z
 ddlmZ dd	d
„ZG dd„ deƒZG dd„ deƒZeddd„ƒZdS )z-
Capture output (stdout, stderr), logs, etc.
é    )Úabsolute_import)ÚcontextmanagerN)ÚStringIOÚPY2)ÚLoggingCapture)ÚtextÚ
c                 C   s0   |r,| r(|r|   |¡s| |7 } | |7 } n|} | S ©N)Úendswith)ÚvalueZ	more_textÚ	separator© r   ú4E:\soccer\ml_env\Lib\site-packages\behave/capture.pyÚadd_text_to   s    
r   c                   @   sl   e Zd ZdZdZdZddd„Zdd„ Zer4d	d
„ Z	ndd„ Z
edd„ ƒZdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚCapturedz+Stores and aggregates captured output data.Ú r   Nc                 C   s(   |p| j | _|p| j | _|p | j | _d S r	   ©ÚemptyÚstdoutÚstderrÚ
log_output)Úselfr   r   r   r   r   r   Ú__init__   s    zCaptured.__init__c                 C   s   | j | _| j | _| j | _d S r	   r   ©r   r   r   r   Úreset"   s    zCaptured.resetc                 C   s   t | jp| jp| jƒS r	   ©Úboolr   r   r   r   r   r   r   Ú__nonzero__)   s    zCaptured.__nonzero__c                 C   s   t | jp| jp| jƒS r	   r   r   r   r   r   Ú__bool__,   s    zCaptured.__bool__c                 C   s"   | j }t|| jƒ}t|| jƒ}|S )zWMakes a simple report of the captured data by concatenating
        all parts.
        )r   r   r   r   )r   Zoutput_textr   r   r   Úoutput/   s    zCaptured.outputc                 C   sN   t |tƒsJ ‚t| j|j| jƒ| _t| j|j| jƒ| _t| j|j| jƒ| _| S )z¿Adds/appends captured output data to this object.

        :param captured:    Captured object whose data should be added.
        :return: self, to allow daisy-chaining (if needed).
        )Ú
isinstancer   r   r   Úlinesepr   r   )r   Úcapturedr   r   r   Úadd9   s    
ÿzCaptured.addc                 C   sz   g }| j r(dt| j ƒ ¡ dg}| |¡ | jrLdt| jƒ ¡ dg}| |¡ | jrjdt| jƒg}| |¡ | j |¡ ¡ S )zcMakes a detailled report of the captured output data.

        :returns: Report as string.
        zCaptured stdout:r   zCaptured stderr:zCaptured logging:)	r   Ú_textÚrstripÚextendr   r   r!   ÚjoinÚstrip)r   Zreport_partsÚpartsr   r   r   Úmake_reportF   s    


zCaptured.make_reportc                 C   s   t | j| j| jƒ}| |¡S )zÛSupports incremental add::

            captured1 = Captured("Hello")
            captured2 = Captured("World")
            captured3 = captured1 + captured2
            assert captured3.stdout == "Hello
World"
        )r   r   r   r   r#   )r   ÚotherZnew_datar   r   r   Ú__add__W   s    zCaptured.__add__c                 C   s
   |   |¡S )zÐSupports incremental add::

            captured1 = Captured("Hello")
            captured2 = Captured("World")
            captured1 += captured2
            assert captured1.stdout == "Hello
World"
        )r#   )r   r+   r   r   r   Ú__iadd__b   s    zCaptured.__iadd__)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r   r   r   r   r   Úpropertyr   r#   r*   r,   r-   r   r   r   r   r      s   


	r   c                   @   sL   e Zd ZdZdd„ Zedd„ ƒZdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ ZdS )ÚCaptureControllerz@Simplifies the lifecycle to capture output from various sources.c                 C   s(   || _ d | _d | _d | _d | _d | _d S r	   )ÚconfigÚstdout_captureÚstderr_captureÚlog_captureÚ
old_stdoutÚ
old_stderr)r   r4   r   r   r   r   o   s    zCaptureController.__init__c                 C   sl   d}d}d}| j jr(| jr(t| j ¡ ƒ}| j jrD| jrDt| j ¡ ƒ}| j jr`| jr`t| j ¡ ƒ}t|||ƒS )zƒProvides access of the captured output data.

        :return: Object that stores the captured output parts (as Captured).
        N)r4   r5   r$   Úgetvaluer6   r7   r   )r   r   r   Zlog_outr   r   r   r"   w   s    zCaptureController.capturedc                 C   sf   |d usJ ‚| j jr$tƒ | _| j|_| j jr<tƒ | _| j|_| j jrbt| j ƒ| _| j ¡  | j|_d S r	   )r4   r5   r   r6   r7   r   Zinveigle)r   Úcontextr   r   r   Úsetup_captureˆ   s    
zCaptureController.setup_capturec                 C   s`   | j jr.| jstj| _| jt_tj| ju s.J ‚| j jr\| jsLtj| _| jt_tj| ju s\J ‚d S r	   ©r4   r5   r8   Úsysr   r6   r9   r   r   r   r   r   Ústart_capture—   s    zCaptureController.start_capturec                 C   s\   | j jr,| jr| jt_d | _tj| jus,J ‚| j jrX| jrH| jt_d | _tj| jusXJ ‚d S r	   r=   r   r   r   r   Ústop_capture¦   s    zCaptureController.stop_capturec                 C   s   | j jr| j ¡  d S r	   )r4   r7   Zabandonr   r   r   r   Úteardown_captureµ   s    z"CaptureController.teardown_capturec                 C   s
   | j  ¡ S )z.Combine collected output and return as string.)r"   r*   r   r   r   r   Úmake_capture_report¹   s    z%CaptureController.make_capture_reportN)r.   r/   r0   r1   r   r2   r"   r<   r?   r@   rA   rB   r   r   r   r   r3   m   s   
r3   Tc                 c   s6   |r,z|   ¡  dV  W |  ¡  q2|  ¡  0 ndV  dS )z¢Provides a context manager that starts capturing output

    .. code-block::

        with capture_output(capture_controller):
            ... # Do something
    N)r?   r@   )Ú
controllerÚenabledr   r   r   Úcapture_outputÑ   s    	rE   )r   )T)r1   Ú
__future__r   Ú
contextlibr   r>   Úsixr   r   Zbehave.log_capturer   Zbehave.textutilr   r$   r   Úobjectr   r3   rE   r   r   r   r   Ú<module>   s   
Ud