o
    沪g                     @  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*d+ fd	d
Zd,ddZd-ddZ				d.d/ddZd0ddZd1d$d%Z	d2d(d)Z
  ZS )3LocalScriptRunnerz9Subclasses ScriptRunner to provide some testing features.Nscript_pathstrsession_stater   pages_managerr   c                   s   t j|sJ 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   returnNonec                   sV   | d u s|  ksJ d j |  j| |tjkr)|d } j| d S d S )Nz$Unexpected ScriptRunnerEvent sender!forward_msg)eventsappend
event_datar   ENQUEUE_FORWARD_MSGforward_msg_queueenqueue)r#   r%   kwargsr(   selfr   d/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/streamlit/testing/v1/local_script_runner.pyrecord_eventK   s   
z0LocalScriptRunner.__init__.<locals>.record_eventF)weak)r#   r$   r%   r   r&   r'   )ospathisfiler   r-   r   r   argsr/   super__init___stater	   r   r
   r   r)   r+   on_eventconnect)r1   r   r   r   r8   r/   r3   	__class__r0   r2   r:   )   s*   
zLocalScriptRunner.__init__r&   r'   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joinr0   r   r   r2   rA   ^   s   
zLocalScriptRunner.joinlist[ForwardMsg]c                 C  s   | j jS )z+Return all messages in our ForwardMsgQueue.)r-   _queuer0   r   r   r2   forward_msgsc   s   zLocalScriptRunner.forward_msgs    widget_stateWidgetStates | Nonetimeoutfloat	page_hashr   c                 C  sV   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.
        rF   T)doseq)widget_statesquery_stringpage_script_hash)	r   	urlencoder
   request_rerunr@   startrequire_widgets_deltasr   rD   )r1   rG   query_paramsrI   rK   rN   
rerun_datatreer   r   r2   rung   s   

zLocalScriptRunner.runboolc                 C  s    | j D ]
}|tjkr dS qdS )NTF)r)   r   SHUTDOWN)r1   er   r   r2   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)r1   r\   r%   r]   r   r   r2   _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
ModuleTyper8   __dict__r/   )r1   rf   moduler   r   r2   _new_module   s   
zLocalScriptRunner._new_module)NN)r   r   r   r   r   r   )r&   r'   )r&   rB   )NNrE   rF   )rG   rH   rI   rJ   rK   r   r&   r   )r&   rX   )r\   r   r%   r   r]   rX   r&   r'   )rf   r   r&   rg   )__name__
__module____qualname____doc__r:   rA   rD   rW   r[   re   rn   __classcell__r   r   r>   r2   r   &   s    
5


r   rE   runnerrI   rJ   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_stoprA   RuntimeError)rt   rI   t0
err_stringr   r   r2   rS      s   
rS   )rE   )rt   r   rI   rJ   r&   r'   )'
__future__r   r5   ru   rj   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   r   r   r2   <module>   s*   z