
    A>qj(                        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y)    )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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 N r       W/opt/rentech/trading_bot/.venv/lib/python3.12/site-packages/websockets/trio/messages.py<lambda>zAssembler.<lambda>'       r   c                      y r   r   r   r   r   r   zAssembler.<lambda>(   r   r   c                <   |  |  t        j                  t        j                        \  | _        | _        |||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)trioopen_memory_channelmathinfsend_framesrecv_frames
ValueErrorhighlowpauseresumepausedget_in_progressclosed)selfr#   r$   r%   r&   s        r   __init__zAssembler.__init__#   s     	-1-E-Edhh-O*$* !)C<CO7DQw !HIIcz !LMM"C	48
  % r   c                   K   y wr   r   r*   decodes     r   getzAssembler.getG   s	     7:   c                   K   y wr   r   r-   s     r   r/   zAssembler.getJ   s	     :=r0   c                   K   y wr   r   r-   s     r   r/   zAssembler.getM   s	     =@r0   c                  K   | 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 t        d      w xY w7 # t        j                  $ r` | j                  j                  }|j                  rJ d       |j                   rJ d       |D ]  }| j                  j#                  |         t        j
                  $ r t        d      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 runningTNstream of frames endedzno task should receivezqueue should be emptyFr   c              3  4   K   | ]  }|j                     y wr   )data).0frames     r   	<genexpr>z Assembler.get.<locals>.<genexpr>   s     7u

7s   )r(   r
   r!   receiver   EndOfChannelEOFErrormaybe_resumeopcoder   r   fin	Cancelledr    _statereceive_tasksr7   send_nowaitr   appendjoinr.   )r*   r.   r9   framesstater7   s         r   r/   zAssembler.getP   s    , "#KLL#
!	)9"..6688 <<4'5<<6+AAA~-WF ii="&"2"2":":"<<E !!#||t+++e$# ii( $)D  xx777;;= KK 9$$ 97889 =~~ 	 !,,33E$22L4LL2$zzB+BB>!' <((44U;<(( ="#;<<= $)D sm   G;D8  D6D8 AG/ E <E=E AG/ 3G;6D8 8EG/ E BG,,G/ /	G88G;c                     y r   r   r-   s     r   get_iterzAssembler.get_iter   s    EHr   c                     y r   r   r-   s     r   rJ   zAssembler.get_iter   s    HKr   c                     y r   r   r-   s     r   rJ   zAssembler.get_iter   s    KNr   c                 K   | 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t!        |j                         |j                  s	 | j                  j                          d{   }| j                          |j                  t"        u sJ |r*j                  |j                  |j                         nt!        |j                         |j                  sd| _         y7 I# t        j
                  $ r	 d| _          t        j                  $ r t        d      w xY w7 # t        j                  $ r t        d      w xY w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.

        r4   TNFr5   )r(   r
   r!   r;   r   rA   r<   r=   r>   r?   r   r   UTF8Decoderr.   r7   r@   bytesr   )r*   r.   r9   decoders       r   rJ   zAssembler.get_iter   s    2 "#KLL#	5**2244E 	||t#u||v'===>\\T)F!mG..UYY77 

## ))
9"..6688 <<4'''nnUZZ;; EJJ'' ))"  %G 5~~ 	#(D    	5344	5( 9$$ 97889s_   G+F F F B$G+)G	 GG	 A4G+ G+F 9GG+G	 	G((G+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.

        r5   N)r)   r=   r    rD   maybe_pause)r*   r9   s     r   putzAssembler.put   s7     ;;344$$U+r   c                    | j                   yt        | j                  j                  j                        | j                   kD  r%| j
                  sd| _        | j                          yyy)z7Pause the writer if queue is above the high water mark.NT)r#   lenr    rB   r7   r'   r%   r*   s    r   rR   zAssembler.maybe_pause   sV     99 t&&++,tyy8DKJJL BM8r   c                    | j                   yt        | j                  j                  j                        | j                   k  r%| j
                  rd| _        | j                          yyy)z7Resume the writer if queue is below the low water mark.NF)r$   rU   r    rB   r7   r'   r&   rV   s    r   r>   zAssembler.maybe_resume  sU     88 t&&++,8T[[DKKKM >I8r   c                `    | j                   ryd| _         | j                  j                          y)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    closerV   s    r   rY   zAssembler.close  s)     ;; 	 r   )
r#   
int | Noner$   rZ   r%   Callable[[], Any]r&   r[   returnNone)r.   Literal[True]r\   str)r.   Literal[False]r\   rO   r   )r.   bool | Noner\   r   )r.   r^   r\   zAsyncIterator[str])r.   r`   r\   zAsyncIterator[bytes])r.   ra   r\   zAsyncIterator[Data])r9   r   r\   r]   )r\   r]   )__name__
__module____qualname____doc__r+   r   r/   rJ   rS   rR   r>   rY   r   r   r   r   r      s       #/$0"" " !	"
 "" 
"H : := =@ @EN H HK KN NH%T

!r   )
__future__r   codecsr   collections.abcr   typingr   r   r   r   r   
exceptionsr
   rG   r   r   r   r   r   __all__getincrementaldecoderrN   r   r   r   r   <module>rm      sM    "   ) 3 3  ) . .  -*f**73J! J!r   