U
    çZßf  ã                   @   s2   d Z ddlZddlZddlZg ZG dd„ dƒZdS )z,
ftp_stat_cache.py - cache for (l)stat data
é    Nc                   @   st   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Ú	StatCacheaZ  
    Implement an LRU (least-recently-used) cache.

    `StatCache` objects have an attribute `max_age`. After this duration after
    _setting_ it a cache entry will expire. For example, if you code

    my_cache = StatCache()
    my_cache.max_age = 10
    my_cache["/home"] = ...

    the value my_cache["/home"] can be retrieved for 10 seconds. After that,
    the entry will be treated as if it had never been in the cache and should
    be fetched again from the remote host.

    Note that the `__len__` method does no age tests and thus may include some
    or many already expired entries.
    iˆ  c                 C   s"   t j | j¡| _d | _|  ¡  d S )N)ÚftputilÚlrucacheZLRUCacheÚ_DEFAULT_CACHE_SIZEÚ_cacheÚmax_ageÚenable©Úself© r   ú6/tmp/pip-unpacked-wheel-g7w5sq0v/ftputil/stat_cache.pyÚ__init__)   s    zStatCache.__init__c                 C   s
   d| _ dS )z1
        Enable storage of stat results.
        TN©Ú_enabledr	   r   r   r   r   0   s    zStatCache.enablec                 C   s
   d| _ dS )aH  
        Disable the cache. Further storage attempts with `__setitem__` won't
        have any visible effect.

        Disabling the cache only effects new storage attempts. Values stored
        before calling `disable` can still be retrieved unless disturbed by a
        `resize` command or normal cache expiration.
        FNr   r	   r   r   r   Údisable6   s    zStatCache.disablec                 C   s   || j _dS )zÁ
        Set number of cache entries to the integer `new_size`. If the new size
        is smaller than the current cache size, relatively long-unused elements
        will be removed.
        N)r   Úsize)r
   Znew_sizer   r   r   ÚresizeC   s    zStatCache.resizec                 C   sF   zt   ¡ | j |¡ W S  tjjk
r@   tj d |¡¡‚Y nX dS )zŠ
        Return the age of a cache entry for `path` in seconds. If the path
        isn't in the cache, raise a `CacheMissError`.
        zno entry for path {} in cacheN)	Útimer   Úmtimer   r   ÚCacheKeyErrorÚerrorÚCacheMissErrorÚformat©r
   Úpathr   r   r   Ú_ageK   s    ÿzStatCache._agec                 C   s   | j  ¡  dS )z7
        Clear (invalidate) all cache entries.
        N)r   Úclearr	   r   r   r   r   W   s    zStatCache.clearc                 C   sB   |  d¡std |¡ƒ‚z| j|= W n tjjk
r<   Y nX dS )a$  
        Invalidate the cache entry for the absolute `path` if present. After
        that, the stat result data for `path` can no longer be retrieved, as if
        it had never been stored.

        If no stat result for `path` is in the cache, do _not_ raise an
        exception.
        ú/z{} must be an absolute pathN)Ú
startswithÚAssertionErrorr   r   r   r   r   r   r   r   r   Ú
invalidate]   s
    zStatCache.invalidatec                 C   s†   | j stj d¡‚| jdk	rJ|  |¡| jkrJ|  |¡ tj d |¡¡‚n8z| j| W S  tj	j
k
r€   tj d |¡¡‚Y nX dS )z‘
        Return the stat entry for the `path`. If there's no stored stat entry
        or the cache is disabled, raise `CacheMissError`.
        zcache is disabledNzentry for path {} has expiredzentry for path {} not found)r   r   r   r   r   r   r    r   r   r   r   r   r   r   r   Ú__getitem__o   s    
ÿÿzStatCache.__getitem__c                 C   s&   |  d¡st‚| jsdS || j|< dS )zi
        Put the stat data for the absolute `path` into the cache, unless it's
        disabled.
        r   N)r   r   r   r   )r
   r   Ústat_resultr   r   r   Ú__setitem__†   s    zStatCache.__setitem__c                 C   s0   z| |  W n t jjk
r&   Y dS X dS dS )zŒ
        Support for the `in` operator. Return a true value, if data for `path`
        is in the cache, else return a false value.
        FTN)r   r   r   r   r   r   r   Ú__contains__   s
    zStatCache.__contains__c                 C   s
   t | jƒS )z
        Return the number of entries in the cache. Note that this may include
        some (or many) expired entries.
        )Úlenr   r	   r   r   r   Ú__len__    s    zStatCache.__len__c                 C   s4   g }t | jƒD ]}| d || | ¡¡ qd |¡S )zG
        Return a string representation of the cache contents.
        z{}: {}Ú
)Úsortedr   Úappendr   Újoin)r
   ÚlinesÚkeyr   r   r   Ú__str__§   s    zStatCache.__str__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r    r!   r#   r$   r&   r-   r   r   r   r   r      s   
r   )r1   r   Zftputil.errorr   Zftputil.lrucacheÚ__all__r   r   r   r   r   Ú<module>   s
   