
    ,hFD                    "   d dl mZ d dl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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mZ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( ddl)m*Z* ddl+m,Z, ejZ                  dk\  r	d dlm.Z.m/Z/ nd dl0m.Z.m/Z/  ed      Z1 edd      Z2 e.d      Z3	 	 	 	 	 	 d%dZ4	 	 	 	 	 	 d&dZ5 G d dee2   e      Z6 G d de,      Z7 G d d       Z8e G d! d"             Z9e	 d'	 	 	 	 	 d(d#       Z:d)d$Z;y)*    )annotationsN)	AwaitableCallable	Generator)Future)AbstractAsyncContextManagerAbstractContextManagercontextmanager)	dataclassfield)isawaitable)LockThread	get_ident)TracebackType)AnyGenericTypeVarcastoverload   )
_eventloop)get_async_backendget_cancelled_exc_classthreadlocals)Event)CancelScopecreate_task_group)AsyncBackend)
TaskStatus)      )TypeVarTupleUnpackT_RetvalT_coT)	covariantPosArgsTc                    	 t         j                  }t         j                  }|j                  | ||      S # t        $ r t	        d      dw xY w)z
    Call a coroutine function from a worker thread.

    :param func: a coroutine function
    :param args: positional arguments for the callable
    :return: the return value of the coroutine function

    9This function can only be run from an AnyIO worker threadNtoken)r   current_async_backendcurrent_tokenAttributeErrorRuntimeErrorrun_async_from_threadfuncargsasync_backendr,   s       S/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/anyio/from_thread.pyrunr7   (   s[    $::** ..tT.GG  G
		    6 Ac                    	 t         j                  }t         j                  }|j                  | ||      S # t        $ r t	        d      dw xY w)z
    Call a function in the event loop thread from a worker thread.

    :param func: a callable
    :param args: positional arguments for the callable
    :return: the return value of the callable

    r*   Nr+   )r   r-   r.   r/   r0   run_sync_from_threadr2   s       r6   run_syncr;   >   s[    $::** --dD-FF  G
	r8   c                  r    e Zd ZU ded<   ded<   ded<   dZded	<   	 	 	 	 dd
ZddZddZ	 	 	 	 	 	 	 	 ddZy)_BlockingAsyncContextManagerzFuture[T_co]_enter_futurezFuture[bool | None]_exit_futurer   _exit_event)NNNzMtuple[type[BaseException] | None, BaseException | None, TracebackType | None]_exit_exc_infoc                     || _         || _        y N)	_async_cm_portal)selfasync_cmportals      r6   __init__z%_BlockingAsyncContextManager.__init__\   s     "    c                  K   	 t               | _        | j                  j                          d {   }| j                  j                  |       	 | j                  j                          d {     | j                  j                  | j                    d {   }|S 7 o# t        $ r!}| j                  j                  |        d }~ww xY w7 `7 7#  | j                  j                  | j                    d {  7  }|c cY S xY wwrC   )r   r@   rD   
__aenter__r>   
set_resultBaseExceptionset_exceptionwait	__aexit__rA   )rF   valueexcresults       r6   run_async_cmz)_BlockingAsyncContextManager.run_async_cmb   s     	1$wD..3355E
 ))%0	
 ""'')))
 44>>33T5H5HIIFM% 6 	,,S1	 *
 J34>>33T5H5HIIIFMs   D	,B" B B" D	C .C/C 3&D	CD	 B" "	C+CCD	C D	'D:C=;	DD	c                    t               | _        | j                  j                  | j                        | _        | j                  j                         S rC   )r   r>   rE   start_task_soonrU   r?   rT   rF   s    r6   	__enter__z&_BlockingAsyncContextManager.__enter__y   s>    #X LL889J9JK!!((**rJ   c                    |||f| _         | j                  j                  | j                  j                         | j
                  j                         S rC   )rA   rE   callr@   setr?   rT   )rF   &_BlockingAsyncContextManager__exc_type'_BlockingAsyncContextManager__exc_value'_BlockingAsyncContextManager__tracebacks       r6   __exit__z%_BlockingAsyncContextManager.__exit__~   sF     )+{B$**../  ''))rJ   N)rG   !AbstractAsyncContextManager[T_co]rH   BlockingPortal)returnbool | None)rc   r&   )r]   type[BaseException] | Noner^   BaseException | Noner_   TracebackType | Nonerc   rd   )	__name__
