U
    g                     @   s   d Z ddlZddlmZmZmZmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZmZ ertddlmZ ed	ZG d
d deeeef ZdS )z2This module contains the StringRegexHandler class.    N)TYPE_CHECKINGAnyMatchOptionalPatternTypeVarUnion)DEFAULT_TRUE)DVType)BaseHandler)CCTHandlerCallback)ApplicationRTc                       s   e Zd ZdZdZefdeeee f e	ee
ef ee d fddZeeee  ddd	Ze
ed
eee  ddddZ  ZS )StringRegexHandlera|  Handler class to handle string updates based on a regex which checks the update content.

    Read the documentation of the :mod:`re` module for more information. The :func:`re.match`
    function is used to determine if an update should be handled by this handler.

    Note:
        This handler is not used to handle Telegram :class:`telegram.Update`, but strings manually
        put in the queue. For example to send messages with the bot using command line or API.

    Warning:
        When setting :paramref:`block` to :obj:`False`, you cannot rely on adding custom
        attributes to :class:`telegram.ext.CallbackContext`. See its docs for more info.

    Args:
        pattern (:obj:`str` | :func:`re.Pattern <re.compile>`): The regex pattern.
        callback (:term:`coroutine function`): The callback function for this handler. Will be
            called when :meth:`check_update` has determined that an update should be processed by
            this handler. Callback signature::

                async def callback(update: str, context: CallbackContext)

            The return value of the callback is usually ignored except for the special case of
            :class:`telegram.ext.ConversationHandler`.
        block (:obj:`bool`, optional): Determines whether the return value of the callback should
            be awaited before processing the next handler in
            :meth:`telegram.ext.Application.process_update`. Defaults to :obj:`True`.

            .. seealso:: :wiki:`Concurrency`

    Attributes:
        pattern (:obj:`str` | :func:`re.Pattern <re.compile>`): The regex pattern.
        callback (:term:`coroutine function`): The callback function for this handler.
        block (:obj:`bool`): Determines whether the return value of the callback should be
            awaited before processing the next handler in
            :meth:`telegram.ext.Application.process_update`.

    )patternzStringRegexHandler[CCT, RT]selfr   callbackblockc                    s.   t  j||d t|tr$t|}|| _d S )N)r   )super__init__
isinstancestrrecompiler   r   	__class__ M/tmp/pip-unpacked-wheel-swnnwir2/telegram/ext/_handlers/stringregexhandler.pyr   L   s    

zStringRegexHandler.__init__)updatereturnc                 C   s$   t |tr t| j| }r |S dS )zDetermines whether an update should be passed to this handler's :attr:`callback`.

        Args:
            update (:obj:`object`): The incoming update.

        Returns:
            :obj:`None` | :obj:`re.match`

        N)r   r   r   matchr   )r   r    r"   r   r   r   check_updateY   s    
zStringRegexHandler.check_updatez)Application[Any, CCT, Any, Any, Any, Any]N)contextr    applicationcheck_resultr!   c                 C   s   | j r|r|g|_dS )z}Add the result of ``re.match(pattern, update)`` to :attr:`CallbackContext.matches` as
        list with one element.
        N)r   matches)r   r$   r    r%   r&   r   r   r   collect_additional_contextg   s    

z-StringRegexHandler.collect_additional_context)__name__
__module____qualname____doc__	__slots__r	   r   r   r   r   r   r   r
   boolr   objectr   r   r#   r(   __classcell__r   r   r   r   r   #   s    &
r   )r,   r   typingr   r   r   r   r   r   r   Ztelegram._utils.defaultvaluer	   Ztelegram._utils.typesr
   Z"telegram.ext._handlers.basehandlerr   Ztelegram.ext._utils.typesr   r   Ztelegram.extr   r   r   r   r   r   r   r   <module>   s   $