U
    �AcjŠ	  ã                   @   sº   d Z ddlmZ ddlmZmZmZmZ ddlm	Z	m
Z
 ddlmZmZmZmZ edƒZG dd„ deƒZG d	d
„ d
eƒZe
G dd„ dƒƒZeeee dœdd„Zeee edœdd„ZdS )z
Buffer byte streams.
é    )Úcount)ÚDictÚIteratorÚListÚTypeVar)ÚFactoryÚdefine)ÚAMPÚCommandÚIntegerÚStringÚTc                   @   s   e Zd ZdZdeƒ fgZdS )Ú
StreamOpenz
    Open a new stream.
    ó   streamIdN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úresponse© r   r   ú>/tmp/pip-unpacked-wheel-nrvc_22y/twisted/trial/_dist/stream.pyr      s   r   c                   @   s$   e Zd ZdZdeƒ fdeƒ fgZdS )ÚStreamWritez,
    Write a chunk of data to a stream.
    r   s   dataN)r   r   r   r   r   ÚBytesÚ	argumentsr   r   r   r   r      s   þr   c                   @   st   e Zd ZU dZeƒ Zee ed< e	e
ƒZeeee f ed< edœdd„Zeeddœd	d
„Zeee dœdd„ZdS )ÚStreamReceiverz9
    Buffering de-multiplexing byte stream receiver.
    Ú_counterÚ_streams)Úreturnc                 C   s   t | jƒ}g | j|< |S )zE
        Open a new stream and return its unique identifier.
        )Únextr   r   )ÚselfZnewIdr   r   r   Úopen+   s    

zStreamReceiver.openN)ÚstreamIdÚchunkr   c                 C   s   | j |  |¡ dS )z�
        Write to an open stream using its unique identifier.

        @raise KeyError: If there is no such open stream.
        N)r   Úappend)r   r!   r"   r   r   r   Úwrite3   s    zStreamReceiver.write)r!   r   c                 C   s   | j  |¡S )z°
        Indicate an open stream may receive no further data and return all of
        its current contents.

        @raise KeyError: If there is no such open stream.
        )r   Úpop)r   r!   r   r   r   Úfinish;   s    zStreamReceiver.finish)r   r   r   r   r   r   r   ÚintÚ__annotations__r   Údictr   r   r   Úbytesr    r$   r&   r   r   r   r   r   "   s   
r   )ÚdataÚ	chunkSizer   c                 c   s0   d}|t | ƒk r,| ||| … V  ||7 }qdS )a  
    Break a byte string into pieces of no more than ``chunkSize`` length.

    @param data: The byte string.

    @param chunkSize: The maximum length of the resulting pieces.  All pieces
        except possibly the last will be this length.

    @return: The pieces.
    r   N)Úlen)r+   r,   Úposr   r   r   r"   E   s    r"   )ÚampÚchunksr   c                 Ã   sF   |   t¡I dH d }t|tƒs"t‚|D ]}| j t||d�I dH  q&|S )zö
    Send the given stream chunks, one by one, over the given connection.

    The chunks are sent using L{StreamWrite} over a stream opened using
    L{StreamOpen}.

    @return: The identifier of the stream over which the chunks were sent.
    Nr!   )r!   r+   )Z
callRemoter   Ú
isinstancer'   ÚAssertionErrorr   )r/   r0   r!   ZoneChunkr   r   r   ÚstreamV   s
    	r3   N)r   Ú	itertoolsr   Útypingr   r   r   r   Úattrsr   r   Ztwisted.protocols.ampr	   r
   r   r   r   r   r   r   r   r*   r'   r"   r3   r   r   r   r   Ú<module>   s   "