__module____qualname____annotations__rA   rI   rU   rY   r`    rJ   r6   r=   r=   T   sr    %% 	   9CQ.+
*.* ** *	*
 
*rJ   r=   c                      e Zd ZddZdddZy)_BlockingPortalTaskStatusc                    || _         y rC   )_future)rF   futures     r6   rI   z"_BlockingPortalTaskStatus.__init__   s	    rJ   Nc                :    | j                   j                  |       y rC   )rp   rM   )rF   rR   s     r6   startedz!_BlockingPortalTaskStatus.started   s    &rJ   )rq   r   rC   )rR   objectrc   None)rh   ri   rj   rI   rs   rl   rJ   r6   rn   rn      s    'rJ   rn   c                  z   e Zd ZdZddZddZddZ	 	 	 	 	 	 	 	 ddZddZddZ	dddZ
	 	 	 	 	 	 	 	 	 	 dd	Z	 	 	 	 	 	 	 	 	 	 	 	 dd
Ze	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 dd       Z	 	 	 	 	 	 ddZedd	 	 	 	 	 	 	 dd       Zedd	 	 	 	 	 	 	 d d       Zdd	 	 	 	 	 	 	 d!dZdd	 	 	 	 	 	 	 d"dZ	 	 	 	 d#dZy)$rb   zLAn object that lets external threads run code in an asynchronous event loop.c                2    t               j                         S rC   )r   create_blocking_portal)clss    r6   __new__zBlockingPortal.__new__   s     "99;;rJ   c                |    t               | _        t               | _        t	               | _        t               | _        y rC   )r   _event_loop_thread_idr   _stop_eventr   _task_groupr   _cancelled_exc_classrX   s    r6   rI   zBlockingPortal.__init__   s,    1:" 7,.$;$=!rJ   c                V   K   | j                   j                          d {    | S 7 wrC   )r~   rL   rX   s    r6   rL   zBlockingPortal.__aenter__   s(     ))+++ 	,s   )')c                   K   | j                          d {    | j                  j                  |||       d {   S 7 )7 wrC   )stopr~   rQ   )rF   exc_typeexc_valexc_tbs       r6   rQ   zBlockingPortal.__aexit__   s>      iik%%//'6JJJ 	Js   AA $AAAAc                v    | j                   t        d      | j                   t               k(  rt        d      y )NzThis portal is not runningz7This method cannot be called from the event loop thread)r|   r0   r   rX   s    r6   _check_runningzBlockingPortal._check_running   s?    %%-;<<%%4I  5rJ   c                T   K   | j                   j                          d{    y7 w)z#Sleep until :meth:`stop` is called.N)r}   rP   rX   s    r6   sleep_until_stoppedz"BlockingPortal.sleep_until_stopped   s     ##%%%s   (&(c                   K   d| _         | j                  j                          |r%| j                  j                  j                          yyw)a.  
        Signal the portal to shut down.

        This marks the portal as no longer accepting new calls and exits from
        :meth:`sleep_until_stopped`.

        :param cancel_remaining: ``True`` to cancel all the remaining tasks, ``False``
            to let them finish before returning

        N)r|   r}   r\   r~   cancel_scopecancel)rF   cancel_remainings     r6   r   zBlockingPortal.stop   sB      &*"))002 s   A
Ac                \   	K   d	 fd}	  ||i |}t        |      rPt               5 	|j                         r	j                          n|j	                  |       | d {   }d d d        n|}|j                         s|j                         	 d 	y 	 d 	y 7 8# 1 sw Y   4xY w#  j                  $ r% |j                          |j                          Y d 	y t        $ r>}|j                         s|j                  |       t        |t              s Y d }~d 	y d }~ww xY w# d 	w xY ww)Nc                    | j                         r5j                  d t               fvrj                  j                         y y y rC   )	cancelledr|   r   r[   r   )fscoperF   s    r6   callbackz+BlockingPortal._call_func.<locals>.callback   s@    {{}!;!;D " 		%,,'	"}rJ   )r   Future[T_Retval]rc   ru   )r   r   r   r   add_done_callbackrM   r   set_running_or_notify_cancelrN   rO   
isinstance	Exception)
rF   r3   r4   kwargsrq   r   retval_or_awaitableretvalrS   r   s
   `        @r6   
