o
    "i%                     @  sz   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	 ddl
mZmZ ddlmZ dgZedZG d	d dZdS )
    )annotationsN)IteratorListOptionalcast   )FrameOpcode)Data	Assemblerzutf-8c                   @  sD   e Zd ZdZdddZddd
dZdddZdddZdddZdS )r   z)
    Assemble messages from frames.

    returnNonec                 C  sF   t  | _t  | _t  | _d| _d| _d | _g | _	d | _
d| _d S )NF)	threadingLockmutexEventmessage_completemessage_fetchedget_in_progressput_in_progressdecoderchunkschunks_queueclosedself r   \/var/www/html/voicebot/backend/venv/lib/python3.10/site-packages/websockets/sync/messages.py__init__   s   




zAssembler.__init__NtimeoutOptional[float]r
   c                 C  s  | j  | jrtd| jrtdd| _W d   n1 sw   Y  | j|}| j P d| _|s<td|dd| jrCtd| j sJJ | j	  | j
du rVd	nd
}|| j}| j reJ | j  g | _| jdu stJ |W  d   S 1 sw   Y  d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:
            timeout: If a timeout is provided and elapses before a complete
                message is received, :meth:`get` raises :exc:`TimeoutError`.

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

        stream of frames ended"get or get_iter is already runningTNFztimed out in z.1fs     )r   r   EOFErrorr   RuntimeErrorr   waitTimeoutErroris_setclearr   joinr   r   setr   )r   r   	completedjoinermessager   r   r   get;   s0   

$zAssembler.getIterator[Data]c                 c  s(   | j 2 | jrtd| jrtd| j}g | _tdt | _	| j
 r,| j	d d| _W d   n1 s9w   Y  |E dH  	 | j	 }|du rNn|V  qD| j 5 d| _| j
 s`J | j
  | jrltd| j rsJ | j  | jg ksJ d| _	W d   dS 1 sw   Y  dS )aw  
        Stream the next message.

        Iterating the return value of :meth:`get_iter` 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:`RuntimeError` is raised.

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

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

        r!   r"   z!queue.SimpleQueue[Optional[Data]]NTF)r   r   r&   r   r'   r   r   queueSimpleQueuer   r   r*   putr1   r+   r   r-   )r   r   chunkr   r   r   get_iterw   sD   
	



"zAssembler.get_iterframer   c                 C  s  | j  | jrtd| jrtd|jtju rtdd| _	n|jtj
u r)d| _	n|jtju r0n		 W d   dS | j	durH| j	|j|j}n|j}| jdu rW| j| n| j| |jsi	 W d   dS | j rpJ | j  | jdur| jd | j rJ d| _W d   n1 sw   Y  | j  | j " d| _| j sJ | j  | jrtdd| _	W d   dS 1 sw   Y  dS )a  
        Add ``frame`` to the next message.

        When ``frame`` is the final frame in a message, :meth:`put` waits until
        the message is fetched, either by calling :meth:`get` or by fully
        consuming the return value of :meth:`get_iter`.

        :meth:`put` assumes that the stream of frames respects the protocol. If
        it doesn't, the behavior is undefined.

        Raises:
            EOFError: If the stream of frames has ended.
            RuntimeError: If two threads run :meth:`put` concurrently.

        r!   zput is already runningstrict)errorsNTF)r   r   r&   r   r'   opcoder	   TEXTUTF8Decoderr   BINARYCONTdecodedatafinr   r   appendr5   r   r*   r-   r   r(   r+   )r   r8   rA   r   r   r   r5      sL   

!


,
"zAssembler.putc                 C  s   | j ; | jr	 W d   dS d| _| jr&| j  | jdur&| jd | jr6| j  W d   dS W d   dS 1 sAw   Y  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)	r   r   r   r   r-   r   r5   r   r   r   r   r   r   close  s   

"zAssembler.close)r   r   )N)r   r    r   r
   )r   r2   )r8   r   r   r   )	__name__
__module____qualname____doc__r   r1   r7   r5   rD   r   r   r   r   r      s    
$
<
BJ)
__future__r   codecsr3   r   typingr   r   r   r   framesr   r	   r
   __all__getincrementaldecoderr=   r   r   r   r   r   <module>   s    
