
    ,h?1                        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d	lmZ d
gZ ej.                  d      Z G d d
      Zy)    )annotationsN)AnyCallableIterableIteratorLiteraloverload   )ConcurrencyError)	OP_BINARYOP_CONTOP_TEXTFrame)Data   )Deadline	Assemblerzutf-8c                     e Zd ZdZddd d f	 	 	 	 	 	 	 	 	 ddZdddZddZedd       Zedd	       Zeddd
       Zed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       Z/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/websockets/sync/messages.py<lambda>zAssembler.<lambda>&       r   c                      y r   r   r   r   r   r   zAssembler.<lambda>'   r   r   c                8   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)	threadingLockmutexqueueSimpleQueueframes
ValueErrorhighlowpauseresumepausedget_in_progressclosed)selfr&   r'   r(   r)   s        r   __init__zAssembler.__init__"   s     ^^%
 8=7H7H7J !)C<CO7DQw !HIIcz !LMM"C	48
  % r   c                   | j                   r	 | j                  j                  d      }nB	 |"|dk  r| j                  j                  d      }n| j                  j                  d|      }|t        d      |S # t        j                  $ r t        d      d w xY w# t        j                  $ r t        d|dd	      d w xY w)
NFblockstream of frames endedr   T)r1   timeoutztimed out in z.1fs)r,   r$   getr"   EmptyEOFErrorTimeoutError)r-   r3   frames      r   get_next_framezAssembler.get_next_frameH   s     ;;Ce4M &7a< KKOO%O8E KKOO$OHE =344 ;; C78dBC ;; M"]73-q#ABLMs   A< AB < B%Cc                b   | j                   5  g }	 	 |j                  | j                  j                  d             ,# t        j
                  $ r Y nw xY w|D ]  }| j                  j                  |        |D ]  }| j                  j                  |        	 d d d        y # 1 sw Y   y xY w)NFr0   )r!   appendr$   r5   r"   r6   put)r-   r$   queuedr9   s       r   reset_queuezAssembler.reset_queue^   s     ZZ 	'FMM$++///">? ;;  '&'   '&'	' 	' 	's'   B%->AB%AAB%%B.c                     y r   r   r-   r3   decodes      r   r5   zAssembler.gett   s    HKr   c                     y r   r   rA   s      r   r5   zAssembler.getw   s    KNr   c                    y r   r   rA   s      r   r5   zAssembler.getz   s    RUr   c                    y r   r   rA   s      r   r5   zAssembler.get}       UXr   c                     y r   r   rA   s      r   r5   zAssembler.get   rF   r   c                   | j                   5  | j                  rt        d      d| _        ddd       	 t        |      }| j	                  |j                  d            }| j                   5  | j                          ddd       |j                  t        u s|j                  t        u sJ ||j                  t        u }|g}|j                  sy	 | j	                  |j                  d            }| j                   5  | j                          ddd       |j                  t        u sJ |j                  |       |j                  syd| _        dj                  d |D              }|r|j!                         S |S # 1 sw Y   QxY w# 1 sw Y   xY w# t        $ r | j                  |        w xY w# 1 sw Y   xY w# d| _        w xY 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:
            timeout: If a timeout is provided and elapses before a complete
                message is received, :meth:`get` raises :exc:`TimeoutError`.
            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.
            TimeoutError: If a timeout is provided and elapses before a
                complete message is received.

        &get() or get_iter() is already runningTNF)raise_if_elapsedr   c              3  4   K   | ]  }|j                     y wr   )data).0r9   s     r   	<genexpr>z Assembler.get.<locals>.<genexpr>   s     7u

