o
    8fI9                     @   sj  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rqzd dl!Z!W n e"yp   esnd dl#Z!Y n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rd dl,Z,edeee-f de
e-e	f fddZ.ededefddZ/e	dLdee- dee- deee-f fddZ0	dLdee-df dee- dee- fddZ1eddde-de
e-e	f fd d!Z2d"e
e-e	f deee'  fd#d$Z3d%e-deee'  fd&d'Z4d%e-deee'  fd(d)Z5d*edefd+d,Z6edefd-d.Z7ed/edefd0d1Z8	dLded/ed2ee+ de9fd3d4Z:ded/edefd5d6Z;d7e-d/ed8e
eef de9fd9d:Z<d;e-d<eee-  de9fd=d>Z=d?ee d/ed@ee- dAee- dBeee-  dCeee-  d2e+d8ee
eef  dDe9dEe9dee fdFdGZ>dHe j?dee j?dIf fdJdKZ@dS )M    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                 C   s8   t | d}t|W  d    S 1 sw   Y  d S )Nrb)opentomllibload)r   f r$   D/home/ertert/spirit/venv/lib/python3.10/site-packages/black/files.py
_load_toml-   s   $r&   c                 C   s   |   S N)resolver   r$   r$   r%   _cached_resolve3   s   r*   srcsstdin_filenamec                    s    durt  fdd| D } | sttt g} dd | D }dd |D }ttjdd |D  dd	 d
}|g|jR D ]4}|d 	 rM|df  S |d 
 rY|df  S |d  rst|d }d|di v rs|df  S q?|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   s     | ]}|d kr
 n|V  qdS )-Nr$   .0sr,   r$   r%   	<genexpr>I       z$find_project_root.<locals>.<genexpr>c                 S   s   g | ]}t tt |qS r$   )r*   r   cwd)r/   srcr$   r$   r%   
<listcomp>M   s    z%find_project_root.<locals>.<listcomp>c                 S   s(   g | ]}t |j| r|gng  qS r$   )listparentsis_dir)r/   r   r$   r$   r%   r6   Q   s    c                 s       | ]}t |V  qd S r'   )set)r/   r8   r$   r$   r%   r2   V       c                 S   s   | j S r'   )partsr)   r$   r$   r%   <lambda>W   s    z#find_project_root.<locals>.<lambda>)keyz.gitz.git directoryz.hgz.hg directorypyproject.tomlblacktoolzfile system root)tuplestrr*   r   r4   maxr;   intersectionr8   existsr9   is_filer&   get)r+   r,   	path_srcssrc_parentscommon_base	directorypyproject_tomlr$   r1   r%   find_project_root8   s.   rO   path_search_start.c              
   C   s~   t | |\}}|d }| rt|S zt }| r t|W S dW S  ttfy> } ztd| W Y d}~dS d}~ww )z;Find the absolute filepath to a pyproject.toml if it existsr@   Nz-Ignoring user configuration directory due to )rO   rH   rD   find_user_pyproject_tomlPermissionErrorRuntimeErrorr   )rP   r,   path_project_root_path_pyproject_tomlpath_user_pyproject_tomler$   r$   r%   find_pyproject_tomli   s    rY   T)	patchablepath_configc                 C   s\   t | }|di di }dd | D }d|vr,t|}|dur,dd |D |d< |S )	zParse a pyproject toml file, pulling out relevant parts for Black.

    If parsing fails, will raise a tomllib.TOMLDecodeError.
    rB   rA   c                 S   s&   i | ]\}}| d d dd|qS )z-- r-   rU   )replace)r/   kvr$   r$   r%   
<dictcomp>   s   & z(parse_pyproject_toml.<locals>.<dictcomp>target_versionNc                 S   s   g | ]}|j  qS r$   )namelowerr/   r_   r$   r$   r%   r6      s    z(parse_pyproject_toml.<locals>.<listcomp>)r&   rI   itemsinfer_target_version)r[   rN   configinferred_target_versionr$   r$   r%   parse_pyproject_toml   s   ri   rN   c              	   C   sf   |  di }| dd}|dur1zt|W S  ty   Y nw zt|W S  ttfy0   Y dS 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)rI   parse_req_python_versionr   parse_req_python_specifierr   )rN   project_metadatarequires_pythonr$   r$   r%   rf      s   


