o
    g(                     @  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 d dl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)AsyncIteratorIterable)AnyCallableGenericTypeVar   )ConcurrencyError)	OP_BINARYOP_CONTOP_TEXTFrame)Data	Assemblerzutf-8Tc                   @  sN   e Zd ZdZdddZdddZdddZd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   R/var/www/visachat/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.putTblockboolc                   sp   | j s3|s
td| jdu sJ 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.stream of frames endedNzcannot call get() concurrently)r   EOFErrorr   r   create_futurecancelpopleft)r   r*   r   r   r    get+   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   r2   r   r   r    reset9   s   zSimpleQueue.resetc                 C  s0   | j dur| j  s| j td dS dS dS )z8Close the queue, raising EOFError in get() if necessary.Nr,   )r   r'   set_exceptionr-   r   r   r   r    abort?   s   zSimpleQueue.abortNr   r   )r   r"   )r%   r   r   r   )T)r*   r+   r   r   )r2   r3   r   r   )
__name__
__module____qualname____doc__r!   r$   r)   r1   r5   r7   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>Y       zAssembler.<lambda>c                   C  r=   r   r   r   r   r   r    r>   Z   r?   high
int | NonelowpauseCallable[[], Any]resumer   r   c                 C  s   t  | _|d ur|d u r|d }|d u r|d ur|d }|d ur4|d ur4|dk r,td||k r4t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@   rB   rC   rE   pausedget_in_progressclosed)r   r@   rB   rC   rE   r   r   r    r!   U   s    
zAssembler.__init__decodebool | Noner   c                   s  | j rtdd| _ z_| j| j I dH }|   |jtu s'|jtu s'J |du r0|jtu }|g}|j	sfz| j| j I dH }W n t
jyR   | j|  w |   |jtu s^J || |j	r6W d| _ nd| _ w ddd |D }|r| S |S )a0  
        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.
            UnicodeDecodeError: If a text frame contains invalid UTF-8.
            ConcurrencyError: If two coroutines run :meth:`get` or
                :meth:`get_iter` concurrently.

        &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>)rJ   r
   rG   r1   rK   maybe_resumeopcoder   r   finr   CancelledErrorr5   r   r&   joinrL   )r   rL   rR   rG   rP   r   r   r    r1   w   s8   

zAssembler.getAsyncIterator[Data]c                 C s  | j rtdd| _ z| j| j I dH }W n tjy$   d| _  w |   |jt	u s5|jt
u s5J |du r>|jt	u }|rMt }||j|jV  n|jV  |js}| j| j I dH }|   |jtu sjJ |rv||j|jV  n|jV  |jrTd| _ dS )a  
        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.
            UnicodeDecodeError: If a text frame contains invalid UTF-8.
            ConcurrencyError: If two coroutines run :meth:`get` or
                :meth:`get_iter` concurrently.

        rN   TNF)rJ   r
   rG   r1   rK   r   rW   rT   rU   r   r   UTF8DecoderrL   rP   rV   r   )r   rL   rR   decoderr   r   r    get_iter   s6   	

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.

        r,   N)rK   r-   rG   r)   maybe_pause)r   rR   r   r   r    r)      s   zAssembler.putc                 C  s>   | j du rdS 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.NT)r@   r#   rG   rI   rC   r   r   r   r    r]        
zAssembler.maybe_pausec                 C  s>   | j du rdS 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.NF)rB   r#   rG   rI   rE   r   r   r   r    rT     r^   zAssembler.maybe_resumec                 C  s   | j rdS d| _ | j  dS )z
        End the stream of frames.

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

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