a
    ö£Be¤  ã                   @   sL   d dl Z d dlZd dlZd dlmZ d dlmZ G dd„ deƒZdd„ Z	dS )é    N)Úlauncher)Úlogc                   @   s6   e Zd ZdZi Zdd„ Zdd„ Zdd„ Zdd	d
„ZdS )ÚCaptureOutputz’Captures output from the specified file descriptor, and tees it into another
    file descriptor while generating DAP "output" events for it.
    c              	   C   sì   || j vsJ ‚| | j |< t d||¡ || _|| _|| _t d¡dd�| _|d u rZd | _	nr|j
| _	|j}|d u sx|dkr|d}zt |¡| _W n0 ty¼   tjd||dd� t d¡| _Y n0 t d	||¡ tj| j|d
�| _| j ¡  d S )NzCapturing {0} of {1}.zutf-8Úsurrogateescape)ÚerrorsÚcp65001z6Unsupported {0} encoding {1!r}; falling back to UTF-8.Úwarning)ÚlevelzUsing encoding {0!r} for {1})ÚtargetÚname)Ú	instancesr   ÚinfoÚcategoryÚ_whoseÚ_fdÚcodecsÚgetincrementaldecoderÚ_decoderÚ_streamÚbufferÚencodingÚ
getencoderÚ_encodeÚ	ExceptionÚswallow_exceptionÚ	threadingÚThreadÚ_workerÚ_worker_threadÚstart)ÚselfZwhoser   ÚfdÚstreamr   © r#   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/debugpy/launcher/output.pyÚ__init__   s4    
üzCaptureOutput.__init__c                 C   s4   | j }|d ur0zt |¡ W n ty.   Y n0 d S )N)r   ÚosÚcloser   )r    r!   r#   r#   r$   Ú__del__7   s    zCaptureOutput.__del__c                 C   s\   | j d urJzt | j d¡}W n ty2   Y qJY n0 t|ƒs>qJ|  |¡ q | jddd� d S )Ni   ó    T©Úfinal)r   r&   Úreadr   ÚlenÚ_process_chunk)r    Úsr#   r#   r$   r   ?   s    

zCaptureOutput._workerFc                 C   sø   | j j||d�}t|ƒdkr d S z"tj d| j| dd¡dœ¡ W n tyT   Y n0 | j	d u rdd S zl|  
|d¡\}}t|ƒ}d}||k rÎ| j	 ||d … ¡}| j	 ¡  |dkrÄt | j¡ d | _qÎ||7 }q‚W n" tyò   t d|| j¡ Y n0 d S )	Nr*   r   Úoutputz
Ú
)r   r0   r   zError printing {0!r} to {1})r   Údecoder-   r   ZchannelZ
send_eventr   Úreplacer   r   r   ÚwriteÚflushr&   r'   r   r   r   )r    r/   r+   Ú_ÚsizeÚiZwrittenr#   r#   r$   r.   L   s2    ÿ

zCaptureOutput._process_chunkN)F)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   r(   r   r.   r#   r#   r#   r$   r      s   "r   c                  C   s2   t j ¡ D ]"\} }t d| |j¡ |j ¡  q
dS )z=Waits for all remaining output to be captured and propagated.z!Waiting for remaining {0} of {1}.N)r   r   Úitemsr   r   r   r   Újoin)r   Úinstancer#   r#   r$   Úwait_for_remaining_outputm   s    r@   )
r   r&   r   Zdebugpyr   Zdebugpy.commonr   Úobjectr   r@   r#   r#   r#   r$   Ú<module>   s   `