
    eI9                        d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
mZmZmZmZmZmZmZmZ d dlmZ d dlmZmZmZ d dlmZmZ d dlmZ d d	lmZ ej@                  d
k\  r	 d dl!Z!nd dl#Z!d dl$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+ erd dl,Z,edeee-f   de
e-e	f   fd       Z.ededefd       Z/e	 d;dee-   dee-   deee-f   fd       Z0	 d;dee-df   dee-   dee-   fdZ1 ed      de-de
e-e	f   fd       Z2de
e-e	f   deee'      fdZ3de-deee'      fd Z4de-deee'      fd!Z5d"edefd#Z6edefd$       Z7ed%edefd&       Z8	 d;ded%ed'ee+   de9fd(Z:ded%edefd)Z;d*e-d%ed+e
eef   de9fd,Z<d-e-d.eee-      de9fd/Z=d0ee   d%ed1ee-   d2ee-   d3eee-      d4eee-      d'e+d+ee
eef      d5e9d6e9dee   fd7Z>d8e j~                  dee j~                  d9f   fd:Z@y# e"$ r
 esd dl#Z!Y w xY w)<    N)	lru_cache)Path)TYPE_CHECKINGAnyDictIterableIteratorListOptionalPatternSequenceTupleUnion)
mypyc_attr)InvalidSpecifier	SpecifierSpecifierSet)InvalidVersionVersion)PathSpec)GitWildMatchPatternError)      )"jupyter_dependencies_are_installed)TargetVersion)err)Reportpathreturnc                 p    t        | d      5 }t        j                  |      cd d d        S # 1 sw Y   y xY w)Nrb)opentomllibload)r   fs     -/usr/lib/python3/dist-packages/black/files.py
_load_tomlr'   -   s.    	dD	 Q||A  s   ,5c                 "    | j                         S N)resolver   s    r&   _cached_resolver,   3   s    <<>    srcsstdin_filenamec           	         t        fd| D              } | s't        t        t        j                                     g} | D cg c])  }t        t        t        j                         |            + }}|D cg c]-  }t        |j                        |j                         r|gng z   / }}t        t        j                  d |D         d       }|g|j                  D ]p  }|dz  j                         r|dfc S |dz  j                         r|dfc S |d	z  j                         sIt        |d	z        }d
|j                  di       v sl|d	fc S  dfS c c}w c c}w )a  Return a directory containing .git, .hg, or pyproject.toml.

    That directory will be a common parent of all files and directories
    passed in `srcs`.

    If no directory in the tree contains a marker that would specify it's the
    project root, the root of the file system is returned.

    Returns a two-tuple with the first element as the project root path and
    the second element as a string describing the method by which the
    project root was discovered.
    c              3   0   K   | ]  }|d k(  rn|  yw)-N ).0sr/   s     r&   	<genexpr>z$find_project_root.<locals>.<genexpr>I   s     E1qCx^Q6Es   c              3   2   K   | ]  }t        |        y wr)   )set)r4   parentss     r&   r6   z$find_project_root.<locals>.<genexpr>V   s     CG3w<C   c                     | j                   S r)   )partsr+   s    r&   <lambda>z#find_project_root.<locals>.<lambda>W   s
     r-   )keyz.gitz.git directoryz.hgz.hg directorypyproject.tomlblacktoolzfile system root)tuplestrr,   r   cwdlistr9   is_dirmaxr8   intersectionexistsis_filer'   get)	r.   r/   src	path_srcsr   src_parentscommon_base	directorypyproject_tomls	    `       r&   find_project_rootrR   8   sj     !EEEODHHJ/01CGHCdhhj#!67HIH
 LUCGT\\tf2>K  C{CD#K
 "8K$7$78 
3	&&(...%%'o--((113'	4D(DEN.,,VR88 "222
3 (((3 Is   .E92Epath_search_start.c                    t        | |      \  }}|dz  }|j                         rt        |      S 	 t               }|j                         rt        |      S dS # t        t
        f$ r}t        d|       Y d}~yd}~ww xY w)z;Find the absolute filepath to a pyproject.toml if it existsr?   Nz-Ignoring user configuration directory due to )rR   rJ   rC   find_user_pyproject_tomlPermissionErrorRuntimeErrorr   )rS   r/   path_project_root_path_pyproject_tomlpath_user_pyproject_tomles          r&   find_pyproject_tomlr]   i   s     -->Oq+.>>""$&''
