o
    DRh~&                     @  s   d dl mZ d dlZd dlZd dlZd dlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZmZmZ ddlmZ dgZed	Zed
ZG dd de	e ZG dd dZdS )    )annotationsN)AnyAsyncIteratorCallableGenericIterableTypeVar   )ConcurrencyError)	OP_BINARYOP_CONTOP_TEXTFrame)Data	Assemblerzutf-8Tc                   @  sL   e Zd ZdZdddZdddZdddZdddZdddZdddZ	dS )SimpleQueuez
    Simplified version of :class:`asyncio.Queue`.

    Provides only the subset of functionality needed by :class:`Assembler`.

    returnNonec                 C  s   t  | _d | _t | _d S N)asyncioget_running_looploop
get_waitercollectionsdequequeueself r   ]/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/websockets/asyncio/messages.py__init__#   s   
zSimpleQueue.__init__intc                 C  s
   t | jS r   )lenr   r   r   r   r    __len__(   s   
zSimpleQueue.__len__itemr   c                 C  s8   | j | | jdur| j s| jd dS dS dS )z+Put an item into the queue without waiting.N)r   appendr   done
set_result)r   r%   r   r   r    put+   s   zSimpleQueue.putc                   sd   | j s-| jdurtd| j | _z| jI dH  W | j  d| _n	| j  d| _w | j  S )z?Remove and return an item from the queue, waiting if necessary.Nzget is already running)r   r   r
   r   create_futurecancelpopleftr   r   r   r    get1   s   



zSimpleQueue.getitemsIterable[T]c                 C  s0   | j du s	J d| jrJ d| j| dS )z)Put back items into an empty, idle queue.Nz%cannot reset() while get() is runningz&cannot reset() while queue isn't empty)r   r   extend)r   r.   r   r   r    reset>   s   zSimpleQueue.resetc                 C  s2   | j d ur| j  s| j td | j  d S )Nstream of frames ended)r   r'   set_exceptionEOFErrorr   clearr   r   r   r    abortD   s   zSimpleQueue.abortNr   r   )r   r"   )r%   r   r   r   )r   r   )r.   r/   r   r   )
__name__
__module____qualname____doc__r!   r$   r)   r-   r1   r6   r   r   r   r    r      s    




