
    e.                        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 d dlmZ d dlZd dlZd dlZd dlmZ d dlZd dlmZ d dlmZ  eej                  Z	 d dlZ eej                  Zn# e $ r d\  ZZY nw xY wej!        "                    edu pe ed          k     d	
          Z#	 d dl$Z% e%j&        d           n# e $ r dZ%Y nw xY wej!        "                    e%du d
          Z'	 d dl(Z(n# e $ r dZ(Y nw xY wej!        "                    e(du d
          Z)	 d dl*Z*n# e $ r dZ*Y nw xY wej!        "                    e*du d
          Z+ ej,        d          Z-dej.        v Z/ej!        "                     ej0                    dk    d
          Z1d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 d Z8d Z9d*dZ:d*dZ;d Z<d+dZ=d,d Z>d-d"Z?d.d$Z@ejA        d/d%            ZB G d& d'          ZCd( ZDd) ZEdS )0    N)EmptyQueue)Thread)Version)serve)state)NNz	1.13.0a23zrequires holoviews)reasonAggzrequires matplotlibzrequires streamzzrequires jupyter_bokehz-Bokeh app running at: http://localhost:(\d+)/posixLinuxzOnly supported on Linux)Alert)Markdown)_ButtonBasec                      dd l m}  |                                 }|                    d          }|                    t
          j                            dd                     |                     |           |S )Nr   o   
      )	matplotlib.pyplotpyplotfigureadd_subplotplotnprandomrandclose)pltfigaxs      0lib/python3.11/site-packages/panel/tests/util.py
