
     "j8H                        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/html/bolsaweb/venv/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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  http	localhostc                 h    || _         || _        d| _        d | _        || _        || _        d | _        d S )Nr  )schemehostportstarted	keep_openstop_timeout_tmp_app_path)selfr   r    r   r   s        r   __init__zBaseDashRunner.__init__@   s;    		"(!    c                     t           NNotImplementedErrorr"   argskwargss      r   startzBaseDashRunner.startI       !!r$   c                     t           r&   r'   r"   s    r   stopzBaseDashRunner.stopL   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.accessibleO   sG    	L"3 	 	 	55	ts    //c                      | j         |i |S r&   )r,   r)   s      r   __call__zBaseDashRunner.__call__W   s    tz4*6***r$   c                     | S r&    r/   s    r   	__enter__zBaseDashRunner.__enter__Z   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   infor0   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                 4    | j          d| j         d| j         S )zThe default server url.z://:)r   r   r   r/   s    r   r6   zBaseDashRunner.urlh   s&     +99$)99di999r$   c                 "    t           j        dk    S )Nwin32)sysplatformr/   s    r   
is_windowszBaseDashRunner.is_windowsm   s    |w&&r$   c                     | j         S r&   )r!   r/   s    r   tmp_app_pathzBaseDashRunner.tmp_app_pathq   s    !!r$   N)r   r   )__name__
__module____qualname____doc__
_next_portr#   r,   r0   staticmethodr7   r9   r<   rC   propertyr6   rJ   rL   r;   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 )Nr;   )superr#   list	threading_activekeys_old_threads)r"   r+   	__class__s     r   r#   zKillerThread.__init__w   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)rX   rY   rZ   r\   ctypes	pythonapiPyThreadState_SetAsyncExcc_long	py_object
SystemExit
ValueError)r"   	thread_idress      r   killzKillerThread.kill{   s    i/00 	< 	<ID---"<<i((&*::*F*F C axx !By!B!BCCCQww ::M),,d   !!:;;;	 	< 	<r$   )rM   rN   rO   r#   ri   __classcell__r]   s   @r   rU   rU   v   sG        ; ; ; ; ;< < < < < < <r$   rU   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    )rW   r#   threadr"   r   r    r]   s      r   r#   zThreadedRunner.__init__   s+    9<HHHr$   c                 |    | j                                         r|                     |          S t          d          )NzThread is not alive.)rq   is_aliver7   r   )r"   r6   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   r_   threadedServer stoppedr;   )scriptsconfigserve_locallycsscopyr   rQ   r   runre   r   r>   	Exceptionr   )optionserrorr   r+   r"   s     r   r   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   rn   targetTc                  8                           j                  S r&   )ru   r6   r/   s   r   <lambda>z&ThreadedRunner.start.<locals>.<lambda>   s    D77AA r$   timeoutFr_   Nzthreaded server failed to start)r   rq   rt   r0   ri   rU   daemonr,   r   untilr   r   r   timesleepr   )r"   r   start_timeoutr+   r   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)rq   ri   joinr   	until_notrt   r    r   r/   s    r   r0   zThreadedRunner.stop   sO    t{+T->???r$   Frn   rn   )	rM   rN   rO   rP   r#   ru   r,   r0   rj   rk   s   @r   rm   rm      s~         
     : : :1I 1I 1I 1If      r$   rm   c                   .     e Zd Zd fd	ZddZd Z xZS )MultiProcessRunnerFrn   c                 Z    t                                          ||           d | _        d S r&   rW   r#   procrr   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)NTrx   ry   r;   )rz   r{   r|   r}   r~   r   re   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&   r7   r6   r/   s   r   r   z*MultiProcessRunner.start.<locals>.<lambda>   s    4??4844 r$   r   T)	r3   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)	recursiver_   )	psutilr   r   pidchildrenri   NoSuchProcessr   TimeoutExpired)r"   processr   s      r   r0   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   )rM   rN   rO   r#   r,   r0   rj   rk   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.
    Frn   c                 \    t                                          ||           d | _        d S rp   r   rr   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: rE   z.serverposixstart dash process with %s)stdoutstderrc                  8                           j                  S r&   r   r/   s   r   r   z%ProcessRunner.start.<locals>.<lambda>1      ttx88 r$   r   'process server has encountered an errorFT)loggingr   r   shlexsplitrJ   r   debug
subprocessPopenPIPEr   r   r   OSErrorrf   r   r   r0   )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   	terminaterL   ospathexistsr   shutilrmtreer   r   communicater    r   ri   )r"   _excepts     r   r0   zProcessRunner.stop;  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   rn   )rM   rN   rO   rP   r#   r,   r0   rj   rk   s   @r   r   r   	  s|         
      % % % %N/ / / / / / /r$   r   c                   (     e Zd Zd fd	ZddZ xZS )	RRunnerFrn   c                 \    t                                          ||           d | _        d S rp   r   rr   s      r   r#   zRRunner.__init__S  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/r_   \/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   rJ   getenvuuiduuid4hexr!   mkdirrL   r   r   r   openwriteinspectstackreplacerealpathwarninglistdirbasenamer   r   copytreer   r   r   r   r   r   r   r   rf   r   r"   r   r   r   r   fpentryassetsassetr   r*   s   `  `       r   r,   zRRunner.startX  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rM   rN   rO   r#   r,   rj   rk   s   @r   r   r   R  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 )	JuliaRunnerFrn   c                 \    t                                          ||           d | _        d S rp   r   rr   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   r_   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 --project 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<<<{6rw//4466$/>Q
 
 
 	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  rk   s   @r   r  r    sY             
Y Y Y Y Y Y Y Yr$   r  )r   )"rH   r   r   r   r   rY   r   r   r   r   r`   r
   r2   r   r   dash.testing.errorsr   r   r   r   dash.testingr   	getLoggerrM   r   r   r   ThreadrU   rm   r   r   r   r  r;   r$   r   <module>r     se   



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