o
    8f                     @   s  d Z ddlZddlZddlZddlZddlZddlmZmZ ddl	m
Z
 ddlmZmZmZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ ejd
krVddlmZ nddlmZ G dd deZde
fddZe Zdede
fddZ eG dd dZ!dS )z;Caching of formatted files with feature-based invalidation.    N)	dataclassfield)Path)DictIterable
NamedTupleSetTuple)user_cache_dir)version)Mode)err)      )Selfc                   @   s&   e Zd ZU eed< eed< eed< dS )FileDatast_mtimest_sizehashN)__name__
__module____qualname__float__annotations__intstr r   r   D/home/ertert/spirit/venv/lib/python3.10/site-packages/black/cache.pyr      s   
 r   returnc                  C   s&   t d} ttjd| }|t }|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.
    blackBLACK_CACHE_DIR)r
   r   osenvironget__version__)default_cache_dir	cache_dirr   r   r   get_cache_dir   s   r'   modec                 C   s   t d|   d S )Nzcache.z.pickle)	CACHE_DIRget_cache_key)r(   r   r   r   get_cache_file2   s   r+   c                   @   s   e Zd ZU eed< eed< eedZe	e
ef ed< ededefddZedede
fd	d
ZededefddZdedefddZdee deee ee f fddZdee ddfddZdS )Cacher(   
cache_file)default_factory	file_datar   c                 C   s   t |}z| }W n! ty+ } ztd| d|  | ||W  Y d}~S d}~ww |s3| ||S |d2}zt|}dd | D }W n tjt	t
fyb   | || Y W  d   S w W d   n1 smw   Y  | |||S )zRead the cache if it exists and is well-formed.

        If it is not well-formed, the call to write later should
        resolve the issue.
        zUnable to read cache file z due to Nrbc                 S   s   i | ]	\}}|t | qS r   )r   .0kvr   r   r   
<dictcomp>P   s    zCache.read.<locals>.<dictcomp>)r+   existsOSErrorr   openpickleloaditemsUnpicklingError
ValueError
IndexError)clsr(   r-   r6   efobjdatar/   r   r   r   read<   s*   

z
Cache.readpathc                 C   s   |   }t| S )zReturn hash digest for path.)
read_byteshashlibsha256	hexdigest)rD   rB   r   r   r   hash_digestV   s   zCache.hash_digestc                 C   s"   |   }t| }t|j|j|S )zReturn file data for path.)statr,   rI   r   r   r   )rD   rJ   r   r   r   r   get_file_data]   s   
zCache.get_file_datasourcec                 C   sd   |  }| jt|}|du rdS | }|j|jkrdS |j|jkr0t|}||j	kr0dS dS )z7Check if source has changed compared to cached version.NTF)
resolver/   r#   r   rJ   r   r   r,   rI   r   )selfrL   res_srcoldstnew_hashr   r   r   
is_changede   s   

zCache.is_changedsourcesc                 C   s>   t  }t  }|D ]}| |r|| q|| q||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.
        )setrS   add)rN   rT   changeddonesrcr   r   r   filtered_cachedu   s   
zCache.filtered_cachedNc                 C   s   | j jdi dd |D  z?tjddd tjt| jjdd}dd | j 	 D }t
j||dd	 W d
   n1 s>w   Y  t|j| j W d
S  tyW   Y d
S w )z6Update the cache file data and write a new cache file.c                 S   s    i | ]}t | t|qS r   )r   rM   r,   rK   )r2   rY   r   r   r   r5      s     zCache.write.<locals>.<dictcomp>T)parentsexist_okF)dirdeletec                 S   s   i | ]
\}}|g |R qS r   r   r1   r   r   r   r5      s       )protocolNr   )r/   updater)   mkdirtempfileNamedTemporaryFiler   r-   parentr;   r9   dumpr!   replacenamer7   )rN   rT   frB   r   r   r   write   s$   
	zCache.write)r   r   r   r   r   r   r   dictr/   r   r   r   classmethodr   rC   staticmethodrI   rK   boolrS   r   r	   r   rZ   rj   r   r   r   r   r,   6   s   
 &r,   )"__doc__rF   r!   r9   sysrc   dataclassesr   r   pathlibr   typingr   r   r   r   r	   platformdirsr
   _black_versionr   r$   
black.moder   black.outputr   version_infor   typing_extensionsr   r'   r)   r+   r,   r   r   r   r   <module>   s,    