mpl_figurer!   C   sh    ######
**,,C			BGGBINN2q!!"""IIcNNNJ    c                    ddi| _         |j         d         dk    sJ t          j                    5  t          j        d           d| _        d d d            n# 1 swxY w Y   |j         d         dk    sJ dg| _        t          | t                    r|j        g dk    sJ nXt          | t                    r|j        ddgk    sJ n3t          | t                    r|j        ddgk    sJ n|j        dgk    sJ d	| _
        |j
        d	k    sJ d
| _        |j        d
k    sJ d| _        |j        dk    sJ d| _        |j        dk    sJ d| _        |j        dk    sJ d| _        |j        dk    sJ d| _        |j        dk    sJ d| _        |j        dk    sJ d| _        |j        dk    sJ d| _        |j        dk    sJ d S )N
backgroundz#fffff0ignorez#ffffffcustom_class)markdownr&   alertzalert-primaryr'   solidi  i  i,     iX  i&  r   stretch_widthmaxmin)styleswarningscatch_warningssimplefilterr$   css_classes
isinstancer   r   r   widthheight
min_height	min_width
max_height	max_widthmarginsizing_modewidth_policyheight_policy)
layoutablemodels     r    check_layoutable_propertiesr@   L   s   %y1J<%2222 
	 	"	" * *h''' )
* * * * * * * * * * * * * * * <%2222,-J*e$$ 5 $Z$Z$ZZZZZZ	J	)	) 5 Z$@@@@@@	J	,	, 5 Wn$====== ^$44444J;#J<3Js""""J?c!!!!Js""""J?c!!!!J<2,J////#J&&&&$J%''''''s   AAA  d   c                   	 d}t          j                     		fd}d d}	 	  |             }|dvrt          d|          |dS |rdS  |            rt          |          n1# t          $ r$} |            rt          |          |Y d}~nd}~ww xY w|r|                    |           nt          j        |dz             )	a  
    Exercice a test function in a loop until it evaluates to True
    or times out.

    The function can either be a simple lambda that returns True or False:
    >>> wait_until(lambda: x.values() == ['x'])

    Or a defined function with an assert:
    >>> def _()
    >>>    assert x.values() == ['x']
    >>> wait_until(_)

    In a Playwright context test you should pass the page fixture:
    >>> wait_until(lambda: x.values() == ['x'], page)

    Parameters
    ----------
    fn : callable
        Callback
    page : playwright.sync_api.Page, optional
        Playwright page
    timeout : int, optional
        Total timeout in milliseconds, by default 5000
    interval : int, optional
        Waiting interval, by default 100

    Adapted from pytest-qt.
    Tc                  F    t          j                     z
  } | dz  }|k    S N  timeelapsed
elapsed_msstarttimeouts     r    	timed_outzwait_until.<locals>.timed_out   '    )++%t^
G##r"   wait_until timed out in  millisecondsNTFz@`wait_until` callback must return None, True or False, returned NrF   )rH   
ValueErrorTimeoutErrorAssertionErrorwait_for_timeoutsleep
fnpagerM   interval__tracebackhide__rN   timeout_msgresulterL   s
     `      @r    
wait_untilr`      s]   < IKKE$ $ $ $ $ $
 DWCCCK(	0RTTF
 000 2'-2 2  
 ~ y{{ 0";///0  	7 	7 	7y{{ 7";//Q67 7 7 7 7	7"  	( !!(++++Jx$'''3(s   
A( (
B2BBc                   	K   d}t          j                     		fd}d d}	 	  |             }t          j        |          r| d{V }|dvrt          d|          |dS |rdS  |            rt	          |          n1# t
          $ r$} |            rt	          |          |Y d}~nd}~ww xY w|r|                    |           d{V  nt          j        |dz             d{V  )	a0  
    Exercise a test function in a loop until it evaluates to True
    or times out.

    The function can either be a simple lambda that returns True or False:
    >>> await async_wait_until(lambda: x.values() == ['x'])

    Or a defined function with an assert:
    >>> async def _()
    >>>    assert x.values() == ['x']
    >>> await async_wait_until(_)

    In a Playwright context test, you should pass the page fixture:
    >>> await async_wait_until(lambda: x.values() == ['x'], page)

    Parameters
    ----------
    fn : callable
        Callback
    page : playwright.async_api.Page, optional
        Playwright page
    timeout : int, optional
        Total timeout in milliseconds, by default 5000
    interval : int, optional
        Waiting interval, by default 100

    Adapted from pytest-qt.
    Tc                  F    t          j                     z
  } | dz  }|k    S rE   rG   rI   s     r    rN   z#async_wait_until.<locals>.timed_out   rO   r"   rP   rQ   NrR   zA`wait_until` callback must return None, True, or False, returned rF   )rH   asyncioiscoroutinerS   rT   rU   rV   rW   rX   s
     `      @r    async_wait_untilre      s     < IKKE$ $ $ $ $ $
 DWCCCK1	0RTTF"6** &%
 000 2'-2 2  
 ~ y{{ 0";///0  	7 	7 	7y{{ 7";//Q67 7 7 7 7	7"  	1 ''1111111111-400000000071s   &B 
B4B//B4c                  ~    t           j        dv rdS t           j        dk    rdS t          dt           j                   )z8
    Get the CTRL modifier on the current platform.
    )linuxwin32ControldarwinMetaz)No control modifier defined for platform )sysplatformrS    r"   r    get_ctrl_modifierro     sB     |)))y		!	!vSS\SSTTTr"   c           
          t          j                    j        t          | f|pdddd|pdd| t	          fd|           t
          j                 d         }|j        }t          ||           |S )Nr   TF )portthreadedshowliveness	server_idprefixc                        t           j        v S N)r   _servers)rv   s   r    <lambda>z serve_and_wait.<locals>.<lambda>  s    yEN2 r"   )rw   )	uuiduuid4hexr   r`   r   rz   rr   wait_for_server)apprZ   rw   rr   kwargsserverrv   s         @r    serve_and_waitr     s    
 I	#|DIA54S\ekeqoq||u{|||2222D999^I&q)F;DD((((Kr"   rq   Tc                     g |                      dfd           t          || fi |}|                     d| |            |rt          fd| d           |fS )Nconsolec                 .                         |           S ry   )append)msgmsgss    r    r{   z!serve_component.<locals>.<lambda>#  s    4;;s#3#3 r"   http://localhost:c                  6    t          d  D                       S )Nc              3   8   K   | ]}d t          |          v V  dS )z"Websocket connection 0 is now openN)str).0r   s     r    	<genexpr>z4serve_component.<locals>.<lambda>.<locals>.<genexpr>(  s/      ``TWCs3xxO``````r"   )any)r   s   r    r{   z!serve_component.<locals>.<lambda>(  s    3``[_````` r"   r   )r[   )onr   gotor`   )rZ   r   suffixwaitr   rr   r   s         @r    serve_componentr   !  s    DGGI3333444#t..v..DII0$000111 u````bfqstttt:r"      c                     t          | fdi|fdt          |          D             }t          |          dk    r|d         n|S )Nrr   c                 B    g | ]}t          j        d             S )r   )requestsget)r   irr   r   s     r    
<listcomp>z%serve_and_request.<locals>.<listcomp>/  s1    OOOHL;T;6;;<<OOOr"   r   r   )r   rangelen)r   r   nrr   r   reqss    ` `  r    serve_and_requestr   -  s\    #33D3F33DOOOOOeAhhOOOD$ii1nn477$.r"      c                    t          j                     }|pd}d|  | d}	 	 t          j        |          j        rd S n# t          $ r Y nw xY wt          j        d           t          j                     |z
  |k    rt          | d          n)Nrq   r   z	/livenessTg?z  did not respond before timeout.)rH   r   r   ok	ExceptionrW   RuntimeError)rr   rw   rM   rL   urls        r    r   r   3  s    IKKE\rF
5d
5F
5
5
5CI	|C  #  	 	 	D	
4IKK((#GGGHHHIs   > 
A
Ac              #   <  K   t           j        dddg| z   }t          j        |t          j        t          j        d|t                    }	 |V  |                                 |                                 d S # t          $ r}|                                 |                                 t          d|           	 |j                                                                        }t          d           t          |           n# t          $ r Y nw xY w d }~ww xY w)Nz-mpanelr   F)stdoutstderrshellcwd	close_fdszAn error occurred: %sz%
---- subprocess stdout follows ----
)rl   
executable
subprocessPopenPIPESTDOUTON_POSIX	terminater   r   printr   readdecode)argsr   cmdpr_   outs         r    run_panel_server   B  s     >4'
2T
9CZ_Z=NV[adpxyyyA 	
	  
 
 
		%q)))	(--//((**C;<<<#JJJJ 	 	 	D	
s=   
A8 8
D8D;A	DD
DDDDDc                       e Zd ZddZddZdS )NBSRreturnNc                     || _         t                      | _        d }t          || j         | j        f          | _        d| j        _        | j                                         dS )z
        NonBlockingStreamReader

        stream: the stream to read from.
                Usually a process' stdout or stderr.
        c                     t          | j        d          D ]}|                    |           |                                  dS )zN
            Collect lines from 'stream' and put them in 'queue'.
            r"   N)iterreadlineputr   )streamqueuelines      r    _populateQueuez%NBSR.__init__.<locals>._populateQueued  sA     V_c22    		$LLNNNNNr"   )targetr   TN)_sr   _qr   _tdaemonrL   )selfr   r   s      r    __init__zNBSR.__init__Y  sg     ''	 	 	 .)+ + +r"   c                 b    	 | j                             |d u|          S # t          $ r Y d S w xY w)N)blockrM   )r   r   r   )r   rM   s     r    r   zNBSR.readlineq  sS    	7;;T)      	 	 	44	s     
..)r   Nry   )__name__
__module____qualname__r   r   rn   r"   r    r   r   X  s<           0     r"   r   c                    t          |           }d }g }t          d          D ]b}|                    d          }|s|                    d          }|                    |           t
                              |          }| nc|,d                    |          }t          j	        d|            t          |                    d                    S )N   g      ?zutf-8z
    zKNo matching log line in process output, following output was captured:

   r   )r   r   r   r   r   APP_PATTERNsearchjoinpytestfailintgroup)r   nbsrmoutput_or   s          r    wait_for_portr   z  s    <<DAF2YY  MM# 	hhwcs##=E yv&&,#), ,	
 	
 	
 qwwqzz??r"   c                     |                     |            |                                 t          j        |           |                    d           d S )Nr   )writeflushosfsyncseek)contentfile_objs     r    
write_filer     sL    NN7NNHXMM!r"   )NrA   rB   )NNN)rq   T)rq   r   N)Nr   ry   )Frc   
contextlibr   rm   rer   rl   rH   r|   r/   r   r   r   	threadingr   numpyr   r   r   packaging.versionr   r   pnpanel.io.serverr   panel.io.stater   __version__pnv	holoviewshv
hv_versionr   markskipifhv_available
matplotlibmplusempl_availablestreamzstreamz_availablejupyter_bokehjb_availablecompiler   builtin_module_namesr   system	unix_onlypanel.pane.alertr   panel.pane.markupr   panel.widgets.buttonr   r!   r@   r`   re   ro   r   r   r   r   contextmanagerr   r   r   r   rn   r"   r    <module>r     sC        				  				     



                        % % % % % %     ! ! ! ! ! !             gbn ((JJ      NB


 {!!"*"Q
WW[=Q=Q0Q)= " ? ?CGENNNN   
CCC""3$;7L"MMNNNN   GGGK&&w$?Q&RR    MMM{!!-4"7@X!YYbjIJJc..Kx00G;D]^^	 " " " " " " & & & & & & , , , , , ,  0( 0( 0(fB( B( B( B(JD1 D1 D1 D1N	U 	U 	U   	 	 	 	/ / / /I I I I    *               D  *    sH   *A? ?
BB=C CC>D DD/D4 4D>=D>