o
    Dfw                     @   st   d 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
y+   dZ	Y nw dZd	ZG d
d deZdS )zCouchDB result store backend.    )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   zCouchDB backend.

    Raises:
        celery.exceptions.ImproperlyConfigured:
            if module :pypi:`pycouchdb` is not available.
    defaulthttp	localhosti`  Nc                    s   t  j|i | || _td u rttd  } } } } }}	|r6t|\}
}}}}}	}
|	r4|	dnd }	|p:| j| _|p@| j	| _	t
|pG| j| _|	pN| j| _|pT| j| _|pZ| j| _d | _d S )N/)super__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__ P/home/ubuntu/webapp/venv/lib/python3.10/site-packages/celery/backends/couchdb.pyr   $   s   
zCouchBackend.__init__c              	   C   s   | j r#| jr#| j d| j  d| j d| j d| j 	}tj|dd}n| j d| j d| j }t|}z|| jW S  tj	j
yM   || j Y S w )zConnect to the CouchDB server.z://:@basic)
authmethod)r   r   r   r   r   r   Serverdatabaser   
exceptionsNotFoundcreate)r   conn_stringserverr&   r&   r'   _get_connection9   s   *
zCouchBackend._get_connectionc                 C   s   | j d u r
|  | _ | j S N)r   r3   r   r&   r&   r'   
connectionG   s   

zCouchBackend.connectionc                 C   s4   t |}z	| j|d W S  tjjy   Y d S w )Nvalue)r   r6   getr   r.   r/   r   keyr&   r&   r'   r8   M   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)_idr7   r7   )r   r6   saver   r.   Conflictr8   )r   r:   r7   datar&   r&   r'   setT   s   
zCouchBackend.setc                    s    fdd|D S )Nc                    s   g | ]}  |qS r&   )r8   ).0r:   r5   r&   r'   
<listcomp>`   s    z%CouchBackend.mget.<locals>.<listcomp>r&   )r   keysr&   r5   r'   mget_   s   zCouchBackend.mgetc                 C   s   | j | d S r4   )r6   deleter9   r&   r&   r'   rD   b   s   zCouchBackend.deleter4   )__name__
__module____qualname____doc__r   r   r   r   r   r   r   r3   propertyr6   r8   r?   rC   rD   __classcell__r&   r&   r$   r'   r      s     
r   )rH   kombu.utils.encodingr   kombu.utils.urlr   celery.exceptionsr   baser   r   ImportError__all__r   r   r&   r&   r&   r'   <module>   s    