
    g?1                        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d	lmZ d
gZ ej        d          Z G d d
          ZdS )    )annotationsN)AnyCallableIterableIteratorLiteraloverload   )ConcurrencyError)	OP_BINARYOP_CONTOP_TEXTFrame)Data   )Deadline	Assemblerzutf-8c                  6   e Zd ZdZddd d fd.dZd/d0dZd1dZed2d            Zed3d            Zed/d2d            Zed/d3d            Zed4d5d             Zd4d5d!Zed6d#            Z	ed7d%            Z	ed/d8d'            Z	d/d8d(Z	d9d*Z
d:d+Zd:d,Z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                     d S N r       d/var/www/html/nodeJS/PythonScripts/venv3.11/lib/python3.11/site-packages/websockets/sync/messages.py<lambda>zAssembler.<lambda>&   s    4 r   c                     d S r   r   r   r   r   r   zAssembler.<lambda>'   s    D r   high
int | NonelowpauseCallable[[], Any]resumereturnNonec                J   t          j                    | _        t          j                    | _        |||dz  }|||dz  }|,|*|dk     rt          d          ||k     rt          d          ||c| _        | _        || _	        || _
        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)	threadingLockmutexqueueSimpleQueueframes
ValueErrorr   r   r   r!   pausedget_in_progressclosed)selfr   r   r   r!   s        r   __init__zAssembler.__init__"   s     ^%%
 8=7H7J7J !)C<CO7DQww !HIIIczz !LMMM"C	48
  % r   timeoutfloat | Noner   c                   | j         r@	 | j                            d          }n# t          j        $ r t          d          d w xY w	 |"|dk    r| j                            d          }n| j                            d|          }n(# t          j        $ r t          d|dd	          d w xY w|t          d          |S )
NFblockstream of frames endedr   T)r6   r2   ztimed out in z.1fs)r/   r+   getr)   EmptyEOFErrorTimeoutError)r0   r2   frames      r   get_next_framezAssembler.get_next_frameH   s     ; 	MCe44; C C C788dBCM &7a<< KOO%O88EE KOO$OHHE; M M M"#A7#A#A#A#ABBLM=3444s   %  A	A B
 
%B/r+   Iterable[Frame]c                T   | j         5  g }	 	 |                    | j                            d                     /# t          j        $ r Y nw xY w|D ]}| j                            |           |D ]}| j                            |           	 d d d            d S # 1 swxY w Y   d S )NTFr5   )r(   appendr+   r9   r)   r:   put)r0   r+   queuedr=   s       r   reset_queuezAssembler.reset_queue^   s    Z 	' 	'F@MM$+///">">???@;    ' '&&&& ' '&&&&'	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's-   B0<ABAABB!$B!decodeLiteral[True]strc                    d S r   r   r0   r2   rE   s      r   r9   zAssembler.gett   s    HKr   Literal[False]bytesc                    d S r   r   rI   s      r   r9   zAssembler.getw   s    KN3r   c                   d S r   r   rI   s      r   r9   zAssembler.getz   s    RURUr   c                   d S r   r   rI   s      r   r9   zAssembler.get}       UXUXr   bool | Noner   c                    d S r   r   rI   s      r   r9   zAssembler.get   rO   r   c                   | j         5  | j        rt          d          d| _        ddd           n# 1 swxY w Y   	 t          |          }|                     |                    d                    }| j         5  |                                  ddd           n# 1 swxY w Y   |j        t          u s|j        t          u sJ ||j        t          u }|g}|j
        s	 |                     |                    d                    }n$# t          $ r |                     |            w xY w| j         5  |                                  ddd           n# 1 swxY w Y   |j        t          u sJ |                    |           |j
        d| _        n# d| _        w xY w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:
            timeout: If a timeout is provided and elapses before a complete
                message is received, :meth:`get` raises :exc:`TimeoutError`.
            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.
            TimeoutError: If a timeout is provided and elapses before a
                complete message is received.

        &get() or get_iter() is already runningTNF)raise_if_elapsedr   c              3  $   K   | ]}|j         V  d S r   )data).0r=   s     r   	<genexpr>z Assembler.get.<locals>.<genexpr>   s$      77u
