
    g;                         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      Zy)    N)TYPE_CHECKINGAnyOptionalcast)greenlet)ChannelOwner
Connection)Error)MainGreenlet)create_remote_object)
Playwright)PipeTransport)AbstractChildWatcherc                   <    e Zd ZddZdefdZdefdZdeddfdZy)	PlaywrightContextManagerreturnNc                 6    |  |  d| _         d | _        d| _        y )NF)	_own_loop_watcher_exit_was_calledselfs    Y/var/www/openai/venv/lib/python3.12/site-packages/playwright/sync_api/_context_manager.py__init__z!PlaywrightContextManager.__init__!   s    8< %    c                 J    	 t        j                          _         j                  j                         rt        d      d fd}t        |      }t        |t        t         j                         j                         _        t        j                         dt        dd f fd} j                  j!                  d|       |j#                           j$                  } j&                  |_        |S # t        $ r$ t        j                          _        d _        Y w xY w)	NTzjIt looks like you are using Playwright Sync API inside the asyncio loop.
Please use the Async API instead.r   c                  l     j                   j                   j                  j                                y N)_looprun_until_complete_connectionrun_as_syncr   s   r   greenlet_mainz9PlaywrightContextManager.__enter__.<locals>.greenlet_main7   s$    JJ))$*:*:*F*F*HIr   channel_ownerc                 f    t        t        |       }t        |      _        j	                          y r   )r   r   SyncPlaywright_playwrightswitch)r$   playwright_implg_selfr   s     r   callback_wrapperz<PlaywrightContextManager.__enter__.<locals>.callback_wrapperE   s%    ":}=O-o>DMMOr   r   r   N)asyncioget_running_loopr   RuntimeErrornew_event_loopr   
is_runningr
   r   r	   r   r   r!   r   
getcurrentr   call_on_object_with_known_namer(   r'   __exit__stop)r   r#   dispatcher_fiberr+   
playwrightr*   s   `    @r   	__enter__z"PlaywrightContextManager.__enter__(   s   	" 113DJ ::  "% 	J (6% $**%JJ	
 $$&	L 	T 	 	77FVW!%%
--
M  	" //1DJ!DN	"s   C5 5)D"!D"c                 "    | j                         S r   )r8   r   s    r   startzPlaywrightContextManager.startS   s    ~~r   argsc                 6   | j                   ry d| _         | j                  j                          | j                  r| j                  j	                          | j
                  rt        j                  | j                        }|D cg c]&  }|j                         r|j                         r%|( c}D ]  }|j                           | j                  j                  | j                  j                                | j                  j	                          y y c c}w )NT)r   r!   	stop_syncr   closer   r-   	all_tasksr   done	cancelledcancelr    shutdown_asyncgens)r   r;   tasksts       r   r4   z!PlaywrightContextManager.__exit__V   s       $""$==MM!>>%%djj1E!&JAqvvx1;;=aJ
 KJJ))$***G*G*IJJJ Js   DD+Dr,   )	__name__
__module____qualname__r   r&   r8   r:   r   r4    r   r   r   r       s3    &)> )V ~  c d r   r   )r-   typingr   r   r   r   r   playwright._impl._connectionr   r	   playwright._impl._errorsr
   playwright._impl._greenletsr    playwright._impl._object_factoryr   playwright._impl._playwrightr   playwright._impl._transportr   playwright.sync_api._generatedr&   asyncio.unix_eventsr   r   rI   r   r   <module>rS      s:     5 5  A * 4 A 3 5 G8B Br   