U
    .eA                     @   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
 W n ek
r`   d Z
ZY nX zdd	lmZ W n ek
r   dZY nX d
ZG dd deZdS )zCouchbase result store backend.    )
_parse_url)ImproperlyConfigured   )KeyValueStoreBackend)PasswordAuthenticator)ClusterN)FMT_AUTO)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-f4liivr4/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_connectionM   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   d   s    zCouchbaseBackend.getc                 C   s8   t d k	r | jj||| jt d n| jj||| jd d S )N)ttlformat)r0   )r   r,   Zupsertexpires)r   r/   valuer%   r%   r&   setg   s    zCouchbaseBackend.setc                 C   s   | j |S r+   )r,   Z	get_multi)r   keysr%   r%   r&   mgetn   s    zCouchbaseBackend.mgetc                 C   s   | j | d S r+   )r,   remover.   r%   r%   r&   deleteq   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   ImportErrorZcouchbase_core._libcouchbaser   __all__r	   r%   r%   r%   r&   <module>   s   
