
    2Bf%                        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j                  d      Z G d	 d      Zy)
    )annotationsN)IteratorListOptionalcast   )FrameOpcode)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    X/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/websockets/sync/messages.py__init__zAssembler.__init__   sl     ^^%

 !* 1(0  %$ =A #% LP     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                        }| j                  j                         rJ | j                  j                          g | _        | 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.
            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sr    )r   r   EOFErrorr   RuntimeErrorr   waitTimeoutErroris_setclearr   joinr   r   setr   )r   timeout	completedjoinermessages        r   getzAssembler.get;   sS   ( ZZ 	({{788##"#GHH#'D 	( ))..w7	ZZ 	#(D  "]73-q#ABB {{788((//111!!'')"&,,"63BF"KK4G++22444  $$&DK$$,,,1	 		( 	(	 	s   6E2CE'E$'E0c              #  r  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       E d{    	 | j                  j                         }|n| "| j                   5  d| _        | j                  j                         sJ | j                  j                          | j                  rt        d      | j                  j                         rJ | j                  j!                          | j
                  g k(  sJ d| _	        ddd       y# 1 sw Y   xY w7 # 1 sw Y   yxY ww)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   chunks      r   get_iterzAssembler.get_iterw   s    & ZZ 	({{788##"#GHH[[FDK $3!!#!D $$++-!!%%d+#'D +	(0 %%))+E}K	  ZZ 	%#(D ((//111!!'') {{788++22444  $$&;;"$$$ $D	% 	%?	( 	(0 		% 	%sB   F7B!F0F7=F)>3F71B#F+	F7F&"F7+F40F7c                   | j                   5  | j                  rt        d      | j                  rt	        d      |j
                  t        j                  u rt        d      | _	        nK|j
                  t        j                  u rd| _	        n'|j
                  t        j                  u rn
	 ddd       y| 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,                  j)                         sJ | j,                  j1                          | j                  rt        d      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, 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   framerA   s      r   r5   zAssembler.put   s
     ZZ )	({{788##"#;<<||v{{**(;.#, )	( )	($ ||'||**5::uyyAzz  (""4(!!%%d+99;)	( )	(B ,,33555!!%%'  ,!!%%d+++22444#'D S)	(X 	!!#ZZ 
	 #(D ''..000  &&( {{788DL
	  
	 ])	( )	(\
	  
	 s'   BI*5BI*B I*AI6*I36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 	+{{	+ 	+ DK ##%%))+$$0%%))$/ ##$$((*	+ 	+ 	+s   B'A:B''B0)returnNone)N)r-   zOptional[float]rG   r   )rG   zIterator[Data])rD   r	   rG   rH   )	__name__
__module____qualname____doc__r   r1   r7   r5   rF    r   r   r   r      s&    
"H:x@%DH T+r   )
__future__r   codecsr3   r   typingr   r   r   r   framesr	   r
   r   __all__getincrementaldecoderr=   r   rM   r   r   <module>rT      sD    "    1 1 "  -*f**73H+ H+r   