o
    h6                     @   s  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	dFdee- dee- deee-f fddZ.	dFdee-df dee- dee- fddZ/eddde-de
e-e	f fddZ0de
e-e	f deee'  fddZ1d e-deee'  fd!d"Z2d e-deee'  fd#d$Z3d%edefd&d'Z4edefd(d)Z5ed*edefd+d,Z6	dFd-ed*ed.ee+ dee- fd/d0Z7d1e-d*ed2e
eef de8fd3d4Z9d5e-d6eee-  de8fd7d8Z:d9ee d*ed:ee- d;ee- d<eee-  d=eee-  d.e+d2ee
eef  d>e8d?e8dee fd@dAZ;dBe j<dee j<dCf fdDdEZ=dS )G    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                    s    durt  fdd| D } | stt  g} dd | D }dd |D }ttjdd |D  dd	 d
}|g|jR D ]&}|d 	 rM|df  S |d 
 rY|df  S |d  re|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 )-N .0sr   r!   A/var/www/html/ai/venv/lib/python3.10/site-packages/black/files.py	<genexpr>>   s    z$find_project_root.<locals>.<genexpr>c                 S   s   g | ]}t t  | qS r!   )r   cwdresolve)r#   srcr!   r!   r&   
<listcomp>B   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#   pathr!   r!   r&   r+   F   s    c                 s       | ]}t |V  qd S N)set)r#   r-   r!   r!   r&   r'   K       c                 S   s   | j S r1   )parts)r/   r!   r!   r&   <lambda>L   s    z#find_project_root.<locals>.<lambda>)keyz.gitz.git directoryz.hgz.hg directorypyproject.tomlzfile system root)tuplestrr   r(   r)   maxr2   intersectionr-   existsr.   is_file)r   r   	path_srcssrc_parentscommon_base	directoryr!   r%   r&   find_project_root-   s*   rB   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 existsr7   Nz-Ignoring user configuration directory due to )rB   r=   r9   find_user_pyproject_tomlPermissionErrorRuntimeErrorr   )rC   r   path_project_root_path_pyproject_tomlpath_user_pyproject_tomler!   r!   r&   find_pyproject_toml\   s    rL   T)	patchablepath_configc                 C   s   t | d}t|}W d   n1 sw   Y  |di di }dd | D }d|vrBt|}|durBd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                 S   s&   i | ]\}}| d d dd|qS )z-- r    rH   )replace)r#   kvr!   r!   r&   
<dictcomp>{   s   & z(parse_pyproject_toml.<locals>.<dictcomp>target_versionc                 S   s   g | ]}|j  qS r!   )namelowerr#   rU   r!   r!   r&   r+      s    z(parse_pyproject_toml.<locals>.<listcomp>)opentomllibloadgetitemsinfer_target_version)rN   fpyproject_tomlconfiginferred_target_versionr!   r!   r&   parse_pyproject_tomlr   s   re   rb   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)r^   parse_req_python_versionr   parse_req_python_specifierr   )rb   project_metadatarequires_pythonr!   r!   r&   r`      s   


r`   rj   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)rj   versionr!   r!   r&   rg      s   rg   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.)valuerZ   r!   r!   r&   rV      s    z.parse_req_python_specifier.<locals>.<dictcomp>c                    s   g | ]} | qS r!   r!   rZ   target_version_mapr!   r&   r+      s    z.parse_req_python_specifier.<locals>.<listcomp>)strip_specifier_setr   r   r,   filter)rj   specifier_setcompatible_versionsr!   rq   r&   rh      s   rh   ru   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===.>   rx   ,c                 s   r0   r1   )r9   r"   r!   r!   r&   r'      r3   z&strip_specifier_set.<locals>.<genexpr>)r9   appendoperatorr   ro   r   majorminorlenrl   r   join)ru   
specifiersr$   ro   strippedr!   r!   r&   rs      s   



rs   c                  C   sD   t jdkrt d } |  S tjdd}t| d } |  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	~/.configrQ   )	sysplatformr   homeosenvironr^   
expanduserr)   )user_config_pathconfig_rootr!   r!   r&   rD      s   
rD   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: )r=   r[   	readlinesr   
from_linesr   r   )r   	gitignorelinesgfrK   r!   r!   r&   get_gitignore   s   
r   r/   reportc              
   C   s   z2|   r| nt |  }| }z|| }W W |S  ty2   |r.|| d|  Y W dS w  tyP } z|rE|| d|  W Y d}~dS d}~ww )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_posixrn   path_ignoredOSError)r/   r   r   abspathnormalized_pathroot_relative_pathrK   r!   r!   r&   normalize_path_maybe_ignore   s&   	
r   r   gitignore_dictc              	   C   sX   ||  }|  D ]!\}}z	|| }W n ty    Y  dS w ||r) dS qdS )NTF)r_   r   r   rn   
match_file)r   r   r   r/   gitignore_pathpatternrelative_pathr!   r!   r&   _path_is_ignored  s   
r   r   r   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 ]}
|
 | }|r(t|||r(||
d qd| }|
 r4|d7 }t||r@||
d qt||rL||
d qt||rX||
d qt|
||}|du 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)r   absoluter   r   r   r   r.   r   r   r   gen_python_filesiterdirr=   suffixr   r   )r   r   r   r   r   r   r   r   r   r   childr   r   new_gitignore_dictinclude_matchr!   r!   r&   r   1  sp   


r   ra   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)ra   r   r!   r!   r&   wrap_stream_for_windows  s   r   r1   )>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_infor\   r   tomliblack.handle_ipynb_magicsr   
black.moder   black.outputr   black.reportr   coloramar9   rB   rL   re   r`   rg   rh   rs   rD   r   r   r   r   r   r   TextIOWrapperr   r!   r!   r!   r&   <module>   s    4

/













Y