
    ɭde6                     N   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# e"$ r	 esd dl#Z!Y nw xY w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	 d8dee-         dee-         deee-f         fd            Z.	 d8dee-df         dee-         dee-         fdZ/ ed          de-de
e-e	f         fd            Z0de
e-e	f         deee'                  fdZ1de-deee'                  fdZ2de-deee'                  fdZ3dedefd Z4edefd!            Z5ed"edefd#            Z6	 d8d$ed"ed%ee+         dee-         fd&Z7d'e-d"ed(e
eef         de8fd)Z9d*e-d+eee-                  de8fd,Z:d-ee         d"ed.ee-         d/ee-         d0eee-                  d1eee-                  d%e+d(ee
eef                  d2e8d3e8dee         fd4Z;d5e j<        dee j<        d6f         fd7Z=dS )9    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srcsstdin_filenamereturnc                    t          fd| D                       } | s3t          t          j                                                              g} d | D             }d |D             }t          t          j        d |D              d           }|g|j        R D ]Y}|dz  	                                r|d	fc S |d
z  
                                r|dfc S |dz                                  r|dfc S Z|dfS )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.
    Nc              3   ,   K   | ]}|d k    rn|V  dS )-N ).0sr   s     +lib/python3.11/site-packages/black/files.py	<genexpr>z$find_project_root.<locals>.<genexpr>>   s/      EE1qCxx^^QEEEEEE    c                 t    g | ]5}t          t          j                    |                                          6S r$   )r   cwdresolve)r%   srcs     r'   
<listcomp>z%find_project_root.<locals>.<listcomp>B   s4    AAASdhjj#&&..00AAAr)   c                 j    g | ]0}t          |j                  |                                r|gng z   1S r$   )listparentsis_dir)r%   paths     r'   r.   z%find_project_root.<locals>.<listcomp>F   sG       CGT\=tff2>  r)   c              3   4   K   | ]}t          |          V  d S N)set)r%   r1   s     r'   r(   z$find_project_root.<locals>.<genexpr>K   s(      CCG3w<<CCCCCCr)   c                     | j         S r5   )parts)r3   s    r'   <lambda>z#find_project_root.<locals>.<lambda>L   s     r)   )keyz.gitz.git directoryz.hgz.hg directorypyproject.tomlzfile system root)tuplestrr   r+   r,   maxr6   intersectionr1   existsr2   is_file)r   r   	path_srcssrc_parentscommon_base	directorys    `    r'   find_project_rootrF   -   sq     !EEEEEEEEE +DHJJ&&(())*AADAAAI KT  K CC{CCCD##  K
 "8K$788 / /	&&(( 	/.....%%'' 	.o----((1133 	/.....	/ (((r)   path_search_start.c                 B   t          | |          \  }}|dz  }|                                rt          |          S 	 t                      }|                                rt          |          ndS # t          t
          f$ r}t          d|           Y d}~dS 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 )rF   rA   r=   find_user_pyproject_tomlPermissionErrorRuntimeErrorr   )rG   r   path_project_root_path_pyproject_tomlpath_user_pyproject_tomles          r'   find_pyproject_tomlrQ   \   s     -->OOq+.>>""$$ (&'''
#;#=#=  (//11C()))	

 \*   AAAABBBttttts   2A0 0BBBT)	patchablepath_configc                 V   t          | d          5 }t          j        |          }ddd           n# 1 swxY w Y   |                    di                               di           }d |                                D             }d|vr t          |          }|d |D             |d<   |S )zParse a pyproject toml file, pulling out relevant parts for Black.

    If parsing fails, will raise a tomllib.TOMLDecodeError.
    rbNtoolblackc                 j    i | ]0\  }}|                     d d                               dd          |1S )z-- r#   rM   )replace)r%   kvs      r'   
