B
    7Ør\Ÿ  ã               @   sb   d Z ddlZddlmZ ddlmZ ddlmZ i add„ Z	dd	d
„Z
dd„ Zdd„ Zdd„ ZdS )a7  
This caching is very important for speed and memory optimizations. There's
nothing really spectacular, just some decorators. The following cache types are
available:

- ``time_cache`` can be used to cache something for just a limited time span,
  which can be useful if there's user interaction and the user cannot react
  faster than a certain time.

This module is one of the reasons why |jedi| is not thread-safe. As you can see
there are global variables, which are holding the cache information. Some of
these variables are being cleaned after every API usage.
é    N)Úwraps)Úsettings)Úparser_cachec                s   dˆ j  ‰‡ ‡fdd„}|S )an  
    Decorator for methods::

        class A(object):
            def x(self):
                if self._x:
                    self._x = 10
                return self._x

    Becomes::

        class A(object):
            @underscore_memoization
            def x(self):
                return 10

    A now has an attribute ``_x`` written by this decorator.
    Ú_c                s8   y
t | ˆƒS  tk
r2   ˆ | ƒ}t| ˆ|ƒ |S X d S )N)ÚgetattrÚAttributeErrorÚsetattr)ÚselfÚresult)ÚfuncÚname© ú)lib/python3.7/site-packages/jedi/cache.pyÚwrapper,   s    
z'underscore_memoization.<locals>.wrapper)Ú__name__)r   r   r   )r   r   r   Úunderscore_memoization   s    
r   Fc             C   sn   | r(xt  ¡ D ]}| ¡  qW t ¡  nBx@t  ¡ D ]4}x.t| ¡ ƒD ]\}\}}|t ¡ k rD||= qDW q2W dS )zê Jedi caches many things, that should be completed after each completion
    finishes.

    :param delete_all: Deletes also the cache that is normally not deleted,
        like parser cache, which is important for faster parsing.
    N)Ú_time_cachesÚvaluesÚclearr   ÚlistÚitemsÚtime)Z
delete_allÚcacheZtcÚkeyÚtÚvaluer   r   r   Úclear_time_caches7   s    	
r   c                s   ‡ fdd„}|S )a\  
    This decorator works as follows: Call it with a setting and after that
    use the function with a callable that returns the key.
    But: This function is only called if the key is not available. After a
    certain amount of time (`time_add_setting`) the cache is invalid.

    If the given key is None, the function will not be cached.
    c                s    i ‰ ˆ t ˆ< ‡ ‡‡fdd„}|S )Nc                 sz   ˆ| |Ž}t |ƒ}y ˆ | \}}|t ¡ kr0|S W n tk
rF   Y nX t |ƒ}ttˆƒ}|d k	rvt ¡ | |fˆ |< |S )N)Únextr   ÚKeyErrorr   r   )ÚargsÚkwargsÚ	generatorr   Zexpiryr   Ztime_add)ÚdctÚkey_funcÚtime_add_settingr   r   r   [   s    

z9call_signature_time_cache.<locals>._temp.<locals>.wrapper)r   )r#   r   )r$   )r"   r#   r   Ú_tempW   s    z(call_signature_time_cache.<locals>._tempr   )r$   r%   r   )r$   r   Úcall_signature_time_cacheN   s    	r&   c                s   ‡ fdd„}|S )Nc                s.   i ‰ t ˆƒ‡ ‡‡fdd„ƒ}‡ fdd„|_|S )Nc                 sh   | t | ¡ ƒf}y$ˆ | \}}t ¡ |ˆ k r2|S W n tk
rH   Y nX ˆ| |Ž}t ¡ |fˆ |< |S )N)Ú	frozensetr   r   r   )r   r    r   Zcreatedr
   )r   r   Úsecondsr   r   r   r   s    
z.time_cache.<locals>.decorator.<locals>.wrapperc                  s   ˆ   ¡ S )N)r   r   )r   r   r   Ú<lambda>   s    z/time_cache.<locals>.decorator.<locals>.<lambda>)r   Zclear_cache)r   r   )r(   )r   r   r   Ú	decoratoro   s    ztime_cache.<locals>.decoratorr   )r(   r*   r   )r(   r   Ú
time_cachen   s    r+   c                s   t ˆ ƒ‡ fdd„ƒ}|S )zA normal memoize function.c                sd   | j  di ¡}| ˆ i ¡}|t| ¡ ƒf}y|| S  tk
r^   ˆ | f|ž|Ž}|||< |S X d S )NZ_memoize_method_dct)Ú__dict__Ú
setdefaultr'   r   r   )r	   r   r    Z
cache_dictr"   r   r
   )Úmethodr   r   r   ‡   s    zmemoize_method.<locals>.wrapper)r   )r.   r   r   )r.   r   Úmemoize_method…   s    r/   )F)Ú__doc__r   Ú	functoolsr   Zjedir   Zparso.cacher   r   r   r   r&   r+   r/   r   r   r   r   Ú<module>   s    
 