U
    .e:                     @  s   d Z ddlmZ ddlZddlmZ ddlZddlmZ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
lmZmZ ddlmZ ddlmZ ddlmZ dZdZG dd dZG dd dejZG dd dejZdS )a  MongoDB transport module for kombu.

Features
========
* Type: Virtual
* Supports Direct: Yes
* Supports Topic: Yes
* Supports Fanout: Yes
* Supports Priority: Yes
* Supports TTL: Yes

Connection String
=================
 *Unreviewed*

Transport Options
=================

* ``connect_timeout``,
* ``ssl``,
* ``ttl``,
* ``capped_queue_size``,
* ``default_hostname``,
* ``default_port``,
* ``default_database``,
* ``messages_collection``,
* ``routing_collection``,
* ``broadcast_collection``,
* ``queues_collection``,
* ``calc_queue_size``,
    )annotationsN)Empty)MongoClienterrors
uri_parser)
CursorType)VersionMismatch)_detect_environment)bytes_to_str)dumpsloads)cached_property   )virtualto_rabbitmq_queue_argumentsz3Kombu requires MongoDB version 1.3+ (server is {0})zKKombu requires MongoDB version 2.2+ (server is {0}) for TTL indexes supportc                   @  sF   e Zd ZdZdd Zdd Zdd Zdd	d
Zdd Zdd Z	e	Z
dS )BroadcastCursorzCursor for broadcast queues.c                 C  s   || _ d| _| jdd d S )Nr   F)rewind)_cursor_offsetpurge)selfcursor r   ;/tmp/pip-unpacked-wheel-48hrr5dg/kombu/transport/mongodb.py__init__B   s    zBroadcastCursor.__init__c                 C  s   | j ji | j S N)r   
collectioncount_documentsr   r   r   r   r   get_sizeG   s    zBroadcastCursor.get_sizec                 C  s   | j   d S r   )r   closer   r   r   r   r!   J   s    zBroadcastCursor.closeTc                 C  s2   |r| j   | j ji | _| j | j| _ d S r   )r   r   r   r   r   skip)r   r   r   r   r   r   M   s    
zBroadcastCursor.purgec                 C  s   | S r   r   r   r   r   r   __iter__U   s    zBroadcastCursor.__iter__c              
   C  sj   zt | j}W qX tjjk
rR } z"dt|kr@|   W Y q  W 5 d }~X Y q X qXq |  jd7  _|S )Nznot valid at serverr   )nextr   pymongor   OperationFailurestrr   r   )r   msgexcr   r   r   __next__X   s    zBroadcastCursor.__next__N)T)__name__
__module____qualname____doc__r   r    r!   r   r#   r*   r$   r   r   r   r   r   ?   s   
r   c                      s\  e Zd Zd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dZdZejjd Z fddZdd Zdd Z fddZdd Zdd Zdd Zdd Zdd Z fd d!ZdGd#d$Zd%d& Z d'd( Z!dHd)d*Z"d+d, Z#d-d. Z$d/d0 Z%e&d1d2 Z'e&d3d4 Z(e&d5d6 Z)e&d7d8 Z*e&d9d: Z+d;d< Z,d=d> Z-d?d@ Z.dAdB Z/dCdD Z0dEdF Z1  Z2S )IChannelzMongoDB Channel.TFNi z	127.0.0.1ii  Zkombu_defaultmessageszmessages.routingzmessages.broadcastzmessages.queues)connect_timeoutsslttlcapped_queue_sizedefault_hostnamedefault_portdefault_databasemessages_collectionrouting_collectionbroadcast_collectionqueues_collectioncalc_queue_sizec                   s   t  j|| i | _| j d S r   )superr   _broadcast_cursorsclient)r   Zvargskwargs	__class__r   r   r      s    zChannel.__init__c              	   K  s4   | j r0| jjd|id||| |ddidd d S )N_id$set	x-expires)rC   options	expire_atTZupsert)r3   queues
update_one_get_queue_expire)r   queuer@   r   r   r   
_new_queue   s     	zChannel._new_queuec                 C  s   || j kr8zt| |}W qT tk
r4   d }Y qTX n| jjd|idtjfgd}| jrd| 	| |d krrt
 tt|d S )NrL   priority)sortpayload)_fanout_queuesr$   _get_broadcast_cursorStopIterationr0   Zfind_one_and_deleter%   Z	ASCENDINGr3   _update_queues_expirer   r   r
   )r   rL   r(   r   r   r   _get   s    


zChannel._getc                   s:   | j st |S || jkr*| | S | jd|iS NrL   )r<   r=   _sizerQ   rR   r    r0   r   r   rL   rA   r   r   rW      s
    
