σ
O'^c           @@  sF  d  Z  d d l m Z d d l Z d d l m Z d d l m Z 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 y d d l m Z Wn' e k
 rΫ d e f d     YZ n Xd Z d Z d Z d Z d Z d e f d     YZ d e j f d     YZ d e j f d     YZ d S(   s«   
kombu.transport.mongodb
=======================

MongoDB transport.

:copyright: (c) 2010 - 2013 by Flavio Percoco Premoli.
:license: BSD, see LICENSE for more details.

i    (   t   absolute_importN(   t   errors(   t   loadst   dumps(   t   MongoClientt
   uri_parser(   t   Empty(   t   _detect_environment(   t   bytes_to_stri   (   t   virtual(   t
   CursorTypeR
   c           B@  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyR
      s   s	   127.0.0.1ii  t   messagess   messages.routings   messages.broadcastt   BroadcastCursorc           B@  sM   e  Z d  Z d   Z d   Z d   Z e d  Z d   Z d   Z	 e	 Z
 RS(   s   Cursor for broadcast queues.c         C@  s   | |  _  |  j d t  d  S(   Nt   rewind(   t   _cursort   purget   False(   t   selft   cursor(    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   __init__*   s    	c         C@  s   |  j  j   |  j S(   N(   R   t   countt   _offset(   R   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   get_size/   s    c         C@  s   |  j  j   d  S(   N(   R   t   close(   R   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyR   2   s    c         C@  sD   | r |  j  j   n  |  j  j   |  _ |  j  j |  j  |  _  d  S(   N(   R   R   R   R   t   skip(   R   R   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyR   5   s    c         C@  s   |  S(   N(    (   R   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   __iter__=   s    c         C@  sr   x\ t  r^ y t |  j  } Wn; t j j k
 rY } d | j k rS |  j   q n    q XPq W|  j d 7_ | S(   Ns   not valid at serveri   (	   t   Truet   nextR   t   pymongoR   t   OperationFailuret   messageR   R   (   R   t   msgt   exc(    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   __next__@   s    	
(   R   R   t   __doc__R   R   R   R   R   R   R#   R   (    (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyR   '   s   					t   Channelc           B@  sμ   e  Z d Z e Z i  Z d    Z d   Z d   Z	 d   Z
 d   Z d   Z d d  Z d   Z d d	  Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z e d    Z d   Z d   Z d   Z d   Z d   Z RS(   c         O@  s0   t  t |   j | |   i  |  _ |  j   d  S(   N(   t   superR%   R   t   _broadcast_cursorst   _create_client(   R   t   vargst   kwargs(    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyR   [   s    	c         K@  s   d  S(   N(    (   R   t   queueR*   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt
   _new_queuec   s    c         C@  s¦   | |  j  k rE y t |  j |   } Wqz t k
 rA d  } qz Xn5 |  j   j d i | d 6d i t j d 6d t	  } | d  k r t
    n  t t | d   S(   Nt   queryR+   t   sortt   _idt   removet   payload(   t   _fanout_queuesR   t   get_broadcast_cursort   StopIterationt   Nonet   get_messagest   find_and_modifyR   t	   ASCENDINGR   R   R   R   (   R   R+   R!   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   _getf   s    	c         C@  sB   | |  j  k r" |  j |  j   S|  j   j i | d 6 j   S(   NR+   (   R2   R3   R   R6   t   findR   (   R   R+   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   _sizex   s    c         K@  s+   |  j    j i t |  d 6| d 6 d  S(   NR1   R+   (   R6   t   insertR   (   R   R+   R    R*   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   _put~   s    c         C@  sR   |  j  |  } | |  j k r4 |  j |  j   n |  j   j i | d 6 | S(   NR+   (   R;   R2   t   get_broadcaset_cursorR   R6   R0   (   R   R+   t   size(    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   _purge   s
    s
   mongodb://c         C@  sw  |  j  j } | j } | j |  s1 | | } n  | t |  sN | t 7} n  | j r· d | k r· | j d  \ } } | j } | j r | d | j 7} n  | d | d | } n  | j	 d  k	 rΟ | j	 n t } t j | |  } | d pχ | j }	 |	 d k rd }	 n  i t d 6| j d 6| j r?t | j d	  n d  d
 6}
 |
 j | j  |
 j | d  | |	 |
 f S(   Nt   @s   ://t   :t   databaset   /t   kombu_defaultt   auto_start_requestt   ssliθ  t   connectTimeoutMSt   options(   RD   N(   t
   connectiont   clientt   hostnamet
   startswitht   lent   DEFAULT_HOSTt   useridt   splitt   passwordt   portR5   t   DEFAULT_PORTR   t	   parse_urit   virtual_hostR   RG   t   connect_timeoutt   intt   updatet   transport_options(   R   t   schemeRK   RL   t   headt   tailt   credentialsRS   t   parsedt   dbnameRI   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt
   _parse_uri   s0    				
&c         C@  s&   t  j d k r" | j d d   n  | S(   Ni   RF   (   i   (   R   t   version_tuplet   popR5   (   R   RI   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   _prepare_client_options΅   s    c         C@  s  |  j  d |  \ } } } |  j |  } | | d <t   } | d k rf d d l m } | j   n& | d k r d d l m } |   n  t |   }	 |	 | }
 |	 j	   d } t
 t t | j d	  d
    d k  rς t d j |    n  |  j |
 |  |
 |  _ d  S(   NR[   t   hostt   geventi    (   t   monkeyt   eventlet(   t   monkey_patcht   versiont   .i   i   i   s3   Kombu requires MongoDB version 1.3+ (server is {0})(   i   i   (   Ra   Rd   R   Rf   Rg   t	   patch_allRh   Ri   R   t   server_infot   tuplet   mapRX   RQ   t   NotImplementedErrort   formatt   _create_broadcastt   _client(   R   R[   RL   R`   RI   t   conft   envRg   Ri   t	   mongoconnRC   Rj   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   _openΊ   s&    
	

(c         C@  sH   t  | j   k r d S| j d  p( d } | j t  d | d t d S(   s0   Create capped collection for broadcast messages.Nt   capped_queue_sizei  R?   t   capped(   t   DEFAULT_BROADCAST_COLLECTIONt   collection_namest   gett   create_collectionR   (   R   RC   RI   t   capsize(    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyRr   Υ   s
    c         C@  sR   |  j    j d d g d t |  j   j d	 g  |  j   j d
 d g  d S(   s   Ensure indexes on collections.R+   i   R/   t
   backgroundt   exchangeN(   R+   i   (   R/   i   (   R+   i   (   R+   i   (   R   i   (   R6   t   ensure_indexR   t   get_broadcastt   get_routing(   R   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   _ensure_indexesή   s    c         C@  sQ   t  |  j j | d  } |  j   j j i | d 6 } | t  d   | D  BS(   s'   Get table of bindings for ``exchange``.t   tableR   c         s@  s*   |  ]  } | d  | d | d f Vq d S(   t   routing_keyt   patternR+   N(    (   t   .0t   r(    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pys	   <genexpr>ξ   s   (   t	   frozensett   statet	   exchangesR6   t   routingR:   (   R   R   t   localRoutest   brokerRoutes(    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt	   get_tableη   s
    c         K@  s+   |  j    j i t |  d 6| d 6 d S(   s   Deliver fanout message.R1   R+   N(   R   R<   R   (   R   R   R    R   R*   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   _put_fanoutς   s    c         C@  s   |  j  |  j d k r> |  j | | | |  | |  j | <n  i | d 6| d 6| d 6| d 6} |  j   j | | d t d  S(   Nt   fanoutR   R+   R   R   t   upsert(   t   typeoft   typet   create_broadcast_cursorR2   R   RY   R   (   R   R   R   R   R+   t   meta(    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   _queue_bindχ   s    

c         K@  s   |  j    j i | d 6 t t |   j | |  | |  j k r y |  j j |  } Wn t k
 rk q X| j	   |  j j |  n  d  S(   NR+   (
   R   R0   R&   R%   t   queue_deleteR2   R'   Rc   t   KeyErrorR   (   R   R+   R*   R   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyR     s    
c         C@  s   |  j    |  j   d  S(   N(   Rw   R   (   R   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyR(     s    
c         C@  s#   |  j  d  k r |  j   n  |  j  S(   N(   Rs   R5   R(   (   R   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyRK     s    c         C@  s   |  j  t S(   N(   RK   t   DEFAULT_MESSAGES_COLLECTION(   R   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyR6     s    c         C@  s   |  j  t S(   N(   RK   t   DEFAULT_ROUTING_COLLECTION(   R   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyR     s    c         C@  s   |  j  t S(   N(   RK   Rz   (   R   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyR   !  s    c         C@  sA   y |  j  | SWn+ t k
 r< |  j |  j | d  d  |  SXd  S(   N(   R'   R   R   R2   R5   (   R   R+   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyR3   $  s
    c         C@  s   t  j d
 k r: t d i | d 6d d g d t j  } n% t d i | d 6d d g d	 t  } |  j   j |   } t |  } |  j	 | <| S(   Ni   t   filterR+   R.   s   $naturali   t   cursor_typeR-   t   tailable(   i   (   s   $naturali   (   s   $naturali   (
   R   Rb   t   dictR
   t   TAILABLER   R   R:   R   R'   (   R   R   R   R   R+   R-   R   t   ret(    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyR   /  s    			N(   R   R   R5   Rs   R   t   supports_fanoutR2   R   R,   R9   R;   R=   R@   Ra   Rd   Rw   Rr   R   R   R   R   R   R(   t   propertyRK   R6   R   R   R3   R   (    (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyR%   V   s2   						
)														t	   Transportc           B@  sg   e  Z e Z e Z d  Z e Z e j	 j
 e j f Z
 e j	 j e j e j f Z d Z d Z d   Z RS(   i   t   mongodbR   c         C@  s   t  j S(   N(   R   Rj   (   R   (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   driver_versionQ  s    (   R   R   R%   R   t   can_parse_urlt   polling_intervalRT   t   default_portR	   R₯   t   connection_errorsR   t   ConnectionFailuret   channel_errorsR   t   driver_typet   driver_nameR§   (    (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyR₯   @  s   	(   R$   t
   __future__R    R   R   t   anyjsonR   R   R   R   t
   kombu.fiveR   t	   kombu.synR   t   kombu.utils.encodingR   t    R	   t   pymongo.cursorR
   t   ImportErrort   objectRO   RT   R   R   Rz   R   R%   R₯   (    (    (    s9   /tmp/pip-unpacked-wheel-UAnTfW/kombu/transport/mongodb.pyt   <module>
   s*   /κ