
    MfG                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZd dlmZmZmZmZ d dlmZ  ej        e          ZddZ G d d          Z G d d	ej                  Z G d
 de          Z G d de          Z G d de          Z G d de          Z  G d de          Z!dS )    N)NoAppFoundErrorTestingTimeoutErrorServerCloseErrorDashAppLoadingError)waitappc                     	 t          j        |           }||         }n?# t          $ r2}t                              d           t          d|            |d}~ww xY w|S )a
  Import a dash application from a module. The import path is in dot
    notation to the module. The variable named app will be returned.

    :Example:

        >>> app = import_app("my_app.app")

    Will import the application in module `app` of the package `my_app`.

    :param app_file: Path to the app (dot-separated).
    :type app_file: str
    :param application_name: The name of the dash application instance.
    :raise: dash_tests.errors.NoAppFoundError
    :return: App from module.
    :rtype: dash.Dash
    zthe app name cannot be foundz$No dash `app` instance was found in N)runpy
run_moduleKeyErrorlogger	exceptionr   )app_fileapplication_name
app_moduler   app_name_missings        _/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/dash/testing/application_runners.py
import_appr      s    " %h//
)*      7888=8==
 
	  
 Js    
A-AAc                       e Zd ZdZdZd Zd Zd Zed             Z	d Z
d Zd	 Zed
             Zed             Zed             ZdS )BaseDashRunnerz4Base context manager class for running applications.i  c                 L    d| _         d | _        || _        || _        d | _        d S )Nr  )portstarted	keep_openstop_timeout_tmp_app_path)selfr   r   s      r   __init__zBaseDashRunner.__init__@   s-    	"(!    c                     t           NNotImplementedErrorr   argskwargss      r   startzBaseDashRunner.startG       !!r    c                     t           r"   r#   r   s    r   stopzBaseDashRunner.stopJ   r)   r    c                 h    	 t          j        |            n# t           j        j        $ r Y dS w xY wdS )NFT)requestsget
exceptionsRequestException)urls    r   
accessiblezBaseDashRunner.accessibleM   sG    	L"3 	 	 	55	ts    //c                      | j         |i |S r"   )r(   r%   s      r   __call__zBaseDashRunner.__call__U   s    tz4*6***r    c                     | S r"    r+   s    r   	__enter__zBaseDashRunner.__enter__X   s    r    c                    | j         rb| j        s[	 t                              d           |                                  n+# t
          $ r}t          d| j         d          |d }~ww xY wt                              d           d S )Nzkilling the app runnerzCannot stop server within z	s timeoutz__exit__ complete)r   r   r   infor,   r   r   r   )r   exc_typeexc_val	tracebackcannot_stop_servers        r   __exit__zBaseDashRunner.__exit__[   s    < 	* 	**4555		& * * *&M1BMMM )** 	'(((((s   .? 