777777r   )r(   r.   r   r   r>   r2   maybe_resumeopcoder   r   finr<   rD   r   rA   joinrE   )r0   r2   rE   deadliner=   r+   rV   s          r   r9   zAssembler.get   s   4 Z 	( 	(# Q&'OPPP#'D 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(	)((H ''(8(8%(8(P(PQQE $ $!!###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $<7**eli.G.G.G.G~0WF i % // ((%(@@ EE $    $$V,,,	
 Z ( (%%'''( ( ( ( ( ( ( ( ( ( ( ( ( ( (|w....e$$$ i %  $)D  5D ((((xx7777777 	;;== Ksw   266?F =BF B""F %B"&;F ")D F !D--
F 7EF EF E /F 	F Iterator[str]c                    d S r   r   r0   rE   s     r   get_iterzAssembler.get_iter   s    @Cr   Iterator[bytes]c                    d S r   r   r`   s     r   ra   zAssembler.get_iter   s    CF3r   Iterator[Data]c                    d S r   r   r`   s     r   ra   zAssembler.get_iter   s    FIcr   c              #    K   | j         5  | j        rt          d          d| _        ddd           n# 1 swxY w Y   |                                 }| j         5  |                                  ddd           n# 1 swxY w Y   |j        t          u s|j        t          u sJ ||j        t          u }|r1t                      }|	                    |j
        |j                  V  n	|j
        V  |j        s|                                 }| j         5  |                                  ddd           n# 1 swxY w Y   |j        t          u sJ |r#|	                    |j
        |j                  V  n	|j
        V  |j        d| _        dS )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.

        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.

        rS   TNF)r(   r.   r   r>   rY   rZ   r   r   UTF8DecoderrE   rV   r[   r   )r0   rE   r=   decoders       r   ra   zAssembler.get_iter   sl     2 Z 	( 	(# Q&'OPPP#'D 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( ##%%Z 	  	 	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 |w&&%,)*C*C*C*C>\W,F 	!mmG..UY777777* ) 	!''))E $ $!!###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $<7**** !nnUZ;;;;;;j    ) 	!  %s0   488A;;A?A?D33D7:D7r=   c                    | j         5  | j        rt          d          | j                            |           |                                  ddd           dS # 1 swxY w Y   dS )z
        Add ``frame`` to the next message.

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

        r7   N)r(   r/   r;   r+   rB   maybe_pause)r0   r=   s     r   rB   zAssembler.put  s     Z 	 	{ 97888KOOE"""	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AAA!Ac                    | j         dS | j                                        sJ | 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(   lockedr+   qsizer-   r   r0   s    r   rj   zAssembler.maybe_pause*  su     9Fz  """"" ;**4;*DKJJLLLLL +***r   c                    | j         dS | j                                        sJ | 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)r   r(   rl   r+   rm   r-   r!   rn   s    r   rY   zAssembler.maybe_resume7  su     8Fz  """"" ;$(**t{*DKKKMMMMM +***r   c                   | j         5  | j        r	 ddd           dS d| _        | j        r| j                            d           | j        rd| _        |                                  ddd           dS # 1 swxY w Y   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`.

        NTF)r(   r/   r.   r+   rB   r-   r!   rn   s    r   closezAssembler.closeD  s     Z 	 	{ 	 	 	 	 	 	 	 	 DK# &%%%{ #	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   	A5A
A55A9<A9)
r   r   r   r   r   r    r!   r    r"   r#   r   )r2   r3   r"   r   )r+   r?   r"   r#   )r2   r3   rE   rF   r"   rG   )r2   r3   rE   rJ   r"   rK   )NN)r2   r3   rE   rP   r"   r   )rE   rF   r"   r^   )rE   rJ   r"   rb   )rE   rP   r"   rd   )r=   r   r"   r#   )r"   r#   )__name__
__module____qualname____doc__r1   r>   rD   r	   r9   ra   rB   rj   rY   rq   r   r   r   r   r      s           #/<$0L$ $ $ $ $L    ,' ' ' ', KKK XKNNN XNUUUU XUXXXX XXXXXX XXE E E E EN CCC XCFFF XFIIII XI<% <% <% <% <%|   2           r   )
__future__r   codecsr)   r&   typingr   r   r   r   r   r	   
exceptionsr   r+   r   r   r   r   r   utilsr   __all__getincrementaldecoderrg   r   r   r   r   <module>r}      s   " " " " " "       G G G G G G G G G G G G G G G G ) ) ) ) ) ) 7 7 7 7 7 7 7 7 7 7 7 7             -*f*733F F F F F F F F F Fr   