o
    wvXj"  ã                   @   s„   d 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 zddlZW n ey3   dZY nw d	Zd
ZG dd„ deƒZdS )zCouchDB result store backend.é    )Úabsolute_importÚunicode_literals)Úbytes_to_str)Ú
_parse_url)ÚImproperlyConfiguredé   )ÚKeyValueStoreBackendN)ÚCouchBackendzKYou need to install the pycouchdb library to use the CouchDB result backendc                       sn   e Zd ZdZdZdZdZdZdZdZ	d‡ fdd„	Z
d	d
„ Zedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )r	   zŒCouchDB backend.

    Raises:
        celery.exceptions.ImproperlyConfigured:
            if module :pypi:`pycouchdb` is not available.
    ÚdefaultÚhttpÚ	localhosti`  Nc                    sÆ   t t| ƒj|i |¤Ž || _td u rttƒ‚d  } } } } }}	|r8t|ƒ\}
}}}}}	}
|	r6|	 d¡nd }	|p<| j	| _	|pB| j
| _
t|pI| jƒ| _|	pP| j| _|pV| j| _|p\| j| _d | _d S )Nú/)Úsuperr	   Ú__init__ÚurlÚ	pycouchdbr   ÚERR_LIB_MISSINGr   ÚstripÚschemeÚhostÚintÚportÚ	containerÚusernameÚpasswordÚ_connection)Úselfr   ÚargsÚkwargsÚuschemeÚuhostÚuportÚunameÚupassÚ
ucontainerÚ_©Ú	__class__© úT/var/www/html/myproject/venv/lib/python3.10/site-packages/celery/backends/couchdb.pyr   '   s   
zCouchBackend.__init__c                 C   s�   | j r| jrd| j| j | j| jt| jƒf }tj|dd�}nd| j| jt| jƒf }t |¡}z| | j	¡W S  tj
jyG   | | j	¡ Y S w )zConnect to the CouchDB server.z%s://%s:%s@%s:%sÚbasic)Ú
authmethodz
%s://%s:%s)r   r   r   r   Ústrr   r   ÚServerÚdatabaser   Ú
exceptionsÚNotFoundÚcreate)r   Úconn_stringÚserverr(   r(   r)   Ú_get_connection<   s   þÿ
ÿzCouchBackend._get_connectionc                 C   s   | j d u r
|  ¡ | _ | j S ©N)r   r4   ©r   r(   r(   r)   Ú
connectionM   s   

zCouchBackend.connectionc                 C   s,   z	| j  |¡d W S  tjjy   Y d S w )NÚvalue)r7   Úgetr   r/   r0   ©r   Úkeyr(   r(   r)   r9   S   s
   ÿzCouchBackend.getc                 C   s^   t |ƒ}||dœ}z	| j |¡ W d S  tjjy.   | j |¡}||d< | j |¡ Y d S w )N)Ú_idr8   r8   )r   r7   Úsaver   r/   ÚConflictr9   )r   r;   r8   ÚstateÚdatar(   r(   r)   ÚsetY   s   
üzCouchBackend.setc                    s   ‡ fdd„|D ƒS )Nc                    s   g | ]}ˆ   |¡‘qS r(   )r9   )Ú.0r;   r6   r(   r)   Ú
<listcomp>e   s    z%CouchBackend.mget.<locals>.<listcomp>r(   )r   Úkeysr(   r6   r)   Úmgetd   s   zCouchBackend.mgetc                 C   s   | j  |¡ d S r5   )r7   Údeleter:   r(   r(   r)   rF   g   s   zCouchBackend.deleter5   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r4   Úpropertyr7   r9   rA   rE   rF   Ú__classcell__r(   r(   r&   r)   r	      s     
r	   )rJ   Ú
__future__r   r   Úkombu.utils.encodingr   Úkombu.utils.urlr   Úcelery.exceptionsr   Úbaser   r   ÚImportErrorÚ__all__r   r	   r(   r(   r(   r)   Ú<module>   s   ÿ