A'	A""A'c                     d| j          S )zThe default server url.zhttp://localhost:)r   r+   s    r   r2   zBaseDashRunner.urlf   s     /49...r    c                 "    t           j        dk    S )Nwin32)sysplatformr+   s    r   
is_windowszBaseDashRunner.is_windowsk   s    |w&&r    c                     | j         S r"   )r   r+   s    r   tmp_app_pathzBaseDashRunner.tmp_app_patho   s    !!r    N)__name__
__module____qualname____doc__
_next_portr   r(   r,   staticmethodr3   r5   r8   r?   propertyr2   rE   rG   r7   r    r   r   r   ;   s        >>J" " "" " "" " "   \+ + +  	) 	) 	) / / X/ ' ' X' " " X" " "r    r   c                   $     e Zd Z fdZd Z xZS )KillerThreadc                      t                      j        di | t          t          j                                                  | _        d S )Nr7   )superr   list	threading_activekeys_old_threads)r   r'   	__class__s     r   r   zKillerThread.__init__u   sE    ""6""" !2!7!7!9!9::r    c                    t          t          j                  D ]}|| j        v rt          j                            t	          j        |          t	          j        t                              }|dk    rt          d|           |dk    rAt          j                            t	          j        |          d            t          d          d S )Nr   zInvalid thread id:    zStopping thread failure)rS   rT   rU   rW   ctypes	pythonapiPyThreadState_SetAsyncExcc_long	py_object
SystemExit
ValueError)r   	thread_idress      r   killzKillerThread.killy   s    i/00 	< 	<ID---"<<i((&*::*F*F C axx !By!B!BCCCQww ::M),,d   !!:;;;	 	< 	<r    )rH   rI   rJ   r   rd   __classcell__rX   s   @r   rP   rP   t   sG        ; ; ; ; ;< < < < < < <r    rP   c                   8     e Zd ZdZd fd	Zd Zd	dZd Z xZS )
ThreadedRunnerzkRuns a dash application in a thread.

    This is the default flavor to use in dash integration tests.
    F   c                 \    t                                          ||           d | _        d S N)r   r   )rR   r   threadr   r   r   rX   s      r   r   zThreadedRunner.__init__   s+    9<HHHr    c                 |    | j                                         r|                     |          S t          d          )NzThread is not alive.)rl   is_aliver3   r   )r   r2   s     r   running_and_accessiblez%ThreadedRunner.running_and_accessible   s9    ;!! 	(??3'''!"8999r    c                      fd}d} j         s'|dk     r 	  j        rG j                                        r                                  n j                                         t          |           _        d j        _         j                                         t          j	         fd|            j                                         _         nQ# t          $ rD}t                              |           d _         |d	z  }t          j        d	           Y d
}~nd
}~ww xY w j         s|dk       j                                         _          j         st          d          d
S )z)Start the app server in threading flavor.c                     dj         j        _        dj        j        _                                        } dvr,t
          j        x| d<   _        t
          xj        dz  c_        n| d         _        	  j        dddi|  d S # t          $ r t                              d           Y d S t          $ r!}t                              |           |d }~ww xY w)NTr   rZ   threadedServer stoppedr7   )scriptsconfigserve_locallycsscopyr   rL   r   runr`   r   r:   	Exceptionr   )optionserrorr   r'   r   s     r   rz   z!ThreadedRunner.start.<locals>.run   s    /3CK,+/CGN(kkmmGV##.<.GG$)))Q.)))#FO	11111111 . . .,------     '''s   6B $C.	C7CCr   ri   targetTc                  8                           j                  S r"   )rp   r2   r+   s   r   <lambda>z&ThreadedRunner.start.<locals>.<lambda>   s    D77AA r    timeoutFrZ   Nzthreaded server failed to start)r   rl   ro   r,   rd   rP   daemonr(   r   untilr{   r   r   timesleepr   )r   r   start_timeoutr'   rz   retrieserrs   `` `   r   r(   zThreadedRunner.start   s   	 	 	 	 	 	 	( , 	7Q;;; +{++-- +		((****#666%)"!!###
AAAA=     ${3355     %%%$1
1	! , 	7Q;;, {++--| 	I%&GHHH	I 	Is   B?C 
D+':D&&D+c                     | j                                          | j                                          t          j        | j         j        | j                   d| _        d S )NF)rl   rd   joinr   	until_notro   r   r   r+   s    r   r,   zThreadedRunner.stop   sO    t{+T->???r    Fri   ri   )	rH   rI   rJ   rK   r   rp   r(   r,   re   rf   s   @r   rh   rh      s~         
     : : :1I 1I 1I 1If      r    rh   c                   .     e Zd Zd fd	ZddZd Z xZS )MultiProcessRunnerFri   c                 Z    t                                          ||           d | _        d S r"   rR   r   procrm   s      r   r   zMultiProcessRunner.__init__   s(    L111			r    c                                           dd           _        fd}t          j        |           _         j                                         t          j         fd|           d _        d S )Nr   r   c                  8   dj         j        _        dj        j        _                                        } 	  j        dddi|  d S # t          $ r t                              d            t          $ r!}t          
                    |           |d }~ww xY w)NTrs   rt   r7   )ru   rv   rw   rx   ry   rz   r`   r   r:   r{   r   )r|   r}   r   r'   s     r   r   z(MultiProcessRunner.start.<locals>.target   s    /3CK,+/CGN(kkmmG11111111   ,---     '''s   A
 
.B8BBr~   c                  8                           j                  S r"   r3   r2   r+   s   r   r   z*MultiProcessRunner.start.<locals>.<lambda>   s    4??4844 r    r   T)	r/   r   multiprocessProcessr   r(   r   r   r   )r   r   r   r'   r   s   `` ` r   r(   zMultiProcessRunner.start   s    JJvt,,		 	 	 	 	 	 !(777		
4444mLLLLr    c                    t          j        | j        j                  }|                    d          D ],}	 |                                 # t           j        $ r Y )w xY w	 |                                 n# t           j        $ r Y nw xY w	 |                    d           d S # t           j        t           j        f$ r Y d S w xY w)NT)	recursiverZ   )	psutilr   r   pidchildrenrd   NoSuchProcessr   TimeoutExpired)r   processr   s      r   r,   zMultiProcessRunner.stop   s    .//$$t$44 	 	D		'   	LLNNNN# 	 	 	D		LLOOOOO%v';< 	 	 	DD	s5   AAA#A8 8B
	B