#;#=  (//1 ()	
 	

 \* ;A5ABs   $A A A?'A::A?T)	patchablepath_configc                 t   t        |       }|j                  di       j                  di       }|j                         D ci c](  \  }}|j                  dd      j                  dd      |* }}}d|vr8t	        |      }|+|D cg c]  }|j
                  j                          c}|d<   |S c c}}w c c}w )zParse a pyproject toml file, pulling out relevant parts for Black.

    If parsing fails, will raise a tomllib.TOMLDecodeError.
    rA   r@   z-- r2   rY   target_version)r'   rK   itemsreplaceinfer_target_versionnamelower)r_   rQ   configkvinferred_target_versions         r&   parse_pyproject_tomlrl      s      ,N+//;??LFCI<<>R41aaiib!))#s3Q6RFRv%"6~"F".@W'X1'XF#$M S
 (Ys   -B/!B5rQ   c                     | j                  di       }|j                  dd      }|	 t        |      S y# t        $ r Y nw xY w	 t        |      S # t        t        f$ r Y yw xY w)a#  Infer Black's target version from the project metadata in pyproject.toml.

    Supports the PyPA standard format (PEP 621):
    https://packaging.python.org/en/latest/specifications/declaring-project-metadata/#requires-python

    If the target version cannot be inferred, returns None.
    projectzrequires-pythonN)rK   parse_req_python_versionr   parse_req_python_specifierr   )rQ   project_metadatarequires_pythons      r&   re   re      s     &)))R8&**+<dCO"	+O<<   			-o>> .1 		s    
4 	A A 
A A! A!rr   c                     t        |       }|j                  d   dk7  ry	 t        |j                  d         gS # t        t        f$ r Y yw xY w)zParse a version string (i.e. ``"3.7"``) to a list of TargetVersion.

    If parsing fails, will raise a packaging.version.InvalidVersion error.
    If the parsed version cannot be mapped to a valid TargetVersion, returns None.
    r   r   N   )r   releaser   
IndexError
ValueError)rr   versions     r&   ro   ro      sU     o&GqQgooa0122
# s   9 A
Ac                     t        t        |             }|syt        D ci c]  }d|j                   | }}t	        |j                  |            }|r|D cg c]  }||   	 c}S yc c}w c c}w )zParse a specifier string (i.e. ``">=3.7,<3.10"``) to a list of TargetVersion.

    If parsing fails, will raise a packaging.specifiers.InvalidSpecifier error.
    If the parsed specifier cannot be mapped to a valid TargetVersion, returns None.
    Nz3.)strip_specifier_setr   r   valuerE   filter)rr   specifier_setrj   target_version_mapcompatible_versionss        r&   rp   rp      s|     (_(EFM5BCBqwwi.!+CC%)-*>*>?Q*R%S/BC!"1%CC	 D Ds   A)A.r}   c                 j   g }| D ]  }dt        |      v r|j                  |       #|j                  dv rWt        |j                        }t        |j                   |j                   d|j                         }|j                  |       |j                  dk(  rdt        |j                        }t        |j                        dkD  r%t        d|j                   d|j                         }|j                  |       |j                  |        t        dj                  d |D                    S )	zStrip minor versions for some specifiers in the specifier set.

    For background on version specifiers, see PEP 440:
    https://peps.python.org/pep-0440/#version-specifiers
    *)z~=z==>=z===.>   r   ,c              3   2   K   | ]  }t        |        y wr)   )rC   )r4   r5   s     r&   r6   z&strip_specifier_set.<locals>.<genexpr>   s      <AQ <r:   )rC   appendoperatorr   rx   r   majorminorlenru   r   join)r}   
specifiersr5   rx   strippeds        r&   rz   rz      s    J !#a&=a ZZ44aii(G AJJ<a!OPHh'ZZ3aii(G7??#a'7==/7==/BCa a !  < <<==r-   c                      t         j                  dk(  r"t        j                         dz  } t        |       S t        j
                  j                  dd      }t        |      j                         dz  } t        |       S )a:  Return the path to the top-level user configuration for black.

    This looks for ~\.black on Windows and ~/.config/black on Linux and other
    Unix systems.

    May raise:
    - RuntimeError: if the current user has no homedir
    - PermissionError: if the current process cannot access the user's homedir
    win32z.blackXDG_CONFIG_HOMEz	~/.configr@   )	sysplatformr   homeosenvironrK   
expanduserr,   )user_config_pathconfig_roots     r&   rU   rU      sk     ||w99;1 +,, jjnn%6D,779GC+,,r-   rootc                    | dz  }g }|j                         r+|j                  d      5 }|j                         }ddd       	 t        j                  d|      S # 1 sw Y    xY w# t
        $ r}t        d| d|         d}~ww xY w)z8Return a PathSpec matching gitignore content if present.z
.gitignorezutf-8)encodingNgitwildmatchzCould not parse z: )rJ   r"   	readlinesr   
from_linesr   r   )r   	gitignorelinesgfr\   s        r&   get_gitignorer      s     |#IE^^W^- 	#LLNE	#"">599	# 	# $ ykA3/0s#   AA& A#&	B/BBreportc                 ,   	 t         j                  dk  r| j                         } t        |       }	 |j                  |       y# t        $ r!}|r|j                  | d|        Y d}~yd}~ww xY w# t        $ r |r|j                  | d|        Y yw xY w)z
    Returns whether the path is a symbolic link that points outside the
    root directory. Also returns True if we failed to resolve the path.
    )r         zcannot be read because NTz'is a symbolic link that points outside F)r   version_infoabsoluter,   OSErrorpath_ignoredrelative_torw   )r   r   r   resolved_pathr\   s        r&   $resolves_outside_root_or_cannot_statr     s    i'==?D'-
