U
    de                     @   s   d 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
mZ ddlmZ W n  ek
rt   d	 Z
 ZZY nX d
ZG dd deZd	S )zCouchbase result store backend.    )
_parse_url)ImproperlyConfigured   )KeyValueStoreBackend)PasswordAuthenticator)ClusterClusterOptions)FMT_AUTON)CouchbaseBackendc                       sz   e Zd 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dd Zedd Zdd Zdd Zdd Zdd Z  ZS )r
   zCouchbase backend.

    Raises:
        celery.exceptions.ImproperlyConfigured:
            if module :pypi:`couchbase` is not available.
    default	localhosti  NFTg      @c                    s  | dt t j|| || _td kr0tdd  } } } }}|rpt|\}	}}}}}}	|rl|dnd }| j	j
dd }
|
d k	rt|
tstdni }
|p|
d| j| _t|p|
d| j| _|p|
d| j| _|p|
d	| j| _|p|
d
| j| _d | _d S )NZexpires_typezGYou need to install the couchbase library to use the Couchbase backend./Zcouchbase_backend_settingsz6Couchbase backend settings should be grouped in a dicthostportbucketusernamepassword)
setdefaultintsuper__init__urlr   r   r   stripZappconfget
isinstancedictr   r   r   r   r   _connection)selfr   argskwargsZuhostZuportunameZupassZubucket_config	__class__ =/tmp/pip-unpacked-wheel-mu1yl971/celery/backends/couchbase.pyr   (   s0    
zCouchbaseBackend.__init__c                 C   s~   | j dkrx| jr,| jr,d| j d| j }nd| j }| jrT| jrTt| j| j}nd}t||}|| j}| | _ | j S )z Connect to the Couchbase server.Nzcouchbase://:)	r   r   r   r   r   r   r   r   Zdefault_collection)r   urioptZclusterr   r&   r&   r'   _get_connectionI   s    


z CouchbaseBackend._get_connectionc                 C   s   |   S N)r+   )r   r&   r&   r'   
connection\   s    zCouchbaseBackend.connectionc                 C   s   | j |jS r,   )r-   r   contentr   keyr&   r&   r'   r   `   s    zCouchbaseBackend.getc                 C   s   | j j||| jtd d S )N)Zttlformat)r-   Zupsertexpiresr	   )r   r0   valuer&   r&   r'   setc   s    zCouchbaseBackend.setc                 C   s   | j |S r,   )r-   Z	get_multi)r   keysr&   r&   r'   mgetf   s    zCouchbaseBackend.mgetc                 C   s   | j | d S r,   )r-   remover/   r&   r&   r'   deletei   s    zCouchbaseBackend.delete)N)__name__
__module____qualname____doc__r   r   r   r   r   quietZsupports_autoexpiretimeoutstrZkey_tr   r+   propertyr-   r   r4   r6   r8   __classcell__r&   r&   r$   r'   r
      s$   !
r
   )r<   Zkombu.utils.urlr   Zcelery.exceptionsr   baser   Zcouchbase.authr   Zcouchbase.clusterr   r   Zcouchbase_core._libcouchbaser	   ImportError__all__r
   r&   r&   r&   r'   <module>   s   