ó
O'—^c           @@  sñ  d  Z  d d l m Z d d l Z d d l Z d d l m Z m Z d d l m	 Z	 d d l
 m Z d d l m Z d	 Z yd d l Z d d
 l m Z d d l m Z e j j e j j e j j e j j e j j e j j e j j e j j e j j f	 Z e j j e j j  e j j! e j j e j j e j j e j j" e j j# e j j e j j$ e j j% e j j& e j j e j j' e j( f Z) Wn! e* k
 r®d Z d Z Z) n Xd Z, d Z- d e j. f d „  ƒ  YZ. d e j/ f d „  ƒ  YZ/ d S(   sØ  
kombu.transport.zookeeper
=========================

Zookeeper transport.

:copyright: (c) 2010 - 2013 by Mahendra M.
:license: BSD, see LICENSE for more details.

**Synopsis**

Connects to a zookeeper node as <server>:<port>/<vhost>
The <vhost> becomes the base for all the other znodes. So we can use
it like a vhost.

This uses the built-in kazoo recipe for queues

**References**

- https://zookeeper.apache.org/doc/trunk/recipes.html#sc_recipes_Queues
- https://kazoo.readthedocs.io/en/latest/api/recipe/queue.html

**Limitations**
This queue does not offer reliable consumption. An entry is removed from
the queue prior to being processed. So if an error occurs, the consumer
has to re-queue the item or it will be lost.
i    (   t   absolute_importN(   t   loadst   dumps(   t   Empty(   t   bytes_to_stri   (   t   virtuali	   (   t   KazooClient(   t   Queuei…  s!   Mahendra M <mahendra.m@gmail.com>t   Channelc           B@  s}   e  Z d Z i  Z d  „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d	 „  Z e d
 „  ƒ Z RS(   c         C@  s   t  j j |  j | ƒ S(   N(   t   ost   patht   joint   vhost(   t   selft
   queue_name(    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyt	   _get_pathZ   s    c         C@  sZ   |  j  j | d  ƒ } | d  k rV t |  j |  j | ƒ ƒ } | |  j  | <t | ƒ n  | S(   N(   t   _queuest   gett   NoneR   t   clientR   t   len(   R   R   t   queue(    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyt
   _get_queue]   s    c         K@  s`   y | d d d } Wn t  k
 r/ d } n X|  j | ƒ } | j t | ƒ d t | ƒd  S(   Nt
   propertiest   delivery_infot   priorityi    (   t   KeyErrorR   t   putR   t   MAX_PRIORITY(   R   R   t   messaget   kwargsR   (    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyt   _puti   s    
c         C@  sC   |  j  | ƒ } | j ƒ  } | d  k r3 t ƒ  ‚ n  t t | ƒ ƒ S(   N(   R   R   R   R   R   R   (   R   R   t   msg(    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyt   _getr   s
    c         C@  sL   d } |  j  | ƒ } x0 t rG | j ƒ  } | d  k r: Pn  | d 7} q W| S(   Ni    i   (   R   t   TrueR   R   (   R   R   t   countR    (    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyt   _purge{   s    	c         O@  s<   |  j  | ƒ r8 |  j | ƒ |  j j |  j | ƒ ƒ n  d  S(   N(   t
   _has_queueR$   R   t   deleteR   (   R   R   t   argsR   (    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyt   _delete‡   s    c         C@  s   |  j  | ƒ } t | ƒ S(   N(   R   R   (   R   R   (    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyt   _sizeŒ   s    c         K@  s%   |  j  | ƒ s! |  j | ƒ } n  d  S(   N(   R%   R   (   R   R   R   (    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyt
   _new_queue   s    c         C@  s   |  j  j |  j | ƒ ƒ d  k	 S(   N(   R   t   existsR   R   (   R   R   (    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyR%   ”   s    c         C@  sj   |  j  j } | j p t } d | j | f } t j j d | j d d !ƒ |  _	 t
 | ƒ } | j ƒ  | S(   Ns   %s:%st   /i    iÿÿÿÿ(   t
   connectionR   t   portt   DEFAULT_PORTt   hostnameR	   R
   R   t   virtual_hostR   R   t   start(   R   t   conninfoR.   t   conn_strt   conn(    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyt   _open—   s    "
c         C@  s(   |  j  d  k r! |  j ƒ  |  _  n  |  j  S(   N(   t   _clientR   R6   (   R   (    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyR   ¡   s    N(   t   __name__t
   __module__R   R7   R   R   R   R   R!   R$   R(   R)   R*   R%   R6   t   propertyR   (    (    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyR   U   s   												
t	   Transportc           B@  sX   e  Z e Z d  Z e Z e j j e	 Z e j j
 e Z
 d Z d Z d „  Z d „  Z RS(   i   t	   zookeepert   kazooc         O@  s8   t  d  k r t d ƒ ‚ n  t t |  ƒ j | | Ž  d  S(   Ns"   The kazoo library is not installed(   R=   R   t   ImportErrort   superR;   t   __init__(   R   R'   R   (    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyR@   µ   s    c         C@  s   t  j S(   N(   R=   t   __version__(   R   (    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyt   driver_version»   s    (   R8   R9   R   t   polling_intervalR/   t   default_portR   R;   t   connection_errorst   KZ_CONNECTION_ERRORSt   channel_errorst   KZ_CHANNEL_ERRORSt   driver_typet   driver_nameR@   RB   (    (    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyR;   ¨   s   	(    (0   t   __doc__t
   __future__R    R	   t   sockett   anyjsonR   R   t
   kombu.fiveR   t   kombu.utils.encodingR   t    R   R   R=   t   kazoo.clientR   t   kazoo.recipe.queueR   t
   exceptionst   SystemErrorExceptiont   ConnectionLossExceptiont   MarshallingErrorExceptiont   UnimplementedExceptiont   OperationTimeoutExceptiont   NoAuthExceptiont   InvalidACLExceptiont   AuthFailedExceptiont   SessionExpiredExceptionRF   t   RuntimeInconsistencyExceptiont   DataInconsistencyExceptiont   BadArgumentsExceptiont   ApiErrorExceptiont   NoNodeExceptiont   NodeExistsExceptiont    NoChildrenForEphemeralsExceptiont   NotEmptyExceptiont   InvalidCallbackExceptiont   errorRH   R>   R   R/   t
   __author__R   R;   (    (    (    s;   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/zookeeper.pyt   <module>   sV   																						S