o
    DfV<                     @  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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)maybe_sanitize_url   )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   P/home/ubuntu/webapp/venv/lib/python3.10/site-packages/kombu/transport/mongodb.py__init__C   s   zBroadcastCursor.__init__c                 C  s   | j ji | j S N)r   
collectioncount_documentsr   r   r   r   r   get_sizeH   s   zBroadcastCursor.get_sizec                 C  s   | j   d S r   )r   closer    r   r   r   r"   K   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   N   s   
zBroadcastCursor.purgec                 C  s   | S r   r   r    r   r   r   __iter__V   s   zBroadcastCursor.__iter__c              
   C  sd   	 zt | j}W n tjjy' } zdt|v r"|   W Y d }~q  d }~ww 	 |  jd7  _|S )NTznot valid at serverr   )nextr   pymongor   OperationFailurestrr   r   )r   msgexcr   r   r   __next__Y   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!dGd)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 )HChannelzMongoDB Channel.TFNi z	127.0.0.1ii  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 | i | _| j d S r   )superr   _broadcast_cursorsclient)r   vargskwargs	__class__r   r   r      s   
zChannel.__init__c              	   K  s8   | j r| jjd|id||| |ddidd d S d S )N_id$set	x-expires)rF   options	expire_atTupsert)r5   queues
update_one_get_queue_expire)r   queuerC   r   r   r   
_new_queue   s   	
zChannel._new_queuec                 C  s~   || j v rz	t| |}W n ty   d }Y nw | jjd|idtjfgd}| jr0| 	| |d u r7t
 tt|d S )NrP   priority)sortpayload)_fanout_queuesr%   _get_broadcast_cursorStopIterationr2   find_one_and_deleter&   	ASCENDINGr5   _update_queues_expirer   r   r
   )r   rP   r)   r   r   r   _get   s   


zChannel._getc                   s:   | j s	t |S || jv r| | S | jd|iS NrP   )r>   r?   _sizerU   rV   r!   r2   r   r   rP   rD   r   r   r]      s
   
zChannel._sizec                 K  sr   t ||| j|ddd}| jr1| |d|d< | |}|d ur1|d d u s-||d k r1||d< | j| d S )NT)reverse)rT   rP   rR   zx-message-ttlrJ   )r   _get_message_priorityr5   rO   _get_message_expirer2   
insert_one)r   rP   messagerC   data
msg_expirer   r   r   _put   s   
zChannel._putc                 K  s   | j t||d d S )N)rT   rP   )	broadcastrb   r   )r   exchangerc   routing_keyrC   r   r   r   _put_fanout   s   zChannel._put_fanoutc                 C  s:   |  |}|| jv r| |  |S | jd|i |S r\   )r]   rU   rV   r   r2   delete_many)r   rP   sizer   r   r   _purge   s   

zChannel._purgec                 C  s:   t | jj| d }| jd|i}|t dd |D B S )Ntablerh   c                 s  s&    | ]}|d  |d |d fV  qdS )ri   patternrP   Nr   ).0rr   r   r   	<genexpr>   s
    
z$Channel.get_table.<locals>.<genexpr>)	frozensetstate	exchangesroutingfind)r   rh   localRoutesbrokerRoutesr   r   r   	get_table   s   

zChannel.get_tablec                 C  sp   |  |jdkr| |||| || j|< ||||d}| }| jr+| |d|d< | jj|d|idd d S )Nfanout)rh   rP   ri   ro   rH   rJ   rG   TrK   )	typeoftype_create_broadcast_cursorrU   copyr5   rO   rv   rN   )r   rh   ri   ro   rP   lookuprd   r   r   r   _queue_bind   s   
zChannel._queue_bindc                   s   | j d|i | jr| jd|i t j|fi | || jv rAz| j	|}W n
 t
y4   Y d S w |  | j	| d S d S )NrP   rF   )rv   rk   r5   rM   
delete_oner?   queue_deleterU   r@   popKeyErrorr"   )r   rP   rC   r   rD   r   r   r     s   
zChannel.queue_delete
mongodb://c                 C  s  | j j}|j}||s|| }|t|d  s|| j7 }|jrBd|vrB|d\}}|j}|jr8|d|j 7 }|d | d | }|j	rH|j	n| j
}t||}|d pW|j}	|	dv r_| j}	d| j| jrlt| jd nd d}
|
|d	  | |
}
||	|
fS )
N@z://:database)/NTi  )auto_start_requestr4   connectTimeoutMSrI   )
connectionrA   hostname
startswithlenr7   useridsplitpasswordportr8   r   	parse_urivirtual_hostr9   r4   r3   intupdate_prepare_client_options)r   schemerA   r   headtailcredentialsr   parseddbnamerI   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   readpreference)r&   version_tupler   
isinstancegetr   read_preferences_MONGOS_MODES)r   rI   modesr   r   r   r   B  s   
zChannel._prepare_client_optionsc                 K  s   t |fi |S r   r   )r   	argumentsrC   r   r   r   prepare_queue_argumentsJ  s   zChannel.prepare_queue_argumentsc                 C  s   | j |d\}}}||d< t }|dkrddlm} |  n|dkr,ddlm} |  tdi |}|| }	| d }
|
	d	d }
