o
    Ín~bª  ã                   @   s<   d dl Z d dlZe jdkrejZnejZG dd„ deƒZdS )é    N)é   r   c                   @   s<   e Zd Zdejfdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Z	dS )ÚConstantRateLimitFc                 C   s&   | j  tƒ ¡ t|ƒ| | _|| _dS )aC  Context manager for enforcing constant rate on code inside the block .

        `rate = seconds / times`

        :param times: times to execute per...
        :type  times: :class:`int`
        :param seconds: ...seconds
        :type  seconds: :class:`int`
        :param exit_wait: whether to automatically call :meth:`wait` before exiting the block
        :type  exit_wait: :class:`bool`
        :param sleep_func: Sleep function in seconds. Default: :func:`time.sleep`
        :type  sleep_func: :class:`bool`

        Example:

        .. code:: python

            with RateLimiter(1, 5) as r:
                # code taking 1s
                r.wait()  # blocks for 4s
                # code taking 7s
                r.wait()  # doesn't block
                # code taking 1s
                r.wait()  # blocks for 4s
        N)Ú__dict__ÚupdateÚlocalsÚfloatÚrateÚ
sleep_func)ÚselfÚtimesÚsecondsÚ	exit_waitr	   © r   ú?/usr/local/lib/python3.10/dist-packages/steam/utils/throttle.pyÚ__init__   s   
zConstantRateLimit.__init__c                 C   s   |   ¡  | S ©N)Ú_update_ref©r
   r   r   r   Ú	__enter__)   s   zConstantRateLimit.__enter__c                 C   s   | j r	|  ¡  d S d S r   )r   Úwait)r
   ÚetypeZevalueÚ	tracebackr   r   r   Ú__exit__-   s   ÿzConstantRateLimit.__exit__c                 C   s   t ƒ | j | _d S r   )Ú
_monotonicr   Ú_refr   r   r   r   r   1   s   zConstantRateLimit._update_refc                 C   s6   t ƒ }|| jk rtd| j| ƒ}|  |¡ |  ¡  dS )zBlocks until the rate is metr   N)r   r   Úmaxr	   r   )r
   ÚnowÚdelayr   r   r   r   4   s
   

zConstantRateLimit.waitN)
Ú__name__Ú
__module__Ú__qualname__ÚtimeÚsleepr   r   r   r   r   r   r   r   r   r   
   s    r   )Úsysr!   Úversion_infoÚ	monotonicr   Úobjectr   r   r   r   r   Ú<module>   s    
