
    j(                        d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
 d dl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 G d d	          ZdS )    )annotationsN)AsyncIterator)AnyCallableLiteraloverload   )ConcurrencyError)BINARYCONTTEXTFrame)Data	Assemblerzutf-8c                      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 N r       b/workspace/show-city-briefing-agent/.venv/lib/python3.11/site-packages/websockets/trio/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                H   |  |  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)trioopen_memory_channelmathinfsend_framesrecv_frames
ValueErrorr   r   r   r   pausedget_in_progressclosed)selfr   r   r   r   s        r   __init__zAssembler.__init__#   s     	7:-1-Edh-O-O*$* !)C<CO7DQww !HIIIczz !LMMM"C	48
  % r   decodeLiteral[True]strc                
   K   d S r   r   r-   r/   s     r   getzAssembler.getG   s      7:sr   Literal[False]bytesc                
   K   d S r   r   r3   s     r   r4   zAssembler.getJ   s      :=#r   bool | Noner   c                
   K   d S r   r   r3   s     r   r4   zAssembler.getM   s      =@Sr   c                  K   | j         rt          d          d| _         	 	 | j                                         d{V }n"# t          j        $ r t          d          w xY w|                                  |j        t          u s|j        t          u sJ ||j        t          u }|g}|j        s	 | j                                         d{V }n~# t          j        $ rO | j        j        }|j        r
J d            |j        r
J d            |D ]}| j                            |            t          j        $ r t          d          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 runningTNstream of frames endedzno task should receivezqueue should be emptyFr   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   )r+   r
   r(   receiver#   EndOfChannelEOFErrormaybe_resumeopcoder   r   fin	Cancelledr'   _statereceive_tasksr>   send_nowaitr   appendjoinr/   )r-   r/   r@   framesstater>   s         r   r4   zAssembler.getP   sV     ,  	M"#KLLL#
!	)9".6688888888$ 9 9 978889<4''5<6+A+A+A+A~-WF i %="&"2":":"<"<<<<<<<EE~ 	 	 	 !,3E$2LL4LLLL$zBB+BBBB!' < <(44U;;;;( = = ="#;<<<=!!###|t++++e$$$# i %( $)D  5D (((( xx7777777 	;;== Ks8   A F A!!AF 1C F A;EAF 	F AsyncIterator[str]c                    d S r   r   r3   s     r   get_iterzAssembler.get_iter   s    EHSr   AsyncIterator[bytes]c                    d S r   r   r3   s     r   rR   zAssembler.get_iter   s    HKr   AsyncIterator[Data]c                    d S r   r   r3   s     r   rR   zAssembler.get_iter   s    KN3r   c                 K   | j         rt          d          d| _         	 | j                                         d{V }n8# t          j        $ r	 d| _          t          j        $ r t          d          w xY w|                                  |j	        t          u s|j	        t          u sJ ||j	        t          u }|r2t                      }|                    |j        |j                  W V  nt!          |j                  W V  |j        s	 | j                                         d{V }n"# t          j        $ r t          d          w xY w|                                  |j	        t"          u sJ |r$|                    |j        |j                  W V  nt!          |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.

        r;   TNFr<   )r+   r
   r(   rB   r#   rH   rC   rD   rE   rF   r   r   UTF8Decoderr/   r>   rG   r6   r   )r-   r/   r@   decoders       r   rR   zAssembler.get_iter   s$     2  	M"#KLLL#	5*2244444444EE~ 	 	 	#(D   	5 	5 	53444	5|t##u|v'='='='=>\T)F 	$!mmG..UY7777777 
###### ) 	(
9".6688888888$ 9 9 978889<4'''' (nnUZ;;;;;;; EJ'''''' ) 	("  %s   A 5A6D. .Er@   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.

        r<   N)r,   rD   r'   rK   maybe_pause)r-   r@   s     r   putzAssembler.put   sL     ; 	53444$$U+++r   c                    | j         dS t          | j        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   lenr'   rI   r>   r*   r   r-   s    r   r[   zAssembler.maybe_pause   s^     9F t&+,,ty888DKJJLLLLL 9888r   c                    | j         dS t          | j        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^   r'   rI   r>   r*   r   r_   s    r   rE   zAssembler.maybe_resume  s^     8F t&+,,88T[8DKKKMMMMM 9888r   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)r,   r'   closer_   s    r   rb   zAssembler.close  s7     ; 	F 	     r   )
r   r   r   r   r   r   r   r   r   r    )r/   r0   r   r1   )r/   r5   r   r6   r   )r/   r8   r   r   )r/   r0   r   rP   )r/   r5   r   rS   )r/   r8   r   rU   )r@   r   r   r    )r   r    )__name__
__module____qualname____doc__r.   r   r4   rR   r\   r[   rE   rb   r   r   r   r   r      sf           #/<$0L" " " " "H ::: X:=== X=@@@@ X@E E E E EN HHH XHKKK XKNNNN XNH% H% H% H% H%T   
 
 
 

 
 
 
! ! ! ! ! !r   )
__future__r   codecsr%   collections.abcr   typingr   r   r   r   r#   
exceptionsr
   rN   r   r   r   r   r   __all__getincrementaldecoderrX   r   r   r   r   <module>rn      s   " " " " " "   ) ) ) ) ) ) 3 3 3 3 3 3 3 3 3 3 3 3  ) ) ) ) ) ) . . . . . . . . . . . .       -*f*733J! J! J! J! J! J! J! J! J! J!r   