7s   )r!   r+   r   r   r:   r3   maybe_resumeopcoder   r   finr8   r?   r   r<   joinrB   )r-   r3   rB   deadliner9   r$   rL   s          r   r5   zAssembler.get   s   4 ZZ 	(##&'OPP#'D 	(	)(H ''(8(8%(8(PQE $!!#$<<7*elli.GG~0WF ii // ((%(@E ZZ (%%'(||w..e$ ii  $)D xx777;;= KW	( 	($ $ $  $$V,	
( ( $)D s_   E;8G  -F>AG  !F 1G  =F49G  ;FFG  F11G  4F=9G   	G	c                     y r   r   r-   rB   s     r   get_iterzAssembler.get_iter   s    @Cr   c                     y r   r   rU   s     r   rV   zAssembler.get_iter   s    CFr   c                     y r   r   rU   s     r   rV   zAssembler.get_iter   s    FIr   c              #  f  K   | j                   5  | j                  rt        d      d| _        ddd       | j                         }| j                   5  | j	                          ddd       |j
                  t        u s|j
                  t        u sJ ||j
                  t        u }|r3t               }|j                  |j                  |j                         n|j                   |j                  s| j                         }| j                   5  | j	                          ddd       |j
                  t        u sJ |r)j                  |j                  |j                         n|j                   |j                  sd| _        y# 1 sw Y   _xY w# 1 sw Y   7xY w# 1 sw Y   xY ww)a  
        Stream the next message.

        Iterating the return value of :meth:`get_iter` 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.

        rI   TNF)r!   r+   r   r:   rO   rP   r   r   UTF8DecoderrB   rL   rQ   r   )r-   rB   r9   decoders       r   rV   zAssembler.get_iter   sZ    2 ZZ 	(##&'OPP#'D 	( ##%ZZ 	 	 ||w&%,,)*CC>\\W,F!mG..UYY77** ))'')E $!!#$<<7**nnUZZ;;jj  ))  %G	( 	(	  	 $ $sS   F1F$F1F#B-F1F%!A!F1F1FF1F"F1%F.*F1c                    | j                   5  | j                  rt        d      | j                  j	                  |       | j                          ddd       y# 1 sw Y   yxY w)z
        Add ``frame`` to the next message.

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

        r2   N)r!   r,   r7   r$   r=   maybe_pause)r-   r9   s     r   r=   zAssembler.put  sO     ZZ 	{{788KKOOE"	 	 	s   AAA"c                    | j                   y| j                  j                         s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&   r!   lockedr$   qsizer*   r(   r-   s    r   r]   zAssembler.maybe_pause*  s]     99zz  "" ;;*4;;DKJJL 4?*r   c                    | j                   y| j                  j                         s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'   r!   r_   r$   r`   r*   r)   ra   s    r   rO   zAssembler.maybe_resume7  s]     88zz  "" ;;$((*t{{DKKKM 0;*r   c                   | j                   5  | j                  r
	 ddd       yd| _        | j                  r| j                  j	                  d       | j
                  rd| _        | j                          ddd       y# 1 sw Y   yxY w)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`.

        NTF)r!   r,   r+   r$   r=   r*   r)   ra   s    r   closezAssembler.closeD  sm     ZZ 	{{	 	 DK##%{{#	 	 	s   A>AA>>B)
r&   
int | Noner'   re   r(   Callable[[], Any]r)   rf   returnNoner   )r3   float | Nonerg   r   )r$   zIterable[Frame]rg   rh   )r3   ri   rB   Literal[True]rg   str)r3   ri   rB   Literal[False]rg   bytes)NN)r3   ri   rB   bool | Nonerg   r   )rB   rj   rg   zIterator[str])rB   rl   rg   zIterator[bytes])rB   rn   rg   zIterator[Data])r9   r   rg   rh   )rg   rh   )__name__
__module____qualname____doc__r.   r:   r?   r	   r5   rV   r=   r]   rO   rd   r   r   r   r   r      s       #/$0$$ $ !	$
 "$ 
$L,', K KN NU UX XX XEN C CF FI I<%|2r   )
__future__r   codecsr"   r   typingr   r   r   r   r   r	   
exceptionsr   r$   r   r   r   r   r   utilsr   __all__getincrementaldecoderrZ   r   r   r   r   <module>rz      sM    "    G G ) 7 7   -*f**73F Fr   