
    e                     .   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j4                  d
k\  rddlmZ nddlmZ  G d de      Zde
fdZ e       Zdede
fdZ e G d d             Z!y)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                   ,    e Zd ZU eed<   eed<   eed<   y)FileDatast_mtimest_sizehashN)__name__
__module____qualname__float__annotations__intstr     -/usr/lib/python3/dist-packages/black/cache.pyr   r      s    OL
Ir   r   returnc                      t        d      } t        t        j                  j	                  d|             }|t
        z  }|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_dirs     r    get_cache_dirr+      s:     'w/RZZ^^$57HIJIK'Ir   modec                 8    t         d| j                          dz  S )Nzcache.z.pickle)	CACHE_DIRget_cache_key)r,   s    r    get_cache_filer0   2   s      2 2 45W===r   c                       e Zd ZU eed<   eed<    ee      Ze	e
ef   ed<   ededefd       Zedede
fd       Zededefd	       Zd
edefdZdee   deee   ee   f   fdZdee   ddfdZy)Cacher,   
cache_file)default_factory	file_datar!   c           	         t        |      }	 |j                         }|s	 | ||      S |j	                  d      5 }	 t        j                  |      }|j                         D ci c]  \  }}|t        |  }	}}	 ddd        | ||	      S # t        $ r$}t        d| d|         | ||      cY d}~S d}~ww xY wc c}}w # t
        j                  t        t        f$ r  | ||      cY cddd       S w xY w# 1 sw Y   xY w)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rb)r0   existsOSErrorr   openpickleloaditemsr   UnpicklingError
ValueError
IndexError)
clsr,   r3   r8   efobjdatakvr5   s
             r    readz
Cache.read<   s    $D)
	)&&(F
 tZ((__T" 	-d-:@++d:K9=FAQ!_F	F	- 4Y//  	),ZLDEtZ((	) G**J
C -4,,	- 	--		- 	-sW   B C8(C#B<6C	B9B4.B94B9<C'C5)C84C55C88Dpathc                 h    | j                         }t        j                  |      j                         S )zReturn hash digest for path.)
read_byteshashlibsha256	hexdigest)rH   rD   s     r    hash_digestzCache.hash_digestV   s(      ~~d#--//r   c                     | j                         }t        j                  |       }t        |j                  |j
                  |      S )zReturn file data for path.)statr2   rN   r   r   r   )rH   rP   r   s      r    get_file_datazCache.get_file_data]   s6     yy{  &t||T::r   sourcec                 B   |j                         }| j                  j                  t        |            }|y|j	                         }|j
                  |j
                  k7  ry|j                  |j                  k7  r%t        j                  |      }||j                  k7  ryy)z7Check if source has changed compared to cached version.TF)
resolver5   r'   r   rP   r   r   r2   rN   r   )selfrR   res_srcoldstnew_hashs         r    
is_changedzCache.is_changede   s~    .."nn  W.;\\^::$;;#,,&((1H388#r   sourcesc                     t               }t               }|D ]6  }| j                  |      r|j                  |       &|j                  |       8 ||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.
        )setrZ   add)rU   r[   changeddonesrcs        r    filtered_cachedzCache.filtered_cachedu   sP     !U% 	Cs#C 		
 }r   Nc                     | j                   j                  di |D ci c]/  }t        |j                               t        j                  |      1 c} 	 t        j                  dd       t        j                  t        | j                  j                        d      5 }| j                   j                         D ci c]  \  }}|g | }}}t        j                  ||d       ddd       t        j                   j"                  | j                         yc c}w c c}}w # 1 sw Y   ?xY w# t$        $ r Y yw xY w)	z6Update the cache file data and write a new cache file.T)parentsexist_okF)dirdelete   )protocolNr   )r5   updater   rT   r2   rQ   r.   mkdirtempfileNamedTemporaryFiler3   parentr=   r;   dumpr%   replacenamer9   )rU   r[   ra   frE   rF   rD   s          r    writezCache.write   s    	
GNOs3;;=!5#6#6s#;;O	
	OOD4O8,,../ 1 *.)=)=)?;!%AAuuH; ; D!a01 JJqvvt/ P;1 1  		sB   4DAD5 "D) D#D)+2D5 #D))D2.D5 5	E E)r   r   r   r   r   r   r   dictr5   r   r   r   classmethodr   rG   staticmethodrN   rQ   boolrZ   r   r
   r	   rb   rs   r   r   r    r2   r2   6   s    
J%*4%@ItCM"@0 0 0 02 0$ 03 0 0 ;D ;X ; ; $  x~ %D	3t9@T:U Xd^  r   r2   )"__doc__rK   r%   r;   sysrl   dataclassesr   r   pathlibr   typingr   r   r   r	   r
   platformdirsr   _black_versionr   r(   
black.moder   black.outputr   version_infor   typing_extensionsr   r+   r.   r0   r2   r   r   r    <module>r      s    A  	  
  (  9 9 ' 1  w&z t " O	> >$ > _ _ _r   