Ë
    U¼SfÕ%  ã                   óˆ   — d Z ddlmZ ddlZddlZddlmZ dZdZdZ	dZ
 G d„ d	e«      Z G d
„ de«      Zd„ Z G d„ de«      Zy)a£  Decorators for applying timeout arguments to functions.

These decorators are used to wrap API methods to apply either a
Deadline-dependent (recommended), constant (DEPRECATED) or exponential
(DEPRECATED) timeout argument.

For example, imagine an API method that can take a while to return results,
such as one that might block until a resource is ready:

.. code-block:: python

    def is_thing_ready(timeout=None):
        response = requests.get('https://example.com/is_thing_ready')
        response.raise_for_status()
        return response.json()

This module allows a function like this to be wrapped so that timeouts are
automatically determined, for example:

.. code-block:: python

    timeout_ = timeout.ExponentialTimeout()
    is_thing_ready_with_timeout = timeout_(is_thing_ready)

    for n in range(10):
        try:
            is_thing_ready_with_timeout({'example': 'data'})
        except:
            pass

In this example the first call to ``is_thing_ready`` will have a relatively
small timeout (like 1 second). If the resource is available and the request
completes quickly, the loop exits. But, if the resource isn't yet available
and the request times out, it'll be retried - this time with a larger timeout.

In the broader context these decorators are typically combined with
:mod:`google.api_core.retry` to implement API methods with a signature that
matches ``api_method(request, timeout=None, retry=None)``.
é    )Úunicode_literalsN)Údatetime_helpersg      @g      >@g       @c                   ó<   — e Zd ZdZdej
                  fd„Zd„ Zd„ Zy)ÚTimeToDeadlineTimeouta&  A decorator that decreases timeout set for an RPC based on how much time
    has left till its deadline. The deadline is calculated as
    ``now + initial_timeout`` when this decorator is first called for an rpc.

    In other words this decorator implements deadline semantics in terms of a
    sequence of decreasing timeouts t0 > t1 > t2 ... tn >= 0.

    Args:
        timeout (Optional[float]): the timeout (in seconds) to applied to the
            wrapped function. If `None`, the target function is expected to
            never timeout.
    Nc                 ó    — || _         || _        y ©N)Ú_timeoutÚ_clock)ÚselfÚtimeoutÚclocks      úVC:\Users\user\Documents\project_loop\venv\Lib\site-packages\google/api_core/timeout.pyÚ__init__zTimeToDeadlineTimeout.__init__T   s   € ØˆŒØˆ�ó    c                 ó†   ‡ ‡‡— ‰ j                  «       j                  «       Št        j                  ‰«      ˆˆˆ fd„«       }|S )á  Apply the timeout decorator.

        Args:
            func (Callable): The function to apply the timeout argument to.
                This function must accept a timeout keyword argument.

        Returns:
            Callable: The wrapped function.
        c                  óÂ   •— ‰j                   }|�I‰j                  «       j                  «       }|‰z
  dk  r‰}|‰z
  }t        d‰j                   |z
  «      |d<    ‰| i |¤ŽS )ú#Wrapped function that adds timeout.gü©ñÒMbP?r   r   )r	   r
   Ú	timestampÚmax)ÚargsÚkwargsÚremaining_timeoutÚnow_timestampÚtime_since_first_attemptÚfirst_attempt_timestampÚfuncr   s        €€€r   Úfunc_with_timeoutz9TimeToDeadlineTimeout.__call__.<locals>.func_with_timeoute   su   ø€ ð !%§¡ÐØ Ð,à $§¡£× 7Ñ 7Ó 9�ð
 !Ð#:Ñ:¸UÒBØ$;�Mà+8Ð;RÑ+RÐ(ä$'¨¨4¯=©=Ð;SÑ+SÓ$T��yÑ!á˜Ð( Ñ(Ð(r   )r
   r   Ú	functoolsÚwraps)r   r   r   r   s   `` @r   Ú__call__zTimeToDeadlineTimeout.__call__X   s=   ú€ ð #'§+¡+£-×"9Ñ"9Ó";Ðä	�‰˜Ó	õ	)ó 
ð	)ð( !Ð r   c                 ó8   — dj                  | j                  «      S )Nz&<TimeToDeadlineTimeout timeout={:.1f}>©Úformatr	   ©r   s    r   Ú__str__zTimeToDeadlineTimeout.__str__|   s   € Ø7×>Ñ>¸t¿}¹}ÓMÐMr   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úutcnowr   r!   r&   © r   r   r   r   F   s&   „ ñð  $Ð+;×+BÑ+Bó ò"!óHNr   r   c                   ó$   — e Zd ZdZdd„Zd„ Zd„ Zy)ÚConstantTimeoutaˆ  A decorator that adds a constant timeout argument.

    DEPRECATED: use ``TimeToDeadlineTimeout`` instead.

    This is effectively equivalent to
    ``functools.partial(func, timeout=timeout)``.

    Args:
        timeout (Optional[float]): the timeout (in seconds) to applied to the
            wrapped function. If `None`, the target function is expected to
            never timeout.
    Nc                 ó   — || _         y r   ©r	   )r   r   s     r   r   zConstantTimeout.__init__Ž   s	   € Øˆ�r   c                 óF   ‡ ‡— t        j                  ‰«      ˆˆ fd„«       }|S )r   c                  ó2   •— ‰j                   |d<    ‰| i |¤ŽS ©r   r   r0   )r   r   r   r   s     €€r   r   z3ConstantTimeout.__call__.<locals>.func_with_timeoutœ   s#   ø€ ð !%§¡ˆF�9ÑÙ˜Ð( Ñ(Ð(r   )r   r    )r   r   r   s   `` r   r!   zConstantTimeout.__call__‘   s(   ù€ ô 
