o
    &hj                     @   s  d 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m	Z	m
Z
 ddlmZmZmZmZmZmZmZ ddlmZmZ ddlmZmZ ddlmZ ejd	k r\dd
lmZmZmZmZ nejdk riddlmZmZ g dZ edZ!eZ"dZ#eZ$eZ%eZ&eZ'eZ(e&Z)eZ*eZ+ede+eZ,eZ-eZ.ede-e.e$eeZ/eZ0eZ1e1Z2G dd deZ3ee!dZ4dZ5dZ6dZ7dZ8dZ9dZ:dZ;dZ<edZ=G dd deZ>e2e!j?_@g e!j?_Ae'e!jB_@e'ge!jB_Ae'e!jC_@e'ge!jC_Ade!jD_@e(e*e)ge!jD_Ade!jE_@e(e+e)ge!jE_Ae(e!jF_@g e!jF_Ae(e!jG_@g e!jG_Ade!jH_@e(e*e)ge!jH_Ade!jI_@e(e+e)ge!jI_Ade!jJ_@g e!jJ_Ade!jK_@e(ge!jK_Ae+e!jL_@e"e2e1e%ee,ee3ge!jL_Ae*e!jM_@e"e-ege!jM_Ae-e!jN_@e"e0e%e/ee>ge!jN_Ade!jO_@e-e%ge!jO_Ade!jP_@e-e%ge!jP_Ade!jQ_@e-ge!jQ_Ade!jR_@e-e%ge!jR_AG dd dejSZTG dd dejUZVdd ZWG dd  d e
jXZYejdk rG d!d" d"eZZejd	k rd#d$ Z[neYZ[G d%d& d&Z\G d'd( d(Z]G d)d* d*Z^dS )+z,PEP 3156 event loop based on CoreFoundation.    N)
coroutineseventstasksunix_events)	CFUNCTYPEPOINTER	Structurec_doublec_intc_ulongc_void_p   )	ObjCClass
objc_const)load_libraryobjc_id)CFIndex      )AbstractEventLoopPolicyDefaultEventLoopPolicySafeChildWatcherset_event_loop_policy)r      )r   r   )EventLoopPolicyCocoaLifecycleRubiconEventLoopiOSLifecycleZCoreFoundationc                   @   @   e Zd ZdeeefdefdedefdedefdefgZdS )CFRunLoopTimerContextcopyDescriptioninforeleaseNretainversion__name__
__module____qualname__r   CFStringRefr   r   Z_fields_ r+   r+   m/Users/merlin/projects/employee-monitoring-system/venv/lib/python3.10/site-packages/rubicon/objc/eventloop.pyr    Q       r    kCFRunLoopCommonModes   r         	NSRunLoopc                   @   r   )CFSocketContextr!   r"   r#   Nr$   r%   r&   r+   r+   r+   r,   r3   q   r-   r3   c                       s0   e Zd Zdd Z fddZ fddZ  ZS )CFTimerHandlec                    s    fdd}t |S )Nc                    s      j j d S N)_loop_timersdiscard)Zcftimerextraargscallbackselfr+   r,   cf_timer_callback   s   z;CFTimerHandle._cf_timer_callback.<locals>.cf_timer_callback)CFRunLoopTimerCallBack)r=   r<   r;   r>   r+   r:   r,   _cf_timer_callback   s   z CFTimerHandle._cf_timer_callbackc             	      sj   t  t | | ||d | || _| jj|  t	t
| jddd| jd | _t| jj| jt d S )Nr   )super__init__libcfCFAbsoluteTimeGetCurrentr@   _timeoutr6   r7   addCFRunLoopTimerCreatekCFAllocatorDefault_when	_callback_timerCFRunLoopAddTimer
_cfrunloopr.   )r=   looptimeoutr<   r;   	__class__r+   r,   rB      s(   


zCFTimerHandle.__init__c                    s0   t    t| jj| jt | jj	|  dS )zCancel the Timer handle.N)
rA   cancelrC   CFRunLoopRemoveTimerr6   rM   rK   r.   r7   r8   r=   rP   r+   r,   rR      s
   
zCFTimerHandle.cancel)r'   r(   r)   r@   rB   rR   __classcell__r+   r+   rP   r,   r4      s    	r4   c                       sP   e Zd Zdd Z fddZdd Zdd Zd	d
 Zdd Z fddZ	  Z
