ó
P'—^c           @@  s¶  d  Z  d d l m Z d d l Z d d l m Z d d l m Z d d l 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 m Z d d
 l m Z d d l m Z d d l m Z d d l m Z m Z d d l m Z  d d l! m" Z" d d l# m$ Z$ d d l# m% Z& d d d d g Z' d Z( d Z) d e* f d „  ƒ  YZ+ d e f d „  ƒ  YZ, d e, f d „  ƒ  YZ- d e
 f d „  ƒ  YZ. d e/ f d „  ƒ  YZ0 d S(   s[   
    celery.app.amqp
    ~~~~~~~~~~~~~~~

    Sending and receiving messages using Kombu.

i    (   t   absolute_importN(   t	   timedelta(   t   WeakValueDictionary(   t
   Connectiont   Consumert   Exchanget   Producert   Queue(   t	   Broadcast(   t   ProducerPool(   t   cached_propertyt   uuid(   t	   safe_repr(   t
   maybe_list(   t   signals(   t   itemst   string_t(   t   indent(   t   to_utci   (   t   app_or_default(   t   routest   AMQPt   Queuest   TaskProducert   TaskConsumeri   €sS   
.> {0.name:<16} exchange={0.exchange.name}({0.exchange.type}) key={0.routing_key}
c           B@  s°   e  Z d  Z d Z d d e d d d „ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d e d	 „ Z d
 „  Z d „  Z e Z d „  Z e Z d „  Z e d „  ƒ Z RS(   s¯  Queue nameâ‡’ declaration mapping.

    :param queues: Initial list/tuple or dict of queues.
    :keyword create_missing: By default any unknown queues will be
                             added automatically, but if disabled
                             the occurrence of unknown queues
                             in `wanted` will raise :exc:`KeyError`.
    :keyword ha_policy: Default HA policy for queues with none set.


    c         C@  sÒ   t  j |  ƒ t ƒ  |  _ | |  _ | |  _ | |  _ | d  k rF t n | |  _	 t
 | t t f ƒ r} t  d „  | Dƒ ƒ } n  xN t | pŒ i  ƒ D]: \ } } t
 | t ƒ rº |  j | ƒ n |  j | |  q Wd  S(   Nc         s@  s   |  ] } | j  | f Vq d  S(   N(   t   name(   t   .0t   q(    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pys	   <genexpr>D   s    (   t   dictt   __init__R   t   aliasest   default_exchanget   create_missingt	   ha_policyt   NoneR   t   autoexchanget
   isinstancet   tuplet   listR   R   t   addt
   add_compat(   t   selft   queuesR   R    R!   R#   R   R   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR   ;   s    			c         C@  s4   y |  j  | SWn t k
 r/ t j |  | ƒ SXd  S(   N(   R   t   KeyErrorR   t   __getitem__(   R)   R   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR,   H   s    c         C@  sb   |  j  r/ | j s  | j j r/ |  j  | _ n  t j |  | | ƒ | j r^ | |  j | j <n  d  S(   N(   R   t   exchangeR   R   t   __setitem__t   aliasR   (   R)   R   t   queue(    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR.   N   s    	c         C@  s/   |  j  r |  j |  j | ƒ ƒ St | ƒ ‚ d  S(   N(   R    R'   t   new_missingR+   (   R)   R   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyt   __missing__V   s    	c         K@  sg   t  | t ƒ s |  j | |  S|  j rV | j d k rC i  | _ n  |  j | j ƒ n  | |  | j <| S(   sa  Add new queue.

        The first argument can either be a :class:`kombu.Queue` instance,
        or the name of a queue.  If the former the rest of the keyword
        arguments are ignored, and options are simply taken from the queue
        instance.

        :param queue: :class:`kombu.Queue` instance or name of the queue.
        :keyword exchange: (if named) specifies exchange name.
        :keyword routing_key: (if named) specifies binding key.
        :keyword exchange_type: (if named) specifies type of exchange.
        :keyword \*\*options: (if named) Additional declaration options.

        N(   R$   R   R(   R!   t   queue_argumentsR"   t   _set_ha_policyR   (   R)   R0   t   kwargs(    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR'   [   s    	c         K@  s   | j  d | j d ƒ ƒ | d d  k r6 | | d <n  |  j d  k	 ra |  j | j  d i  ƒ ƒ n  t j | |  } |  | <| S(   Nt   routing_keyt   binding_keyR3   (   t
   setdefaultt   getR"   R!   R4   R   t	   from_dict(   R)   R   t   optionsR   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR(   s   s    c         C@  sM   |  j  } t | t t f ƒ r? | j i d d 6t | ƒ d 6ƒ S| | d <d  S(   Nt   nodess   x-ha-policys   x-ha-policy-params(   R!   R$   R&   R%   t   update(   R)   t   argst   policy(    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR4   }   s
    	i    c         C@  s   |  j  } | s d Sg  t t | ƒ ƒ D]! \ } } t j ƒ  j | ƒ ^ q& } | ri t d j | ƒ | ƒ S| d d t d j | d ƒ | ƒ S(   s/   Format routing table into string for log dumps.t    s   
i    i   (   t   consume_fromt   sortedR   t   QUEUE_FORMATt   stript   formatt
   textindentt   join(   R)   R   t   indent_firstt   activet   _R   t   info(    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyRE   „   s    	7c         K@  s8   |  j  | |  } |  j d k	 r4 | |  j | j <n  | S(   sy   Add new task queue that will be consumed from even when
        a subset has been selected using the :option:`-Q` option.N(   R'   t   _consume_fromR"   R   (   R)   R0   R5   R   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyt
   select_add   s    c         @  s2   | r. t  ‡  f d †  t | ƒ Dƒ ƒ ˆ  _ n  d S(   sÎ   Sets :attr:`consume_from` by selecting a subset of the
        currently defined queues.

        :param include: Names of queues to consume from.
                        Can be iterable or string.
        c         3@  s   |  ] } | ˆ  | f Vq d  S(   N(    (   R   R   (   R)   (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pys	   <genexpr>Ÿ   s   N(   R   R   RL   (   R)   t   include(    (   R)   s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyt   select—   s    c         @  si   ˆ  re t  ˆ  ƒ ‰  |  j d k r> |  j ‡  f d †  |  Dƒ ƒ Sx$ ˆ  D] } |  j j | d ƒ qE Wn  d S(   s¶   Deselect queues so that they will not be consumed from.

        :param exclude: Names of queues to avoid consuming from.
                        Can be iterable or string.

        c         3@  s!   |  ] } | ˆ  k r | Vq d  S(   N(    (   R   t   k(   t   exclude(    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pys	   <genexpr>®   s    N(   R   RL   R"   RO   t   pop(   R)   RQ   R0   (    (   RQ   s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyt   deselect£   s    c         C@  s   t  | |  j | ƒ | ƒ S(   N(   R   R#   (   R)   R   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR1   ´   s    c         C@  s   |  j  d  k	 r |  j  S|  S(   N(   RL   R"   (   R)   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyRA   ·   s    N(   t   __name__t
   __module__t   __doc__R"   RL   t   TrueR   R,   R.   R2   R'   R(   R4   RE   RM   RO   t   select_subsetRS   t   select_removeR1   t   propertyRA   (    (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR   +   s$   					
			
		c        #   B@  sò   e  Z d Z e Z e Z d Z e Z	 d Z
 e Z d d d  „ Z d d d d d d d d d d d d d d d d d d d d d d d d d d d d e j j e j j e j j e j j e j j e j j d „" Z e Z d „  Z e d „  ƒ Z
 RS(   c         O@  s¶   | j  d |  j ƒ |  _ | j  d |  j p- i  ƒ |  _ | j  d |  j ƒ |  _ | pZ |  j } |  j j j |  _ |  j j j |  _ |  j j	 j
 |  _ t t |  ƒ j | | | | Ž d  S(   Nt   retryt   retry_policyt   send_sent_event(   RR   R[   R\   R]   R-   t   appt   amqpR*   t   default_queuet   conft   CELERY_DEFAULT_DELIVERY_MODEt   _default_modet   superR   R   (   R)   t   channelR-   R>   R5   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR   Ç   s    		i    c$   *      K@  sã  | d& k r |  j n | } | d& k r- i  n | } | }% | d& k r] |
 d& k r] |  j } n  | d& k	 r¼ t | t ƒ r | |  j | }% } n	 | j }% |
 p§ | j j }
 | p¶ | j } n  | d& k rê | rê t | t	 ƒ rê | g } n  | d& k r|  j
 } n  | d& k r|  j n | } | r5t |  j |  n |  j }& | pJt ƒ  } | pVg  } | pbi  } t | t t f ƒ s‰t d ƒ ‚ n  t | t ƒ s§t d ƒ ‚ n  | r|  j | d ƒ | pÏ|  j j ƒ  } | t d | ƒ } |  j rt | ƒ j |  j j ƒ } qn  t |	 t j ƒ rƒ|  j |	 d ƒ | pC|  j j ƒ  } | t d |	 ƒ }	 |  j rƒt |	 ƒ j |  j j ƒ }	 qƒn  | o’| j ƒ  } |	 o¤|	 j ƒ  }	 i | d 6| d 6| d 6| d	 6| pÏd
 d 6| d 6|	 d 6|  j d 6| d 6| d 6| | f d 6| p| d 6| d 6}' | r]| d | d |' d |
 d | d | d | d |$ d | ƒ n  |  j |' d |
 d | d | p|  j d | p|  j d | d | d |& d | d | d  | d | |$ |! ré|  d | d |' d |
 d | ƒ n  |# r)|" d | d! | d | d | d	 | d | d | p"| ƒ n  |  j rß| p>|  j  }( |
 pM|  j }) t |) t! ƒ rk|) j }) n  |( j d" i
 | d# 6| d$ 6t" | ƒ d 6t" | ƒ d	 6| d 6| d 6|	 d 6|% d% 6|) d 6| d 6|  d | d | ƒn  | S('   s   Send task message.s!   task args must be a list or tuples    task kwargs must be a dictionaryt	   countdownt   secondst   expirest   taskt   idR>   R5   i    t   retriest   etat   utct	   callbackst   errbackst	   timelimitt   tasksett   chordt   sendert   bodyR-   R6   t   declaret   headerst
   propertiesR\   t
   serializert   compressionR[   t   reply_tot   correlation_idt   delivery_modet   task_ids	   task-sentR   R   R0   N(#   R"   R[   R`   R$   R   R*   R   R-   R6   R   Rc   R   R\   R   R&   R%   t
   ValueErrort   _verify_secondsR^   t   nowR   Rm   R   t
   astimezonet   timezonet   numberst   Realt	   isoformatt   publishRx   Ry   R]   t   event_dispatcherR   R   (*   R)   t	   task_namet	   task_argst   task_kwargsRf   Rl   R}   t   group_idt
   taskset_idRh   R-   t   exchange_typeR‡   R[   R\   R0   R€   Rk   Rr   Rn   Ro   R6   Rx   R|   Ry   Rz   t
   time_limitt   soft_time_limitRu   Rv   t   send_before_publisht   before_receiverst   send_after_publisht   after_receiverst   send_task_sentt   sent_receiversR5   t   qnamet   _rpRt   t   evdt   exname(    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyt   publish_taskÓ   sÊ    	"		!	!


		c         C@  s)   | t  k  r% t d | | f ƒ ‚ n  | S(   Ns   %s is out of range: %r(   t   INT_MINR~   (   R)   t   st   what(    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR   Z  s    c         C@  s   |  j  j j d t ƒ S(   Nt   enabled(   R^   t   eventst
   Dispatchert   False(   R)   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR‡   _  s    N(   RT   RU   R"   R^   R¡   t   auto_declareR[   R\   RW   Rm   R‡   R]   R   R   t   before_task_publisht   sendt	   receiverst   after_task_publisht	   task_sentRš   t
   delay_taskR   R
   (    (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR   ¾   s6   										v	t   TaskPublisherc           B@  s   e  Z d  Z d d d „ Z RS(   s,   Deprecated version of :class:`TaskProducer`.c         O@  sÅ   t  | j d |  j ƒ ƒ |  _ | j d |  j ƒ |  _ | j d |  j pK i  ƒ |  _ | p` |  j } t | t ƒ s t | | j d d ƒ ƒ } n  |  j j j	 |  _	 t
 t |  ƒ j | | | | Ž d  S(   NR^   R[   R\   R   t   direct(   R   RR   R^   R[   R\   R-   R$   R   R_   R*   Rd   R©   R   (   R)   Re   R-   R>   R5   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR   i  s    	N(   RT   RU   RV   R"   R   (    (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR©   f  s   c           B@  s    e  Z d Z d d d d  „ Z RS(   c         K@  st   | p |  j  |  _  | d  k r0 |  j  j j } n  t t |  ƒ j | | pc t |  j  j j	 j
 j ƒ  ƒ d | | d  S(   Nt   accept(   R^   R"   Ra   t   CELERY_ACCEPT_CONTENTRd   R   R   R&   R_   R*   RA   t   values(   R)   Re   R*   R^   R«   t   kw(    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR   y  s    $N(   RT   RU   R"   R^   R   (    (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR   v  s   c           B@  s  e  Z e Z e Z e Z e Z e Z e	 Z
 d Z d Z d Z d  „  Z d „  Z d d d d „ Z	 d d d „ Z e d „  ƒ Z e Z e d „  ƒ Z e Z e d „  ƒ Z e d „  ƒ Z e j d „  ƒ Z e d	 „  ƒ Z e d
 „  ƒ Z e d „  ƒ Z e Z e d „  ƒ Z RS(   c         C@  s   | |  _  d  S(   N(   R^   (   R)   R^   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR     s    c         C@  s   t  j |  j j j ƒ |  _ d  S(   N(   t   _routest   prepareR^   Ra   t   CELERY_ROUTESt   _rtable(   R)   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyt   flush_routes   s    c         C@  sª   |  j  j } | d k r$ | j } n  | d k r< | j } n  | rs | j rs t | j d |  j d | j ƒf } n  | d k rˆ |  j	 n | } |  j
 | |  j | | | ƒ S(   sa   Create new :class:`Queues` instance, using queue defaults
        from the current configuration.R-   R6   N(   R^   Ra   R"   t   CELERY_CREATE_MISSING_QUEUESt   CELERY_QUEUE_HA_POLICYt   CELERY_DEFAULT_QUEUER   R   t   CELERY_DEFAULT_ROUTING_KEYR#   t
   queues_cls(   R)   R*   R    R!   R#   Ra   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR   £  s    	c         C@  s7   t  j |  j | p |  j |  j j d | ƒ d |  j ƒS(   s   Return the current task router.R´   R^   (   R¯   t   RouterR   R*   R^   t   either(   R)   R*   R    (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR¹   ·  s    c         C@  s   |  j  j |  j d d ƒS(   sw   Return consumer configured to consume from the queues
        we are configured for (``app.amqp.queues.consume_from``).t   reverses   amqp.TaskConsumer(   R^   t   subclass_with_selft   consumer_cls(   R)   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR   ½  s    c         C@  sm   |  j  j } |  j  j |  j d d d |  j d | j d | j d | j d | j d | j	 d	 | j
 d
 | j ƒ	S(   s_   Return publisher used to send tasks.

        You should use `app.send_task` instead.

        R»   s   amqp.TaskProducerR-   R6   Rx   Ry   R[   R\   R]   Rm   (   R^   Ra   R¼   t   producer_clsR   R·   t   CELERY_TASK_SERIALIZERt   CELERY_MESSAGE_COMPRESSIONt   CELERY_TASK_PUBLISH_RETRYt    CELERY_TASK_PUBLISH_RETRY_POLICYt   CELERY_SEND_TASK_SENT_EVENTt   CELERY_ENABLE_UTC(   R)   Ra   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR   Å  s    									c         C@  s   |  j  |  j j j S(   N(   R*   R^   Ra   R¶   (   R)   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR`   Û  s    c         C@  s   |  j  |  j j j ƒ S(   s"   Queue nameâ‡’ declaration mapping.(   R   R^   Ra   t   CELERY_QUEUES(   R)   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR*   ß  s    c         C@  s   |  j  | ƒ S(   N(   R   (   R)   R*   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR*   ä  s    c         C@  s#   |  j  d  k r |  j ƒ  n  |  j  S(   N(   R²   R"   R³   (   R)   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR   è  s    c         C@  s
   |  j  ƒ  S(   N(   R¹   (   R)   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyt   routerî  s    c         C@  sF   |  j  d  k r? t |  j j d |  j j j d |  j ƒ|  _  n  |  j  S(   Nt   limitR   (   t   _producer_poolR"   R	   R^   t   poolRÇ   R   (   R)   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyt   producer_poolò  s    c         C@  s   t  |  j j j |  j j j ƒ S(   N(   R   R^   Ra   t   CELERY_DEFAULT_EXCHANGEt   CELERY_DEFAULT_EXCHANGE_TYPE(   R)   (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR   ý  s    N(   RT   RU   R   R   t   BrokerConnectionR   R¾   R   R½   R   R¸   R"   R²   RÈ   R#   R   R³   R¹   R
   t   get_task_consumerR©   R`   R*   t   setterRZ   R   RÆ   RÊ   t   publisher_poolR   (    (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyR   …  s4   			(1   RV   t
   __future__R    Rƒ   t   datetimeR   t   weakrefR   t   kombuR   R   R   R   R   t   kombu.commonR   t   kombu.poolsR	   t   kombu.utilsR
   R   t   kombu.utils.encodingR   t   kombu.utils.functionalR   t   celeryR   t   celery.fiveR   R   t   celery.utils.textR   RF   t   celery.utils.timeutilsR   R@   R   R   R¯   t   __all__R›   RC   R   R   R   R©   R   t   objectR   (    (    (    s1   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/amqp.pyt   <module>   s0   (“¨