�‰˜Ó	ô	)ó 
ð	)ð
 !Ð r   c                 ó8   — dj                  | j                  «      S )Nz <ConstantTimeout timeout={:.1f}>r#   r%   s    r   r&   zConstantTimeout.__str__¤   s   € Ø1×8Ñ8¸¿¹ÓGÐGr   r   )r'   r(   r)   r*   r   r!   r&   r,   r   r   r.   r.   €   s   „ ñó ò!ó&Hr   r.   c              #   ó  K  — |�,t        j                  «       t        j                  |¬«      z   }nt        j                  j                  }| }	 t        j                  «       }t        ||t        ||z
  j                  «      «      –— ||z  }Œ?­w)aV  A generator that yields exponential timeout values.

    Args:
        initial (float): The initial timeout.
        maximum (float): The maximum timeout.
        multiplier (float): The multiplier applied to the timeout.
        deadline (float): The overall deadline across all invocations.

    Yields:
        float: A timeout value.
    )Úseconds)r   r+   ÚdatetimeÚ	timedeltar   ÚminÚfloatr6   )ÚinitialÚmaximumÚ
multiplierÚdeadlineÚdeadline_datetimer   Únows          r   Ú_exponential_timeout_generatorrA   ¨   s”   è ø€ ð ÐÜ,×3Ñ3Ó5¼×8JÑ8JØô9
ñ 
Ñô %×-Ñ-×1Ñ1Ðà€GØ
Ü×%Ñ%Ó'ˆÜààäÐ$ sÑ*×3Ñ3Ó4ó
ò 	
ð ˜JÑ&ˆð ùs   ‚BBc                   ó2   — e Zd ZdZeeeefd„Zd„ Z	d„ Z
d„ Zy)ÚExponentialTimeoutaT  A decorator that adds an exponentially increasing timeout argument.

    DEPRECATED: the concept of incrementing timeout exponentially has been
    deprecated. Use ``TimeToDeadlineTimeout`` instead.

    This is useful if a function is called multiple times. Each time the
    function is called this decorator will calculate a new timeout parameter
    based on the the number of times the function has been called.

    For example

    .. code-block:: python

    Args:
        initial (float): The initial timeout to pass.
        maximum (float): The maximum timeout for any one call.
        multiplier (float): The multiplier applied to the timeout for each
            invocation.
        deadline (Optional[float]): The overall deadline across all
            invocations. This is used to prevent a very large calculated
            timeout from pushing the overall execution time over the deadline.
            This is especially useful in conjunction with
            :mod:`google.api_core.retry`. If ``None``, the timeouts will not
            be adjusted to accommodate an overall deadline.
    c                 ó<   — || _         || _        || _        || _        y r   )Ú_initialÚ_maximumÚ_multiplierÚ	_deadline)r   r;   r<   r=   r>   s        r   r   zExponentialTimeout.__init__ä   s!   € ð  ˆŒØˆŒØ%ˆÔØ!ˆ�r   c                 ó\   — t        | j                  | j                  | j                  |¬«      S )zóReturn a copy of this timeout with the given deadline.

        Args:
            deadline (float): The overall deadline across all invocations.

        Returns:
            ExponentialTimeout: A new instance with the given deadline.
        )r;   r<   r=   r>   )rC   rE   rF   rG   )r   r>   s     r   Úwith_deadlinez ExponentialTimeout.with_deadlineð   s+   € ô "Ø—M‘MØ—M‘MØ×'Ñ'Øô	
ð 	
r   c                 ó²   ‡‡— t        | j                  | j                  | j                  | j                  «      Št        j                  ‰«      ˆˆfd„«       }|S )r   c                  ó0   •— t        ‰«      |d<    ‰| i |¤ŽS r3   )Únext)r   r   r   Útimeoutss     €€r   r   z6ExponentialTimeout.__call__.<locals>.func_with_timeout  s#   ø€ ô !% X£ˆF�9ÑÙ˜Ð( Ñ(Ð(r   )rA   rE   rF   rG   rH   r   r    )r   r   r   rN   s    ` @r   r!   zExponentialTimeout.__call__   sO   ù€ ô 2Ø�M‰M˜4Ÿ=™=¨$×*:Ñ*:¸D¿N¹Nó
ˆô 
�‰˜Ó	ô	)ó 
ð	)ð
 !Ð r   c                 óz   — dj                  | j                  | j                  | j                  | j                  «      S )NzW<ExponentialTimeout initial={:.1f}, maximum={:.1f}, multiplier={:.1f}, deadline={:.1f}>)r$   rE   rF   rG   rH   r%   s    r   r&   zExponentialTimeout.__str__  s1   € ð2ß28±&Ø—‘˜tŸ}™}¨d×.>Ñ.>ÀÇÁó3ð	
r   N)r'   r(   r)   r*   Ú_DEFAULT_INITIAL_TIMEOUTÚ_DEFAULT_MAXIMUM_TIMEOUTÚ_DEFAULT_TIMEOUT_MULTIPLIERÚ_DEFAULT_DEADLINEr   rJ   r!   r&   r,   r   r   rC   rC   É   s)   „ ñð8 )Ø(Ø.Ø"ó
"ò
ò !ó,
r   rC   )r*   Ú
__future__r   r7   r   Úgoogle.api_corer   rP   rQ   rR   rS   Úobjectr   r.   rA   rC   r,   r   r   ú<module>rW      se   ðñ&õP (ã Û å ,àÐ ØÐ Ø!Ð ð Ð ô7N˜Fô 7Nôt%H�fô %HòP'ôBS
˜õ S
r   