a
    Ò�éi^  ã                   @   s¦   d dl Z d dlmZmZmZmZ d dl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 er”d d
lmZ G dd„ dƒZdS )é    N)ÚTYPE_CHECKINGÚAnyÚOptionalÚcast)Úgreenlet)ÚChannelOwnerÚ
Connection)Úcompute_driver_executable)ÚError)Úcreate_remote_object)Ú
Playwright)ÚPipeTransport)ÚAbstractChildWatcherc                   @   sF   e Zd Zddœdd„Zedœdd„Zedœdd„Zedd	œd
d„ZdS )ÚPlaywrightContextManagerN©Úreturnc                 C   s   |  |  d| _ d | _d| _d S )NF)Ú	_own_loopÚ_watcherÚ_exit_was_called©Úself© r   úT/home/Claro/venv/lib/python3.9/site-packages/playwright/sync_api/_context_manager.pyÚ__init__!   s
    z!PlaywrightContextManager.__init__c                    sÀ   zt  ¡ ˆ_W n" ty0   t  ¡ ˆ_dˆ_Y n0 ˆj ¡ rDtdƒ‚d dœ‡fdd„}t|ƒ}t	|t
tˆjtƒ ƒˆjƒˆ_t ¡ ‰ td dœ‡ ‡fdd„}ˆj d	|¡ | ¡  ˆj}ˆj|_|S )
NTzjIt looks like you are using Playwright Sync API inside the asyncio loop.
Please use the Async API instead.r   c                      s   ˆ j  ˆ j ¡ ¡ d S ©N)Ú_loopÚrun_until_completeÚ_connectionZrun_as_syncr   r   r   r   Úgreenlet_main7   s    z9PlaywrightContextManager.__enter__.<locals>.greenlet_main)Úchannel_ownerr   c                    s    t t| ƒ}t|ƒˆ_ˆ  ¡  d S r   )r   r   ÚSyncPlaywrightÚ_playwrightÚswitch)r   Zplaywright_impl©Zg_selfr   r   r   Úcallback_wrapperE   s    

z<PlaywrightContextManager.__enter__.<locals>.callback_wrapperr   )ÚasyncioZget_running_loopr   ÚRuntimeErrorZnew_event_loopr   Z
is_runningr
   r   r   r   r   r	   r   Z
getcurrentr   Zcall_on_object_with_known_namer"   r!   Ú__exit__Ústop)r   r   Zdispatcher_fiberr$   Z
playwrightr   r#   r   Ú	__enter__(   s0    

ÿüz"PlaywrightContextManager.__enter__c                 C   s   |   ¡ S r   )r)   r   r   r   r   ÚstartS   s    zPlaywrightContextManager.start)Úargsr   c                 G   sx   | j r
d S d| _ | j ¡  | jr*| j ¡  | jrtt | j¡}dd„ |D ƒD ]}| 	¡  qJ| j 
| j ¡ ¡ | j ¡  d S )NTc                 S   s    g | ]}|  ¡ s| ¡ s|‘qS r   )ÚdoneZ	cancelled)Ú.0Útr   r   r   Ú
<listcomp>_   ó    z5PlaywrightContextManager.__exit__.<locals>.<listcomp>)r   r   Z	stop_syncr   Úcloser   r%   Z	all_tasksr   Úcancelr   Zshutdown_asyncgens)r   r+   Ztasksr.   r   r   r   r'   V   s    


z!PlaywrightContextManager.__exit__)	Ú__name__Ú
__module__Ú__qualname__r   r    r)   r*   r   r'   r   r   r   r   r       s   +r   )r%   Útypingr   r   r   r   r   Zplaywright._impl._connectionr   r   Zplaywright._impl._driverr	   Zplaywright._impl._errorsr
   Z playwright._impl._object_factoryr   Zplaywright._impl._playwrightr   Zplaywright._impl._transportr   Zplaywright.sync_api._generatedr    Zasyncio.unix_eventsr   r   r   r   r   r   Ú<module>   s   