o
    wvXjp  ã                   @   s   d Z ddlmZmZ ddlZddl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 dd
lmZ ddlmZ zddlmZ W n eyW   dZY nw dZeeƒZdZe d¡jed�Z e!edƒZ"eddƒZ#eefdd„Z$e
eG dd„ de%ƒƒƒZ&G dd„ de%ƒZ'dS )z"Timer scheduling Python callbacks.é    )Úabsolute_importÚunicode_literalsN)Ú
namedtuple)Údatetime)Útotal_ordering)Úproxy)Úwraps)Ú	monotonicÚpython_2_unicode_compatible)Ú
get_logger)Útime)Úutc)ÚEntryÚTimerÚto_timestampé   ©ÚtzinfoÚpypy_version_infoÚ	scheduled)ÚetaÚpriorityÚentryc                 C   sF   t | tƒr!| jdu r| j|d�} tƒ |ƒ  }t| t  ¡ | dƒS | S )z^Convert datetime to timestamp.

    If d' is already a timestamp, then that will be used.
    Nr   r   )Ú
isinstancer   r   ÚreplaceÚ_timeÚmaxÚEPOCHÚtotal_seconds)ÚdÚdefault_timezoner   Údiff© r"   úU/var/www/html/myproject/venv/lib/python3.10/site-packages/kombu/asynchronous/timer.pyr   #   s   

r   c                   @   s\   e Zd ZdZes
dZddd„Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
edd„ ƒZejdd„ ƒZdS )r   zSchedule Entry.)ÚfunÚargsÚkwargsÚtrefÚcanceledÚ	_last_runÚ__weakref__Nc                 C   s4   || _ |pg | _|pi | _t| ƒ| _d | _d| _d S )NF)r$   r%   r&   Úweakrefproxyr'   r)   r(   )Úselfr$   r%   r&   r"   r"   r#   Ú__init__;   s   



zEntry.__init__c                 C   s   | j | ji | j¤ŽS ©N)r$   r%   r&   ©r,   r"   r"   r#   Ú__call__C   ó   zEntry.__call__c                 C   s$   zd| j _W d S  ty   Y d S w ©NT)r'   r(   ÚReferenceErrorr/   r"   r"   r#   ÚcancelF   s
   ÿzEntry.cancelc                 C   s   d  | jj| j| j¡S )Nz!<TimerEntry: {0}(*{1!r}, **{2!r}))Úformatr$   Ú__name__r%   r&   r/   r"   r"   r#   Ú__repr__L   s   ÿzEntry.__repr__c                 C   s   t | ƒt |ƒk S r.   )Úid)r,   Úotherr"   r"   r#   Ú__lt__Q   s   zEntry.__lt__c                 C   s   | j S r.   ©r(   r/   r"   r"   r#   Ú	cancelledT   s   zEntry.cancelledc                 C   s
   || _ d S r.   r;   )r,   Úvaluer"   r"   r#   r<   X   s   
©NN)r6   Ú
__module__Ú__qualname__Ú__doc__ÚIS_PYPYÚ	__slots__r-   r0   r4   r7   r:   Úpropertyr<   Úsetterr"   r"   r"   r#   r   0   s    

r   c                   @   sè   e Zd ZdZeZdZd+dd„Zdd„ Zdd„ Zd,dd„Z	d,dd„Z
d,dd„Zdd
efdd„Zd
efdd„Zejfdd„Zdd„ Zdd„ Zdd„ Zeeejejfdd„Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zeejfd'd(„ƒZed)d*„ ƒZdS )-r   zAsync timer implementation.Nc                 K   s$   t |ptƒ| _|p| j| _g | _d S r.   )ÚfloatÚDEFAULT_MAX_INTERVALÚmax_intervalÚon_errorÚ_queue)r,   rH   rI   r&   r"   r"   r#   r-   d   s   
zTimer.__init__c                 C   ó   | S r.   r"   r/   r"   r"   r#   Ú	__enter__i   ó   zTimer.__enter__c                 G   s   |   ¡  d S r.   )Ústop©r,   Úexc_infor"   r"   r#   Ú__exit__l   ó   zTimer.__exit__r"   r   c                 C   s$   |si n|}|   |  |||¡||¡S r.   )Úenter_atr   )r,   r   r$   r%   r&   r   r"   r"   r#   Úcall_ato   ó   zTimer.call_atc                 C   s$   |si n|}|   ||  |||¡|¡S r.   )Úenter_afterr   )r,   Úsecsr$   r%   r&   r   r"   r"   r#   Ú
call_afters   rU   zTimer.call_afterc                    sP   |si n|}ˆ  ˆ ||¡‰tˆ ƒ‡ ‡‡‡‡fdd„ƒ}|ˆ_d ˆ_ˆ ˆˆˆ¡S )Nc                     sÞ   ˆj tƒ }}|r|ˆj  nˆ}zE|r:|ˆkr:|ˆ_ ˆ | i |¤ŽW ˆjs9ˆj }|r/ˆ||  nˆ}ˆ |ˆˆ¡ S S W ˆjsTˆj }|rIˆ||  nˆ}ˆ |ˆˆ¡ d S d S ˆjsnˆj }|rdˆ||  nˆ}ˆ |ˆˆ¡ w w r.   )r)   r	   r(   rV   )r%   r&   ÚlastÚnowÚlsinceÚnext©r$   r   rW   r,   r'   r"   r#   Ú_reschedules{   s*   ý€ 
ýýz+Timer.call_repeatedly.<locals>._reschedules)r   r   r$   r)   rV   )r,   rW   r$   r%   r&   r   r^   r"   r]   r#   Úcall_repeatedlyw   s   zTimer.call_repeatedlyc              
   C   sf   |du r|ƒ }t |tƒr,zt|ƒ}W n ty+ } z|  |¡s ‚ W Y d}~dS d}~ww |  |||¡S )zÜEnter function into the scheduler.

        Arguments:
            entry (~kombu.asynchronous.timer.Entry): Item to enter.
            eta (datetime.datetime): Scheduled time.
            priority (int): Unused.
        N)r   r   r   Ú	ExceptionÚhandle_errorÚ_enter)r,   r   r   r   r   Úexcr"   r"   r#   rS   �   s   

