o
    ^i                     @   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 ddlm	Z	m
Z
 ddlmZ ddl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rTd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)Iterable)	dataclassfield)Path)
NamedTuple)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   P/var/www/html/stock_analysis/be/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_file3   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>Q   s    zCache.read.<locals>.<dictcomp>)r(   existsOSErrorr
   openpickleloaditemsUnpicklingError
ValueError
IndexError)clsr%   r*   r3   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)rA   r?   r   r   r   hash_digestW   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_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   rG   r   r   r)   rF   r   )selfrI   res_srcoldstnew_hashr   r   r   
is_changedf   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_cachedv   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   )r/   rV   r   r   r   r2      s     zCache.write.<locals>.<dictcomp>T)parentsexist_okF)dirdeletec                 S   s   i | ]
\}}|g |R qS r   r   r.   r   r   r   r2      s       )protocolNr   )r,   updater&   mkdirtempfileNamedTemporaryFiler   r*   parentr8   r6   dumpr   replacenamer4   )rK   rQ   fr?   r   r   r   write   s$   
zCache.write)r   r   r   r	   r   r   r   dictr,   r   r   classmethodr   r@   staticmethodrF   rH   boolrP   r   tuplerR   rW   rg   r   r   r   r   r)   7   s   
 &r)   ) __doc__rC   r   r6   sysr`   collections.abcr   dataclassesr   r   pathlibr   typingr   platformdirsr   _black_versionr   r!   
black.moder	   black.outputr
   version_infor   typing_extensionsr   r$   r&   r(   r)   r   r   r   r   <module>   s.    