S )CFSocketHandlec                 C   sx   | j | jjvrt| jj| jt d | _d S |tkr#| j	r#| j	\}}n|t
kr0| jr0| j\}}nd }|r:||  d S d S r5   )_fdr6   _socketsrC   CFRunLoopRemoveSourcerM   _srcr.   kCFSocketReadCallBack_readerkCFSocketWriteCallBack_writer)r=   ZcfSocketZcallbackTypeZignoredAddressZignoredDatacontextr<   r;   r+   r+   r,   _cf_socket_callback   s   z"CFSocketHandle._cf_socket_callbackc                   s   t  t| jd| | | jj|< d| _d| _|| _t	
t| jttB tB | jd| _t	| jttB  t	t| jd| _t	| jj| jt t	| jttB tB  dS )zRegister a file descriptor with the CFRunLoop, or modify its state so that
        it's listening for both notifications (read and write) rather than just one;
        used to implement add_reader and add_writer.Nr   )rA   rB   CFSocketCallbackr`   r6   rX   r\   r^   rW   rC   CFSocketCreateWithNativerH   r[   r]   kCFSocketConnectCallBackrJ   
_cf_socketCFSocketSetSocketFlags*kCFSocketAutomaticallyReenableReadCallBack+kCFSocketAutomaticallyReenableWriteCallBackCFSocketCreateRunLoopSourcerZ   CFRunLoopAddSourcerM   r.   CFSocketDisableCallBacks)r=   rN   fdrP   r+   r,   rB     s8   
	
zCFSocketHandle.__init__c                 C      t | jt ||f| _dS )z/Add a callback for read activity on the socket.N)rC   CFSocketEnableCallBacksrd   r[   r\   r=   r<   r;   r+   r+   r,   enable_read7     zCFSocketHandle.enable_readc                 C       t | jt d| _|   dS )z4Remove the callback for read activity on the socket.N)rC   rj   rd   r[   r\   rR   rT   r+   r+   r,   disable_read<     zCFSocketHandle.disable_readc                 C   rl   )z0Add a callback for write activity on the socket.N)rC   rm   rd   r]   r^   rn   r+   r+   r,   enable_writeB  rp   zCFSocketHandle.enable_writec                 C   rq   )z5Remove the callback for write activity on the socket.N)rC   rj   rd   r]   r^   rR   rT   r+   r+   r,   disable_writeG  rs   zCFSocketHandle.disable_writec                    s`   | j du r*| jdu r,| jr.t   | jj| j= t	| jj
| jt t| j dS dS dS dS )a1  (Potentially) cancel the socket handle.

        A socket handle can have both reader and writer components; a call to cancel a
        socket handle will only be successful if *both* the reader and writer component
        have been disabled. If either is still active, cancel() will be a no-op.
        N)r\   r^   rZ   rA   rR   r6   rX   rW   rC   rY   rM   r.   CFSocketInvalidaterd   rT   rP   r+   r,   rR   M  s   
zCFSocketHandle.cancel)r'   r(   r)   r`   rB   ro   rr   rt   ru   rR   rU   r+   r+   rP   r,   rV      s    'rV   c                    s"   d u rt   fdd}|S )Nc                     s   j  g| R   d S r5   )run)r;   r<   r_   r+   r,   rJ   b  s   z#context_callback.<locals>._callback)contextvarscopy_context)r_   r<   rJ   r+   rx   r,   context_callback^  s   r{   c                       s   e Zd Zd3 fdd	Z fddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zd3ddZd3d d!Zdd"d#d$ZeZdd"d%d&Zdd"d'd(Zd)d* Z fd+d,Z fd-d.Zd/d0 Zd1d2 Z  ZS )4CFEventLoopNc                    s>   || _ tt | _d| _t | _i | _i | _	t
   d S )NF)
_lifecyclerC   CFRetainCFRunLoopGetCurrentrM   _runningsetr7   _accept_futuresrX   rA   rB   r=   Z	lifecyclerP   r+   r,   rB   i  s   zCFEventLoop.__init__c                    s   t | j t   d S r5   )rC   	CFReleaserM   rA   __del__rT   rP   r+   r,   r   t  s   zCFEventLoop.__del__c                 G   H   z| j | }W n ty   t| |d}|| j |< Y nw ||| d S N)rN   rk   )rX   KeyErrorrV   ro   r=   rk   r<   r;   handler+   r+   r,   _add_readerx     zCFEventLoop._add_readerc                 G      | j ||g|R   dS )zAdd a reader callback.

        Method is a direct call through to _add_reader to reflect an internal
        implementation detail added in Python3.5.
        N)r   r=   rk   r<   r;   r+   r+   r,   