rf   rn   c              	   C   sF   t | }|jd dkrdS z	t|jd gW S  ttfy"   Y dS 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)rn   versionr$   r$   r%   rk      s   rk   c                    sJ   t t| }|s
dS dd tD  t| }|r# fd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                 S   s   i | ]	}d |j  |qS )z3.)valuerd   r$   r$   r%   r`      s    z.parse_req_python_specifier.<locals>.<dictcomp>c                    s   g | ]} | qS r$   r$   rd   target_version_mapr$   r%   r6      s    z.parse_req_python_specifier.<locals>.<listcomp>)strip_specifier_setr   r   r7   filter)rn   specifier_setcompatible_versionsr$   ru   r%   rl      s   rl   ry   c                 C   s   g }| D ]T}dt |v r|| q|jdv r0t|j}t|j |j d|j }|| q|jdkrSt|j}t|j	dkrMtd|j d|j }|| q|| qt
d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                 s   r:   r'   )rD   r.   r$   r$   r%   r2      r<   z&strip_specifier_set.<locals>.<genexpr>)rD   appendoperatorr   rs   r   majorminorlenrp   r   join)ry   
specifiersr0   rs   strippedr$   r$   r%   rw      s   



rw   c                  C   sD   t jdkrt d } t| S tjdd}t| d } 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	~/.configrA   )	sysplatformr   homeosenvironrI   
expanduserr*   )user_config_pathconfig_rootr$   r$   r%   rQ      s   
rQ   rootc              
   C   s   | d }g }|  r$|jdd}| }W d   n1 sw   Y  ztd|W S  tyB } ztd| d|   d}~ww )z8Return a PathSpec matching gitignore content if present.z
.gitignorezutf-8)encodingNgitwildmatchzCould not parse z: )rH   r    	readlinesr   
from_linesr   r   )r   	gitignorelinesgfrX   r$   r$   r%   get_gitignore   s   
r   reportc              
   C   s   zt jdk r
|  } t| }W n ty- } z|r"|| d|  W Y d}~dS d}~ww z|| W dS  tyK   |rH|| d|  Y dS 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_torr   )r   r   r   resolved_pathrX   r$   r$   r%   $resolves_outside_root_or_cannot_stat  s$   	
r   c                    s^   z|    W S  ty   Y nw t fdd| jD d }|d ur(| |S t|  S )Nc                 3   s     | ]}t | kr|V  qd S r'   )r*   )r/   pr   r$   r%   r2   &  r3   z,best_effort_relative_path.<locals>.<genexpr>)r   r   rr   nextr8   r*   )r   r   root_parentr$   r   r%   best_effort_relative_path   s   
r   root_relative_pathgitignore_dictc              	   C   sX   ||  }|  D ]!\}}z	|| }W n ty    Y  dS w ||r) dS qdS )NTF)re   r   as_posixrr   
match_file)r   r   r   r   gitignore_pathpatternrelative_pathr$   r$   r%   _path_is_ignored-  s   
r   normalized_pathr   c                 C   s$   |r| | nd }t|o|dS )Nr   )searchboolgroup)r   r   matchr$   r$   r%   path_is_excluded?  s   r   pathsincludeexcludeextend_excludeforce_excludeverbosequietc                c   sb   |  sJ d| | D ]}
|
  sJ |
| }|r,t|||r,||
d qd| }|
 r8|d7 }t||rD||
d qt||rP||
d qt||r\||
d qt|
||rcq|
 r|durxi |||
 t|
i}nd}t	|

 |||||||||	d
E dH  q|
 r|
jd	krt|p|	 d
sq|r||nd}|r|
V  q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)is_absoluter   r   r   r   r9   r   r   r   gen_python_filesiterdirrH   suffixr   r   )r   r   r   r   r   r   r   r   r   r   childr   new_gitignore_dictinclude_matchr$   r$   r%   r   G  sp   


r   r#   zcolorama.AnsiToWin32c                 C   s:   zddl m} W n ty   |  Y S w || dddddS )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   r$   r$   r%   wrap_stream_for_windows  s   r   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   coloramarD   r&   r*   rO   rY   ri   rf   rk   rl   rw   rQ   r   r   r   r   r   r   r   TextIOWrapperr   r$   r$   r$   r%   <module>   s    4
$
1













Y