U
    Íø.e  ã                   @  sr   d Z ddlmZ ddlZddlmZ G dd„ dƒZG dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZ	eee	dœZ
dS )z‚Virtual AMQ Exchange.

Implementations of the standard exchanges defined
by the AMQ protocol  (excluding the `headers` exchange).
é    )ÚannotationsN)Úescape_regexc                   @  s4   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚExchangeTypez™Base class for exchanges.

    Implements the specifics for an exchange type.

    Arguments:
    ---------
        channel (ChannelT): AMQ Channel.
    Nc                 C  s
   || _ d S ©N)Úchannel)Úselfr   © r   úD/tmp/pip-unpacked-wheel-48hrr5dg/kombu/transport/virtual/exchange.pyÚ__init__   s    zExchangeType.__init__c                 C  s   t dƒ‚dS )z¡Lookup all queues matching `routing_key` in `exchange`.

        Returns
        -------
            str: queue name, or 'default' if no queues matched.
        zsubclass responsibilityN)ÚNotImplementedError©r   ÚtableÚexchangeÚrouting_keyÚdefaultr   r   r	   Úlookup   s    zExchangeType.lookupc                 C  s
   |d|fS )zÃPrepare queue-binding.

        Returns
        -------
            Tuple[str, Pattern, str]: of `(routing_key, regex, queue)`
                to be stored for bindings to this exchange.
        Nr   ©r   Úqueuer   r   Ú	argumentsr   r   r	   Úprepare_bind&   s    zExchangeType.prepare_bindc                 C  s8   ||d ko6||d ko6||d ko6|p*i |d p4i kS )z3Return true if `prev` and `exchange` is equivalent.ÚtypeÚdurableÚauto_deleter   r   )r   Úprevr   r   r   r   r   r   r   r	   Ú
equivalent0   s    
ÿ
þýzExchangeType.equivalent)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r
   r   r   r   r   r   r   r	   r      s   		
r   c                   @  s$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚDirectExchangezTDirect exchange.

    The `direct` exchange routes based on exact routing keys.
    Údirectc                   s   ‡ fdd„|D ƒS )Nc                   s   h | ]\}}}|ˆ kr|’qS r   r   )Ú.0ÚrkeyÚ_r   ©r   r   r	   Ú	<setcomp>B   s   ÿz(DirectExchange.lookup.<locals>.<setcomp>r   r   r   r$   r	   r   A   s    
ÿzDirectExchange.lookupc                 K  s2   | j j}| j j}|||ƒD ]}|||f|Ž qd S r   )r   Ú_lookupÚ_put©r   Úmessager   r   Úkwargsr&   r'   r   r   r   r	   ÚdeliverG   s    zDirectExchange.deliverN©r   r   r   r   r   r   r+   r   r   r   r	   r   9   s   r   c                   @  sJ   e Zd ZdZdZdddœZi Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ ZdS )ÚTopicExchangezºTopic exchange.

    The `topic` exchange routes messages based on words separated by
    dots, using wildcard characters ``*`` (any single word), and ``#``
    (one or more words).
    Útopicz.*?[^\.]z.*?)Ú*ú#c                   s   ‡ ‡fdd„|D ƒS )Nc                   s"   h | ]\}}}ˆ  |ˆ ¡r|’qS r   )Ú_match)r!   r"   Úpatternr   ©r   r   r   r	   r%   `   s   ÿz'TopicExchange.lookup.<locals>.<setcomp>r   r   r   r3   r	   r   _   s    ÿzTopicExchange.lookupc                   sH   | j j}| j j}| j j‰ ‡ fdd„|||ƒD ƒD ]}|||f|Ž q0d S )Nc                   s   g | ]}|r|ˆ kr|‘qS r   r   )r!   Úq©Z
deadletterr   r	   Ú
<listcomp>i   s     ÿz)TopicExchange.deliver.<locals>.<listcomp>)r   r&   r'   Zdeadletter_queuer(   r   r5   r	   r+   e   s
    zTopicExchange.deliverc                 C  s   ||   |¡|fS r   )Úkey_to_patternr   r   r   r	   r   m   s    zTopicExchange.prepare_bindc                   s(   dd  ‡ fdd„t|dƒ d¡D ƒ¡ S )z0Get the corresponding regex for any routing key.z^%s$z\.c                 3  s   | ]}ˆ j  ||¡V  qd S r   )Ú	wildcardsÚget)r!   Úword©r   r   r	   Ú	<genexpr>r   s   ÿz/TopicExchange.key_to_pattern.<locals>.<genexpr>z.#*Ú.)Újoinr   Úsplit)r   r"   r   r;   r	   r7   p   s    þzTopicExchange.key_to_patternc                 C  sF   z| j | }W n, tk
r:   t |tj¡ }| j |< Y nX | |¡S )z¸Match regular expression (cached).

        Same as :func:`re.match`, except the regex is compiled and cached,
        then reused on subsequent matches with the same pattern.
        )Ú	_compiledÚKeyErrorÚreÚcompileÚUÚmatch)r   r2   ÚstringZcompiledr   r   r	   r1   w   s
    zTopicExchange._matchN)r   r   r   r   r   r8   r@   r   r+   r   r7   r1   r   r   r   r	   r-   N   s   ÿr-   c                   @  s$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚFanoutExchangeaþ  Fanout exchange.

    The `fanout` exchange implements broadcast messaging by delivering
    copies of all messages to all queues bound to the exchange.

    To support fanout the virtual channel needs to store the table
    as shared state.  This requires that the `Channel.supports_fanout`
    attribute is set to true, and the `Channel._queue_bind` and
    `Channel.get_table` methods are implemented.

    See Also
    --------
        the redis backend for an example implementation of these methods.
    Úfanoutc                 C  s   dd„ |D ƒS )Nc                 S  s   h | ]\}}}|’qS r   r   )r!   r#   r   r   r   r	   r%   —   s     z(FanoutExchange.lookup.<locals>.<setcomp>r   r   r   r   r	   r   –   s    zFanoutExchange.lookupc                 K  s    | j jr| j j|||f|Ž d S r   )r   Zsupports_fanoutZ_put_fanout)r   r)   r   r   r*   r   r   r	   r+   ™   s      ÿÿzFanoutExchange.deliverNr,   r   r   r   r	   rG   „   s   rG   )r    r.   rH   )r   Ú
__future__r   rB   Zkombu.utils.textr   r   r   r-   rG   ZSTANDARD_EXCHANGE_TYPESr   r   r   r	   Ú<module>   s   +6ý