add_reader     zCFEventLoop.add_readerc                 C   *   z
| j |   W dS  ty   Y dS w NTF)rX   rr   r   r=   rk   r+   r+   r,   _remove_reader     zCFEventLoop._remove_readerc                 C      |  | dS )zRemove a reader callback.

        Method is a direct call through to _remove_reader to reflect an internal
        implementation detail added in Python3.5.
        N)r   r   r+   r+   r,   remove_reader     zCFEventLoop.remove_readerc                 G   r   r   )rX   r   rV   rt   r   r+   r+   r,   _add_writer  r   zCFEventLoop._add_writerc                 G   r   )zAdd a writer callback.

        Method is a direct call through to _add_writer to reflect an internal
        implementation detail added in Python3.5.
        N)r   r   r+   r+   r,   
add_writer  r   zCFEventLoop.add_writerc                 C   r   r   )rX   ru   r   r   r+   r+   r,   _remove_writer  r   zCFEventLoop._remove_writerc                 C   r   )zRemove a writer callback.

        Method is a direct call through to _remove_writer to reflect an internal
        implementation detail added in Python3.5.
        N)r   r   r+   r+   r,   remove_writer  r   zCFEventLoop.remove_writerc                 C   s(   t |s
t|rtd| ddS )z7Check whether the given callback is a coroutine or not.zcoroutines cannot be used with z()N)r   iscoroutineinspectiscoroutinefunction	TypeError)r=   r<   namer+   r+   r,   _check_not_coroutine  s   z CFEventLoop._check_not_coroutinec                 C   s   | j S )z*Returns True if the event loop is running.)r   rT   r+   r+   r,   
is_running  s   zCFEventLoop.is_runningc              	   C   s   |   }|sttdrt rtd|s"d| _ttdr"t|  z| j  W |s:d| _ttdr<td dS dS dS |sNd| _ttdrOtd w w w )zCInternal implementation of run using the CoreFoundation event loop._get_running_loopz7Cannot run the event loop while another loop is runningT_set_running_loopFN)	r   hasattrr   r   RuntimeErrorr   r   r}   start)r=   	recursiver+   r+   r,   rw     s8   



zCFEventLoop.runc              	      sh    fdd}t j| d}|| z jdi | W || n|| w | s0td| S )a\  Run until the Future is done.

        If the argument is a coroutine, it is wrapped in a Task.

        WARNING: It would be disastrous to call run_until_complete()
        with the same coroutine twice -- it would wrap it in two
        different Tasks and that can't be good.

        Return the Future's result, or raise its exception.
        c                    s       d S r5   )stop)frT   r+   r,   r        z,CFEventLoop.run_until_complete.<locals>.stop)rN   z+Event loop stopped before Future completed.Nr+   )r   ensure_futureadd_done_callbackrun_foreverremove_done_callbackdoner   result)r=   futurekwr   r+   rT   r,   run_until_complete  s   
zCFEventLoop.run_until_completec                 C   sP   | j s| |r	|nt| j |  rtdz|   W |   dS |   w )zRun until stop() is called.\Recursively calling run_forever is forbidden. To recursively run the event loop, call run().N)r}   _set_lifecycleCFLifecyclerM   r   r   rw   r   r   r+   r+   r,   r     s   
zCFEventLoop.run_foreverc                 C   sZ   | j s| |r	|nt| j |  rtdd| _ttdr$t	|  | 
| j j dS )a  A non-blocking version of :meth:`run_forever`.

        This may seem like nonsense; however, an iOS app is not expected to invoke a
        blocking "main event loop" method. As a result, we need to be able to *start*
        Python event loop handling, but then return control to the main app to start the
        actual event loop.

        The implementation is effectively all the parts of a call to
        :meth:`run_forever()`, but without any of the shutdown/cleanup logic.
        r   Tr   N)r}   r   r   rM   r   r   r   r   r   r   	call_soonr   r   r+   r+   r,   run_forever_cooperatively
  s   

z%CFEventLoop.run_forever_cooperatively)r_   c                G   s"   |  |d t| dt|||dS )aT  Arrange for a callback to be called as soon as possible.

        This operates as a FIFO queue: callbacks are called in the
        order in which they are registered.  Each callback will be
        called exactly once.

        Any positional arguments after the callback will be passed to
        the callback when it is called.
        r   r   rN   rO   r<   r;   r   r4   r{   )r=   r<   r_   r;   r+   r+   r,   r   +  s   
