
    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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)AnyAsyncIteratorCallableGenericIterableTypeVar   )ConcurrencyError)	OP_BINARYOP_CONTOP_TEXTFrame)Data	Assemblerzutf-8Tc                  @    e Zd ZdZd	dZd
dZddZ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    P/var/www/openai/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                   sl| j                  t        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.Nzget is already running)r   r   r   r   create_futurecancelpopleftr   s    r    getzSimpleQueue.get1   s     zz*&'?@@"ii557DO'oo%%&&("&zz!!##	 &&&("&s0   AB=B BB ;B=B #B::B=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.reset>   s<    &O(OO&::GGG~

% r"   c                    | j                   >| j                   j                         s$| j                   j                  t        d             | j                  j                          y )Nstream of frames ended)r   r(   set_exceptionEOFErrorr   clearr   s    r    abortzSimpleQueue.abortD   sC    ??&t/C/C/EOO))(3K*LM

r"   NreturnNone)r<   int)r*   r   r<   r=   )r<   r   )r3   zIterable[T]r<   r=   )
__name__
__module____qualname____doc__r!   r%   r+   r0   r4   r:    r"   r    r   r      s%    ?
-$!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   rC   rC   r"   r    <lambda>zAssembler.<lambda>_   s    4r"   c                      y r   rC   rC   r"   r    rG   zAssembler.<lambda>`   s    Dr"   c                    t               | _        ||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   rM   rN   rO   rP   s        r    r!   zAssembler.__init__[   s{     +6- ;!)C7DEE#:HII"C	48
  % r"   c                V  K   | j                   rt        d      | j                  rt        d      d| _        	 | j                  j                          d{   }| j                          |j                  t        u s|j                  t        u sJ ||j                  t        u }|g}|j                  se	 | j                  j                          d{   }| j                          |j                  t        u sJ |j                  |       |j                  sed| _        dj!                  d |D              }|r|j#                         S |S 7 # t        j                  $ r	 d| _         w xY w7 # t        j                  $ r$ | j                  j                  |       d| _         w xY w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:
            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.
            ConcurrencyError: If two coroutines run :meth:`get` or
                :meth:`get_iter` concurrently.

        r6   &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     7u

s   )rS   r8   rR   r   rK   r0   r   CancelledErrormaybe_resumeopcoder   r   finr4   r   r'   joindecode)r   r`   rY   rK   rW   s        r    r0   zAssembler.getz   s|    * ;;344"#KLL  $	++//++E 	||w&%,,)*CCC>\\W,F ))"kkoo// <<7***MM%  ))  %xx777;;= K= ,%% 	#(D 	 0))  !!&)',$s`   6F)E EE AF)5E/ E-E/ AF)3F)E E**F)-E/ /7F&&F)c               d  K   | j                   rt        d      | j                  rt        d      d| _        	 | 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                          d{   }| j                          |j                  t         u sJ |r*j                  |j                  |j                         n|j                   |j                  sd| _        y7 6# t        j                  $ r	 d| _         w xY w7 w)ap  
        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.
            ConcurrencyError: If two coroutines run :meth:`get` or
                :meth:`get_iter` concurrently.

        r6   rU   TNF)rS   r8   rR   r   rK   r0   r   r[   r\   r]   r   r   UTF8Decoderr`   rW   r^   r   )r   r`   rY   decoders       r    get_iterzAssembler.get_iter   s\    0 ;;344"#KLL  $	++//++E 	||w&%,,)*CCC>\\W,F!mG..UYY77** ))
 ++//++E<<7***nnUZZ;;jj  ))  %9 ,%% 	#(D 	& ,sB   6F0F FF B8F0F.A/F0F0F F++F0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.

        r6   N)rS   r8   rK   r+   maybe_pause)r   rY   s     r    r+   zAssembler.put   s3     ;;344r"   c                    t        | j                        | j                  kD  r%| j                  sd| _        | j	                          yyy)z7Pause the writer if queue is above the high water mark.TN)r$   rK   rM   rQ   rO   r   s    r    rf   zAssembler.maybe_pause	  s7     t{{dii'DKJJL 1<'r"   c                    t        | j                        | j                  k  r%| j                  rd| _        | j	                          yyy)z7Resume the writer if queue is below the low water mark.FN)r$   rK   rN   rQ   rP   r   s    r    r\   zAssembler.maybe_resume  s7     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)rS   rK   r:   r   s    r    closezAssembler.close  s'     ;; 	r"   )
rM   r>   rN   z
int | NonerO   Callable[[], Any]rP   rk   r<   r=   r   )r`   bool | Noner<   r   )r`   rl   r<   zAsyncIterator[Data])rY   r   r<   r=   r;   )r?   r@   rA   rB   r!   r0   rd   r+   rf   r\   rj   rC   r"   r    r   r   K   sj    " #/$0  !	
 " 
>>@?%Br"   )
__future__r   r   codecsr   typingr   r   r   r   r   r	   
exceptionsr   rK   r   r   r   r   r   __all__getincrementaldecoderrb   r   r   r   rC   r"   r    <module>rs      sj    "     * 7 7  -*f**73CL-'!* -`Z Zr"   