r   c                   @  sl   e Zd ZdZdddd dd fd$ddZd%d&ddZd%d'ddZd(ddZd)ddZd)d d!Z	d)d"d#Z
dS )*r   a  
    Assemble messages from frames.

    :class:`Assembler` expects only data frames. The stream of frames must
    respect the protocol; if it doesn't, the behavior is undefined.

    Args:
        pause: Called when the buffer of frames goes above the high water mark;
            should pause reading from the network.
        resume: Called when the buffer of frames goes below the low water mark;
            should resume reading from the network.

       Nc                   C     d S r   r   r   r   r   r    <lambda>_       zAssembler.<lambda>c                   C  r=   r   r   r   r   r   r    r>   `   r?   highr"   low
int | NonepauseCallable[[], Any]resumer   r   c                 C  sh   t  | _|d u r|d }|dk rtd||k rtd||| _| _|| _|| _d| _d| _d| _	d S )N   r   z%low must be positive or equal to zeroz)high must be greater than or equal to lowF)
r   frames
ValueErrorr@   rA   rC   rE   pausedget_in_progressclosed)r   r@   rA   rC   rE   r   r   r    r!   [   s   
zAssembler.__init__decodebool | Noner   c                   s  | j rtd| jrtdd| _z
| j I dH }W n tjy(   d| _ w |   |j	t
u s9|j	tu s9J |du rB|j	t
u }|g}|jsxz
| j I dH }W n tjyd   | j| d| _ w |   |j	tu spJ || |jrHd| _ddd |D }|r| S |S )	a  
        Read the next message.

        :meth:`get` returns a single :class:`str` or :class:`bytes`.

        If the message is fragmented, :meth:`get` waits until the last frame is
        received, then it reassembles the message and returns it. To receive
        messages frame by frame, use :meth:`get_iter` instead.

        Args:
            decode: :obj:`False` disables UTF-8 decoding of text frames and
                returns :class:`bytes`. :obj:`True` forces UTF-8 decoding of
                binary frames and returns :class:`str`.

        Raises:
            EOFError: If the stream of frames has ended.
            ConcurrencyError: If two coroutines run :meth:`get` or
                :meth:`get_iter` concurrently.

        r2   &get() or get_iter() is already runningTNF    c                 s  s    | ]}|j V  qd S r   )data).0framer   r   r    	<genexpr>   s    z Assembler.get.<locals>.<genexpr>)rK   r4   rJ   r
   rG   r-   r   CancelledErrormaybe_resumeopcoder   r   finr1   r   r&   joinrL   )r   rL   rR   rG   rP   r   r   r    r-   z   sD   

zAssembler.getAsyncIterator[Data]c                 C s  | j rtd| jrtdd| _z
| j I dH }W n tjy(   d| _ w |   |j	t
u s9|j	tu s9J |du rB|j	t
u }|rQt }||j|jV  n|jV  |js~| j I dH }|   |j	tu skJ |rw||j|jV  n|jV  |jrXd| _dS )ap  
        Stream the next message.

        Iterating the return value of :meth:`get_iter` asynchronously yields a
        :class:`str` or :class:`bytes` for each frame in the message.

        The iterator must be fully consumed before calling :meth:`get_iter` or
        :meth:`get` again. Else, :exc:`ConcurrencyError` is raised.

        This method only makes sense for fragmented messages. If messages aren't
        fragmented, use :meth:`get` instead.

        Args:
            decode: :obj:`False` disables UTF-8 decoding of text frames and
                returns :class:`bytes`. :obj:`True` forces UTF-8 decoding of
                binary frames and returns :class:`str`.

        Raises:
            EOFError: If the stream of frames has ended.
            ConcurrencyError: If two coroutines run :meth:`get` or
                :meth:`get_iter` concurrently.

        r2   rN   TNF)rK   r4   rJ   r
   rG   r-   r   rT   rU   rV   r   r   UTF8DecoderrL   rP   rW   r   )r   rL   rR   decoderr   r   r    get_iter   s:   

zAssembler.get_iterrR   r   c                 C  s&   | j rtd| j| |   dS )z
        Add ``frame`` to the next message.

        Raises:
            EOFError: If the stream of frames has ended.

        r2   N)rK   r4   rG   r)   maybe_pause)r   rR   r   r   r    r)      s   zAssembler.putc                 C  s0   t | j| jkr| jsd| _|   dS dS dS )z7Pause the writer if queue is above the high water mark.TN)r#   rG   r@   rI   rC   r   r   r   r    r]   	     zAssembler.maybe_pausec                 C  s0   t | j| jkr| jrd| _|   dS dS dS )z7Resume the writer if queue is below the low water mark.FN)r#   rG   rA   rI   rE   r   r   r   r    rU     r^   zAssembler.maybe_resumec                 C  s   | j rdS d| _ | j  dS )z
        End the stream of frames.

        Callling :meth:`close` concurrently with :meth:`get`, :meth:`get_iter`,
        or :meth:`put` is safe. They will raise :exc:`EOFError`.

        NT)rK   rG   r6   r   r   r   r    close  s   zAssembler.close)
r@   r"   rA   rB   rC   rD   rE   rD   r   r   r   )rL   rM   r   r   )rL   rM   r   rY   )rR   r   r   r   r7   )r8   r9   r:   r;   r!   r-   r\   r)   r]   rU   r_   r   r   r   r    r   K   s    @
A

)
__future__r   r   codecsr   typingr   r   r   r   r   r   
exceptionsr
   rG   r   r   r   r   r   __all__getincrementaldecoderrZ   r   r   r   r   r   r   r    <module>   s     	
0