zCFEventLoop.call_soonc                G   s"   |  |d t| |t|||dS )a8  Arrange for a callback to be called at a given time.

        Return a Handle: an opaque object with a cancel() method that
        can be used to cancel the call.

        The delay can be an int or float, expressed in seconds.  It is
        always relative to the current time.

        Each callback will be called exactly once.  If two callbacks
        are scheduled for exactly the same time, it undefined which
        will be called first.

        Any positional arguments after the callback will be passed to
        the callback when it is called.
        
call_laterr   r   )r=   delayr<   r_   r;   r+   r+   r,   r   @  s   zCFEventLoop.call_laterc                G   s*   |  |d t| ||   t|||dS )z|Like call_later(), but uses an absolute time.

        Absolute time corresponds to the event loop's time() method.
        call_atr   )r   r4   timer{   )r=   whenr<   r_   r;   r+   r+   r,   r   Y  s   
zCFEventLoop.call_atc                 C   s   t  S )zReturn the time according to the event loop's clock.

        This is a float expressed in seconds since an epoch, but the epoch, precision,
        accuracy and drift are unspecified and may differ per event loop.
        )rC   rD   rT   r+   r+   r,   r   g  s   zCFEventLoop.timec                    s   t    | j  dS )zStop running the event loop.

        Every callback already scheduled will still run.  This simply informs
        run_forever to stop looping after a complete iteration.
        N)rA   r   r}   rT   rP   r+   r,   r   o  s   
zCFEventLoop.stopc                    sJ   | j r| j  }|  | j s| jr| j }|  | jst   dS )zClose the event loop.

        This clears the queues and shuts down the executor, but does not wait for the
        executor to finish.

        The event loop must not be running.
        N)r   poprR   r7   rA   close)r=   r   handlerrP   r+   r,   r   x  s   

zCFEventLoop.closec                 C   sB   | j dur	td|  rtd|| _ tjdk r|| j_ dS dS )z<Set the application lifecycle that is controlling this loop.NzLifecycle is already setz;You can't set a lifecycle on a loop that's already running.r   )r}   
ValueErrorr   r   sysversion_info_policyr   r+   r+   r,   r     s   

zCFEventLoop._set_lifecyclec                 C   s$   |j rdS | j|jg|jR   dS )a  Add a callback to be invoked ASAP.

        The inherited behavior uses a self-pipe to wake up the event loop in a thread-
        safe fashion, which causes the logic in run_once() to empty the list of handlers
        that are awaiting invocation.

        CFEventLoop doesn't use run_once(), so adding handlers to self._ready results in
        handlers that aren't invoked. Instead, we create a 0-interval timer to invoke
        the callback as soon as possible.
        N)
_cancelledr   rJ   _args)r=   r   r+   r+   r,   _add_callback  s   zCFEventLoop._add_callbackr5   )r'   r(   r)   rB   r   r   r   r   r   r   r   r   r   r   r   rw   r   r   r   r   call_soon_threadsafer   r   r   r   r   r   r   rU   r+   r+   rP   r,   r|   h  s4    		

