
    5i                       d dl mZ d dlZd dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d d	lmZmZmZ d d
lmZ d dlmZmZ erd dlmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$  G d de      Z%dddZ&y)    )annotationsN)TYPE_CHECKINGAny)parse)runtime)ForwardMsgQueue)MemoryFragmentStorage)MemoryUploadedFileManager)	RerunDataScriptRunnerScriptRunnerEvent)ScriptCache)ElementTreeparse_tree_from_messages)
ForwardMsg)WidgetStates)PagesManager)ScriptRunContext)SafeSessionStatec                       e Zd ZdZ	 	 d		 	 	 	 	 	 	 	 	 	 	 d
 fdZddZddZ	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZddZ	 	 	 	 	 	 	 	 ddZ	ddZ
 xZS )LocalScriptRunnerz9Subclasses ScriptRunner to provide some testing features.c                    t         j                  j                  |      st        d|       t	                _        | _        | _        ||nd _        ||ni  _	        t         -  d| j                  j                  t        d      t               t               ddit!               |	       g  _        g  _        	 	 	 	 	 	 	 	 d fd	} j&                  j)                  |d
       y)z7Initializes the ScriptRunner for the given script_path.zFile not found at N ztest session idz/mock/uploademailztest@example.com)	
session_idmain_script_pathsession_stateuploaded_file_mgrscript_cacheinitial_rerun_data	user_infofragment_storagepages_managerc                    | | k7  rt        d      j                  j                  |       j                  j                  |       |t        j
                  k(  r!|d   }j                  j                  |       y y )Nz$Unexpected ScriptRunnerEvent sender!forward_msg)RuntimeErroreventsappend
event_datar   ENQUEUE_FORWARD_MSGforward_msg_queueenqueue)sendereventkwargsr%   selfs       r/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/streamlit/testing/v1/local_script_runner.pyrecord_eventz0LocalScriptRunner.__init__.<locals>.record_eventL   su    
 !fn"#IJJKKu%OO""6* )===$]3&&..{; >    F)weak)r-   zScriptRunner | Noner.   r   r/   r   returnNone)ospathisfileFileNotFoundErrorr   r+   script_pathr   argsr/   super__init___stater
   r   r   r	   r'   r)   on_eventconnect)r0   r;   r   r#   r<   r/   r2   	__class__s   `      r1   r>   zLocalScriptRunner.__init__)   s     ww~~k*#&8$FGG!0!2&* ,D"	 & 2f((,,337G$({ 2324' 	 
	
 02%'	<'	<0A	<MP	<	<  	l7r3   c                R    | j                   | j                   j                          yy)z7Wait for the script thread to finish, if it is running.N)_script_threadjoinr0   s    r1   rE   zLocalScriptRunner.join^   s%    *$$& +r3   c                .    | j                   j                  S )z+Return all messages in our ForwardMsgQueue.)r+   _queuerF   s    r1   forward_msgszLocalScriptRunner.forward_msgsc   s    %%,,,r3   c                    d}|rt        j                  |d      }t        |||      }| j                  |       | j                  s| j                          t        | |       t        | j                               S )z|Run the script, and parse the output messages for querying
        and interaction.

        Timeout is in seconds.
         T)doseq)widget_statesquery_stringpage_script_hash)	r   	urlencoder   request_rerunrD   startrequire_widgets_deltasr   rI   )r0   widget_statequery_paramstimeout	page_hashrN   
rerun_datas          r1   runzLocalScriptRunner.rung   so      ??<tDL&%&


 	:&""JJLtW-'(9(9(;<<r3   c                :    t        d | j                  D              S )Nc              3  B   K   | ]  }|t         j                  k(    y w)N)r   SHUTDOWN).0es     r1   	<genexpr>z3LocalScriptRunner.script_stopped.<locals>.<genexpr>   s     Hq1)222Hs   )anyr'   rF   s    r1   script_stoppedz LocalScriptRunner.script_stopped   s    HDKKHHHr3   c                    |s%| j                   j                  |j                         | j                  j	                  | |       t        j                         j                  j                          y )N)r.   )	_session_stateon_script_finishedwidget_ids_this_runr@   sendr   get_instancemedia_file_mgrremove_orphaned_files)r0   ctxr.   premature_stops       r1   _on_script_finishedz%LocalScriptRunner._on_script_finished   sW     2233J3JK 	4u- 	--CCEr3   c                    t        j                  |      }| j                  |j                  d<   | j                  |j                  d<   |S )N__args__kwargs)types
ModuleTyper<   __dict__r/   )r0   namemodules      r1   _new_modulezLocalScriptRunner._new_module   s;    !!$'$(II!&*kk
#r3   )NN)r;   strr   r   r#   r   r<   r   r/   r   r5   r6   )r5   r6   )r5   zlist[ForwardMsg])NN   rK   )
rT   zWidgetStates | NonerU   zdict[str, Any] | NonerV   floatrW   rv   r5   r   )r5   bool)rj   r   r.   r   rk   ry   r5   r6   )rs   rv   r5   ztypes.ModuleType)__name__
__module____qualname____doc__r>   rE   rI   rY   ra   rl   ru   __classcell__)rB   s   @r1   r   r   &   s    C 3838 (38 $	38
 38 38 
38j'
- -1.2=)= ,= 	=
 = 
=:IF#F,=FOSF	Fr3   r   c                B   t        j                          }t        j                          |z
  |k  rAt        j                  d       | j                         ryt        j                          |z
  |k  rAd| d}| j                          | j	                          t        |      )zWait for the given ScriptRunner to emit a completion event. If the timeout
    is reached, the runner will be shutdown and an error will be thrown.
    gMbP?Nz#AppTest script run timed out after z(s))timesleepra   request_stoprE   r&   )runnerrV   t0
err_strings       r1   rS   rS      s    
 
B
))+
W
$

5  " ))+
W
$ 7wisCJ 
KKM
z
""r3   )rw   )r   r   rV   rx   r5   r6   )'
__future__r   r7   r   rp   typingr   r   urllibr   	streamlitr   #streamlit.runtime.forward_msg_queuer   streamlit.runtime.fragmentr	   .streamlit.runtime.memory_uploaded_file_managerr
   streamlit.runtime.scriptrunnerr   r   r   +streamlit.runtime.scriptrunner.script_cacher   !streamlit.testing.v1.element_treer   r   streamlit.proto.ForwardMsg_pb2r    streamlit.proto.WidgetStates_pb2r   streamlit.runtime.pages_managerr   7streamlit.runtime.scriptrunner_utils.script_run_contextr   *streamlit.runtime.state.safe_session_stater   r   rS   r   r3   r1   <module>r      sW    # 	   %   ? < T U U C S9=<XKs sl#r3   