
    g*                       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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 ed          Z G d dee                   Z G d d	          ZdS )    )annotationsN)AsyncIteratorIterable)AnyCallableGenericLiteralTypeVaroverload   )ConcurrencyError)	OP_BINARYOP_CONTOP_TEXTFrame)Data	Assemblerzutf-8Tc                  D    e Zd ZdZddZddZdd	ZdddZddZddZ	dS )SimpleQueuez
    Simplified version of :class:`asyncio.Queue`.

    Provides only the subset of functionality needed by :class:`Assembler`.

    returnNonec                t    t          j                    | _        d | _        t	          j                    | _        d S N)asyncioget_running_looploop
get_waitercollectionsdequequeueselfs    g/var/www/html/nodeJS/PythonScripts/venv3.11/lib/python3.11/site-packages/websockets/asyncio/messages.py__init__zSimpleQueue.__init__   s-    ,..	7;+6+<+>+>


    intc                *    t          | j                  S r   )lenr!   r"   s    r$   __len__zSimpleQueue.__len__"   s    4:r&   itemr   c                    | j                             |           | j        5| 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+   s     r$   putzSimpleQueue.put%   s]    
$?&t/C/C/E/E&O&&t,,,,, '&&&r&   Tblockboolc                n  K   | j         s|st          d          | j        
J d            | j                                        | _        	 | j         d{V  | j                                         d| _        n%# | j                                         d| _        w xY 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#   r1   s     r$   getzSimpleQueue.get+   s      z 		' 97888?**,L***"i5577DO'o%%%%%%%&&((("& &&((("&&&&&z!!###s   A9 9"BitemsIterable[T]c                ~    | j         
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#   r:   s     r$   resetzSimpleQueue.reset9   sL    &&(O&&&:GGGGGG
%     r&   c                    | j         B| j                                         s+| j                             t          d                     dS dS dS )z8Close the queue, raising EOFError in get() if necessary.Nr4   )r   r.   set_exceptionr5   r"   s    r$   abortzSimpleQueue.abort?   sP    ?&t/C/C/E/E&O))(3K*L*LMMMMM '&&&r&   Nr   r   )r   r'   )r+   r   r   r   )T)r1   r2   r   r   )r:   r;   r   r   )
__name__
__module____qualname____doc__r%   r*   r0   r9   r>   rA    r&   r$   r   r      s         ? ? ? ?
   - - - -$ $ $ $ $! ! ! !N N N N N Nr&   r   c                      e Zd ZdZddd d fd&dZed'd            Zed(d            Zed)d*d            Zd)d*dZed+d            Zed,d            Zed)d-d            Zd)d-dZd.d"Zd/d#Z	d/d$Z
d/d%ZdS )0r   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 r   rG   rG   r&   r$   <lambda>zAssembler.<lambda>Y   s    4 r&   c                     d S r   rG   rG   r&   r$   rJ   zAssembler.<lambda>Z   s    D r&   high
int | NonelowpauseCallable[[], Any]resumer   r   c                   t                      | _        |||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)
r   frames
ValueErrorrL   rN   rO   rQ   pausedget_in_progressclosed)r#   rL   rN   rO   rQ   s        r$   r%   zAssembler.__init__U   s     +6-- !)C<CO7DQww !HIIIczz !LMMM"C	48
  % r&   decodeLiteral[True]strc                
   K   d S r   rG   r#   rY   s     r$   r9   zAssembler.getw   s      7:sr&   Literal[False]bytesc                
   K   d S r   rG   r]   s     r$   r9   zAssembler.getz   s      :=#r&   bool | Noner   c                
   K   d S r   rG   r]   s     r$   r9   zAssembler.get}   s      =@Sr&   c                  K   | j         rt          d          d| _         	 | j                            | j                    d{V }|                                  |j        t          u s|j        t          u sJ ||j        t          u }|g}|j	        s	 | j                            | j                    d{V }n.# t          j        $ r | j                            |            w xY w|                                  |j        t          u sJ |                    |           |j	        d| _         n# d| _         w xY w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 runningTNFr&   c              3  $   K   | ]}|j         V  d S r   )data).0frames     r$   	<genexpr>z Assembler.get.<locals>.<genexpr>   s$      77u
