o
    ,&]i                     @  s   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rpd 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 deZ%ddddZ&dS )    )annotationsN)TYPE_CHECKINGAny)parse)runtime)ForwardMsgQueue)MemoryFragmentStorage)MemoryUploadedFileManager)	RerunDataScriptRunnerScriptRunnerEvent)ScriptCache)ElementTreeparse_tree_from_messages)
ForwardMsg)WidgetStates)PagesManager)ScriptRunContext)SafeSessionStatec                      sn   e Zd ZdZ		d/d0 fddZd1ddZd2ddZ				d3d4dd Zd5d"d#Zd6d)d*Z	d7d-d.Z
  ZS )8LocalScriptRunnerz9Subclasses ScriptRunner to provide some testing features.Nscript_pathstrsession_stater   pages_managerr   argsr   kwargsreturnNonec                   s   t j|std| t  _| _| _|dur|nd _|dur&|ni  _	t
 jd| jjtdt t ddit |d	 g  _g  _d fdd} jj|dd dS )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_pathr   uploaded_file_mgrscript_cacheinitial_rerun_data	user_infofragment_storager   senderScriptRunner | Noneeventr   r   r   r   r   c                   sV   | d ur|  krt d j|  j| |tjkr)|d } j| d S d S )Nz$Unexpected ScriptRunnerEvent sender!forward_msg)RuntimeErroreventsappend
event_datar   ENQUEUE_FORWARD_MSGforward_msg_queueenqueue)r'   r)   r   r*   selfr   f/var/www/html/IGF-ODF-V3/venv/lib/python3.10/site-packages/streamlit/testing/v1/local_script_runner.pyrecord_eventL   s   
z0LocalScriptRunner.__init__.<locals>.record_eventF)weak)r'   r(   r)   r   r   r   r   r   )ospathisfileFileNotFoundErrorr   r0   r   r   r   r   super__init___stater	   r   r
   r   r,   r.   on_eventconnect)r3   r   r   r   r   r   r5   	__class__r2   r4   r<   )   s,   
zLocalScriptRunner.__init__c                 C  s   | j dur| j   dS dS )z7Wait for the script thread to finish, if it is running.N)_script_threadjoinr2   r   r   r4   rC   ^   s   
zLocalScriptRunner.joinlist[ForwardMsg]c                 C  s   | j jS )z+Return all messages in our ForwardMsgQueue.)r0   _queuer2   r   r   r4   forward_msgsc   s   zLocalScriptRunner.forward_msgs    widget_stateWidgetStates | Nonequery_paramsdict[str, Any] | Nonetimeoutfloat	page_hashr   c                 C  sR   d}|rt j|dd}t|||d}| | | js|   t| | t|  S )z|Run the script, and parse the output messages for querying
        and interaction.

        Timeout is in seconds.
        rH   T)doseq)widget_statesquery_stringpage_script_hash)	r   	urlencoder
   request_rerunrB   startrequire_widgets_deltasr   rF   )r3   rI   rK   rM   rO   rR   
rerun_datar   r   r4   rung   s   

zLocalScriptRunner.runboolc                 C  s   t dd | jD S )Nc                 s  s    | ]}|t jkV  qd S )N)r   SHUTDOWN).0er   r   r4   	<genexpr>   s    z3LocalScriptRunner.script_stopped.<locals>.<genexpr>)anyr,   r2   r   r   r4   script_stopped   s   z LocalScriptRunner.script_stoppedctxr   r)   r   premature_stopc                 C  s4   |s	| j |j | jj| |d t j  d S )N)r)   )	_session_stateon_script_finishedwidget_ids_this_runr>   sendr   get_instancemedia_file_mgrremove_orphaned_files)r3   ra   r)   rb   r   r   r4   _on_script_finished   s   z%LocalScriptRunner._on_script_finishednametypes.ModuleTypec                 C  s&   t |}| j|jd< | j|jd< |S )N__args__kwargs)types
ModuleTyper   __dict__r   )r3   rk   moduler   r   r4   _new_module   s   
zLocalScriptRunner._new_module)NN)r   r   r   r   r   r   r   r   r   r   r   r   )r   r   )r   rD   )NNrG   rH   )
rI   rJ   rK   rL   rM   rN   rO   r   r   r   )r   rZ   )ra   r   r)   r   rb   rZ   r   r   )rk   r   r   rl   )__name__
__module____qualname____doc__r<   rC   rF   rY   r`   rj   rs   __classcell__r   r   r@   r4   r   &   s    
5


r   rG   runnerrM   rN   r   r   c                 C  sb   t   }t   | |k rt d |  rdS t   | |k sd| d}|   |   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sleepr`   request_stoprC   r+   )ry   rM   t0
err_stringr   r   r4   rW      s   
rW   )rG   )ry   r   rM   rN   r   r   )'
__future__r   r7   rz   ro   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   rW   r   r   r   r4   <module>   s*   v