
    ;Ig(                        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 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y)    )annotationsN)AsyncIteratorIterable)AnyCallableGenericTypeVar   )ConcurrencyError)	OP_BINARYOP_CONTOP_TEXTFrame)Data	Assemblerzutf-8Tc                  B    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	y)SimpleQueuez
    Simplified version of :class:`asyncio.Queue`.

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

    c                v    t        j                         | _        d | _        t	        j
                         | _        y N)asyncioget_running_looploop
get_waitercollectionsdequequeueselfs    X/var/www/html/answerous/venv/lib/python3.12/site-packages/websockets/asyncio/messages.py__init__zSimpleQueue.__init__   s)    ,,.	7;+6+<+<+>
    c                ,    t        | j                        S r   )lenr   r   s    r    __len__zSimpleQueue.__len__"   s    4::r"   c                    | j                   j                  |       | j                  7| j                  j                         s| j                  j	                  d       yyy)z+Put an item into the queue without waiting.N)r   appendr   done
set_result)r   items     r    putzSimpleQueue.put%   sH    

$??&t/C/C/EOO&&t, 0F&r"   c                  K   | j                   su|st        d      | j                  J d       | j                  j	                         | _        	 | j                   d{    | j                  j                          d| _        | j                   j                         S 7 ?# | j                  j                          d| _        w xY ww)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   blocks     r    getzSimpleQueue.get+   s     zz788??*L,LL*"ii557DO'oo%%&&("&zz!!##	 &&&("&s0   ACB  BB  #;CB   #CCc                    | j                   J d       | j                  rJ d       | j                  j                  |       y)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   itemss     r    resetzSimpleQueue.reset9   s<    &O(OO&::GGG~

% r"   c                    | j                   @| j                   j                         s%| j                   j                  t        d             yyy)z8Close the queue, raising EOFError in get() if necessary.Nr-   )r   r(   set_exceptionr.   r   s    r    abortzSimpleQueue.abort?   s<    ??&t/C/C/EOO))(3K*LM 0F&r"   NreturnNone)r<   int)r*   r   r<   r=   )T)r2   boolr<   r   )r6   zIterable[T]r<   r=   )
__name__
__module____qualname____doc__r!   r%   r+   r3   r7   r:    r"   r    r   r      s&    ?
-$!Nr"   r   c                  l    e Zd ZdZddd d f	 	 	 	 	 	 	 	 	 ddZdddZdddZddZdd	Zdd
Z	ddZ
y)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                      y r   rD   rD   r"   r    <lambda>zAssembler.<lambda>Y       r"   c                      y r   rD   rD   r"   r    rG   zAssembler.<lambda>Z   rH   r"   c                    t               | _        |||dz  }|||dz  }|"| |dk  rt        d      ||k  rt        d      ||c| _        | _        || _        || _        d| _        d| _        d| _	        y )N   r   z%low must be positive or equal to zeroz)high must be greater than or equal to lowF)
r   frames
ValueErrorhighlowpauseresumepausedget_in_progressclosed)r   rN   rO   rP   rQ   s        r    r!   zAssembler.__init__U   s     +6- !)C<CO7DQw !HIIcz !LMM"C	48
  % r"   c                &  K   | j                   rt        d      d| _         	 | j                  j                  | j                          d{   }| j                          |j                  t        u s|j                  t        u sJ ||j                  t        u }|g}|j                  sq	 | j                  j                  | j                          d{   }| j                          |j                  t        u sJ |j                  |       |j                  sqd| _         dj                  d |D              }|r|j!                         S |S 7 7 |# t        j                  $ r | j                  j                  |        w xY w# d| _         w xY ww)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  4   K   | ]  }|j                     y wr   )data).0frames     r    	<genexpr>z Assembler.get.<locals>.<genexpr>   s     7u

7s   )rS   r   rL   r3   rT   maybe_resumeopcoder   r   finr   CancelledErrorr7   r   r'   joindecode)r   ra   rZ   rL   rX   s        r    r3   zAssembler.getw   sZ    , "#KLL#
	)++//dkk/::E<<7*elli.GGG~0WF ii"&++//dkk/"BBE !!#||w...e$ ii $)D xx777;;= K7 ; C--  KK%%f-	 $)D sZ   F)F EAF *)E EE AF 3FF E 0FF 	FFc               f  K   | j                   rt        d      d| _         	 | j                  j                  | j                          d{   }| j                          |j                  t        u s|j                  t        u sJ ||j                  t        u }|r4t               }|j                  |j                  |j                         n|j                   |j                  s| j                  j                  | j                          d{   }| j                          |j                  t        u sJ |r*j                  |j                  |j                         n|j                   |j                  sd| _         y7 B# t
        j                  $ r	 d| _          w xY w7 w)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.

        rV   TNF)rS   r   rL   r3   rT   r   r_   r\   r]   r   r   UTF8Decoderra   rX   r^   r   )r   ra   rZ   decoders       r    get_iterzAssembler.get_iter   sW    2 "#KLL#	++//dkk/::E 	||w&%,,)*CCC>\\W,F!mG..UYY77** ))
 ++//dkk/::E<<7***nnUZZ;;jj  ))  %9 ;%% 	#(D 	& ;sB   F1)F FF CF1F/A/F1F1F F,,F1c                    | j                   rt        d      | j                  j                  |       | j	                          y)z
        Add ``frame`` to the next message.

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

        r-   N)rT   r.   rL   r+   maybe_pause)r   rZ   s     r    r+   zAssembler.put   s3     ;;344r"   c                    | j                   yt        | j                        | j                   kD  r%| j                  sd| _        | j	                          yyy)z7Pause the writer if queue is above the high water mark.NT)rN   r$   rL   rR   rP   r   s    r    rg   zAssembler.maybe_pause  sF     99 t{{dii'DKJJL 1<'r"   c                    | j                   yt        | j                        | j                   k  r%| j                  rd| _        | j	                          yyy)z7Resume the writer if queue is below the low water mark.NF)rO   r$   rL   rR   rQ   r   s    r    r\   zAssembler.maybe_resume  sF     88 t{{txx'DKKDKKKM -8'r"   c                `    | j                   ryd| _         | j                  j                          y)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)rT   rL   r:   r   s    r    closezAssembler.close  s'     ;; 	r"   )
rN   
int | NonerO   rl   rP   Callable[[], Any]rQ   rm   r<   r=   r   )ra   bool | Noner<   r   )ra   rn   r<   zAsyncIterator[Data])rZ   r   r<   r=   r;   )r@   rA   rB   rC   r!   r3   re   r+   rg   r\   rk   rD   r"   r    r   r   E   sl    "  #/$0     !	 
 "  
 D:xA%F		r"   )
__future__r   r   codecsr   collections.abcr   r   typingr   r   r   r	   
exceptionsr   rL   r   r   r   r   r   __all__getincrementaldecoderrc   r   r   r   rD   r"   r    <module>rv      si    "    3 2 2 ) 7 7  -*f**73CL-N'!* -N`c cr"   