a
    õ£BeV  ã                   @  sœ   d dl mZ d dlZejdks"J ‚d dlZd dlmZ d dlmZm	Z	m
Z
mZ ddlmZ dd	lmZ dd
lmZ dgZG dd„ dƒZG dd„ deeƒZdS )é    )ÚannotationsNÚwin32)Úcontextmanager)ÚContextManagerÚIteratorÚTextIOÚcasté   ©ÚDummyContexté   )Ú	PipeInput)Ú
Vt100InputÚPosixPipeInputc                   @  sH   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„ZdS )Ú_PipezCWrapper around os.pipe, that ensures we don't double close any end.ÚNone©Úreturnc                 C  s    t  ¡ \| _| _d| _d| _d S )NF)ÚosÚpipeÚread_fdÚwrite_fdÚ_read_closedÚ_write_closed©Úself© r   úb/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/prompt_toolkit/input/posix_pipe.pyÚ__init__   s    z_Pipe.__init__c                 C  s    | j r
dS t | j¡ d| _ dS )z!Close read-end if not yet closed.NT)r   r   Úcloser   r   r   r   r   Ú
close_read   s    z_Pipe.close_readc                 C  s    | j r
dS t | j¡ d| _ dS )z"Close write-end if not yet closed.NT)r   r   r   r   r   r   r   r   Úclose_write$   s    z_Pipe.close_writec                 C  s   |   ¡  |  ¡  dS )zClose both read and write ends.N)r    r!   r   r   r   r   r   ,   s    z_Pipe.closeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r!   r   r   r   r   r   r      s
   r   c                      s¢   e Zd ZdZdZdddddœ‡ fdd	„Zeeddd
dœdd„ƒƒZdddœdd„Z	dddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Z‡  ZS ) r   a  
    Input that is send through a pipe.
    This is useful if we want to send the input programmatically into the
    application. Mostly useful for unit testing.

    Usage::

        with PosixPipeInput.create() as input:
            input.send_text('inputdata')
    r   Ú r   Ústrr   )Ú_pipeÚ_textr   c                   sT   |ˆ _ G ‡ fdd„dƒ}tƒ  tt|ƒ ƒ¡ ˆ  |¡ ˆ j jd7  _ˆ jjˆ _d S )Nc                      s0   e Zd ZdZddœdd„Zddœ‡ fdd„Zd	S )
z&PosixPipeInput.__init__.<locals>.Stdinúutf-8Úboolr   c                 S  s   dS )NTr   ©Ústdinr   r   r   ÚisattyG   s    z-PosixPipeInput.__init__.<locals>.Stdin.isattyÚintc                   s   ˆ j jS ©N)r   r   r,   r   r   r   ÚfilenoJ   s    z-PosixPipeInput.__init__.<locals>.Stdin.filenoN)r"   r#   r$   Úencodingr.   r1   r   r   r   r   ÚStdinD   s   r3   r   )r   Úsuperr   r   r   Ú	send_textÚ	__class__Ú_id)r   r(   r)   r3   ©r6   r   r   r   @   s    	
zPosixPipeInput.__init__zIterator[PosixPipeInput])Útextr   c                 c  s0   t ƒ }zt||d�V  W | ¡  n
| ¡  0 d S )N)r(   r)   )r   r   r   )Úclsr9   r   r   r   r   ÚcreateT   s    zPosixPipeInput.createÚbytes)Údatar   c                 C  s   t  | jj|¡ d S r0   )r   Úwriter   r   ©r   r=   r   r   r   Ú
send_bytes]   s    zPosixPipeInput.send_bytesc                 C  s   t  | jj| d¡¡ dS )zSend text to the input.r*   N)r   r>   r   r   Úencoder?   r   r   r   r5   `   s    zPosixPipeInput.send_textzContextManager[None]r   c                 C  s   t ƒ S r0   r
   r   r   r   r   Úraw_moded   s    zPosixPipeInput.raw_modec                 C  s   t ƒ S r0   r
   r   r   r   r   Úcooked_modeg   s    zPosixPipeInput.cooked_modec                 C  s   | j  ¡  dS )zClose pipe fds.N)r   r!   r   r   r   r   r   j   s    zPosixPipeInput.closec                 C  s   d| j › �S )z@
        This needs to be unique for every `PipeInput`.
        zpipe-input-)r7   r   r   r   r   Útypeahead_hashr   s    zPosixPipeInput.typeahead_hash)r&   )r&   )r"   r#   r$   r%   r7   r   Úclassmethodr   r;   r@   r5   rB   rC   r   rD   Ú__classcell__r   r   r8   r   r   2   s   )Ú
__future__r   ÚsysÚplatformr   Ú
contextlibr   Útypingr   r   r   r   Úutilsr   Úbaser   Zvt100r   Ú__all__r   r   r   r   r   r   Ú<module>   s   ÿ