_call_funczBlockingPortal._call_func   s)    	(	"&"7"7./ ] 7e'')00:#66F7 7 - ##%!!&)E & E# 77 7 (( 	2MMO//1 E  	##%$$S) c9- . E	 Es   D,B) 8B"B#B'B) 2!D% D,D,BB&"B) )/D"D% D,D"$2DD% D,D""D% %D))D,c                    t         )a%  
        Spawn a new task using the given callable.

        Implementers must ensure that the future is resolved when the task finishes.

        :param func: a callable
        :param args: positional arguments to be passed to the callable
        :param kwargs: keyword arguments to be passed to the callable
        :param name: name of the task (will be coerced to a string if not ``None``)
        :param future: a future that will resolve to the return value of the callable,
            or the exception raised during its execution

        )NotImplementedError)rF   r3   r4   r   namerq   s         r6   _spawn_task_from_threadz&BlockingPortal._spawn_task_from_thread   s
    * "!rJ   c                     y rC   rl   rF   r3   r4   s      r6   r[   zBlockingPortal.call  s    
 rJ   c                     y rC   rl   r   s      r6   r[   zBlockingPortal.call  s     rJ   c                `    t        t         | j                  |g| j                               S )a3  
        Call the given function in the event loop thread.

        If the callable returns a coroutine object, it is awaited on.

        :param func: any callable
        :raises RuntimeError: if the portal is not running or if this method is called
            from within the event loop thread

        )r   r%   rW   rT   r   s      r6   r[   zBlockingPortal.call  s-     H2d224?$?FFHIIrJ   N)r   c                    y rC   rl   rF   r3   r   r4   s       r6   rW   zBlockingPortal.start_task_soon$       rJ   c                    y rC   rl   r   s       r6   rW   zBlockingPortal.start_task_soon,  r   rJ   c               d    | j                          t               }| j                  ||i ||       |S )a  
        Start a task in the portal's task group.

        The task will be run inside a cancel scope which can be cancelled by cancelling
        the returned future.

        :param func: the target function
        :param args: positional arguments passed to ``func``
        :param name: name of the task (will be coerced to a string if not ``None``)
        :return: a future that resolves with the return value of the callable if the
            task completes successfully, or with the exception raised in the task
        :raises RuntimeError: if the portal is not running or if this method is called
            from within the event loop thread
        :rtype: concurrent.futures.Future[T_Retval]

        .. versionadded:: 3.0

        )r   r   r   )rF   r3   r   r4   r   s        r6   rW   zBlockingPortal.start_task_soon4  s2    0 	$h$$T4T1=rJ   c                   dfd}| j                          t               t              }t               }|j                  |       | j	                  ||d|i||       |j                         fS )a  
        Start a task in the portal's task group and wait until it signals for readiness.

        This method works the same way as :meth:`.abc.TaskGroup.start`.

        :param func: the target function
        :param args: positional arguments passed to ``func``
        :param name: name of the task (will be coerced to a string if not ``None``)
        :return: a tuple of (future, task_status_value) where the ``task_status_value``
            is the value passed to ``task_status.started()`` from within the target
            function
        :rtype: tuple[concurrent.futures.Future[T_Retval], Any]

        .. versionadded:: 3.0

        c                   j                         sn| j                         rj                          y | j                         r j	                  | j                                y t        d      }j	                  |       y y )Nz1Task exited without calling task_status.started())doner   r   	exceptionrO   r0   )rq   rS   task_status_futures     r6   	task_donez,BlockingPortal.start_task.<locals>.task_doneh  sl    %**,##%&--/%%'&44V5E5E5GH&KC '44S9 -rJ   task_status)rq   r   rc   ru   )r   r   rn   r   r   rT   )rF   r3   r   r4   r   r   r   r   s          @r6   
start_taskzBlockingPortal.start_taskQ  so    .
	: 	%+X/0BCH	I&$$T4-1MtUVW$++---rJ   c                    t        ||       S )a  
        Wrap an async context manager as a synchronous context manager via this portal.

        Spawns a task that will call both ``__aenter__()`` and ``__aexit__()``, stopping
        in the middle until the synchronous context manager exits.

        :param cm: an asynchronous context manager
        :return: a synchronous context manager

        .. versionadded:: 2.1

        )r=   )rF   cms     r6   wrap_async_context_managerz)BlockingPortal.wrap_async_context_manager|  s     ,B55rJ   rc   rb   rc   ru   )r   re   r   rf   r   rg   rc   rd   )F)r   boolrc   ru   )
r3   <Callable[[Unpack[PosArgsT]], Awaitable[T_Retval] | T_Retval]r4   tuple[Unpack[PosArgsT]]r   dict[str, Any]rq   r   rc   ru   )r3   r   r4   r   r   r   r   rt   rq   r   rc   ru   r3   1Callable[[Unpack[PosArgsT]], Awaitable[T_Retval]]r4   Unpack[PosArgsT]rc   r%   r3   &Callable[[Unpack[PosArgsT]], T_Retval]r4   r   rc   r%   )r3   r   r4   r   rc   r%   )r3   r   r4   r   r   rt   rc   r   )r3   r   r4   r   r   rt   rc   r   )r3   r   r4   r   r   rt   rc   r   )r3   z"Callable[..., Awaitable[T_Retval]]r4   rt   r   rt   rc   ztuple[Future[T_Retval], Any])r   ra   rc   zAbstractContextManager[T_co])rh   ri   rj   __doc__rz   rI   rL   rQ   r   r   r   r   r   r   r[   rW   r   r   rl   rJ   r6   rb   rb      s>   V<>K,K &K %	K
 
K&3 (J( &( 	(
 !( 
(T"J" &" 	"
 " !" 
". ?   
	  :CS	 JJJ  J 
	J" 
 	?   	
 
  
 	4   	
 
  	J   	
 
B 	).0). ). 	).
 
&).V636	%6rJ   rb   c                      e Zd ZU dZdZded<   dZded<    ede	      Z	d
ed<    edd      Z
ded<    ed      Zded<    edd      Zded<   ddZ	 	 	 	 	 	 	 	 ddZy)BlockingPortalProvidera=  
    A manager for a blocking portal. Used as a context manager. The first thread to
    enter this context manager causes a blocking portal to be started with the specific
    parameters, and the last thread to exit causes the portal to be shut down. Thus,
    there will be exactly one blocking portal running in this context as long as at
    least one thread has entered this context manager.

    The parameters are the same as for :func:`~anyio.run`.

    :param backend: name of the backend
    :param backend_options: backend options

    .. versionadded:: 4.4
    asynciostrbackendNdict[str, Any] | Nonebackend_optionsF)initdefault_factoryr   _lockr   )r   defaultint_leases)r   rb   rE   z-AbstractContextManager[BlockingPortal] | None
_portal_cmc                (   | j                   5  | j                  Dt        | j                  | j                        | _        | j                  j                         | _        | xj                  dz  c_        | j                  cd d d        S # 1 sw Y   y xY w)Nr   )r   r   start_blocking_portalr   r   rY   rE   r   rX   s    r6   rY   z BlockingPortalProvider.__enter__  sn    ZZ 	 &"7LL$"6"6#  $88:LLAL<<	  	  	 s   A1BBc                    d }| j                   5  | j                  sJ | j                  dkD  sJ | xj                  dz  c_        | j                  s| j                  }d | _        | `d d d        |r|j	                  d d d        y y # 1 sw Y    xY w)Nr   r   )r   r   r   rE   r`   )rF   r   r   r   	portal_cms        r6   r`   zBlockingPortalProvider.__exit__  s     DH	ZZ 	!??"<<!##LLAL<< OO	"&L	! tT40 	! 	!s   ABBr   )r   re   r   rf   r   rg   rc   ru   )rh   ri   rj   r   r   rk   r   r   r   r   r   rE   r   rY   r`   rl   rJ   r6   r   r     s     GS-1O*1UD9E49eQ/GS/#/G^/@EDAJ= 	 1,1 &1 %	1
 
1rJ   r   c              #     K   dfdd fd}t               t        |d      }|j                          	 d}j                         }	 | 	 	 |j                  |j                  |       |j                          y# t        $ r d} w xY w# t        $ r Y ,w xY w# 	 |j                  |j                  |       w # t        $ r Y w w xY wxY w# |j                          w xY ww)a|  
    Start a new event loop in a new thread and run a blocking portal in its main task.

    The parameters are the same as for :func:`~anyio.run`.

    :param backend: name of the backend
    :param backend_options: backend options
    :return: a context manager that yields a blocking portal

    .. versionchanged:: 3.0
        Usage as a context manager is now required.

    c                    K   t               4 d {   } j                  |        | j                          d {    d d d       d {    y 7 >7 7 	# 1 d {  7  sw Y   y xY wwrC   )rb   rM   r   )portal_rq   s    r6   
run_portalz)start_blocking_portal.<locals>.run_portal  sb     !# 	0 	0wg&--///	0 	0 	0/	0 	0 	0 	0sT   A,AA,%AAA A,AA,AA,A)A A)%A,c                     j                         r	 t        j                         y y # t        $ r0} j	                         sj                  |        Y d } ~ y Y d } ~ y d } ~ ww xY w)N)r   r   )r   r   r7   rN   r   rO   )rS   r   r   rq   r   s    r6   run_blocking_portalz2start_blocking_portal.<locals>.run_blocking_portal  s]    ..0. 1
 ! .{{}((-- %.s   - 	A&!A!!A&T)targetdaemonFNr   )	r   r   startrT   rN   r[   r   r0   join)r   r   r   threadcancel_remaining_tasksrH   rq   r   s   ``    @@r6   r   r     s     $0
. . &,XF.t<F
LLN!&		L
FKK)?@ 	  	%)"	   FKK)?@  	s|   7C)C B B 3C)BB# 	B C B  C #C%CC	CCCCC C&&C)c                 x    	 t         j                  } | j	                          y# t        $ r t        d      dw xY w)aa  
    Check if the cancel scope of the host task's running the current worker thread has
    been cancelled.

    If the host task's current cancel scope has indeed been cancelled, the
    backend-specific cancellation exception will be raised.

    :raises RuntimeError: if the current thread was not spawned by
        :func:`.to_thread.run_sync`

    r*   N)r   r-   r/   r0   check_cancelled)r5   s    r6   r   r     sD    &2&H&H !!#  G
	s   # 9r   r   )r   N)r   r   r   r   rc   z$Generator[BlockingPortal, Any, None]r   )<
__future__r   syscollections.abcr   r   r   concurrent.futuresr   
contextlibr   r	   r
   dataclassesr   r   inspectr   	threadingr   r   r   typesr   typingr   r   r   r   r   _corer   _core._eventloopr   r   r   _core._synchronizationr   _core._tasksr   r   abcr   
abc._tasksr    version_infor#   r$   typing_extensionsr%   r&   r(   r7   r;   r=   rn   rb   r   r   r   rl   rJ   r6   <module>r      sE   " 
 : : % 
 )  - -    V V ) 8  "w++6:v&
#H
;HDTHH,G
0G9IGG,2*74=2H 2*j'
 'z6 z6z 51 51 51p GK11/D1)1 1h$rJ   