o
    .j                     @  sf   d dl mZ d dlZd dlmZmZmZ d dlmZ dZ	G dd dej
ZG dd	 d	ZdddZdS )    )annotationsN)CancelledErroreventstasks)TracebackType)Timeouttimeoutc                   @  s    e Zd ZdZdZdZdZdZdS )_StatecreatedactiveexpiringexpiredfinishedN)__name__
__module____qualname__CREATEDENTEREDEXPIRINGEXPIREDEXITED r   r   X/home/thesage/.local/lib/python3.10/site-packages/urllib3_future/contrib/ssa/_timeout.pyr	      s    r	   c                   @  s`   e Zd ZdZd ddZd!dd	Zd d
dZd"ddZd#ddZd$ddZ	d%ddZ
d&ddZdS )'r   zAsynchronous context manager for cancelling overdue coroutines.

    Use `timeout()` or `timeout_at()` rather than instantiating this class directly.
    whenfloat | NonereturnNonec                 C  s   t j| _d| _d| _|| _dS )zSchedule a timeout that will trigger at a given loop time.

        - If `when` is `None`, the timeout will never trigger.
        - If `when < loop.time()`, the timeout will trigger on the next
          iteration of the event loop.
        N)r	   r   _state_timeout_handler_task_when)selfr   r   r   r   __init__   s   
zTimeout.__init__c                 C  s   | j S )zReturn the current deadline.)r    r!   r   r   r   r   (   s   zTimeout.whenc                 C  s   | j tjur| j tju rtdtd| j j d|| _| jdur'| j  |du r0d| _dS t	
 }|| krC|| j| _dS ||| j| _dS )zReschedule the timeout.zTimeout has not been enteredzCannot change state of z TimeoutN)r   r	   r   r   RuntimeErrorvaluer    r   cancelr   get_running_looptime	call_soon_on_timeoutcall_at)r!   r   loopr   r   r   
reschedule,   s   


zTimeout.rescheduleboolc                 C  s   | j tjtjfv S )z$Is timeout expired during execution?)r   r	   r   r   r#   r   r   r   r   C   s   zTimeout.expiredstrc                 C  s\   dg}| j tju r| jd urt| jdnd }|d|  d|}d| j j d| dS )N    zwhen= z
<Timeout []>)r   r	   r   r    roundappendjoinr%   )r!   infor   info_strr   r   r   __repr__G   s   
zTimeout.__repr__	'Timeout'c                   sL   | j tjurtdt }|d u rtdtj| _ || _| | j	 | S )Nz Timeout has already been enteredz$Timeout should be used inside a task)
r   r	   r   r$   r   current_taskr   r   r-   r    )r!   taskr   r   r   
__aenter__O   s   zTimeout.__aenter__exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | Nonec                   s~   | j tjtjfv sJ | jd usJ | jd ur | j  d | _| j tju r3tj| _ |tu r1t	|d S | j tju r=tj
| _ d S N)r   r	   r   r   r   r   r&   r   r   TimeoutErrorr   )r!   r?   rA   rC   r   r   r   	__aexit__Z   s   

zTimeout.__aexit__c                 C  s:   | j tju sJ | jd usJ | j  tj| _ d | _d S rE   )r   r	   r   r   r&   r   r   r#   r   r   r   r*   s   s
   

zTimeout._on_timeoutN)r   r   r   r   )r   r   )r   r.   )r   r/   )r   r;   )r?   r@   rA   rB   rC   rD   r   r   )r   r   )r   r   r   __doc__r"   r   r-   r   r:   r>   rG   r*   r   r   r   r   r      s    






r   delayr   r   c                 C  s&   t  }t| dur| |  S dS )a	  Timeout async context manager.

    Useful in cases when you want to apply timeout logic around block
    of code or in cases when asyncio.wait_for is not suitable. For example:

    >>> async with asyncio.timeout(10):  # 10 seconds timeout
    ...     await long_running_task()


    delay - value in seconds or None to disable timeout logic

    long_running_task() is interrupted by raising asyncio.CancelledError,
    the top-most affected timeout() context manager converts CancelledError
    into TimeoutError.
    N)r   r'   r   r(   )rI   r,   r   r   r   r   }   s   r   )rI   r   r   r   )
__future__r   enumasyncior   r   r   typesr   __all__Enumr	   r   r   r   r   r   r   <module>   s    h