t
tt|
	d
}|dk rYtt|
| jrg|dk rgtt|
|	S )N)r   hostgeventr   )monkeyeventlet)monkey_patchversion-.)r   r   )   r   r   )r   r	   r   r   	patch_allr   r   r   server_infor   tuplemapr   r   E_SERVER_VERSIONformatr5   E_NO_TTL_INDEXES)r   r   r   r   confenvr   r   	mongoconnr   version_strr   r   r   r   _openM  s&   
zChannel._openc                 C  s*   | j | v r	dS |j| j | jdd dS )z0Create capped collection for broadcast messages.NT)rl   capped)r<   list_collection_namescreate_collectionr6   r   r   r   r   r   _create_broadcasth  s   
zChannel._create_broadcastc                 C  s   || j  }|jg ddd || j dg || j }|ddg | jrC|jdgdd |jdgdd || j jdgdd d	S d	S )
zEnsure indexes on collections.)rP   r   )rR   r   )rF   r   T)
backgroundr   )rh   r   )rJ   r   r   )expireAfterSecondsN)r:   create_indexr<   r;   r5   r=   )r   r   r2   rv   r   r   r   _ensure_indexesq  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   rA     s   zChannel.clientc                 C     | j | j S r   )rA   r:   r    r   r   r   r2        zChannel.messagesc                 C  r   r   )rA   r;   r    r   r   r   rv     r   zChannel.routingc                 C  r   r   )rA   r<   r    r   r   r   rg     r   zChannel.broadcastc                 C  r   r   )rA   r=   r    r   r   r   rM     r   zChannel.queuesc              	   C  s6   z| j | W S  ty   | | j| d d | Y S w r   )r@   r   r~   rU   r^   r   r   r   rV     s   zChannel._get_broadcast_cursorc                 C  sR   t jdkrd|itjd}nd|idd}| jjdi |}t| }| j|< |S )Nr   rP   )filtercursor_typeT)querytailabler   )r&   r   r   TAILABLErg   rw   r   r@   )r   rh   ri   ro   rP   r   r   retr   r   r   r~     s   
z Channel._create_broadcast_cursorc                 C  s6   | di  d}|d ur|  tjt|d S d S )N
properties
expirationmilliseconds)r   get_nowdatetime	timedeltar   )r   rc   valuer   r   r   ra     s   zChannel._get_message_expirec              	   C  sn   t |tr| jd|i}|sdS |d }n|}z|d | }W n ttfy,   Y dS w |  tj|d S )zGet expiration header named `argument` of queue definition.

        Note:
        ----
            `queue` must be either queue name or options itself.
        rF   NrI   r   r   )	r   r(   rM   find_oner   	TypeErrorr   r   r   )r   rP   argumentdocrd   r   r   r   r   rO     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.rH   NrP   rG   rJ   rF   )rO   rv   update_manyrM   )r   rP   rJ   r   r   r   rZ     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)r   )3r,   r-   r.   r/   supports_fanoutrU   r4   r5   r3   r6   r>   r7   r8   r9   r:   r;   r<   r=   r   r0   from_transport_optionsr   rQ   r[   r]   rf   rj   rm   rz   r   r   r   r   r   r   r   r   r   r   rA   r2   rv   rg   rM   rV   r~   ra   rO   rZ   r   __classcell__r   r   rD   r   r0   o   sd    


)
	




r0   c                   @  sz   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g ddZdd	 ZddddZdS )	TransportzMongoDB Transport.Tr   mongodbr&   )directtopicr{   )exchange_typec                 C  s   t jS r   )r&   r   r    r   r   r   driver_version   s   zTransport.driver_versionF**urir(   returnc                 C  sB   |sdS |r|S d|vrt |S |dd\}}dt ||gS )Nr   ,r   )r   r   join)r   r   include_passwordmaskuri1	remainderr   r   r   as_uri  s   zTransport.as_uriN)Fr   )r   r(   r   r(   )r,   r-   r.   r/   r0   can_parse_urlpolling_intervalr8   r   r   connection_errorsr   ConnectionFailurechannel_errorsr'   driver_typedriver_name
implementsextendrs   r   r   r   r   r   r   r     s(    
r   )"r/   
__future__r   r   rP   r   r&   r   r   r   pymongo.cursorr   kombu.exceptionsr   kombu.utils.compatr	   kombu.utils.encodingr
   kombu.utils.jsonr   r   kombu.utils.objectsr   kombu.utils.urlr    r   baser   r   r   r   r0   r   r   r   r   r   <module>   s,    /  |