o
    wvXjz  ã                   @   s¼   d Z ddlmZmZ ddlZddl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 d	d
lmZ zddlmZ ddlmZ W n eyQ   d ZZY nw dZG dd„ deƒZdS )zArangoDb result store backend.é    )Úabsolute_importÚunicode_literalsN)Ú	timedelta)Ústr_t)Úcached_property)Ú
_parse_url)ÚImproperlyConfiguredé   )ÚKeyValueStoreBackend)Ú
connection)ÚAQLQueryError)ÚArangoDbBackendc                       sŽ   e Zd ZdZdZdZdZdZdZdZ	dZ
eZd‡ fdd„	Zed	d
„ ƒZedd„ ƒZedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )r   aõ  ArangoDb backend.

    Sample url
    "arangodb://username:password@host:port/database/collection"
    *arangodb_backend_settings* is where the settings are present
    (in the app.conf)
    Settings should contain the host, port, username, password, database name,
    collection name else the default will be chosen.
    Default database name and collection name is celery.

    Raises
    ------
    celery.exceptions.ImproperlyConfigured:
        if module :pypi:`pyArango` is not available.

    z	127.0.0.1Ú8529ÚceleryNÚhttpc                    sb  t t| ƒj|i |¤Ž tdu rtdƒ‚|| _|du r'd } } } } }}	nt|ƒ\}
}}}}	}}|du r;d }}n| d¡\}}| jj	 
dd¡}|durXt|tƒsWtdƒ‚ni }|pb| 
d| j¡| _t|pm| 
d| j¡ƒ| _| 
d| j¡| _|p€| 
d	| j¡| _|pŠ| 
d
| j¡| _|p”| 
d| j¡| _|	pž| 
d| j¡| _dj| j| j| jd�| _d| _dS )z8Parse the url or load the settings from settings object.NzEYou need to install the pyArango library to use the ArangoDb backend.ú/Úarangodb_backend_settingsz5ArangoDb backend settings should be grouped in a dictÚhostÚportÚhttp_protocolÚdatabaseÚ
collectionÚusernameÚpasswordz{http_protocol}://{host}:{port})r   r   r   )Úsuperr   Ú__init__Úpy_arango_connectionr   Úurlr   ÚsplitÚappÚconfÚgetÚ
isinstanceÚdictr   Úintr   r   r   r   r   r   ÚformatÚarangodb_urlÚ_connection)Úselfr   ÚargsÚkwargsr   r   r   r   r   r   Ú_schemaÚdatabase_collectionÚ_queryÚconfig©Ú	__class__© úU/var/www/html/myproject/venv/lib/python3.10/site-packages/celery/backends/arangodb.pyr   ;   sF   ÿý


ÿÿÿÿ
zArangoDbBackend.__init__c                 C   s(   | j du rtj| j| j| jd�| _ | j S )zConnect to the arangodb server.N)Ú	arangoURLr   r   )r'   r   Ú
Connectionr&   r   r   ©r(   r1   r1   r2   r   i   s   
þzArangoDbBackend.connectionc                 C   s   | j | j S )z&Database Object to the given database.)r   r   r5   r1   r1   r2   Údbs   s   zArangoDbBackend.dbc                 C   s   t | jd�S )N)Úseconds)r   Úexpiresr5   r1   r1   r2   Úexpires_deltax   s   zArangoDbBackend.expires_deltac              
   C   s¶   z+t  dj| j|d�¡ | j dj| j|d�¡}|jd d }|d u r&W d S t |¡W S  t	yC } zt  
|¡ W Y d }~d S d }~w tyZ } zt  
|¡ W Y d }~d S d }~ww )Nz*RETURN DOCUMENT("{collection}/{key}").task)r   ÚkeyÚresultr   )ÚloggingÚdebugr%   r   r6   ÚAQLQueryÚresponseÚjsonÚdumpsr   ÚerrorÚ	Exception)r(   r:   Úqueryr;   Úaql_errÚerrr1   r1   r2   r!   |   s0   ÿÿÿÿ
€
€þzArangoDbBackend.getc              
   C   s˜   zt  dj| j||d�¡ | j dj| j||d�¡ W dS  ty4 } zt  |¡ W Y d}~dS d}~w tyK } zt  |¡ W Y d}~dS d}~ww )z<Insert a doc with value into task attribute and _key as key.z:INSERT {{ task: {task}, _key: "{key}" }} INTO {collection})r   r:   ÚtaskN©	r<   r=   r%   r   r6   r>   r   rB   rC   )r(   r:   ÚvalueÚstaterE   rF   r1   r1   r2   Úset“   s(   þÿþ
ÿ€€ÿzArangoDbBackend.setc              
   C   sò   z,t  |¡}t dj| j|d�¡ | j dj| j|d�¡}g }	 | |j	d ¡ | 
¡  q  ty>   dd„ |D ƒ}| Y S  ty[ } zt |¡ d gt|ƒ W  Y d }~S d }~w tyx } zt |¡ d gt|ƒ W  Y d }~S d }~ww )Nzy
                FOR key in {keys}
                    RETURN DOCUMENT(CONCAT("{collection}/", key).task
                )r   ÚkeysTr;   c                 S   s"   g | ]}|d u r
|nt  |¡‘qS ©N)r@   rA   )Ú.0r;   r1   r1   r2   Ú
<listcomp>¿   s    ÿÿz(ArangoDbBackend.mget.<locals>.<listcomp>)r@   rA   r<   r=   r%   r   r6   r>   Úextendr?   Ú	nextBatchÚStopIterationr   rB   ÚlenrC   )r(   rL   Ú	json_keysrD   ÚresultsÚvaluesrE   rF   r1   r1   r2   Úmget§   sB   
üÿüÿþþ
€
€þzArangoDbBackend.mgetc              
   C   s”   zt  dj|| jd�¡ | j dj|| jd�¡ W d S  ty2 } zt  |¡ W Y d }~d S d }~w tyI } zt  |¡ W Y d }~d S d }~ww )Nz*REMOVE {{ _key: "{key}" }} IN {collection})r:   r   rH   )r(   r:   rE   rF   r1   r1   r2   ÚdeleteË   s$   ÿÿÿ
ÿ€€ÿzArangoDbBackend.deletec              
   C   s    | j  ¡ | j  ¡ }zdj| j|d�}t |¡ | j 	|¡ W dS  t
y8 } zt |¡ W Y d}~dS d}~w tyO } zt |¡ W Y d}~dS d}~ww )zDelete expired meta-data.zcFOR item IN {collection} FILTER item.task.date_done < "{remove_before}" REMOVE item IN {collection})r   Úremove_beforeN)r   Únowr9   Ú	isoformatr%   r   r<   r=   r6   r>   r   rB   rC   )r(   rY   rD   rE   rF   r1   r1   r2   ÚcleanupÜ   s   ü
€€ÿzArangoDbBackend.cleanuprM   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   Úkey_tr   Úpropertyr   r6   r   r9   r!   rK   rW   rX   r\   Ú__classcell__r1   r1   r/   r2   r      s,    .
	

$r   )r`   Ú
__future__r   r   r@   r<   Údatetimer   Úkombu.utils.encodingr   Úkombu.utils.objectsr   Úkombu.utils.urlr   Úcelery.exceptionsr   Úbaser
   ÚpyArangor   r   ÚpyArango.theExceptionsr   ÚImportErrorÚ__all__r   r1   r1   r1   r2   Ú<module>   s$   ÿ