
    YHh                       d dl m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
m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"m#Z# d dl$m%Z%  G d d      Z&edgdf   Z'dddZ(y)    )annotationsN)AsyncExitStacksuppress)TracebackType)AnyCallable)	urlencode
urlunparse)default)BackendType)find_available_port)REACTPY_TESTING_DEFAULT_TIMEOUT)	component)use_callback
use_effect	use_state)ComponentConstructor)LogAssertionErrorcapture_reactpy_logslist_logged_exceptions)Refc                      e Zd ZU dZded<   ded<    e       Z	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZedd       Z	ddd
Z
d	eej                  df	 	 	 	 	 	 	 	 	 ddZddZ	 	 	 	 	 	 	 	 ddZy)BackendFixturea"  A test fixture for running a server and imperatively displaying views

    This fixture is typically used alongside async web drivers like ``playwight``.

    Example:
        .. code-block::

            async with BackendFixture() as server:
                server.mount(MyComponent)
    list[logging.LogRecord]_recordszasyncio.Task[Any]_server_futureNc                    || _         |xs t        |      | _        t               \  | _        | _        |t        j                  n|| _        ||d}t        |      || _
        |xs t        | _        || _        y )NzXIf an application instance its corresponding server implementation must be provided too.)hostr   port_hotswapmount_root_componentr   currenttimeout
ValueError_appdefault_serverimplementation_options)selfr   r   appr(   optionsr$   msgs           P/var/www/html/planif/env/lib/python3.12/site-packages/reactpy/testing/backend.py__init__zBackendFixture.__init__)   s{     	5/5	+3:(
D(7>+33G 	 ?~5lCS/!	,>    c                    | j                   S )zA list of captured log records)r   )r*   s    r.   log_recordszBackendFixture.log_recordsA   s     }}r0    c           
     p    t        d| j                   d| j                   |dt        |xs d      dg      S )zReturn a URL string pointing to the host and point of the server

        Args:
            path: the path to a resource on the server
            query: a dictionary or list of query parameters
        http:r3    )r
   r   r   r	   )r*   pathquerys      r.   urlzBackendFixture.urlF   sD     99+Qtyyk*%+2&	
 		
r0   Tc                4    t        | j                  ||||      S )a  Return a list of logged exception matching the given criteria

        Args:
            log_level: The level of log to check
            exclude_exc_types: Any exception types to ignore
            del_log_records: Whether to delete the log records for yielded exceptions
        )r   r2   )r*   patterntypes	log_leveldel_log_recordss        r.   r   z%BackendFixture.list_logged_exceptionsX   s&     &
 	
