U
    P’“e’#  ã                   @   sV   d Z ddlmZ ddlmZ ddlmZ dZG dd„ deƒZ	dekrRe 
e	jd	¡ d
S )a´  
Cache manager
=============

The cache manager can be used to store python objects attached to a unique
key. The cache can be controlled in two ways: with a object limit or a
timeout.

For example, we can create a new cache with a limit of 10 objects and a
timeout of 5 seconds::

    # register a new Cache
    Cache.register('mycache', limit=10, timeout=5)

    # create an object + id
    key = 'objectid'
    instance = Label(text=text)
    Cache.append('mycache', key, instance)

    # retrieve the cached object
    instance = Cache.get('mycache', key)

If the instance is NULL, the cache may have trashed it because you've
not used the label for 5 seconds and you've reach the limit.
é    )Úenviron)ÚLogger)ÚClock)ÚCachec                   @   s’   e Zd ZdZi Zi Zeddd„ƒZeddd„ƒZeddd„ƒZ	edd	d
„ƒZ
eddd„ƒZeddd„ƒZeddd„ƒZedd„ ƒZedd„ ƒZdS )r   z3See module documentation for more information.
    Nc                 C   s:   ||dœt j| < i t j| < t d| t|ƒt|ƒf ¡ dS )aß  Register a new category in the cache with the specified limit.

        :Parameters:
            `category`: str
                Identifier of the category.
            `limit`: int (optional)
                Maximum number of objects allowed in the cache.
                If None, no limit is applied.
            `timeout`: double (optional)
                Time after which to delete the object if it has not been used.
                If None, no timeout is applied.
        )ÚlimitÚtimeoutz.Cache: register <%s> with limit=%s, timeout=%sN)r   Ú_categoriesÚ_objectsr   ÚdebugÚstr)Úcategoryr   r   © r   ú./tmp/pip-unpacked-wheel-xzebddm3/kivy/cache.pyÚregister)   s    þ
ÿÿzCache.registerc                 C   s°   t |ddƒrdS |dkr tdƒ‚ztj|  }W n$ tk
rR   t d|  ¡ Y dS X |p^|d }|d }|dk	rŒttj|  ƒ|krŒt 	| ¡ ||t
 ¡ t
 ¡ dœtj|  |< dS )	aj  Add a new object to the cache.

        :Parameters:
            `category`: str
                Identifier of the category.
            `key`: str
                Unique identifier of the object to store.
            `obj`: object
                Object to store in cache.
            `timeout`: double (optional)
                Time after which to delete the object if it has not been used.
                If None, no timeout is applied.

        :raises:
            `ValueError`: If `None` is used as `key`.

        .. versionchanged:: 2.0.0
            Raises `ValueError` if `None` is used as `key`.

        Z_nocacheFNz%"None" cannot be used as key in Cachez#Cache: category <%s> does not existr   r   )Úobjectr   Ú
lastaccessÚ	timestamp)ÚgetattrÚ
ValueErrorr   r   ÚKeyErrorr   ÚwarningÚlenr	   Ú_purge_oldestr   Úget_time)r   ÚkeyÚobjr   Úcatr   r   r   r   Úappend?   s$    
üzCache.appendc                 C   sH   z*t  ¡ tj|  | d< tj|  | d W S  tk
rB   | Y S X dS )aP  Get a object from the cache.

        :Parameters:
            `category`: str
                Identifier of the category.
            `key`: str
                Unique identifier of the object in the store.
            `default`: anything, defaults to None
                Default value to be returned if the key is not found.
        r   r   N)r   r   r   r	   Ú	Exception©r   r   Údefaultr   r   r   Úgets   s
    z	Cache.getc                 C   s2   zt j|  | d W S  tk
r,   | Y S X dS )aZ  Get the object timestamp in the cache.

        :Parameters:
            `category`: str
                Identifier of the category.
            `key`: str
                Unique identifier of the object in the store.
            `default`: anything, defaults to None
                Default value to be returned if the key is not found.
        r   N©r   r	   r   r   r   r   r   Úget_timestamp…   s    zCache.get_timestampc                 C   s2   zt j|  | d W S  tk
r,   | Y S X dS )ab  Get the objects last access time in the cache.

        :Parameters:
            `category`: str
                Identifier of the category.
            `key`: str
                Unique identifier of the object in the store.
            `default`: anything, defaults to None
                Default value to be returned if the key is not found.
        r   Nr"   r   r   r   r   Úget_lastaccess–   s    zCache.get_lastaccessc                 C   s^   zD|dk	r*t j|  |= t d| |f ¡ ni t j| < t d|  ¡ W n tk
rX   Y nX dS )a-  Purge the cache.

        :Parameters:
            `category`: str
                Identifier of the category.
            `key`: str (optional)
                Unique identifier of the object in the store. If this
                argument is not supplied, the entire category will be purged.
        NzCache: Removed %s:%s from cachez%Cache: Flushed category %s from cache)r   r	   r   Útracer   )r   r   r   r   r   Úremove§   s    ÿ
ÿ
zCache.removeé   c           	      C   sò   t  d|  ¡ dd l}t ¡ }g }tj|  D ]Z}tj|  | }|d |d   krZ|kr`q, nq,| ||d |f¡ t  d|d  ¡ q,d}||krîz4|d7 }| |¡\}}t  d|||t ¡ f ¡ W n t	k
rÞ   Y d S X t 
| |¡ qŒd S )NzCache: Remove oldest in %sr   r   r   zCache: <<< %fr'   zCache: %d => %s %f %f)r   r%   Úheapqr   r   r   r	   ÚheappushÚheappopr   r&   )	r   Zmaxpurger(   ÚtimeZ	heap_listr   r   Únr   r   r   r   r   ¾   s*     ÿ
zCache._purge_oldestc                 C   sÖ   t  ¡ }tjD ]Â}|tjkrqtj| d }|d k	rT| |krT|d9 }|tj| d< qttj|  ¡ ƒD ]h}tj| | d }tj| | d }|d k	rš|}|d kr¤qf|| |krft d||f ¡ t 	||¡ qfqd S )Nr   é   r   z.Cache: Removed %s:%s from cache due to timeout)
r   r   r   r	   r   ÚlistÚkeysr   r%   r&   )ÚdtZcurtimer   r   r   r   Z
objtimeoutr   r   r   Ú_purge_by_timeoutÕ   s*    

ÿzCache._purge_by_timeoutc               	   C   sV   t dƒ tjD ]B} t d|  ¡ ttj|  ƒttj|  d ƒttj|  d ƒf ƒ qdS )z%Print the cache usage to the console.zCache usage :z * %s : %d / %s, timeout=%sr   r   N)Úprintr   r   Ú
capitalizer   r	   r   )r   r   r   r   Úprint_usageø   s    
üzCache.print_usage)NN)N)N)N)N)N)r'   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   Ústaticmethodr   r   r!   r#   r$   r&   r   r1   r4   r   r   r   r   r   "   s*   3
"r   ZKIVY_DOC_INCLUDEr'   N)r8   Úosr   Zkivy.loggerr   Z
kivy.clockr   Ú__all__r   r   Zschedule_intervalr1   r   r   r   r   Ú<module>   s    c