zChannel._sizec                 K  sr   t ||| j|ddd}| jrb| |d|d< | |}|d k	rb|d d ksZ||d k rb||d< | j| d S )NT)reverse)rP   rL   rN   zx-message-ttlrG   )r   Z_get_message_priorityr3   rK   _get_message_expirer0   
insert_one)r   rL   messager@   dataZ
msg_expirer   r   r   _put   s    


zChannel._putc                 K  s   | j t||d d S )N)rP   rL   )	broadcastr[   r   )r   exchanger\   routing_keyr@   r   r   r   _put_fanout   s    zChannel._put_fanoutc                 C  s8   |  |}|| jkr$| |  n| jd|i |S rV   )rW   rQ   rR   r   r0   delete_many)r   rL   sizer   r   r   _purge   s
    

zChannel._purgec                 C  s:   t | jj| d }| jd|i}|t dd |D B S )Ntabler`   c                 s  s$   | ]}|d  |d |d fV  qdS )ra   patternrL   Nr   ).0rr   r   r   	<genexpr>   s   z$Channel.get_table.<locals>.<genexpr>)	frozensetstateZ	exchangesroutingfind)r   r`   ZlocalRoutesZbrokerRoutesr   r   r   	get_table   s    
zChannel.get_tablec                 C  sp   |  |jdkr*| |||| || j|< ||||d}| }| jrV| |d|d< | jj|d|idd d S )Nfanout)r`   rL   ra   rg   rE   rG   rD   TrH   )	Ztypeoftype_create_broadcast_cursorrQ   copyr3   rK   rm   rJ   )r   r`   ra   rg   rL   lookupr]   r   r   r   _queue_bind   s"       
zChannel._queue_bindc                   s~   | j d|i | jr&| jd|i t j|f| || jkrzz| j	|}W n t
k
rd   Y nX |  | j	| d S )NrL   rC   )rm   rc   r3   rI   Z
delete_oner=   queue_deleterQ   r>   popKeyErrorr!   )r   rL   r@   r   rA   r   r   rv     s    
zChannel.queue_delete
mongodb://c                 C  s  | j j}|j}||s || }|t|d  s:|| j7 }|jrd|kr|d\}}|j}|jrp|d|j 7 }|d | d | }|j	r|j	n| j
}t||}|d p|j}	|	dkr| j}	d| j| jrt| jd nd d}
|
|d	  | |
}
||	|
fS )
N@z://:database)/NTi  )auto_start_requestr2   ZconnectTimeoutMSrF   )
connectionr?   hostname
startswithlenr5   Zuseridsplitpasswordportr6   r   	parse_uriZvirtual_hostr7   r2   r1   intupdate_prepare_client_options)r   schemer?   r   headtailcredentialsr   parseddbnamerF   r   r   r   
_parse_uri  s4    


zChannel._parse_uric                 C  sB   t jdkr>|dd  t|dtr>t jj}||d  |d< |S )N   r~   Zreadpreference)r%   version_tuplerw   
isinstancegetr   Zread_preferencesZ_MONGOS_MODES)r   rF   modesr   r   r   r   A  s    
zChannel._prepare_client_optionsc                 K  s   t |f|S r   r   )r   	argumentsr@   r   r   r   prepare_queue_argumentsI  s    zChannel.prepare_queue_argumentsc                 C  s   | j |d\}}}||d< t }|dkr>ddlm} |  n|dkrXddlm} |  tf |}|| }	| d }
|
	d	d }
t
tt|
	d
}|dk rtt|
n| jr|dk rtt|
|	S )N)r   hostgeventr   )monkeyeventlet)monkey_patchversion-.)r   r   )   r   )r   r	   r   r   Z	patch_allr   r   r   Zserver_infor   tuplemapr   r   E_SERVER_VERSIONformatr3   E_NO_TTL_INDEXES)r   r   r   r   confenvr   r   Z	mongoconnr|   version_strr   r   r   r   _openL  s&    

