
    ynh                         d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	 ddl
mZmZmZmZ  e	e      Zddddded	ef   d
eeegdf      dededeej&                     deeg ef      deddfdZy)a  This module contains a network retry loop implementation.
Its specifically tailored to handling the Telegram API and its errors.

.. versionadded:: 21.11

Hint:
    It was originally part of the `Updater` class, but as part of #4657 it was extracted into its
    own module to be used by other parts of the library.

Warning:
    Contents of this module are intended to be used internally by the library and *not* by the
    user. Changes to this module are not considered breaking changes and may not be documented in
    the changelog.
    N)	Coroutine)CallableOptional)
get_logger)InvalidToken
RetryAfterTelegramErrorTimedOut)	on_err_cb
stop_event
is_running	action_cb.r   descriptionintervalr   r   max_retriesreturnc                 "   K   d| d|xs d }dt         f fd}t        j                  d       |}	d}
 |       rG	  |        d{   s	 |
d	z  }
y	 |}	|
d	z  }
|	rt        j                  |	       d{     |       rFyy7 <# t        $ r3}d
}t        j	                  d||       ||j
                  z   }	Y d}~gd}~wt        $ r#}t        j                  d|       d}	Y d}~d}~wt        $ r t        j                  d        t        $ rg}|r ||       |dk  s|
|k  rt        j                  d|
|       nt        j                  d|
|        |	dk(  rd	nt        dd|	z        }	Y d}~d}~ww xY w# |
d	z  }
w xY w7 w)aD  Perform a loop calling `action_cb`, retrying after network errors.

    Stop condition for loop:
        * `is_running()` evaluates :obj:`False` or
        * return value of `action_cb` evaluates :obj:`False`
        * or `stop_event` is set.
        * or `max_retries` is reached.

    Args:
        action_cb (:term:`coroutine function`): Network oriented callback function to call.
        on_err_cb (:obj:`callable`): Optional. Callback to call when TelegramError is caught.
            Receives the exception object as a parameter.

            Hint:
                Only required if you want to handle the error in a special way. Logging about
                the error is already handled by the loop.

            Important:
                Must not raise exceptions! If it does, the loop will be aborted.
        description (:obj:`str`): Description text to use for logs and exception raised.
        interval (:obj:`float` | :obj:`int`): Interval to sleep between each call to
            `action_cb`.
        stop_event (:class:`asyncio.Event` | :obj:`None`): Event to wait on for stopping the
            loop. Setting the event will make the loop exit even if `action_cb` is currently
            running. Defaults to :obj:`None`.
        is_running (:obj:`callable`): Function to check if the loop should continue running.
            Must return a boolean value. Defaults to `lambda: True`.
        max_retries (:obj:`int`): Maximum number of retries before stopping the loop.

            * < 0: Retry indefinitely.
            * 0: No retries.
            * > 0: Number of retries.

    zNetwork Retry Loop (z):c                       y)NT r       `/var/www/html/profi_bot/bot/venv/lib/python3.12/site-packages/telegram/ext/_utils/networkloop.py<lambda>z$network_retry_loop.<locals>.<lambda>Y   s    $r   r   c                  
  K   s         d {   S t        j                                } t        j                  j                               }t        j                  | |ft         j                         d {   \  }}t	        j
                  t         j                        5  |D ]  }|j                           	 d d d        ||v rt        j                  d       y| j                         S 7 7 x# 1 sw Y   8xY ww)N)return_whenz%s CancelledF)asynciocreate_taskwaitFIRST_COMPLETED
contextlibsuppressCancelledErrorcancel_LOGGERdebugresult)action_cb_task	stop_taskdonependingtaskr   
log_prefixr   s        r   	do_actionz%network_retry_loop.<locals>.do_action[   s     "$$ ,,Y[9''
(9:	%llY'W5L5L
 
g   !7!78   9 MM.*5$$&& %
 98s:   DC3A+D<C5=*D'C7 4D5D7D <Dz%s Startingr   N   g      ?z/%s %s. Adding %s seconds to the specified time.z'%s Timed out: %s. Retrying immediately.z&%s Invalid token. Aborting retry loop.z(%s Failed run number %s of %s. Retrying.z(%s Failed run number %s of %s. Aborting.   g      ?)boolr#   r$   r   inforetry_afterr
   r   	exceptionr	   minr   sleep)r   r   r   r   r   r   r   effective_is_runningr,   cur_intervalretriesexc
slack_timetoetelegram_excr+   s   `   `          @r   network_retry_loopr<   ,   s    X (}B7J%7,'T '& MM-,LG

 #	"$$B qLGE %@ $LqLG-----O 
 $ 	8JLLA:sT^ &7L 	MMCZQTUL 	F
S 	S,'Q'K"7>
GU` !!>
GU`  !- 11s2s\?Q7RL	S& qLG .s   =F
B B
B FF F;F<FF
B 	E?)C>F E?C-(F -+E?AE:4F :E??F F		F)__doc__r   r   collections.abcr   typingr   r   telegram._utils.loggingr   telegram.errorr   r   r	   r
   __name__r#   strfloatEventr/   intr<   r   r   r   <module>rG      s   &   % % . L L
X
 <@ +//3l.Y'l. -$!678l. 	l.
 l. 'l. "d(+,l. l. 
l.r   