U
    g                     @   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mZmZ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)BoundedSemaphore)TracebackType)AnyAsyncContextManager	AwaitableOptionalTypeTypeVarfinal_BUPTBaseUpdateProcessor)boundc                   @   s   e Zd ZdZdZedddZeedddZe	e
e  e	e e	e d	d
ddZeedddZeedd	dddZed	dddZed	dddZeedd	d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   E/tmp/pip-unpacked-wheel-swnnwir2/telegram/ext/_baseupdateprocessor.py__init__B   s    
zBaseUpdateProcessor.__init__)r   returnc                    s<   z|   I dH  W n$ tk
r6   |  I dH   Y nX | S )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__H   s    
zBaseUpdateProcessor.__aenter__N)exc_typeexc_valexc_tbr   c                    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__Y   s    zBaseUpdateProcessor.__aexit__r   c                 C   s   | j S )zM:obj:`int`: The maximum number of updates that can be processed concurrently.)r   r   r   r   r   r   b   s    z*BaseUpdateProcessor.max_concurrent_updatesAwaitable[Any]update	coroutiner   c                    s   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_updateg   s    z%BaseUpdateProcessor.do_process_updatec                    s   dS )zInitializes the processor so resources can be allocated. Must be implemented by a
        subclass.

        .. seealso::
            :meth:`shutdown`
        Nr   r   r   r   r   r   y   s    zBaseUpdateProcessor.initializec                    s   dS )zShutdown the processor so resources can be freed. Must be implemented by a subclass.

        .. seealso::
            :meth:`initialize`
        Nr   r   r   r   r   r      s    zBaseUpdateProcessor.shutdownc              
      s6   | j 4 I dH  | ||I dH  W 5 Q I dH R X 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)__name__
__module____qualname____doc__	__slots__intr   r   r   r	   r
   BaseExceptionr   r"   propertyr   r   objectr)   r   r   r   r*   r   r   r   r   r      s2   #
	c                   @   sB   e Zd ZdZdZedddddZddd	d
Zd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$   Nr%   c                    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_updater#   c                    s   dS zDoes nothing.Nr   r   r   r   r   r      s    z SimpleUpdateProcessor.initializec                    s   dS r5   r   r   r   r   r   r      s    zSimpleUpdateProcessor.shutdown)	r+   r,   r-   r.   r/   r3   r)   r   r   r   r   r   r   r4      s   r4   N)r.   abcr   r   Zasyncior   typesr   typingr   r   r   r	   r
   r   r   r   r   r4   r   r   r   r   <module>   s   $ 