o
    h                     @   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 ejd	krPd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)      )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   A/var/www/html/ai/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_file1   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 |}| s| ||S |d2}zt|}dd | D }W n tjttfy<   | || Y W  d   S w W d   n1 sGw   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.
        rbc                 S   s   i | ]	\}}|t | qS r   )r   .0kvr   r   r   
<dictcomp>I   s    zCache.read.<locals>.<dictcomp>N)	r*   existsopenpickleloaditemsUnpicklingError
ValueError
IndexError)clsr'   r,   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)rA   r?   r   r   r   hash_digestO   s   zCache.hash_digestc                 C   s"   |   }t| }t|j|j|S )zReturn file data for path.)statr+   rF   r   r   r   )rA   rG   r   r   r   r   get_file_dataV   s   
zCache.get_file_datasourcec                 C   sl   |  }| jt|}|du rdS | }|j|jkrdS t|jt|jkr4t	|}||j
kr4dS dS )z7Check if source has changed compared to cached version.NTF)resolver.   r"   r   rG   r   r   r   r+   rF   r   )selfrI   res_srcoldstnew_hashr   r   r   
is_changed^   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.
        )setrP   add)rK   rQ   changeddonesrcr   r   r   filtered_cachedn   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   rJ   r+   rH   )r1   rV   r   r   r   r4      s    zCache.write.<locals>.<dictcomp>T)parentsexist_okF)dirdeletec                 S   s   i | ]
\}}|g |R qS r   r   r0   r   r   r   r4      s       )protocolNr   )r.   updater(   mkdirtempfileNamedTemporaryFiler   r,   parentr9   r7   dumpr    replacenameOSError)rK   rQ   fr?   r   r   r   write}   s$   
	zCache.write)r   r   r   r   r   r   r   dictr.   r   r   r   classmethodr   r@   staticmethodrF   rH   boolrP   r   r	   r   rW   rh   r   r   r   r   r+   5   s   
 &r+   ) __doc__rC   r    r7   sysr`   dataclassesr   r   pathlibr   typingr   r   r   r   r	   platformdirsr
   _black_versionr   r#   
black.moder   version_infor   typing_extensionsr   r&   r(   r*   r+   r   r   r   r   <module>   s*    