<dictcomp>z(parse_pyproject_toml.<locals>.<dictcomp>{   s<    RRR41aaiib!!))#s33QRRRr)   target_versionc                 @    g | ]}|j                                         S r$   )namelowerr%   r\   s     r'   r.   z(parse_pyproject_toml.<locals>.<listcomp>   s"    'X'X'X1'X'X'Xr)   )opentomllibloadgetitemsinfer_target_version)rS   fpyproject_tomlconfiginferred_target_versions        r'   parse_pyproject_tomlrm   r   s     
k4	 	  )A a) ) ) ) ) ) ) ) ) ) ) ) ) ) )+//;;??LLFRR6<<>>RRRFv%%"6~"F"F".'X'X@W'X'X'XF#$Ms   266rj   c                     |                      di           }|                     dd          }|G	 t          |          S # t          $ r Y nw xY w	 t          |          S # t          t          f$ r Y nw xY wdS )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)rf   parse_req_python_versionr   parse_req_python_specifierr   )rj   project_metadatarequires_pythons      r'   rh   rh      s     &)))R88&**+<dCCO"	+O<<< 	 	 	D		-o>>> .1 	 	 	D	 4s!   ? 
AAA A32A3rs   c                     t          |           }|j        d         dk    rdS 	 t          |j        d                   gS # t          t          f$ r Y dS 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)rs   versions     r'   rp   rp      sl     o&&GqQtgoa01122
#   tts   ? AAc                     t          t          |                     }|sdS d t          D             t          |                                        }|rfd|D             S dS )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.
    Nc                 $    i | ]}d |j          |S )z3.)valuerb   s     r'   r]   z.parse_req_python_specifier.<locals>.<dictcomp>   s"    CCC.qw..!CCCr)   c                      g | ]
}|         S r$   r$   )r%   r\   target_version_maps     r'   r.   z.parse_req_python_specifier.<locals>.<listcomp>   s    CCC!"1%CCCr)   )strip_specifier_setr   r   r0   filter)rs   specifier_setcompatible_versionsr~   s      @r'   rq   rq      s     (_(E(EFFM tCC]CCC%)-*>*>?Q*R*R%S%S DCCCC/BCCCC4r)   r   c                 l   g }| D ]}dt          |          v r|                    |           *|j        dv rOt          |j                  }t          |j         |j         d|j                   }|                    |           |j        dk    rat          |j                  }t          |j	                  dk    rt          d|j         d|j                   }|                    |           |                    |           t          d                    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   4   K   | ]}t          |          V  d S r5   )r=   )r%   r&   s     r'   r(   z&strip_specifier_set.<locals>.<genexpr>   s(       < <AQ < < < < < <r)   )r=   appendoperatorr   ry   r   majorminorlenrv   r   join)r   
specifiersr&   ry   strippeds        r'   r   r      sB    J ! !#a&&==a    Z444ai((G AJ!O!O!O!O!OPPHh''''Z3ai((G7?##a''B7=BB7=BBCCa    a     < < < < <<<===r)   c                      t           j        dk    rt          j                    dz  } nDt          j                            dd          }t          |                                          dz  } |                                 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	~/.configrW   )	sysplatformr   homeosenvironrf   
expanduserr,   )user_config_pathconfig_roots     r'   rI   rI      sl     |w9;;1jnn%6DD,,7799GC##%%%r)   rootc                 8   | dz  }g }|                                 rB|                    d          5 }|                                }ddd           n# 1 swxY w Y   	 t          j        d|          S # 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: )rA   rc   	readlinesr   
from_linesr   r   )r   	gitignorelinesgfrP   s        r'   get_gitignorer      s     |#IE #^^W^-- 	#LLNNE	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#">5999#   /y//A//000s)   AAAA4 4
B>BBr3   reportc                    	 |                                  r| nt          j                    | z  }|                                }	 |                    |                                          }n,# t          $ r |r|                    | d|            Y dS w xY wn3# t          $ r&}|r|                    | d|            Y d}~dS d}~ww xY w|S )zrNormalize `path`. May return `None` if `path` was ignored.

    `report` is where "path ignored" output goes.
    z'is a symbolic link that points outside Nzcannot be read because )	is_absoluter   r+   r,   relative_toas_posixrx   path_ignoredOSError)r3   r   r   abspathnormalized_pathroot_relative_pathrP   s          r'   normalize_path_maybe_ignorer      s   **,,C$$$(**t2C!//++	!0!<!<T!B!B!K!K!M!M 	 	 	 ##JDJJ   44	      	E&C&C&CDDDttttt
 s<   A B 'A+ *B +%BB BB 
C"CCr   gitignore_dictc                     || z  }|                                 D ]W\  }}	 |                    |                                          }n# t          $ r Y  nw xY w|                    |          r dS XdS )NTF)rg   r   r   rx   
match_file)r   r   r   r3   gitignore_pathpatternrelative_paths          r'   _path_is_ignoredr     s    
 $$D $2#7#7#9#9  	 ,,^<<EEGGMM 	 	 	EE	m,, 	44	5s   'A
AAr   r   c                 |    |r|                     |           nd }t          |o|                    d                    S )Nr   )searchboolgroup)r   r   matchs      r'   path_is_excludedr   )  s<     07@GNN?+++DE(%++a..)))r)   pathsincludeexcludeextend_excludeforce_excludeverbosequietc             #     K   |                                 sJ d|             | D ]}
|
                                                    |                                          }|r(t	          |||          r|                    |
d           fd|z   }|
                                r|dz  }t          ||          r|                    |
d           t          ||          r|                    |
d           t          ||          r|                    |
d           t          |
||          }||
                                rP|i |||
z  t          |
          i}nd}t          |
                                |||||||||	
  
        E d{V  r|
                                r?|
j        d	k    rt          |p|	 
          s|r|                    |          nd}|r|
V  dS )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)r   absoluter   r   r   r   r2   r   r   r   gen_python_filesiterdirrA   suffixr   r   )r   r   r   r   r   r   r   r   r   r   childr   r   new_gitignore_dictinclude_matchs                  r'   r   r   1  s     , WWWQUWWWWW ? ?"^^--99$??HHJJ  	.n
 
 	 'JKKK !#55<<>> 	&#%.88 	'QRRR.?? 	H   .>> 	'WXXX5eT6JJ"<<>> 	 )&$&5L-"6"6& &""
 &*"'"           ]]__ 	|x''0R)E	1 1 1' BISGNN+=>>>tM ? ?r)   ri   zcolorama.AnsiToWin32c                 T    	 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)ri   r   s     r'   wrap_stream_for_windowsr     s]    U333333
 {1d%5tTTTT	    s    ''r5   )>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   version_inford   r   tomliblack.handle_ipynb_magicsr   
black.moder   black.outputr   black.reportr   coloramar=   rF   rQ   rm   rh   rp   rq   r   rI   r   r   r   r   r   r   TextIOWrapperr   r$   r)   r'   <module>r      s   				 				 



                                      ' & & & & & J J J J J J J J J J 5 5 5 5 5 5 5 5       C C C C C Cw$ $ $ $ 	$####$
  H H H H H H $ $ $ $ $ $             OOO 9=+) +)
3-+))1#+)
49+) +) +) +)^ IM S#X8@c]   , dc d38n    $cNd=!"   2c htM?R6S     mAT8U    ">| > > > > >2 &$ & & & &&      "  $ 

 V c]	   :
 x( 
	   $**gcl#* 
* * * *VD>V
V S\V S\	V
 WS\*V GCL)V V T$.12V V V d^V V V VrU	U
2334U U U U U Us   #A( (A65A6