
    5i1                    *   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e      Z ed       G d d             Z ed       G d d             Z	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ G d d      Zy)    )annotationsN)	dataclassfieldreplace)Enum)TYPE_CHECKINGcast)util)ChatInputValue)WidgetStateWidgetStates)ContextInfoc                      e Zd ZdZdZdZy)ScriptRequestTypeCONTINUESTOPRERUNN)__name__
__module____qualname__r   r   r        ~/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/streamlit/runtime/scriptrunner_utils/script_requests.pyr   r      s    H
 D Er   r   T)frozenc                      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      Zded<   dZded<   dZded<    e
e      Zded<   dZded<   ddZy)	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_infoc                ,    t        j                  |       S Nr
   repr_selfs    r   __repr__zRerunData.__repr__A       zz$r   returnr   )r   r   r   __doc__r   __annotations__r!   r"   r#   r$   r   listr'   r)   r*   setr+   r,   r3   r   r   r   r   r   ,   s    5L#)-M&-cIs #K"#(#>y>%*d*M4&+C&@8@'+L$+ r   r   c                  D    e Zd ZU dZded<   dZded<   ed	d       Zd
dZy)ScriptRequestz,A STOP or RERUN request and associated data.r   typeNzRerunData | None_rerun_datac                |    | j                   t        j                  urt        d      t	        d| j
                        S )Nz)RerunData is only set for RERUN requests.r   )r=   r   r   RuntimeErrorr	   r>   r1   s    r   
rerun_datazScriptRequest.rerun_dataL   s4    99-333JKKK!1!122r   c                ,    t        j                  |       S r.   r/   r1   s    r   r3   zScriptRequest.__repr__R   r4   r   )r6   r   r5   )	r   r   r   r7   r8   r>   propertyrA   r3   r   r   r   r<   r<   E   s-    6
$(K!(3 3
 r   r<   c                "    t        |       xr | 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)   s     r   '_fragment_run_should_not_preempt_scriptrE   V   s     !"C+C'CCr   c                   | s|sy| s|S |s| S |j                   D ci c]  }|j                  | }}ddt        d      fg}| j                   D ]  }|D ]{  \  }}|j                  d      |k(  st	        ||      |k7  s+|j                  |j                        }|r%|j                  d      |k(  s]t	        ||      |k(  sm|||j                  <   }  t               }	|	j                   j                  |j                                |	S c c}w )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.
    N)trigger_valueFchat_input_value)datavalue)	widgetsidChatInputValueProto
WhichOneofgetattrgetr   extendvalues)

old_states
new_stateswstatestates_by_idtrigger_value_types	old_statetrigger_value_typeunset_valuenew_trigger_val	coalesceds
             r   _coalesce_widget_statesr]   d   s5    j )3(:(:,$		6,L ,
 	!	0d;<  '' ;	/B 	;+$$W-1CCI'9:kI"."2"29<<"@ ' $..w7;MM  1CDS1:L.+	;;0 I\0023G,s   Dc                  8    e Zd ZdZddZddZd	dZd
dZddZy)ScriptRequestszAn interface for communicating with a ScriptRunner. Thread-safe.

    AppSession makes requests of a ScriptRunner through this class, and
    ScriptRunner handles those requests.
    c                ~    t        j                         | _        t        j                  | _        t               | _        y r.   )	threadingLock_lockr   r   _stater   r>   r1   s    r   __init__zScriptRequests.__init__   s'    ^^%
'00$;r   c                p    | j                   5  t        j                  | _        ddd       y# 1 sw Y   yxY w)zRequest that the ScriptRunner stop running. A stopped ScriptRunner
        can't be used anymore. STOP requests succeed unconditionally.
        N)rc   r   r   rd   r1   s    r   request_stopzScriptRequests.request_stop   s-     ZZ 	1+00DK	1 	1 	1s   ,5c                   | j                   5  | j                  t        j                  k(  r
	 ddd       y| j                  t        j                  k(  rKt        j
                  | _        |j                  rt        |d|j                  g      }|| _        	 ddd       y| j                  t        j
                  k(  rt        | j                  j                  |j                        }|j                  rBg | j                  j                  }|j                  |vr7|j                  |j                         n|j                  r|j                  }ng }t        |j                  ||j                  |j                   ||j"                  |j$                  |j&                  |j(                  	      | _        	 ddd       yt+        d| j                         # 1 sw Y   yxY w)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: )rc   rd   r   r   r   r   r$   r   r>   r]   r!   r'   appendr   r   r"   r#   r+   r)   r*   r,   r@   )r2   new_datacoalesced_statesr'   s       r   request_rerunzScriptRequests.request_rerun   s    ZZ >	Q{{/444>	Q >	Q
 {{/888 055 ''& $(+3+?+?*@ H $, )>	Q >	Q, {{/555 $;$$22H4J4J$  '' )N$*:*:*L*L(M%++3DD)001E1EF//(0(B(B%
 )+%#,!)!6!6"2%-%>%>&00&7*2*H*H-5-N-N"*"8"8!)!6!6
$  w>	Q >	Q| !A$++OPP}>	Q >	Qs   GAGDG6GGc                   | j                   t        j                  k(  sQ| j                   t        j                  k(  r5t	        | j
                  j                  | j
                  j                        ry| j                  5  | j                   t        j                  k(  rt	        | j
                  j                  | j
                  j                        r
	 ddd       yt        j                  | _         t        t        j                  | j
                        cddd       S | j                   t        j                  k7  rt        d| j                    d      t        t        j                        cddd       S # 1 sw Y   yxY w)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.
        Nri   z. This should never happen.)rd   r   r   r   rE   r>   r'   r)   rc   r<   r   r@   r1   s    r   on_scriptrunner_yieldz$ScriptRequests.on_scriptrunner_yield   s/    ;;+444KK,2227  22  99 ZZ 	9{{/555 ;$$66$$==  	9 	9 088$%6%<%<d>N>NO	9 	9 {{/444"6t{{mC^_  !!2!7!78#	9 	9 	9s   <AE28E2AE22E;c                `   | j                   5  | j                  t        j                  k(  rBt        j                  | _        t        t        j                  | j                        cddd       S t        j                  | _        t        t        j                        cddd       S # 1 sw Y   yxY w)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)rc   rd   r   r   r   r<   r>   r   r1   s    r   on_scriptrunner_readyz$ScriptRequests.on_scriptrunner_ready%  s     ZZ 	9{{/555/88$%6%<%<d>N>NO	9 	9 ,00DK !2!7!78	9 	9 	9s   AB$--B$$B-N)r6   None)rk   r   r6   r(   )r6   zScriptRequest | None)r6   r<   )	r   r   r   r7   re   rg   rm   ro   rq   r   r   r   r_   r_      s$    '
1HQT)9V9r   r_   )r'   r&   r)   r(   r6   r(   )rS   r    rT   r    r6   r    )
__future__r   ra   dataclassesr   r   r   enumr   typingr   r	   	streamlitr
   streamlit.proto.Common_pb2r   rM    streamlit.proto.WidgetStates_pb2r   r   streamlit.proto.ClientState_pb2r   r   r   r<   rE   r]   r_   r   r   r   <module>r{      s    #  1 1  &  L F;  $     0 $      D D"D 
D6#61D66rZ9 Z9r   