B
    Khh                 @   s
  d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZ eZeZe
eef Zeeef Zedd	d
Ze ZeedddZeedddZeedddZeee e
e	e e	e f dddZeee eddddZdS )z;Caching of formatted files with feature-based invalidation.    N)Path)DictIterableSetTuple)user_cache_dir)version)Mode)returnc              C   s"   t dtd} ttjd| }|S )a[  Get the cache directory used by black.

    Users can customize this directory on all systems using `BLACK_CACHE_DIR`
    environment variable. By default, the cache directory is the user cache directory
    under the black application.

    This result is immediately set to a constant `black.cache.CACHE_DIR` as to avoid
    repeated calls.
    Zblack)r   ZBLACK_CACHE_DIR)r   __version__r   osenvironget)Zdefault_cache_dir	cache_dir r   ./tmp/pip-install-vv81h98y/black/black/cache.pyget_cache_dir   s    r   )moder
   c             C   sZ   t | }| si S |d2}yt|}W n tjttfk
rJ   i S X W dQ R X |S )zRead the cache if it exists and is well formed.

    If it is not well formed, the call to write_cache later should resolve the issue.
    rbN)get_cache_fileexistsopenpickleloadUnpicklingError
ValueError
IndexError)r   
cache_fileZfobjcacher   r   r   
read_cache(   s    r   c             C   s   t d|   d S )Nzcache.z.pickle)	CACHE_DIRZget_cache_key)r   r   r   r   r   :   s    r   )pathr
   c             C   s   |   }|j|jfS )zKReturn the information used to check if a file is already formatted or not.)statst_mtimest_size)r!   r"   r   r   r   get_cache_info>   s    r%   )r   sourcesr
   c             C   sX   t  t   }}x@|D ]8}| }| t|t|krB|| q|| qW ||fS )zSplit an iterable of paths in `sources` into two sets.

    The first contains paths of files that modified on disk or are not in the
    cache. The other contains paths to non-modified files.
    )setresolver   strr%   add)r   r&   tododonesrcZres_srcr   r   r   filter_cachedD   s    
r.   )r   r&   r   r
   c          	   C   s   t |}ybtjddd | dd |D }tjt|jdd}tj||dd W d	Q R X t	
|j| W n tk
r~   Y nX d	S )
zUpdate the cache file.T)parentsexist_okc             S   s   i | ]}t |t| qS r   )r%   r)   r(   ).0r-   r   r   r   
<dictcomp>[   s    zwrite_cache.<locals>.<dictcomp>F)dirdelete   )protocolN)r   r    mkdirtempfileNamedTemporaryFiler)   parentr   dumpr   replacenameOSError)r   r&   r   r   Z	new_cachefr   r   r   write_cacheT   s    r@   ) __doc__r   r   r8   Zpathlibr   typingr   r   r   r   Zplatformdirsr   Z_black_versionr   r   Z
black.moder	   floatZ	TimestampintZFileSize	CacheInfor)   Cacher   r    r   r   r%   r.   r@   r   r   r   r   <module>   s&   &