!!$'
   (?s&CD  (OPTv&VWs(   .A A0 	A-A((A-0 BBc                     	 | j                         j                        S # t        $ r Y nw xY wt        fd| j                  D        d       }|| j                  |      S t        |       j                        S )Nc              3   @   K   | ]  }t        |      k(  s|  y wr)   )r,   )r4   pr   s     r&   r6   z,best_effort_relative_path.<locals>.<genexpr>&  s     Na?13E3MNs   )r   r   rw   nextr9   r,   )r   r   root_parents    ` r&   best_effort_relative_pathr      su    }}**400 N4<<NPTUK,,4 ,,T22s   " 	..root_relative_pathgitignore_dictc                     || z  }|j                         D ]8  \  }}	 |j                  |      j                         }|j	                  |      s8 y y# t        $ r Y  yw xY w)NTF)rc   r   as_posixrw   
match_file)r   r   r   r   gitignore_pathpatternrelative_paths          r&   _path_is_ignoredr   -  s}    
 $$D $2#7#7#9 	 ,,^<EEGM m, 	  	 		s   A	AAnormalized_pathr   c                 h    |r|j                  |       nd }t        |xr |j                  d            S )Nr   )searchboolgroup)r   r   matchs      r&   path_is_excludedr   ?  s.     07GNN?+DE(%++a.))r-   pathsincludeexcludeextend_excludeforce_excludeverbosequietc             #   T  K   |j                         s
J d|        | D ]  }
|
j                         sJ |
j                  |      j                         }|r t        |||      r|j	                  |
d       Wd|z   }|
j                         r|dz  }t        ||      r|j	                  |
d       t        ||      r|j	                  |
d       t        ||      r|j	                  |
d       t        |
||      r|
j                         rF|i |||
z  t        |
      i}nd}t        |
j                         |||||||||	
      E d{    2|
j                         sD|
j                  d	k(  rt        |xs |	 
      sf|r|j                  |      nd}|s|
  y7 [w)a7  Generate all files under `path` whose paths are not excluded by the
    `exclude_regex`, `extend_exclude`, or `force_exclude` regexes,
    but are included by the `include` regex.

    Symbolic links pointing outside of the `root` directory are ignored.

    `report` is where output about exclusions goes.
    z/INTERNAL ERROR: `root` must be absolute but is z!matches a .gitignore file content/z(matches the --exclude regular expressionz/matches the --extend-exclude regular expressionz.matches the --force-exclude regular expressionN)r   r   z.ipynb)warnT)is_absoluter   r   r   r   rF   r   r   r   gen_python_filesiterdirrJ   suffixr   r   )r   r   r   r   r   r   r   r   r   r   childr   new_gitignore_dictinclude_matchs                 r&   r   r   G  s    , W!PQUPVWW ?  """"..t4==? .n
 'JK !#55<<>#%.8'QR.?H .>'WX/tVD<<> )&$&5L-"6&"
 &*"'"   ]]_||x'0R)E	1 BIGNN+=>tM?Vs   EF(
F&F(#9F(	F(r%   zcolorama.AnsiToWin32c                 L    	 ddl m}  || dddd      S # t        $ r | cY S w xY w)at  
    Wrap stream with colorama's wrap_stream so colors are shown on Windows.

    If `colorama` is unavailable, the original stream is returned unmodified.
    Otherwise, the `wrap_stream()` function determines whether the stream needs
    to be wrapped for a Windows environment and will accordingly either return
    an `AnsiToWin32` wrapper or the original stream.
    r   )wrap_streamNFT)convertstrip	autoresetwrap)colorama.initialiser   ImportError)r%   r   s     r&   wrap_stream_for_windowsr     s6    U3
 1d%5tTT	  s    ##r)   )Aior   r   	functoolsr   pathlibr   typingr   r   r   r   r	   r
   r   r   r   r   r   mypy_extensionsr   packaging.specifiersr   r   r   packaging.versionr   r   pathspecr   pathspec.patterns.gitwildmatchr   r   r#   r   tomliblack.handle_ipynb_magicsr   
black.moder   black.outputr   black.reportr   coloramarC   r'   r,   rR   r]   rl   re   ro   rp   rz   rU   r   r   r   r   r   r   r   TextIOWrapperr   r3   r-   r&   <module>r      s   	 	 
      ' J J 5  Cw$  H $   U49% $sCx.  
 $ 4   9=-)
3--))1#-)
49-) -)b IMS#X8@c], dc d38n  "cNd=!"2c htM?R6S  mAT8U ">| > >2 -$ - -&    "  $

 V 
	4
3D 
3 
3 
3
 x( 
	$**gcl#* 
*VD>V
V S\V S\	V
 WS\*V GCL)V V T$.12V V V d^VrU	U
2334UG  $#$s   'G% %G43G4