o
    Zzj¿  ã                   @   s–   d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZmZ ddlmZ e	r0ddlmZ edd	d
�ZG dd	„ d	ed	 eƒZG dd„ deƒZdS )z-This module contains the BaseProcessor class.é    )ÚABCÚabstractmethod)ÚAbstractAsyncContextManager)ÚTracebackType)ÚTYPE_CHECKINGÚAnyÚTypeVarÚfinal)ÚTrackedBoundedSemaphore)Ú	AwaitableÚ_BUPTÚBaseUpdateProcessor)Úboundc                   @   sÒ   e Zd ZdZdZdefdd„Zdedefdd	„Zd
e	e
 dB de
dB dedB ddfdd„Zedefdd„ƒZedefdd„ƒZededdddfdd„ƒZeddd„ƒZeddd„ƒZededdddfdd„ƒZdS ) r   a¤  An abstract base class for update processors. You can use this class to implement
    your own update processor.

    Instances of this class can be used as asyncio context managers, where

    .. code:: python

        async with processor:
            # code

    is roughly equivalent to

    .. code:: python

        try:
            await processor.initialize()
            # code
        finally:
            await processor.shutdown()

    .. seealso:: :meth:`__aenter__` and :meth:`__aexit__`.

    .. seealso:: :wiki:`Concurrency`

    .. versionadded:: 20.4

    Args:
        max_concurrent_updates (:obj:`int`): The maximum number of updates to be processed
            concurrently. If this number is exceeded, new updates will be queued until the number
            of currently processed updates decreases.

    Raises:
        :exc:`ValueError`: If :paramref:`max_concurrent_updates` is a non-positive integer.
    )Ú_max_concurrent_updatesÚ
_semaphoreÚmax_concurrent_updatesc                 C   s(   || _ | jdk rtdƒ‚t| jƒ| _d S )Né   z4`max_concurrent_updates` must be a positive integer!)r   r   Ú
ValueErrorr
   r   )Úselfr   © r   ú\/var/www/kodo/tsheker/venv/lib/python3.10/site-packages/telegram/ext/_baseupdateprocessor.pyÚ__init__H   s   
zBaseUpdateProcessor.__init__r   Úreturnc                 Ã   s6   �z
|   ¡ I dH  W | S  ty   |  ¡ I dH  ‚ w )a+  |async_context_manager| :meth:`initializes <initialize>` the Processor.

        Returns:
            The initialized Processor instance.

        Raises:
            :exc:`Exception`: If an exception is raised during initialization, :meth:`shutdown`
                is called in this case.
        N)Ú
initializeÚ	ExceptionÚshutdown©r   r   r   r   Ú
__aenter__N   s   €
ýþzBaseUpdateProcessor.__aenter__Úexc_typeNÚexc_valÚexc_tbc                 Ã   s   �|   ¡ I dH  dS )zD|async_context_manager| :meth:`shuts down <shutdown>` the Processor.N)r   )r   r   r   r    r   r   r   Ú	__aexit___   s   €zBaseUpdateProcessor.__aexit__c                 C   s   | j S )zM:obj:`int`: The maximum number of updates that can be processed concurrently.)r   r   r   r   r   r   h   s   z*BaseUpdateProcessor.max_concurrent_updatesc                 C   s   | j | jj S )a  :obj:`int`: The number of updates currently being processed.

        Caution:
            This value is a snapshot of the current number of updates being processed. It may
            change immediately after being read.

        .. versionadded:: 21.11
        )r   r   Úcurrent_valuer   r   r   r   Úcurrent_concurrent_updatesm   s   
z.BaseUpdateProcessor.current_concurrent_updatesÚupdateÚ	coroutineúAwaitable[Any]c                 Ã   ó   �dS )a¦  Custom implementation of how to process an update. Must be implemented by a subclass.

        Warning:
            This method will be called by :meth:`process_update`. It should *not* be called
            manually.

        Args:
            update (:obj:`object`): The update to be processed.
            coroutine (:term:`Awaitable`): The coroutine that will be awaited to process the
                update.
        Nr   ©r   r$   r%   r   r   r   Údo_process_updatey   ó    z%BaseUpdateProcessor.do_process_updatec                 Ã   r'   )zŸInitializes the processor so resources can be allocated. Must be implemented by a
        subclass.

        .. seealso::
            :meth:`shutdown`
        Nr   r   r   r   r   r   ‹   r*   zBaseUpdateProcessor.initializec                 Ã   r'   )z’Shutdown the processor so resources can be freed. Must be implemented by a subclass.

        .. seealso::
            :meth:`initialize`
        Nr   r   r   r   r   r   ”   r*   zBaseUpdateProcessor.shutdownc              	   Ã   sT   �| j 4 I dH š |  ||¡I dH  W d  ƒI dH  dS 1 I dH s#w   Y  dS )a0  Calls :meth:`do_process_update` with a semaphore to limit the number of concurrent
        updates.

        Args:
            update (:obj:`object`): The update to be processed.
            coroutine (:term:`Awaitable`): The coroutine that will be awaited to process the
                update.
        N)r   r)   r(   r   r   r   Úprocess_updateœ   s   €.ÿz"BaseUpdateProcessor.process_update©r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__Úintr   r   r   ÚtypeÚBaseExceptionr   r!   Úpropertyr   r#   r   Úobjectr)   r   r   r	   r+   r   r   r   r   r   "   sL    #
þýü
û	þýüþýüc                   @   s>   e Zd ZdZdZdeddddfdd	„Zdd
d„Zddd„ZdS )ÚSimpleUpdateProcessora  Instance of :class:`telegram.ext.BaseUpdateProcessor` that immediately awaits the
    coroutine, i.e. does not apply any additional processing. This is used by default when
    :attr:`telegram.ext.ApplicationBuilder.concurrent_updates` is :obj:`int`.

    .. versionadded:: 20.4
    r   r$   r%   r&   r   Nc                 Ã   s   �|I dH  dS )a  Immediately awaits the coroutine, i.e. does not apply any additional processing.

        Args:
            update (:obj:`object`): The update to be processed.
            coroutine (:term:`Awaitable`): The coroutine that will be awaited to process the
                update.
        Nr   r(   r   r   r   r)   ¸   s   €z'SimpleUpdateProcessor.do_process_updatec                 Ã   r'   ©zDoes nothing.Nr   r   r   r   r   r   Æ   r*   z SimpleUpdateProcessor.initializec                 Ã   r'   r8   r   r   r   r   r   r   É   r*   zSimpleUpdateProcessor.shutdownr,   )	r-   r.   r/   r0   r1   r6   r)   r   r   r   r   r   r   r7   ®   s    þý
ü
r7   N)r0   Úabcr   r   Ú
contextlibr   Útypesr   Útypingr   r   r   r	   Útelegram.ext._utils.asyncior
   Úcollections.abcr   r   r   r7   r   r   r   r   Ú<module>   s    