o
    ,&]i1                     @  s   d dl mZ d dlZd dlmZmZmZ d dlmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZmZ e
r>d d	lmZ G d
d deZeddG dd dZeddG dd dZd ddZd!ddZG dd dZdS )"    )annotationsN)	dataclassfieldreplace)Enum)TYPE_CHECKINGcast)util)ChatInputValue)WidgetStateWidgetStates)ContextInfoc                   @  s   e Zd ZdZdZdZdS )ScriptRequestTypeCONTINUESTOPRERUNN)__name__
__module____qualname__r   r   r    r   r   r/var/www/html/IGF-ODF-V3/venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner_utils/script_requests.pyr      s    r   T)frozenc                   @  s   e Zd ZU dZdZded< dZded< dZded< dZded	< dZ	d
ed< e
edZded< dZded< dZded< e
edZded< dZded< dddZdS )	RerunDataz+Data attached to RERUN requests. Immutable. strquery_stringNWidgetStates | Nonewidget_statespage_script_hash	page_namez
str | Nonefragment_id)default_factory	list[str]fragment_id_queueFboolis_fragment_scoped_rerunis_auto_rerunzset[str]cached_message_hasheszContextInfo | Nonecontext_inforeturnc                 C  
   t | S Nr	   repr_selfr   r   r   __repr__A      
zRerunData.__repr__r)   r   )r   r   r   __doc__r   __annotations__r   r   r   r    r   listr#   r%   r&   setr'   r(   r0   r   r   r   r   r   ,   s   
 r   c                   @  s>   e Zd ZU dZded< dZded< edd	d
ZdddZdS )ScriptRequestz,A STOP or RERUN request and associated data.r   typeNzRerunData | None_rerun_datar)   r   c                 C  s    | j tjur
tdtd| jS )Nz)RerunData is only set for RERUN requests.r   )r8   r   r   RuntimeErrorr   r9   r.   r   r   r   
rerun_dataL   s   zScriptRequest.rerun_datar   c                 C  r*   r+   r,   r.   r   r   r   r0   R   r1   zScriptRequest.__repr__)r)   r   r2   )	r   r   r   r3   r4   r9   propertyr;   r0   r   r   r   r   r7   E   s   
 r7   r#   r"   r%   r$   r)   c                 C  s   t | o| S )a7  Returns whether the currently running script should be preempted due to a
    fragment rerun.

    Reruns corresponding to fragment runs that weren't caused by calls to
    `st.rerun(scope="fragment")` should *not* cancel the current script run
    as doing so will affect elements outside of the fragment.
    )r$   )r#   r%   r   r   r   '_fragment_run_should_not_preempt_scriptV   s   r=   
old_statesr   
new_statesc           	      C  s   | s|sdS | s
|S |s| S dd |j D }ddtddfg}| j D ]2}|D ]-\}}|d|krSt|||krS||j}|rN|d|krSt|||krS|||j< q&q"t }|j |  |S )ad  Coalesce an older WidgetStates into a newer one, and return a new
    WidgetStates containing the result.

    For most widget values, we just take the latest version.

    However, any trigger_values (which are set by buttons) that are True in
    `old_states` will be set to True in the coalesced result, so that button
    presses don't go missing.
    Nc                 S  s   i | ]}|j |qS r   )id).0wstater   r   r   
<dictcomp>w   s    z+_coalesce_widget_states.<locals>.<dictcomp>)trigger_valueFchat_input_value)datavalue)	widgetsChatInputValueProto
WhichOneofgetattrgetr@   r   extendvalues)	r>   r?   states_by_idtrigger_value_types	old_statetrigger_value_typeunset_valuenew_trigger_val	coalescedr   r   r   _coalesce_widget_statesd   s4   

rV   c                   @  sB   e Zd ZdZdddZdddZdddZdddZdddZdS )ScriptRequestszAn interface for communicating with a ScriptRunner. Thread-safe.

    AppSession makes requests of a ScriptRunner through this class, and
    ScriptRunner handles those requests.
    r)   Nonec                 C  s   t  | _tj| _t | _d S r+   )	threadingLock_lockr   r   _stater   r9   r.   r   r   r   __init__   s   
zScriptRequests.__init__c                 C  s4   | j  tj| _W d   dS 1 sw   Y  dS )zRequest that the ScriptRunner stop running. A stopped ScriptRunner
        can't be used anymore. STOP requests succeed unconditionally.
        N)r[   r   r   r\   r.   r   r   r   request_stop   s   
"zScriptRequests.request_stopnew_datar   r$   c                 C  s"  | j  | jtjkr	 W d   dS | jtjkr5tj| _|jr)t|d|jgd}|| _	 W d   dS | jtjkrt	| jj
|j
}|jrXg | jj}|j|vrW||j n	|jr_|j}ng }t|j||j|j||j|j|j|jd	| _	 W d   dS td| j 1 sw   Y  dS )a5  Request that the ScriptRunner rerun its script.

        If the ScriptRunner has been stopped, this request can't be honored:
        return False.

        Otherwise, record the request and return True. The ScriptRunner will
        handle the rerun request as soon as it reaches an interrupt point.
        NF)r    r#   T)	r   r   r   r   r#   r'   r%   r&   r(    Unrecognized ScriptRunnerState: )r[   r\   r   r   r   r   r    r   r9   rV   r   r#   appendr   r   r   r   r'   r%   r&   r(   r:   )r/   r_   coalesced_statesr#   r   r   r   request_rerun   sT   


>zScriptRequests.request_rerunScriptRequest | Nonec                 C  s   | j tjks| j tjkrt| jj| jjrdS | jF | j tjkrEt| jj| jjr3	 W d   dS tj| _ t	tj| jW  d   S | j tj
krTtd| j  dt	tj
W  d   S 1 scw   Y  dS )a  Called by the ScriptRunner when it's at a yield point.

        If we have no request or a RERUN request corresponding to one or more fragments
        (that is not a fragment-scoped rerun), return None.

        If we have a (full script or fragment-scoped) RERUN request, return the request
        and set our internal state to CONTINUE.

        If we have a STOP request, return the request and remain stopped.
        Nr`   z. This should never happen.)r\   r   r   r   r=   r9   r#   r%   r[   r7   r   r:   r.   r   r   r   on_scriptrunner_yield   s2   

$z$ScriptRequests.on_scriptrunner_yieldr7   c                 C  sl   | j ) | jtjkrtj| _ttj| jW  d   S tj| _ttjW  d   S 1 s/w   Y  dS )aY  Called by the ScriptRunner when it's about to run its script for
        the first time, and also after its script has successfully completed.

        If we have a RERUN request, return the request and set
        our internal state to CONTINUE.

        If we have a STOP request or no request, set our internal state
        to STOP.
        N)r[   r\   r   r   r   r7   r9   r   r.   r   r   r   on_scriptrunner_ready%  s   
$z$ScriptRequests.on_scriptrunner_readyN)r)   rX   )r_   r   r)   r$   )r)   rd   )r)   r7   )	r   r   r   r3   r]   r^   rc   re   rf   r   r   r   r   rW      s    



J+rW   )r#   r"   r%   r$   r)   r$   )r>   r   r?   r   r)   r   )
__future__r   rY   dataclassesr   r   r   enumr   typingr   r   	streamlitr	   streamlit.proto.Common_pb2r
   rI    streamlit.proto.WidgetStates_pb2r   r   streamlit.proto.ClientState_pb2r   r   r   r7   r=   rV   rW   r   r   r   r   <module>   s$   

9