ó
O'—^c           @@  sq  d  Z  d d l m Z d d l Z d d l Z d d l Z y  d d l Z d d l m Z Wn e k
 rw d Z Z n Xd d l
 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 e d ƒ Z d Z d Z d
 Z e j e j Z Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e j  f d „  ƒ  YZ  d e j! f d „  ƒ  YZ! d S(   s=   
kombu.transport.zmq
===================

ZeroMQ transport.

i    (   t   absolute_importN(   t   ZMQError(   t   Empty(   t
   get_logger(   t   pickle(   t   cached_property(   t   pollt   READi   (   t   virtuals   kombu.transport.zmqi³  i€   t   MultiChannelPollerc           B@  sh   e  Z e Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 d „ Z e d „  ƒ Z RS(
   c         C@  s%   t  ƒ  |  _ i  |  _ t ƒ  |  _ d  S(   N(   t   sett	   _channelst   _fd_to_chanR   t   poller(   t   self(    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   __init__(   s    	c         C@  sc   x9 |  j  D]. } y |  j j | ƒ Wq
 t k
 r7 q
 Xq
 W|  j j ƒ  |  j  j ƒ  d  |  _ d  S(   N(   R   R   t
   unregistert   KeyErrorR   t   cleart   None(   R   t   fd(    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   close0   s    c         C@  s   |  j  j | ƒ d  S(   N(   R   t   add(   R   t   channel(    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR   :   s    c         C@  s0   |  j  j | ƒ |  j j | j j j d  ƒ d  S(   N(   R   t   discardR   t   popt   clientt
   connectionR   R   (   R   R   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR   =   s    c         C@  s9   | j  j } | |  j | j <|  j j | j |  j ƒ d  S(   N(   R   R   R   R   R   t   registert
   eventflags(   R   R   t   conn(    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt	   _registerA   s    c         C@  s%   x |  j  D] } |  j | ƒ q
 Wd  S(   N(   R   R   (   R   R   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   on_poll_startF   s    c         C@  s   |  j  | } | j ƒ  | f S(   N(   R   t   drain_events(   R   t   filenot   chan(    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   on_readableJ   s    c         C@  sP   |  j  ƒ  |  j j | ƒ } x$ | p( g  D] \ } } |  j | ƒ SWt ƒ  ‚ d  S(   N(   R    R   R   R$   R   (   R   t   timeoutt   eventsR"   t   _(    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   getN   s
    
c         C@  s   |  j  S(   N(   R   (   R   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   fdsW   s    N(   t   __name__t
   __module__R   R   R   R   R   R   R   R    R$   R   R(   t   propertyR)   (    (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR	   %   s   		
						t   Clientc           B@  s\   e  Z d  e e d e d d „ Z d „  Z d d d „ Z d „  Z	 d „  Z
 e d „  ƒ Z RS(   s   tcp://127.0.0.1c         C@  s¥  y | j  d ƒ \ } } Wn t k
 r8 d } | } n X| j  d ƒ }	 | |  _ | d k rl t d ƒ ‚ n  | p~ t j j ƒ  |  _ | r¾ |  j j t j	 ƒ |  _
 |  j
 j d j |  ƒ ƒ n	 d  |  _
 |  j j t j ƒ |  _ t t d ƒ r|  j j t j | ƒ n |  j j t j | ƒ | r<|  j j t j | ƒ n  xb |	 D]Z }
 | d k rxd |
 k rx|
 d t t ƒ 7}
 n  d j | d |
 g ƒ }
 |  j |
 ƒ qCWd  S(	   Ns   ://t   tcpt   ;s   Currently only TCP can be useds   tcp://*:{0.port}t   SNDHWMt   :t    (   t   splitt
   ValueErrort   portt   NotImplementedErrort   zmqt   Contextt   instancet   contextt   sockett   PULLt   sinkt   bindt   formatR   t   PUSHt   ventt   hasattrt
   setsockoptR0   t   HWMt   SWAPt   strt   DEFAULT_PORTt   joint   connect(   R   t   uriR5   t   hwmt	   swap_sizet   enable_sinkR:   t   schemet   partst	   endpointst   endpoint(    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR   ^   s2    
		c         C@  s   |  j  j | ƒ d  S(   N(   RA   RI   (   R   RQ   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyRI   †   s    c         C@  sž   |  j  } yK | d  k	 rI | j | } | _ z | j ƒ  SWd  | | _ Xn
 | j ƒ  SWnC t k
 r™ } | j t j k r“ t j	 t j | j
 ƒ ‚ qš ‚  n Xd  S(   N(   R=   R   t   RCVTIMEOt   recvR   t   errnoR7   t   EAGAINR;   t   errort   strerror(   R   t   queueR%   R=   t   prev_timeoutt   exc(    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR(   ‰   s    	c         K@  s   |  j  j | ƒ S(   N(   RA   t   send(   R   RX   t   messaget   kwargs(    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   putš   s    c         C@  sF   |  j  r& |  j  j r& |  j  j ƒ  n  |  j j sB |  j j ƒ  n  d  S(   N(   R=   t   closedR   RA   (   R   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR      s    c         C@  s   |  j  r |  j  S|  j S(   N(   R=   RA   (   R   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR   £   s    	N(   R*   R+   RG   t   DEFAULT_HWMR   t   TrueR   RI   R(   R^   R   R,   R   (    (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR-   \   s   	&			t   Channelc           B@  s“   e  Z e Z e Z d Z e Z e	 Z
 e j j d Z d „  Z d d „ Z d „  Z d „  Z d d „ Z d	 „  Z d
 „  Z d „  Z e d „  ƒ Z RS(   RK   RL   RM   t	   port_incrc         O@  sR   t  t |  ƒ } | j | | Ž  |  j j j |  j j j |  ƒ |  j j |  _ d  S(   N(	   t   superRb   R   R   R   R_   t   cycleR   t   connection_errors(   R   t   argsR]   t   super_(    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR   ·   s
    c         C@  sg   y t  |  j j | | ƒ ƒ SWnC t j k
 rb } | j t j k r\ | d k r\ t ƒ  ‚ qc ‚  n Xd  S(   Ni    (   t   loadsR   R(   R;   RV   RT   RU   R   (   R   RX   R%   RZ   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   _getÁ   s    c         K@  s#   |  j  j | t | d ƒ |  d  S(   Niÿÿÿÿ(   R   R^   t   dumps(   R   RX   R\   R]   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   _putÊ   s    c         C@  s   d S(   Ni    (    (   R   RX   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   _purgeÍ   s    c         C@  s   | j  d | ƒ S(   NR%   (   R(   (   R   Re   R%   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   _pollÐ   s    c         C@  s_   |  j  sH |  j j j |  ƒ y |  j d j ƒ  WqH t k
 rD qH Xn  t t |  ƒ j ƒ  d  S(   NR   (	   R_   R   Re   R   t   __dict__R   R   Rd   Rb   (   R   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR   Ó   s    	c         C@  s   | |  j  d |  j S(   Ni   (   t
   channel_idRc   (   R   R5   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   _prepare_portÜ   s    c         C@  sj   |  j  j } |  j | j p t ƒ } |  j d | j p9 d d | d |  j d |  j d |  j	 d |  j  j
 ƒ S(   NRJ   s   tcp://127.0.0.1R5   RK   RL   RM   R:   (   R   R   Rq   R5   RG   R-   t   hostnameRK   RL   RM   R:   (   R   t   conninfoR5   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   _create_clientß   s    			c         C@  s
   |  j  ƒ  S(   N(   Rt   (   R   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR   é   s    N(   RK   RL   RM   Rc   (   R*   R+   R-   R`   RK   R   RL   Ra   RM   t   DEFAULT_INCRRc   R   Rb   t   from_transport_optionsR   Rj   Rl   Rm   Rn   R   Rq   Rt   R   R   (    (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyRb   ª   s    		
							
t	   Transportc           B@  sŸ   e  Z e Z e Z e Z d  Z d Z e	 j
 j e f Z e Z d Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d	 „  Z e d
 „  ƒ Z RS(   t   zeromqR7   c         O@  sD   t  d  k r t d ƒ ‚ n  t t |  ƒ j | | Ž  t ƒ  |  _ d  S(   Ns    The zmq library is not installed(   R7   R   t   ImportErrorRd   Rw   R   R	   Re   (   R   Rg   R]   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR   û   s    c         C@  s   t  j S(   N(   R7   t   __version__(   R   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   driver_version  s    c         @  s\   |  j  ‰ | j ˆ _ | j ‰  |  j ‰ ˆ j ‰ ‡  ‡ ‡ ‡ f d †  } | j j | ƒ d  S(   Nc          @  s1   ˆ ƒ  g  ˆ j  D] }  ˆ  |  ˆ |  ƒ ^ q d  S(   N(   R)   (   R   (   t
   add_readerRe   t   cycle_poll_startR$   (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR      s    (   Re   R   R|   R$   R    t   on_tickR   (   R   R   t   loopR    (    (   R|   Re   R}   R$   s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   register_with_event_loop  s    				c         C@  s   |  j  |  j j | ƒ ƒ d  S(   N(   t   _handle_eventRe   R$   (   R   R"   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR$     s    c         C@  s®   t  } xw | j D]l } y | j j d | ƒ } Wn1 t j k
 rb } | j t j k r\ q n  ‚  q X| j | | f ƒ t	 } q W| sª t j t j t
 j t j ƒ ƒ ‚ n  d  S(   NR%   (   t   Falset   channelsRe   R(   R;   RV   RT   RU   R   Ra   t   osRW   (   R   R   R%   t   more_to_readR   t   evtRZ   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR!     s    
c         C@  s^   | \ } } | \ } } | s. | |  j  k rI t d j | | ƒ ƒ ‚ n  |  j  | | ƒ d  S(   Ns.   Message for queue {0!r} without consumers: {1}(   t
   _callbacksR   R?   (   R   R†   t   itemR   R\   RX   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR   $  s    c         C@  s   |  j  j t t |  ƒ j ƒ  S(   N(   R:   R_   Rd   Rw   t   establish_connection(   R   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR‰   -  s    
c         C@  sC   t  t |  ƒ j | ƒ y | j d j ƒ  Wn t k
 r> n Xd  S(   NR:   (   Rd   Rw   t   close_connectionRo   t   termR   (   R   R   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyRŠ   1  s
    c         C@  s   t  j d ƒ S(   Ni   (   R7   R8   (   R   (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyR:   8  s    N(   R*   R+   Rb   Ra   t   can_parse_urlRG   t   default_portt   driver_typet   driver_nameR   Rw   Rf   R   t   supports_evR   t   polling_intervalR   R{   R€   R$   R!   R   R‰   RŠ   R   R:   (    (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyRw   î   s"   								("   t   __doc__t
   __future__R    RT   R„   R;   R7   R   Ry   R   t
   kombu.fiveR   t	   kombu.logR   t   kombu.serializationR   t   kombu.utilsR   t   kombu.utils.eventioR   R   R2   R   t   loggerRG   R`   Ru   Rk   Ri   t   objectR	   R-   Rb   Rw   (    (    (    s5   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zmq.pyt   <module>   s0   7ND