zChannel._openc                 C  s*   | j | krdS |j| j | jdd dS )z0Create capped collection for broadcast messages.NT)rd   Zcapped)r:   Zlist_collection_namesZcreate_collectionr4   r   r|   r   r   r   _create_broadcastg  s    zChannel._create_broadcastc                 C  s   || j  }|jdddgdd || j dg || j }|ddg | jr|jdgdd	 |jdgdd	 || j jdgdd	 d
S )zEnsure indexes on collections.)rL   r   )rN   r   )rC   r   T)Z
background)r`   r   )rG   r   r   )ZexpireAfterSecondsN)r8   Zcreate_indexr:   r9   r3   r;   )r   r|   r0   rm   r   r   r   _ensure_indexesp  s    
 

 zChannel._ensure_indexesc                 C  s    |   }| | | | |S )zActually creates connection.)r   r   r   r   r   r   r   _create_client  s    

zChannel._create_clientc                 C  s   |   S r   )r   r   r   r   r   r?     s    zChannel.clientc                 C  s   | j | j S r   )r?   r8   r   r   r   r   r0     s    zChannel.messagesc                 C  s   | j | j S r   )r?   r9   r   r   r   r   rm     s    zChannel.routingc                 C  s   | j | j S r   )r?   r:   r   r   r   r   r_     s    zChannel.broadcastc                 C  s   | j | j S r   )r?   r;   r   r   r   r   rI     s    zChannel.queuesc              	   C  s<   z| j | W S  tk
r6   | | j| d d | Y S X d S r   )r>   rx   rr   rQ   rX   r   r   r   rR     s       zChannel._get_broadcast_cursorc                 C  sN   t jdkrd|itjd}nd|idd}| jjf |}t| }| j|< |S )Nr   rL   )filterZcursor_typeT)queryZtailable)r%   r   r   ZTAILABLEr_   rn   r   r>   )r   r`   ra   rg   rL   r   r   retr   r   r   rr     s    
z Channel._create_broadcast_cursorc                 C  s6   | di  d}|d k	r2|  tjt|d S d S )NZ
propertiesZ
expirationZmilliseconds)r   get_nowdatetime	timedeltar   )r   r\   valuer   r   r   rZ     s    zChannel._get_message_expirec              	   C  sp   t |tr,| jd|i}|s"dS |d }n|}z|d | }W n ttfk
rZ   Y dS X |  tj|d S )zGet expiration header named `argument` of queue definition.

        Note:
        ----
            `queue` must be either queue name or options itself.
        rC   NrF   r   r   )	r   r'   rI   Zfind_onerx   	TypeErrorr   r   r   )r   rL   argumentdocr]   r   r   r   r   rK     s    

zChannel._get_queue_expirec                 C  sL   |  |d}|sdS | jd|idd|ii | jd|idd|ii dS )z,Update expiration field on queues documents.rE   NrL   rD   rG   rC   )rK   rm   Zupdate_manyrI   )r   rL   rG   r   r   r   rT     s     
 
zChannel._update_queues_expirec                 C  s
   t j  S )zReturn current time in UTC.)r   utcnowr   r   r   r   r     s    zChannel.get_now)ry   )ry   )3r+   r,   r-   r.   Zsupports_fanoutrQ   r2   r3   r1   r4   r<   r5   r6   r7   r8   r9   r:   r;   r   r/   Zfrom_transport_optionsr   rM   rU   rW   r^   rb   re   ro   ru   rv   r   r   r   r   r   r   r   r   r?   r0   rm   r_   rI   rR   rr   rZ   rK   rT   r   __classcell__r   r   rA   r   r/   n   sb   


)
	




r/   c                   @  sp   e Zd ZdZeZdZdZejZej	j
ejf Z
ej	jejejf ZdZdZej	jjedddgd	Zd
d ZdS )	TransportzMongoDB Transport.Tr   Zmongodbr%   directZtopicrp   )Zexchange_typec                 C  s   t jS r   )r%   r   r   r   r   r   driver_version  s    zTransport.driver_versionN)r+   r,   r-   r.   r/   Zcan_parse_urlZpolling_intervalr6   r   r   Zconnection_errorsr   ZConnectionFailureZchannel_errorsr&   Zdriver_typeZdriver_nameZ
implementsextendrk   r   r   r   r   r   r     s$   r   ) r.   
__future__r   r   rL   r   r%   r   r   r   Zpymongo.cursorr   Zkombu.exceptionsr   Zkombu.utils.compatr	   Zkombu.utils.encodingr
   Zkombu.utils.jsonr   r   Zkombu.utils.objectsr    r   baser   r   r   r   r/   r   r   r   r   r   <module>   s(    /  |