777777r&   )rW   r   rT   r9   rX   maybe_resumeopcoder   r   finr   CancelledErrorr>   r   r-   joinrY   )r#   rY   rh   rT   rf   s        r$   r9   zAssembler.get   s     ,  	M"#KLLL#
	)+//dk/::::::::E<7**eli.G.G.G.G~0WF i 
%"&+//dk/"B"BBBBBBBEE-    K%%f---	
 !!###|w....e$$$ i 
% $)D  5D ((((xx7777777 	;;== Ks%   A2D1 &B; :D1 ;+C&&AD1 1	D:AsyncIterator[str]c                    d S r   rG   r]   s     r$   get_iterzAssembler.get_iter   s    EHSr&   AsyncIterator[bytes]c                    d S r   rG   r]   s     r$   rq   zAssembler.get_iter   s    HKr&   AsyncIterator[Data]c                    d S r   rG   r]   s     r$   rq   zAssembler.get_iter   s    KN3r&   c                 K   | j         rt          d          d| _         	 | j                            | j                    d{V }n# t
          j        $ r	 d| _          w xY w|                                  |j        t          u s|j        t          u sJ ||j        t          u }|r2t                      }|                    |j        |j                  W V  n
|j        W V  |j        s| j                            | j                    d{V }|                                  |j        t          u sJ |r$|                    |j        |j                  W V  n
|j        W V  |j        d| _         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.

        rd   TNF)rW   r   rT   r9   rX   r   rm   rj   rk   r   r   UTF8DecoderrY   rf   rl   r   )r#   rY   rh   decoders       r$   rq   zAssembler.get_iter   s     2  	M"#KLLL#	+//dk/::::::::EE% 	 	 	#(D 	 	|w&&%,)*C*C*C*C>\W,F 	!mmG..UY7777777* ) 	!
 +//dk/::::::::E<7**** !nnUZ;;;;;;;j     ) 	!  %s   &A A rh   r   c                    | j         rt          d          | j                            |           |                                  dS )z
        Add ``frame`` to the next message.

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

        r4   N)rX   r5   rT   r0   maybe_pause)r#   rh   s     r$   r0   zAssembler.put  sI     ; 	53444r&   c                    | j         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)rL   r)   rT   rV   rO   r"   s    r$   rz   zAssembler.maybe_pause  sW     9F t{di'''DKJJLLLLL ('''r&   c                    | j         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)rN   r)   rT   rV   rQ   r"   s    r$   rj   zAssembler.maybe_resume!  sW     8F t{tx''DK'DKKKMMMMM ('''r&   c                X    | 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)rX   rT   rA   r"   s    r$   closezAssembler.close,  s6     ; 	F 	r&   )
rL   rM   rN   rM   rO   rP   rQ   rP   r   r   )rY   rZ   r   r[   )rY   r^   r   r_   r   )rY   ra   r   r   )rY   rZ   r   ro   )rY   r^   r   rr   )rY   ra   r   rt   )rh   r   r   r   rB   )rC   rD   rE   rF   r%   r   r9   rq   r0   rz   rj   r~   rG   r&   r$   r   r   E   sa        "  #/<$0L         D ::: X:=== X=@@@@ X@: : : : :x HHH XHKKK XKNNNN XNA% A% A% A% A%F   	 	 	 		 	 	 	     r&   )
__future__r   r   codecsr   collections.abcr   r   typingr   r   r   r	   r
   r   
exceptionsr   rT   r   r   r   r   r   __all__getincrementaldecoderrw   r   r   r   rG   r&   r$   <module>r      s`   " " " " " "       3 3 3 3 3 3 3 3 E E E E E E E E E E E E E E E E ) ) ) ) ) ) 7 7 7 7 7 7 7 7 7 7 7 7       -*f*733GCLL-N -N -N -N -N'!* -N -N -N`u u u u u u u u u ur&   