a
    ”˜ëjw  ã                   @  sè   d dl mZ dZd dlZd dlmZmZmZ d dlm	Z	m
Z
 d dlmZmZ ddlmZmZmZmZ dd	lmZmZmZmZmZmZ ejd
krœd dlmZ nd dlmZ e	dd�G dd„ deƒƒZG dd„ deeƒZG dd„ deƒZdS )é    )Úannotations)ÚBufferedByteReceiveStreamÚBufferedByteStreamÚBufferedConnectableN)ÚCallableÚIterableÚMapping)Ú	dataclassÚfield)ÚAnyÚSupportsIndexé   )ÚClosedResourceErrorÚDelimiterNotFoundÚEndOfStreamÚIncompleteRead)ÚAnyByteReceiveStreamÚAnyByteStreamÚAnyByteStreamConnectableÚByteReceiveStreamÚ
ByteStreamÚByteStreamConnectable)é   é   )ÚoverrideF)Úeqc                   @  s¸   e Zd ZU dZded< eded�Zded< eddd�Zd	ed
< ddœdd„Z	e
ddœdd„ƒZe
ddœdd„ƒZdddœdd„Zd%dddœdd„Zdddœdd „Zdddd!œd"d#„Zd$S )&r   z’
    Wraps any bytes-based receive stream and uses a buffer to provide sophisticated
    receiving capabilities in the form of a byte stream.
    r   Úreceive_streamF)ÚinitÚdefault_factoryÚ	bytearrayÚ_buffer)r   ÚdefaultÚboolÚ_closedÚNone©Úreturnc                 Ã  s   | j  ¡ I d H  d| _d S )NT)r   Úacloser#   ©Úself© r*   úU/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/anyio/streams/buffered.pyr'   )   s    z BufferedByteReceiveStream.acloseÚbytesc                 C  s
   t | jƒS )z"The bytes currently in the buffer.)r,   r    r(   r*   r*   r+   Úbuffer-   s    z BufferedByteReceiveStream.bufferzMapping[Any, Callable[[], Any]]c                 C  s   | j jS ©N)r   Úextra_attributesr(   r*   r*   r+   r/   2   s    z*BufferedByteReceiveStream.extra_attributeszIterable[SupportsIndex])Údatar&   c                C  s   | j  |¡ dS )a;  
        Append data directly into the buffer.

        Any data in the buffer will be consumed by receive operations before receiving
        anything from the wrapped stream.

        :param data: the data to append to the buffer (can be bytes or anything else
            that supports ``__index__()``)

        N)r    Úextend)r)   r0   r*   r*   r+   Ú	feed_data6   s    z#BufferedByteReceiveStream.feed_dataé   Úint)Ú	max_bytesr&   c                 Ã  s”   | j r
t‚| jr2t| jd |… ƒ}| jd |…= |S t| jtƒrP| j |¡I d H S | j ¡ I d H }t|ƒ|krŒ| j 	||d … ¡ |d |… S |S d S r.   )
r#   r   r    r,   Ú
isinstancer   r   ÚreceiveÚlenr1   )r)   r5   Úchunkr*   r*   r+   r7   C   s    z!BufferedByteReceiveStream.receive)Únbytesr&   c              
   Ã  sª   |t | jƒ }|dkr8| jd|… }| jd|…= t|ƒS z4t| jtƒrZ| j |¡I dH }n| j ¡ I dH }W n* ty– } zt|‚W Y d}~n
d}~0 0 | j 	|¡ q dS )a-  
        Read exactly the given amount of bytes from the stream.

        :param nbytes: the number of bytes to read
        :return: the bytes read
        :raises ~anyio.IncompleteRead: if the stream was closed before the requested
            amount of bytes could be read from the stream

        r   N)
r8   r    r,   r6   r   r   r7   r   r   r1   )r)   r:   Ú	remainingÚretvalr9   Úexcr*   r*   r+   Úreceive_exactlyX   s    z)BufferedByteReceiveStream.receive_exactly)Ú	delimiterr5   r&   c           	   
   Ã  sÌ   t |ƒ}d}| j ||¡}|dkrL| jd|… }| jd|t |ƒ …= t|ƒS t | jƒ|krbt|ƒ‚z| j ¡ I dH }W n* ty  } zt|‚W Y d}~n
d}~0 0 t	t | jƒ| d dƒ}| j 
|¡ qdS )aM  
        Read from the stream until the delimiter is found or max_bytes have been read.

        :param delimiter: the marker to look for in the stream
        :param max_bytes: maximum number of bytes that will be read before raising
            :exc:`~anyio.DelimiterNotFound`
        :return: the bytes read (not including the delimiter)
        :raises ~anyio.IncompleteRead: if the stream was closed before the delimiter
            was found
        :raises ~anyio.DelimiterNotFound: if the delimiter is not found within the
            bytes read up to the maximum allowed

        r   Né   )r8   r    Úfindr,   r   r   r7   r   r   Úmaxr1   )	r)   r?   r5   Zdelimiter_sizeÚoffsetÚindexÚfoundr0   r=   r*   r*   r+   Úreceive_untils   s    z'BufferedByteReceiveStream.receive_untilN)r3   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r
   r   r    r#   r'   Úpropertyr-   r/   r2   r7   r>   rF   r*   r*   r*   r+   r      s   
r   c                      sL   e Zd ZdZddœ‡ fdd„Zeddœdd	„ƒZed
ddœdd„ƒZ‡  ZS )r   z…
    A full-duplex variant of :class:`BufferedByteReceiveStream`. All writes are passed
    through to the wrapped stream as-is.
    r   )Ústreamc                   s   t ƒ  |¡ || _dS )z:
        :param stream: the stream to be wrapped

        N)ÚsuperÚ__init__Ú_stream©r)   rM   ©Ú	__class__r*   r+   rO       s    zBufferedByteStream.__init__r$   r%   c                 Ã  s   | j  ¡ I d H  d S r.   )rP   Úsend_eofr(   r*   r*   r+   rT   ¨   s    zBufferedByteStream.send_eofr,   )Úitemr&   c                 Ã  s   | j  |¡I d H  d S r.   )rP   Úsend)r)   rU   r*   r*   r+   rV   ¬   s    zBufferedByteStream.send)	rG   rH   rI   rJ   rO   r   rT   rV   Ú__classcell__r*   r*   rR   r+   r   š   s   r   c                   @  s,   e Zd Zddœdd„Zeddœdd„ƒZd	S )
r   r   ©Úconnectablec                 C  s
   || _ dS )z>
        :param connectable: the connectable to wrap

        NrX   )r)   rY   r*   r*   r+   rO   ²   s    zBufferedConnectable.__init__r   r%   c                 Ã  s   | j  ¡ I d H }t|ƒS r.   )rY   Úconnectr   rQ   r*   r*   r+   rZ   ¹   s    zBufferedConnectable.connectN)rG   rH   rI   rO   r   rZ   r*   r*   r*   r+   r   ±   s   r   ) Ú
__future__r   Ú__all__ÚsysÚcollections.abcr   r   r   Zdataclassesr	   r
   Útypingr   r   Ú r   r   r   r   Úabcr   r   r   r   r   r   Úversion_infor   Ztyping_extensionsr   r   r   r*   r*   r*   r+   Ú<module>   s    	
{