U
    g                     @  s*   d dl mZ d dlmZ G dd dZdS )    )annotations)	Generatorc                   @  s   e Zd ZdZddddZ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dZ	ddddZ
ddddZdS )StreamReaderz
    Generator-based stream reader.

    This class doesn't support concurrent calls to :meth:`read_line`,
    :meth:`read_exact`, or :meth:`read_to_eof`. Make sure calls are
    serialized.

    None)returnc                 C  s   t  | _d| _d S )NF)	bytearraybuffereofself r   6/tmp/pip-unpacked-wheel-dx_q7dq3/websockets/streams.py__init__   s    zStreamReader.__init__intzGenerator[None, None, bytes])mr   c                 c  s   d}d}| j d|d }|dkr$qjt| j }||krLtd| d| d| jrbtd| dd	V  q||krtd| d| d| j d	| }| j d	|= |S )
a  
        Read a LF-terminated line from the stream.

        This is a generator-based coroutine.

        The return value includes the LF character.

        Args:
            m: Maximum number bytes to read; this is a security limit.

        Raises:
            EOFError: If the stream ends without a LF.
            RuntimeError: If the stream ends in more than ``m`` bytes.

        r      
   read  bytes, expected no more than  bytesstream ends after z bytes, before end of lineN)r   findlenRuntimeErrorr	   EOFError)r   r   nprr   r   r   	read_line   s     
zStreamReader.read_line)r   r   c                 c  sf   |dkst t| j|k rH| jr@t| j}td| d| ddV  q| jd| }| jd|= |S )z
        Read a given number of bytes from the stream.

        This is a generator-based coroutine.

        Args:
            n: How many bytes to read.

        Raises:
            EOFError: If the stream ends in less than ``n`` bytes.

        r   r   z bytes, expected r   N)AssertionErrorr   r   r	   r   )r   r   r   r   r   r   r   
read_exact6   s    
zStreamReader.read_exactc                 c  sT   | j s6t| j}||kr.td| d| ddV  q | jdd }| jdd= |S )a  
        Read all bytes from the stream.

        This is a generator-based coroutine.

        Args:
            m: Maximum number bytes to read; this is a security limit.

        Raises:
            RuntimeError: If the stream ends in more than ``m`` bytes.

        r   r   r   N)r	   r   r   r   )r   r   r   r   r   r   r   read_to_eofM   s    
zStreamReader.read_to_eofzGenerator[None, None, bool]c                 c  s    | j r
dS | jrdS dV  q dS )zy
        Tell whether the stream has ended and all data was read.

        This is a generator-based coroutine.

        FTN)r   r	   r
   r   r   r   at_eofc   s
    zStreamReader.at_eofbytes)datar   c                 C  s    | j rtd|  j|7  _dS )z
        Write data to the stream.

        :meth:`feed_data` cannot be called after :meth:`feed_eof`.

        Args:
            data: Data to write.

        Raises:
            EOFError: If the stream has ended.

        stream endedN)r	   r   r   )r   r$   r   r   r   	feed_datas   s    zStreamReader.feed_datac                 C  s   | j rtdd| _ dS )z
        End the stream.

        :meth:`feed_eof` cannot be called more than once.

        Raises:
            EOFError: If the stream has ended.

        r%   TN)r	   r   r
   r   r   r   feed_eof   s    
zStreamReader.feed_eofc                 C  s   | j dd= dS )zG
        Discard all buffered data, but don't end the stream.

        N)r   r
   r   r   r   discard   s    zStreamReader.discardN)__name__
__module____qualname____doc__r   r   r    r!   r"   r&   r'   r(   r   r   r   r   r      s   	"r   N)
__future__r   typingr   r   r   r   r   r   <module>   s   