o
    / h%  ã                   @  sz   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	 ddl
mZmZ ddlmZ dgZe d¡ZG d	d„ dƒZdS )
é    )ÚannotationsN)ÚIteratorÚListÚOptionalÚcasté   )ÚFrameÚOpcode)ÚDataÚ	Assemblerzutf-8c                   @  sD   e Zd ZdZddd„Zddd
d„Zddd„Zddd„Zddd„ZdS )r   z)
    Assemble messages from frames.

    ÚreturnÚNonec                 C  sF   t  ¡ | _t  ¡ | _t  ¡ | _d| _d| _d | _g | _	d | _
d| _d S )NF)Ú	threadingÚLockÚmutexÚEventÚmessage_completeÚmessage_fetchedÚget_in_progressÚput_in_progressÚdecoderÚchunksÚchunks_queueÚclosed©Úself© r   úQ/var/www/html/govbot/env/lib/python3.10/site-packages/websockets/sync/messages.pyÚ__init__   s   




zAssembler.__init__NÚtimeoutúOptional[float]r
   c                 C  s  | j  | jrtdƒ‚| jrtdƒ‚d| _W d  ƒ n1 sw   Y  | j |¡}| j P d| _|s<td|d›dƒ‚| jrCtdƒ‚| j ¡ sJJ ‚| j 	¡  | j
du rVd	nd
}| | j¡}| j ¡ reJ ‚| j ¡  g | _| jdu stJ ‚|W  d  ƒ S 1 s€w   Y  dS )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`.

        Raises:
            EOFError: If the stream of frames has ended.
            RuntimeError: If two threads run :meth:`get` or :meth:``get_iter`
                concurrently.

        ústream of frames endedú"get or get_iter is already runningTNFztimed out in z.1fÚsó    Ú )r   r   ÚEOFErrorr   ÚRuntimeErrorr   ÚwaitÚTimeoutErrorÚis_setÚclearr   Újoinr   r   Úsetr   )r   r   Ú	completedÚjoinerÚmessager   r   r   Úget;   s0   ù

$èzAssembler.getúIterator[Data]c                 c  s(   | j 2 | jrtdƒ‚| jrtdƒ‚| j}g | _tdt ¡ ƒ| _	| j
 ¡ r,| j	 d¡ d| _W d  ƒ n1 s9w   Y  |E dH  	 | j	 ¡ }|du rNn|V  qD| j 5 d| _| j
 ¡ s`J ‚| j
 ¡  | jrltdƒ‚| j ¡ rsJ ‚| j ¡  | jg ksJ ‚d| _	W d  ƒ dS 1 sw   Y  dS )aw  
        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:`RuntimeError` is raised.

        This method only makes sense for fragmented messages. If messages aren't
        fragmented, use :meth:`get` instead.

        Raises:
            EOFError: If the stream of frames has ended.
            RuntimeError: If two threads run :meth:`get` or :meth:``get_iter`
                concurrently.

        r!   r"   z!queue.SimpleQueue[Optional[Data]]NTF)r   r   r&   r   r'   r   r   ÚqueueÚSimpleQueuer   r   r*   Úputr1   r+   r   r-   )r   r   Úchunkr   r   r   Úget_iterw   sD   €ý
	ë

ü

"òzAssembler.get_iterÚframer   c                 C  sš  | j Ž | jrtdƒ‚| jrtdƒ‚|jtju rtdd| _	n|jtj
u r)d| _	n|jtju r0n		 W d  ƒ dS | j	durH| j	 |j|j¡}n|j}| jdu rW| j |¡ n| j |¡ |jsi	 W d  ƒ dS | j ¡ rpJ ‚| j ¡  | jdur€| j d¡ | j ¡ r‡J ‚d| _W d  ƒ n1 s”w   Y  | j ¡  | j " d| _| j ¡ s¬J ‚| j ¡  | jr¸tdƒ‚d| _	W d  ƒ dS 1 sÆw   Y  dS )a  
        Add ``frame`` to the next message.

        When ``frame`` is the final frame in a message, :meth:`put` waits until
        the message is fetched, either by calling :meth:`get` or by fully
        consuming the return value of :meth:`get_iter`.

        :meth:`put` assumes that the stream of frames respects the protocol. If
        it doesn't, the behavior is undefined.

        Raises:
            EOFError: If the stream of frames has ended.
            RuntimeError: If two threads run :meth:`put` concurrently.

        r!   zput is already runningÚstrict)ÚerrorsNTF)r   r   r&   r   r'   Úopcoder	   ÚTEXTÚUTF8Decoderr   ÚBINARYÚCONTÚdecodeÚdataÚfinr   r   Úappendr5   r   r*   r-   r   r(   r+   )r   r8   rA   r   r   r   r5   ¹   sL   ñ

ã!

×
,
"özAssembler.putc                 C  s   | j ; | jr	 W d  ƒ dS d| _| jr&| j ¡  | jdur&| j d¡ | jr6| j ¡  W d  ƒ dS W d  ƒ dS 1 sAw   Y  dS )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)	r   r   r   r   r-   r   r5   r   r   r   r   r   r   Úclose  s   þ

ò"ózAssembler.close)r   r   )N)r   r    r   r
   )r   r2   )r8   r   r   r   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r1   r7   r5   rD   r   r   r   r   r      s    
$
<
BJ)Ú
__future__r   Úcodecsr3   r   Útypingr   r   r   r   Úframesr   r	   r
   Ú__all__Úgetincrementaldecoderr=   r   r   r   r   r   Ú<module>   s    