€ýzTimer.enter_atc                 C   s   |   ||ƒ | |¡S r.   )rS   )r,   rW   r   r   r   r"   r"   r#   rV       r1   zTimer.enter_afterc                 C   s   || j t|||ƒƒ |S r.   )rJ   r   )r,   r   r   r   Úpushr"   r"   r#   rb   £   s   zTimer._enterc              
   C   s\   z|ƒ  W d S  t y- } z|  |¡s"tjd|dd� W Y d }~d S W Y d }~d S d }~ww )NzError in timer: %rT)rP   )r`   ra   ÚloggerÚerror)r,   r   rc   r"   r"   r#   Úapply_entry§   s   
ÿ€ÿzTimer.apply_entryc                 C   s   | j r
|   |¡ dS d S r2   )rI   rO   r"   r"   r#   ra   ®   s   
þzTimer.handle_errorc                 C   s   d S r.   r"   r/   r"   r"   r#   rN   ³   rM   z
Timer.stopc                 c   sˆ   � | j }| j}	 |r@|d }|ƒ |d }}	||	k r%||	| |ƒdfV  n||ƒ}
|
|u r:|d }|js9d|fV  q|||
ƒ ndV  q)zâIterate over schedule.

        This iterator yields a tuple of ``(wait_seconds, entry)``,
        where if entry is :const:`None` the caller should wait
        for ``wait_seconds`` until it polls the schedule again.
        é   r   Nr   r>   )rH   rJ   r(   )r,   ÚminÚnowfunÚpoprd   rH   ÚqueueÚeventArZ   r   ÚeventBr   r"   r"   r#   Ú__iter__¶   s$   €
îzTimer.__iter__c                 C   s   g | j d d …< d S r.   )rJ   r/   r"   r"   r#   ÚclearÕ   s   zTimer.clearc                 C   s   |  ¡  d S r.   )r4   )r,   r'   r"   r"   r#   r4   Ø   rR   zTimer.cancelc                 C   s
   t | jƒS r.   )ÚlenrJ   r/   r"   r"   r#   Ú__len__Û   s   
zTimer.__len__c                 C   s   dS r2   r"   r/   r"   r"   r#   Ú__nonzero__Þ   rM   zTimer.__nonzero__c                    s&   t | jƒ}‡ fdd„|gt|ƒ D ƒS )z%Snapshot of underlying datastructure.c                    s   g | ]}ˆ |ƒ‘qS r"   r"   )Ú.0Úv©Ú_popr"   r#   Ú
<listcomp>å   s    zTimer.queue.<locals>.<listcomp>)ÚlistrJ   rq   )r,   rw   Úeventsr"   rv   r#   rl   á   s   
zTimer.queuec                 C   rK   r.   r"   r/   r"   r"   r#   Úscheduleç   s   zTimer.scheduler>   )r"   Nr   )r6   r?   r@   rA   r   rI   r-   rL   rQ   rT   rX   r_   r	   rS   rV   ÚheapqÚheappushrb   rg   ra   rN   ri   Úheappopro   rp   r4   rr   rs   rD   rl   r{   r"   r"   r"   r#   r   ]   s6    




ÿr   )(rA   Ú
__future__r   r   r|   ÚsysÚcollectionsr   r   Ú	functoolsr   Úweakrefr   r+   Ú
vine.utilsr   Ú
kombu.fiver	   r
   Ú	kombu.logr   r   r   Úpytzr   ÚImportErrorÚ__all__r6   re   rG   Úutcfromtimestampr   r   ÚhasattrrB   r   r   Úobjectr   r   r"   r"   r"   r#   Ú<module>   s8   ÿ

+