r0   c                   K   t                _         j                  j                  t                      _         j
                  xs  j                  j                         } j                  j                  | j                   j                         t        j                         }t        j                   j                  j                  | j                   j                   |            d fd} j                  j#                  |       	 t        j$                  |j'                          j(                         d {     S 7 # t*        $ r, t        j$                   j(                         d {  7    w xY ww)Nc                    K   j                          t        t        j                        5  t        j                   j
                         d {    d d d        y 7 # 1 sw Y   y xY ww)Nr$   )cancelr   asyncioCancelledErrorwait_forr$   )r*   server_futures   r.   stop_serverz.BackendFixture.__aenter__.<locals>.stop_server|   s]       "'001 L&&}dllKKKL LKL Ls.   *A.%A"A A"	A. A""A+'A.rB   )returnNone)r   _exit_stackenter_contextr   r   r&   r(   create_development_app	configurer"   r)   rD   Eventcreate_taskserve_development_appr   r   push_async_callbackrF   waitr$   	Exception)r*   r+   startedrH   rG   s   `   @r.   
__aenter__zBackendFixture.__aenter__n   s&    )+((667K7MNiiG4..EEG%%c4+?+?O--/++55TYY		7
	L
 	,,[9	""7<<>4<<HHH  I 	""=$,,GGG	s<   DF2E E
E F
E -F9E<:FFc                   K   | j                   j                          d {    | j                  d        | j                  d      }|rd}t	        |      |d   y 7 ;w)NF)r?   zUnexpected logged exceptionr   )rK   acloser!   r   r   )r*   exc_type	exc_value	tracebacklogged_errorsr-   s         r.   	__aexit__zBackendFixture.__aexit__   sc      %%'''

433E3J/C#C(mA.>>  	(s   AA<A)z	127.0.0.1NNNNN)r   strr   z
int | Noner+   
Any | Noner(   zBackendType[Any] | Noner,   r_   r$   zfloat | NonerI   rJ   )rI   r   )r3   N)r8   r^   r9   r_   rI   r^   )
r<   r^   r=   z!type[Any] | tuple[type[Any], ...]r>   intr?   boolrI   zlist[BaseException])rI   r   )rY   ztype[BaseException] | NonerZ   zBaseException | Noner[   zTracebackType | NonerI   rJ   )__name__
__module____qualname____doc____annotations__r   rK   r/   propertyr2   r:   rT   loggingERRORr   rV   r]   r7   r0   r.   r   r      s
   	 &%%% "K  26" $     	 
 0      
 0  
( 3<  $

 1
 	

 
 

,<?,? (? (	?
 
?r0   r   Callable[[], Any] | Nonec                    t        d       | r%t               t        dfd       }dfd}||fS t        dfd       }dfd}||fS )a  Swap out components from a layout on the fly.

    Since you can't change the component functions used to create a layout
    in an imperative manner, you can use ``hotswap`` to do this so
    long as you set things up ahead of time.

    Parameters:
        update_on_change: Whether or not all views of the layout should be updated on a swap.

    Example:
        .. code-block:: python

            import reactpy

            show, root = reactpy.hotswap()
            PerClientStateServer(root).run_in_thread("localhost", 8765)

            @reactpy.component
            def DivOne(self):
                return {"tagName": "div", "children": [1]}

            show(DivOne)

            # displaying the output now will show DivOne

            @reactpy.component
            def DivTwo(self):
                return {"tagName": "div", "children": [2]}

            show(DivTwo)

            # displaying the output now will show DivTwo
    c                      y Nr7   r7   r0   r.   <lambda>z_hotswap.<locals>.<lambda>       r0   c                 x    t        fd      \  } t        fd      dfd}t        |        |        S )Nc                      j                   S rm   r#   constructor_refs   r.   rn   z+_hotswap.<locals>.HotSwap.<locals>.<lambda>   s    o>U>U r0   c                       fd      S )Nc                    S rm   r7   )_news    r.   rn   z=_hotswap.<locals>.HotSwap.<locals>.<lambda>.<locals>.<lambda>   s    RU r0   r7   )rx   _set_constructors   `r.   rn   z+_hotswap.<locals>.HotSwap.<locals>.<lambda>   s    7G7V r0   c                 2    j                           fdS )Nc                 &    j                         S rm   )removeset_constructorset_constructor_callbackss   r.   rn   zA_hotswap.<locals>.HotSwap.<locals>.add_callback.<locals>.<lambda>   s    8??P r0   )addr}   s   r.   add_callbackz/_hotswap.<locals>.HotSwap.<locals>.add_callback   s    )--o>PPr0   )rI   zCallable[[], None])r   r   r   )constructorr   ry   r~   rt   r   s     @@r.   HotSwapz_hotswap.<locals>.HotSwap   s9     -66U,V)K)*+VWOQ |$= r0   c                @    | xs d x} _         D ]
  } ||         y )Nc                      y rm   r7   r7   r0   r.   rn   z(_hotswap.<locals>.swap.<locals>.<lambda>   ro   r0   rr   )r   r~   rt   r   s     r.   swapz_hotswap.<locals>.swap   s,    4?4QLQK/1#< -,-r0   c                 $     j                         S rm   rr   rs   s   r.   r   z_hotswap.<locals>.HotSwap   s    "**,,r0   c                    | xs d _         y )Nc                      y rm   r7   r7   r0   r.   rn   z(_hotswap.<locals>.swap.<locals>.<lambda>   ro   r0   rr   )r   rt   s    r.   r   z_hotswap.<locals>.swap   s    &1&ClO#r0   )rI   r   )r   rj   rI   rJ   )r   setr   )update_on_changer   r   rt   r   s      @@r.   r    r       sd    D /2,.?ONQe!		! 
	!	- = 
	- 
	-	D =r0   )F)r   ra   rI   z'tuple[_MountFunc, ComponentConstructor]))
__future__r   rD   rh   
contextlibr   r   r=   r   typingr   r   urllib.parser	   r
   reactpy.backendr   r'   reactpy.backend.typesr   reactpy.backend.utilsr   reactpy.configr   reactpy.core.componentr   reactpy.core.hooksr   r   r   reactpy.core.typesr   reactpy.testing.logsr   r   r   reactpy.utilsr   r   
_MountFuncr    r7   r0   r.   <module>r      si    "   /    . 5 - 5 : , B B 3 
 @? @?F 12D89
Dr0   