o
    "h                     @  s   d dl m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rLd 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 )    )annotationsN)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Z	e
d&ddZe
d'ddZd(ddZd)d*ddZd+dd Zd,d"d#Zd$S )-BufferedByteReceiveStreamz
    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returnNonec                   s   | j  I d H  d| _d S )NT)r   acloser!   self r'   I/var/www/html/venv/lib/python3.10/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.bufferMapping[Any, Callable[[], Any]]c                 C  s   | j jS N)r   extra_attributesr%   r'   r'   r(   r-   ,   s   z*BufferedByteReceiveStream.extra_attributesdataIterable[SupportsIndex]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&   r.   r'   r'   r(   	feed_data0   s   z#BufferedByteReceiveStream.feed_data   	max_bytesintc                   s   | j rt| jrt| jd | }| jd |= |S t| jtr)| j|I d H S | j I d H }t||krG| j	||d   |d | S |S r,   )
r!   r   r   r)   
isinstancer   r   receivelenr0   )r&   r3   chunkr'   r'   r(   r6   =   s   z!BufferedByteReceiveStream.receivenbytesc              
     s   	 |t | j }|dkr| jd| }| jd|= t|S zt| jtr/| j|I dH }n| j I dH }W n tyG } zt|d}~ww | j	| q)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

        Tr   N)
r7   r   r)   r5   r   r   r6   r   r   r0   )r&   r9   	remainingretvalr8   excr'   r'   r(   receive_exactlyR   s$   
z)BufferedByteReceiveStream.receive_exactly	delimiterc           	   
     s   t |}d}	 | j||}|dkr(| jd| }| jd|t | = t|S t | j|kr3t|z
| j I dH }W n tyL } zt|d}~ww t	t | j| d d}| j
| q)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   TN   )r7   r   findr)   r   r   r6   r   r   maxr0   )	r&   r>   r3   delimiter_sizeoffsetindexfoundr.   r<   r'   r'   r(   receive_untilm   s(   z'BufferedByteReceiveStream.receive_untilNr"   r#   )r"   r)   )r"   r+   )r.   r/   r"   r#   )r2   )r3   r4   r"   r)   )r9   r4   r"   r)   )r>   r)   r3   r4   r"   r)   )__name__
__module____qualname____doc____annotations__r   r   r   r!   r$   propertyr*   r-   r1   r6   r=   rF   r'   r'   r'   r(   r      s   
 


r   c                      s>   e Zd ZdZd fddZeddd	ZedddZ  ZS )BufferedByteStreamz
    A full-duplex variant of :class:`BufferedByteReceiveStream`. All writes are passed
    through to the wrapped stream as-is.
    streamr   c                   s   t  | || _dS )z:
        :param stream: the stream to be wrapped

        N)super__init___streamr&   rO   	__class__r'   r(   rQ      s   
zBufferedByteStream.__init__r"   r#   c                   s   | j  I d H  d S r,   )rR   send_eofr%   r'   r'   r(   rV      s   zBufferedByteStream.send_eofitemr)   c                   s   | j |I d H  d S r,   )rR   send)r&   rW   r'   r'   r(   rX      s   zBufferedByteStream.send)rO   r   rG   )rW   r)   r"   r#   )	rH   rI   rJ   rK   rQ   r   rV   rX   __classcell__r'   r'   rT   r(   rN      s    rN   c                   @  s$   e Zd Zd
ddZedddZd	S )BufferedConnectableconnectabler   c                 C  s
   || _ dS )z>
        :param connectable: the connectable to wrap

        N)r[   )r&   r[   r'   r'   r(   rQ      s   
zBufferedConnectable.__init__r"   rN   c                   s   | j  I d H }t|S r,   )r[   connectrN   rS   r'   r'   r(   r\      s   zBufferedConnectable.connectN)r[   r   )r"   rN   )rH   rI   rJ   rQ   r   r\   r'   r'   r'   r(   rZ      s    
rZ   )
__future__r   syscollections.abcr   r   r   dataclassesr   r   typingr   r	    r   r   r   r   abcr   r   r   r   r   r   version_infor   typing_extensionsr   rN   rZ   r'   r'   r'   r(   <module>   s     
	{