!	r|   c                   @   sV   e Zd ZdZdd Zdd Zdd Zdd	 Zej	d
k r)dd Z
dd Zdd ZdS dS )r   a  Rubicon event loop policy.

        In this policy, each thread has its own event loop. However, we only
        automatically create an event loop by default for the main thread; other
        threads by default have no event loop.

        **DEPRECATED** - Python 3.14 deprecated the concept of manually creating
        EventLoopPolicies. Create and use a ``RubiconEventLoop`` instance instead of
        installing an event loop policy and calling ``asyncio.new_event_loop()``.
        c                 C   s`   t jdtdd d | _d | _tjdk rt | _	d | _
t | _| j| j_| jj| _| jj| _d S )NzCustom EventLoopPolicy instances have been deprecated by Python 3.14. Create and use a `RubiconEventLoop` instance directly instead of installing an event loop policy and calling `asyncio.new_event_loop()`.r/   )
stacklevelr   )warningswarnDeprecationWarningr}   _default_loopr   r   	threadingLockZ_watcher_lock_watcherr   r   new_event_loopget_event_loopset_event_looprT   r+   r+   r,   rB     s   	



zEventLoopPolicy.__init__c                 C   s4   | j st t kr|  }nt| j}| |_|S )z&Create a new event loop and return it.)r   r   current_threadmain_threadget_default_loopr|   r}   r   r=   rN   r+   r+   r,   r     s   

zEventLoopPolicy.new_event_loopc                 C   s   | j s|  | _ | j S )zGet the default event loop.)r   _new_default_looprT   r+   r+   r,   r     s   
z EventLoopPolicy.get_default_loopc                 C   s   t | j}| |_|S r5   )r|   r}   r   r   r+   r+   r,   r     s   
z!EventLoopPolicy._new_default_loopr   c                 C   s|   t j1 | jd u r$t | _t t kr,| j| j W d    d S W d    d S W d    d S 1 s7w   Y  d S r5   )	r   _lockr   r   r   r   r   attach_loopr   rT   r+   r+   r,   _init_watcher  s   
"zEventLoopPolicy._init_watcherc                 C   s   | j du r	|   | j S )a  Get the watcher for child processes.

                If not yet set, a :class:`~asyncio.SafeChildWatcher` object is
                automatically created.

                .. note::
                    Child watcher support was removed in Python 3.14
                N)r   r   rT   r+   r+   r,   get_child_watcher  s   
	z!EventLoopPolicy.get_child_watcherc                 C   s   | j dur
| j   || _ dS )zSet the watcher for child processes.

                .. note::
                    Child watcher support was removed in Python 3.14
                N)r   r   )r=   watcherr+   r+   r,   set_child_watcher  s   


z!EventLoopPolicy.set_child_watcherN)r'   r(   r)   __doc__rB   r   r   r   r   r   r   r   r   r+   r+   r+   r,   r     s    
r   c                  C   sN   t   t jddtd t } W d   n1 sw   Y  t|  |  S )z*Create a new Rubicon CFEventLoop instance.ignorezE^Custom EventLoopPolicy instances have been deprecated by Python 3.14)messagecategoryN)r   catch_warningsfilterwarningsr   r   r   r   )policyr+   r+   r,   r     s   

r   c                   @   (   e Zd ZdZdd Zdd Zdd ZdS )	r   z0A lifecycle manager for raw CoreFoundation apps.c                 C   
   || _ d S r5   )rM   )r=   Z	cfrunloopr+   r+   r,   rB        
zCFLifecycle.__init__c                 C   s   t   d S r5   )rC   CFRunLoopRunrT   r+   r+   r,   r     r   zCFLifecycle.startc                 C   s   t | j d S r5   )rC   CFRunLoopStoprM   rT   r+   r+   r,   r   "     zCFLifecycle.stopNr'   r(   r)   r   rB   r   r   r+   r+   r+   r,   r     
    r   c                   @   r   )	r   z8A life cycle manager for Cocoa (``NSApplication``) apps.c                 C   r   r5   )_application)r=   Zapplicationr+   r+   r,   rB   )  r   zCocoaLifecycle.__init__c                 C   s   | j   d S r5   )r   rw   rT   r+   r+   r,   r   ,     zCocoaLifecycle.startc                 C   s   | j d  d S r5   )r   	terminaterT   r+   r+   r,   r   /  r   zCocoaLifecycle.stopNr   r+   r+   r+   r,   r   &  r   r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   z6A life cycle manager for iOS (``UIApplication``) apps.c                 C   s   t j  d S r5   )r2   ZcurrentRunLooprw   rT   r+   r+   r,   r   6  r   ziOSLifecycle.startc                 C   s   t t   d S r5   )rC   r   CFRunLoopGetMainrT   r+   r+   r,   r   9  s   ziOSLifecycle.stopN)r'   r(   r)   r   r   r   r+   r+   r+   r,   r   3  s    r   )_r   ry   r   r   r   r   asyncior   r   r   r   ctypesr   r   r   r	   r
   r   r   apir   r   Zruntimer   r   typesr   r   r   r   r   r   __all__rC   ZCFAllocatorRefrH   Z	CFDataRefZCFOptionFlagsr*   Z	CFTypeRefZCFRunLoopRefZCFRunLoopModeZCFRunLoopSourceRefZCFRunLoopTimerRefr?   ZCFSocketRefZCFSocketCallbackTypera   ZCFSocketNativeHandleZCFTimeIntervalZCFAbsoluteTimer    r.   ZkCFSocketNoCallBackr[   ZkCFSocketAcceptCallBackZkCFSocketDataCallBackrc   r]   rf   rg   r2   r3   rD   restypeargtypesr   r~   ri   rL   r   r   rY   rS   r   r   rG   rh   rb   rj   rm   rv   re   TimerHandler4   HandlerV   r{   SelectorEventLoopr|   r   r   r   r   r   r+   r+   r+   r,   <module>   s    $

	





/g
  @[
