
    gQ&                        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	 ddl
mZmZmZmZ ddlmZ dgZ ej"                  d	      Z G d
 d      Zy)    )annotationsN)Iteratorcast   )ConcurrencyError)	OP_BINARYOP_CONTOP_TEXTFrame)Data	Assemblerzutf-8c                  :    e Zd ZdZddZd	d
dZddZddZddZy)r   z)
    Assemble messages from frames.

    c                    t        j                         | _        t        j                         | _        t        j                         | _        d| _        d| _        d | _        g | _	        d | _
        d| _        y )NF)	threadingLockmutexEventmessage_completemessage_fetchedget_in_progressput_in_progressdecoderchunkschunks_queueclosedselfs    M/var/www/openai/venv/lib/python3.12/site-packages/websockets/sync/messages.py__init__zAssembler.__init__   sk     ^^%
 !* 1(0  %$ :> #% DH     Nc                   | j                   5  | j                  rt        d      | j                  rt	        d      d| _        ddd       | j
                  j                  |      }| j                   5  d| _        |st        d|dd      | j                  rt        d      | j
                  j                         sJ | j
                  j                          | j                  d	nd
}|j                  | j                        }g | _        | j                  J | j                  j                         rJ | j                  j                          |cddd       S # 1 sw Y   xY w# 1 sw Y   yxY w)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.
            ConcurrencyError: If two threads run :meth:`get` or :meth:`get_iter`
                concurrently.
            TimeoutError: If a timeout is provided and elapses before a
                complete message is received.

        stream of frames ended&get() or get_iter() is already runningTNFztimed out in z.1fsr     )r   r   EOFErrorr   r   r   waitTimeoutErroris_setclearr   joinr   r   r   set)r   timeout	completedjoinermessages        r   getzAssembler.get>   s@   , ZZ{{788##&'OPP#'D   ))..w7	ZZ#(D  "]73-q#ABB {{788((//111!!'')"&,,"63BF"KK4GDK$$,,,++22444  $$&1 Z Z Zs   6E2CE'E$'E0c              #    K   | j                   5  | j                  rt        d      | j                  rt	        d      | j
                  }g | _        t        dt        j                               | _	        | j                  j                         r| j                  j                  d       d| _        ddd       D ]  }|  | j                  j                         x}!| | j                  j                         x}!| j                   5  d| _        | j                  rt        d      | j                  j                         sJ | j                  j                          | j
                  g k(  sJ d| _	        | j                  j                         rJ | j                  j!                          ddd       y# 1 sw Y   
xY w# 1 sw Y   yxY ww)a~  
        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:`ConcurrencyError` 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.
            ConcurrencyError: If two threads run :meth:`get` or :meth:`get_iter`
                concurrently.

        r"   r#   zqueue.SimpleQueue[Data | None]NTF)r   r   r&   r   r   r   r   queueSimpleQueuer   r   r)   putr1   r*   r   r,   )r   r   chunks      r   get_iterzAssembler.get_iter|   s    & ZZ{{788##&'OPP[[FDK $0!!#!D $$++-!!%%d+#'D + 2 EK ))--//u<K ))--//u< ZZ#(D  {{788((//111!!'');;"$$$ $D++22444  $$& Z= Z< Zs=   GB!F80AG GB#G/	G8G=GG
Gc                   | j                   5  | j                  rt        d      | j                  rt	        d      |j
                  t        u rt        d      | _        n.|j
                  t        u rd| _        n|j
                  t        u sJ | j                  1| j                  j                  |j                  |j                        }n|j                  }| j                  | j                  j!                  |       n| j                  j#                  |       |j                  s
	 ddd       y| j$                  j'                         rJ | j$                  j)                          | j                  | j                  j#                  d       | j*                  j'                         rJ d| _        ddd       | j*                  j-                          | j                   5  d| _        | j                  rt        d      | j*                  j'                         sJ | j*                  j/                          d| _        ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)a,  
        Add ``frame`` to the next message.

        When ``frame`` is the final frame in a message, :meth:`put` waits until
        the message is fetched, which can be achieved 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.
            ConcurrencyError: If two threads run :meth:`put` concurrently.

        r"   zput is already runningstrict)errorsNTF)r   r   r&   r   r   opcoder
   UTF8Decoderr   r   r	   decodedatafinr   r   appendr5   r   r)   r,   r   r'   r*   )r   framer>   s      r   r5   zAssembler.put   s     ZZ{{788##&'?@@||w&*(;*#||w... ||'||**5::uyyAzz  (""4(!!%%d+995 Z< ,,33555!!%%'  ,!!%%d+++22444#'D M T 	!!#ZZ#(D  {{788''..000  &&(DL ZY ZX Zs    DI0B IAIIIc                f   | j                   5  | j                  r
	 ddd       yd| _        | j                  rA| j                  j	                          | j
                  | j
                  j                  d       | j                  r| j                  j	                          ddd       y# 1 sw Y   yxY w)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   s    r   closezAssembler.close  s     ZZ{{ Z DK ##%%))+$$0%%))$/ ##$$((* ZZs   B'A:B''B0)returnNone)N)r-   zfloat | NonerD   r   )rD   zIterator[Data])rA   r   rD   rE   )	__name__
__module____qualname____doc__r   r1   r7   r5   rC    r    r   r   r      s%    
$L<|?'BF P+r    )
__future__r   codecsr3   r   typingr   r   
exceptionsr   framesr   r	   r
   r   r   __all__getincrementaldecoderr<   r   rJ   r    r   <module>rR      sG    "    ! ) 7 7  -*f**73I+ I+r    