B% %CCr   r   )rH   rI   rJ   r   r(   r,   re   rf   s   @r   r   r      s`             
   0      r    r   c                   <     e Zd ZdZd
 fd	Z	 	 	 	 	 ddZd	 Z xZS )ProcessRunnerz}Runs a dash application in a waitress-serve subprocess.

    This flavor is closer to production environment but slower.
    Fri   c                 \    t                                          ||           d | _        d S rk   r   rm   s      r   r   zProcessRunner.__init__  +    9<HHH			r    Nr   r   c           	          |s|st          j        d           dS | _        t          j        |r|nd| d| d| d j                   }t                              d|           	 t          j	        |t          j
        t          j
        	           _        t          j         fd
|           nM# t          t          f$ r9 t                              d           d _                                          Y dS w xY wd _        dS )z7Start the server with waitress-serve in process flavor.zAthe process runner needs to start with at least one valid commandNz waitress-serve --listen=0.0.0.0: :z.serverposixstart dash process with %s)stdoutstderrc                  8                           j                  S r"   r   r+   s   r   r   z%ProcessRunner.start.<locals>.<lambda>/      ttx88 r    r   'process server has encountered an errorFT)loggingr}   r   shlexsplitrE   r   debug
subprocessPopenPIPEr   r   r   OSErrorra   r   r   r,   )r   r   r   raw_commandr   r   r&   s   `      r   r(   zProcessRunner.start  sD     	k 	MS   F	{bKKaDaa:aaHXaaao%	
 
 
 	14888	"(Z_Z_  DI J8888-PPPPP$ 	 	 	FGGG DLIIKKKFF		 s   )A	B3 3AC=<C=c                    | j         r'	 t                              d| j         j                   | j                                          | j        r]t          j                            | j                  r9t          	                    d| j                   t          j        | j                   t          j        }| j                             | j                   nW# |$ rO t                              d           | j                                          | j                                          Y nw xY wt                              d           d S )Nzproc.terminate with pid %szremoving temporary app path %sr   zPsubprocess terminate not success, trying to kill the subprocess in a safe mannerzprocess stop completes!)r   r   r:   r   	terminaterG   ospathexistsr   shutilrmtreer   r   communicater   r   rd   )r   _excepts     r   r,   zProcessRunner.stop9  sA   9 	((8$)-HHH	##%%%$ 58I)J)J 5LL!A4CTUUUM$"3444$3	%% - &      ( ( (  6   	   	%%'''''( 	-.....s   CC AD-,D-r   )Nr   Nr   ri   )rH   rI   rJ   rK   r   r(   r,   re   rf   s   @r   r   r     s|         
      % % % %N/ / / / / / /r    r   c                   (     e Zd Zd fd	ZddZ xZS )	RRunnerFri   c                 \    t                                          ||           d | _        d S rk   r   rm   s      r   r   zRRunner.__init__Q  r   r       Nc                 	    t           j                            |          r]t           j                            |          r>s:t           j                            |          t
                              d           nWt           j                             j        sdnt          j	        d          t          j                    j                   _        	 t          j         j                   n0# t           $ r# t
                              d j                   Y nw xY wt           j                             j        d          }t
                              d           t
                              d|           t
                              d           t
                              d	|           t'          |d
d          5 }|                    |           ddd           n# 1 swxY w Y   |}st+          j                    D ]}d|d                             dd          vr_t           j                            t           j                            |d                             t
                              d            nr4t
                              d           fdt          j                  D             }|D ]}t           j                             j        t           j                            |                    }	t           j                            |	          r/t
                              d|	           t9          j        |	           t
                              d| j                   t9          j        ||	           t
                              dt          j        |	                     nt
                              d           t
                              d|           t?          j         dt           j                            |           d j                   }
t
                              d|
           	 tC          j"        |
tB          j#        tB          j#         j        r j        n           _$        tK          j&         fd|            n9# t           tN          f$ r% t
                              d!           d" _(        Y dS w xY wd# _(        dS )$z-Start the server with subprocess and Rscript.z&RRunner inferred cwd from app path: %s/tmpTEMPcannot make temporary folder %szapp.Rz$RRunner start => app is R code chunkz+make a temporary R file for execution => %szcontent of the dashR app%swutf-8encodingN/dash/testing/rZ   \/get cwd from inspect => %sz3RRunner inferred cwd from the Python call stack: %sc                     g | ]t}|                     d           t          j                            t          j                            |                    Tt          j                            |          uS __
startswithr   r   isdirr   .0_cwds     r   
<listcomp>z!RRunner.start.<locals>.<listcomp>  t       <<-- 35'--SRS@T@T2U2UGLLa((  r    delete existing target %scopying %s => %scopied with %szRRunner found no cwd in the Python call stack. You may wish to specify an explicit working directory using something like: dashr.run_server(app, cwd=os.path.dirname(__file__))z Run dashR app with Rscript => %szRscript -e 'source("z")'r   r   r   r   r   c                  8                           j                  S r"   r   r+   s   r   r   zRRunner.start.<locals>.<lambda>  r   r    r   r   FT)r   r   isfiler   dirnamer   r:   r   rE   getenvuuiduuid4hexr   mkdirrG   r   r   r   openwriteinspectstackreplacerealpathwarninglistdirbasenamer   r   copytreer   r   r   r   r   r   r   r   ra   r   r   r   r   r   r   fpentryassetsassetr   r&   s   `  `       r   r(   zRRunner.startV  s    7>># <	27>>##6#6 <	 Kgooc**DcJJJ "$"oD29V3D3DdjllFV" "DW*++++ W W W  !BDDUVVVVVW7<< 17;;DKK>???KKEtLLLLL3444LLs###dC'222 b               C  $]__  E'uQx/?/?c/J/JJJ goobg.>.>uQx.H.HII'CSIII K  QSVWWW   Z__   $ G GEW\\$*;RW=M=Me=T=TUUFw~~f-- .%@&IIIf---LL!3UD<MNNNOE6222LL!12:f3E3EFFFFG K   	6<<<{?BG$4$4S$9$9???o%
 
 
 	14888	"(!!)-):CD%%	  DI J8888-PPPPP$ 	 	 	FGGG DLFF	
 s7   C0 0*DDG##G'*G'
AR# #2SSr   )r   NrH   rI   rJ   r   r(   re   rf   s   @r   r   r   P  sY             
X X X X X X X Xr    r   c                   (     e Zd Zd fd	ZddZ xZS )	JuliaRunnerFri   c                 \    t                                          ||           d | _        d S rk   r   rm   s      r   r   zJuliaRunner.__init__  r   r       Nc                 	    t           j                            |          r]t           j                            |          r>s:t           j                            |          t
                              d           nWt           j                             j        sdnt          j	        d          t          j                    j                   _        	 t          j         j                   n0# t           $ r# t
                              d j                   Y nw xY wt           j                             j        d          }t
                              d           t
                              d|           t
                              d           t
                              d	|           t'          |d
d          5 }|                    |           ddd           n# 1 swxY w Y   |}st+          j                    D ]}d|d                             dd          vr_t           j                            t           j                            |d                             t
                              d            nr4t
                              d           fdt          j                  D             }|D ]}t           j                             j        t           j                            |                    }	t           j                            |	          r/t
                              d|	           t9          j        |	           t
                              d| j                   t9          j        ||	           t
                              dt          j        |	                     nt
                              d           t
                              d|           t?          j         dt           j                            |            j                   }
t
                              d|
           	 tC          j"        |
tB          j#        tB          j#         j        r j        n           _$        tK          j&         fd|           n9# t           tN          f$ r% t
                              d            d! _(        Y dS w xY wd" _(        dS )#z+Start the server with subprocess and julia.z*JuliaRunner inferred cwd from app path: %sr   r   r   zapp.jlz,JuliaRunner start => app is Julia code chunkz/make a temporary Julia file for execution => %szcontent of the Dash.jl appr   r   r   r   Nr   rZ   r   r   r   z7JuliaRunner inferred cwd from the Python call stack: %sc                     g | ]t}|                     d           t          j                            t          j                            |                    Tt          j                            |          uS r   r   r   s     r   r   z%JuliaRunner.start.<locals>.<listcomp>  r   r    r   r   r   zJuliaRunner found no cwd in the Python call stack. You may wish to specify an explicit working directory using something like: dashjl.run_server(app, cwd=os.path.dirname(__file__))z Run Dash.jl app with julia => %szjulia r   zstart Dash.jl process with %sr   c                  8                           j                  S r"   r   r+   s   r   r   z#JuliaRunner.start.<locals>.<lambda>  r   r    r   r   FTr   r   s   `  `       r   r(   zJuliaRunner.start  s    7>># >	27>>##6#6 >	 Ogooc**H#NNN "$"oD29V3D3DdjllFV" "DW*++++ W W W  !BDDUVVVVVW7<< 18<<DKKFGGGKKI4PPPLL5666LLs###dC'222 b               C  $]__  E'uQx/?/?c/J/JJJ goobg.>.>uQx.H.HII'CSIII K  Ms     Z__   $ G GEW\\$*;RW=M=Me=T=TUUFw~~f-- .%@&IIIf---LL!3UD<MNNNOE6222LL!12:f3E3EFFFFG L   	6<<<{;BG$4$4S$9$9;;tCVWWW4d;;;	"(!!)-):CD%%	  DI J8888-PPPPP$ 	 	 	FGGG DLFF	
 s7   C0 0*DDG##G'*G'	AR" "2SSr   )r  Nr   rf   s   @r   r   r     sY             
W W W W W W W Wr    r   )r   )"rC   r   r   r   r   rT   r   r   r   r   r[   r
   r.   r   r   dash.testing.errorsr   r   r   r   dash.testingr   	getLoggerrH   r   r   r   ThreadrP   rh   r   r   r   r   r7   r    r   <module>r	     se   



 				                                       		8	$	$   86" 6" 6" 6" 6" 6" 6" 6"r< < < < <9# < < <.G G G G G^ G G GT/ / / / / / / /dF/ F/ F/ F/ F/N F/ F/ F/R^ ^ ^ ^ ^m ^ ^ ^B] ] ] ] ]- ] ] ] ] ]r    