3
_½µ_ä  ã               @   s®   d Z dZdZdZddlmZ ddlmZ ddlZyddlZW n e	e
fk
rX   dZY nX G dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZeZeZG dd„ deƒZeZeZdS )zDaniel Greenfeldzpydanny@gmail.comz1.5.2ZBSDé    )Úwraps)ÚtimeNc               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Úcached_propertya   
    A property that is only computed once per instance and then replaces itself
    with an ordinary attribute. Deleting the attribute resets the property.
    Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76
    c             C   s   t |dƒ| _|| _d S )NÚ__doc__)Úgetattrr   Úfunc)Úselfr   © r	   ú</tmp/pip-install-xpigntgx/cached-property/cached_property.pyÚ__init__   s    zcached_property.__init__c             C   sB   |d kr| S t r&t j| jƒr&| j|ƒS | j|ƒ }|j| jj< |S )N)ÚasyncioÚiscoroutinefunctionr   Ú_wrap_in_coroutineÚ__dict__Ú__name__)r   ÚobjÚclsÚvaluer	   r	   r
   Ú__get__   s    
zcached_property.__get__c                s"   t ˆ ƒtj‡ ‡fdd„ƒƒ}|ƒ S )Nc                 s"   t jˆjˆ ƒƒ} | ˆ jˆjj< | S )N)r   Zensure_futurer   r   r   )Úfuture)r   r   r	   r
   Úwrapper(   s    z3cached_property._wrap_in_coroutine.<locals>.wrapper)r   r   Ú	coroutine)r   r   r   r	   )r   r   r
   r   '   s    z"cached_property._wrap_in_coroutineN)r   Ú
__module__Ú__qualname__r   r   r   r   r	   r	   r	   r
   r      s   
r   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )Úthreaded_cached_propertyz…
    A cached_property version for use in environments where multiple threads
    might concurrently try to access the property.
    c             C   s    t |dƒ| _|| _tjƒ | _d S )Nr   )r   r   r   Ú	threadingÚRLockÚlock)r   r   r	   r	   r
   r   8   s    z!threaded_cached_property.__init__c             C   s\   |d kr| S |j }| jj}| j�2 y|| S  tk
rL   |j|| j|ƒƒS X W d Q R X d S )N)r   r   r   r   ÚKeyErrorÚ
setdefault)r   r   r   Úobj_dictÚnamer	   r	   r
   r   =   s    z threaded_cached_property.__get__N)r   r   r   r   r   r   r	   r	   r	   r
   r   2   s   r   c               @   sB   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )Úcached_property_with_ttlz×
    A property that is only computed once per instance and then replaces itself
    with an ordinary attribute. Setting the ttl to a number expresses how long
    the property will last before being timed out.
    Nc             C   s*   t |ƒr|}d }nd }|| _| j|ƒ d S )N)ÚcallableÚttlÚ_prepare_func)r   r$   r   r	   r	   r
   r   T   s    z!cached_property_with_ttl.__init__c             C   s   | j |ƒ | S )N)r%   )r   r   r	   r	   r
   Ú__call__]   s    
z!cached_property_with_ttl.__call__c       	      C   sz   |d kr| S t ƒ }|j}| j}y|| \}}W n tk
rB   Y nX | joV| j|| k }|s`|S | j|ƒ}||f||< |S )N)r   r   r   r   r$   r   )	r   r   r   Únowr    r!   r   Zlast_updatedZttl_expiredr	   r	   r
   r   a   s    
z cached_property_with_ttl.__get__c             C   s   |j j| jd ƒ d S )N)r   Úpopr   )r   r   r	   r	   r
   Ú
__delete__u   s    z#cached_property_with_ttl.__delete__c             C   s   |t ƒ f|j| j< d S )N)r   r   r   )r   r   r   r	   r	   r
   Ú__set__x   s    z cached_property_with_ttl.__set__c             C   s&   || _ |r"|j| _|j| _|j| _d S )N)r   r   r   r   )r   r   r	   r	   r
   r%   {   s
    z&cached_property_with_ttl._prepare_func)N)
r   r   r   r   r   r&   r   r)   r*   r%   r	   r	   r	   r
   r"   M   s   
	r"   c                   s.   e Zd ZdZd‡ fdd„	Z‡ fdd„Z‡  ZS )Ú!threaded_cached_property_with_ttlz…
    A cached_property version for use in environments where multiple threads
    might concurrently try to access the property.
    Nc                s   t t| ƒj|ƒ tjƒ | _d S )N)Úsuperr+   r   r   r   r   )r   r$   )Ú	__class__r	   r
   r   Ž   s    z*threaded_cached_property_with_ttl.__init__c          
      s$   | j � tt| ƒj||ƒS Q R X d S )N)r   r,   r+   r   )r   r   r   )r-   r	   r
   r   ’   s    z)threaded_cached_property_with_ttl.__get__)N)r   r   r   r   r   r   Ú__classcell__r	   r	   )r-   r
   r+   ˆ   s   r+   )Ú
__author__Ú	__email__Ú__version__Ú__license__Ú	functoolsr   r   r   r   ÚImportErrorÚSyntaxErrorÚobjectr   r   r"   Zcached_property_ttlZtimed_cached_propertyr+   Zthreaded_cached_property_ttlZtimed_threaded_cached_propertyr	   r	   r	   r
   Ú<module>   s$   
 7