a
    g	g4                     @   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rzd dl!Z!W q e"y   esd dl#Z!Y q0 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e- eee-f dddZ.ee-df ee- dddZ/edde-e
e-e	f dddZ0e
e-e	f eee'  dddZ1e-eee'  ddd Z2e-eee'  dd!d"Z3eed#d$d%Z4e ed&d'd(Z5e eed)d*d+Z6d=eeee+ ee- d,d-d.Z7ee
eef e+e8d/d0d1Z9e-eee-  e8d2d3d4Z:ee eee- ee- eee-  eee-  e+ee
eef  e8e8ee d5d6d7Z;e j<ee j<d8f d9d:d;Z=dS )>    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 } | s4tt  g} dd | D }dd |D }ttjdd |D  dd	 d
}|g|jR D ]L}|d 	 r|df  S |d 
 r|df  S |d  r~|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!   N/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/black/files.py	<genexpr>>       z$find_project_root.<locals>.<genexpr>c                 S   s   g | ]}t t  | qS r!   )r   cwdresolve)r#   srcr!   r!   r&   
<listcomp>B   r(   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   s   | ]}t |V  qd S N)set)r#   r.   r!   r!   r&   r'   K   r(   c                 S   s   | j S r1   )parts)r0   r!   r!   r&   <lambda>L   r(   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   Z	path_srcsZsrc_parentsZcommon_base	directoryr!   r%   r&   find_project_root-   s(    r>   .)path_search_startr   c              
   C   s~   t | \}}|d }| r$t|S zt }| r<t|ndW S  ttfyx } ztd| W Y d}~dS d}~0 0 dS )z;Find the absolute filepath to a pyproject.toml if it existsr6   Nz-Ignoring user configuration directory due to )r>   r<   r8   find_user_pyproject_tomlPermissionErrorRuntimeErrorr   )r?   Zpath_project_root_Zpath_pyproject_tomlZpath_user_pyproject_tomler!   r!   r&   find_pyproject_toml\   s    
rE   T)Z	patchable)path_configr   c                 C   s   t | d}t|}W d   n1 s*0    Y  |di di }dd | D }d|vrt|}|durdd	 |D |d< |S )
zParse a pyproject toml file, pulling out relevant parts for Black.

    If parsing fails, will raise a tomllib.TOMLDecodeError.
    rbNZtoolblackc                 S   s&   i | ]\}}| d d dd|qS )z-- r    rC   )replace)r#   kvr!   r!   r&   
<dictcomp>y   r(   z(parse_pyproject_toml.<locals>.<dictcomp>Ztarget_versionc                 S   s   g | ]}|j  qS r!   )namelowerr#   rL   r!   r!   r&   r,   ~   r(   z(parse_pyproject_toml.<locals>.<listcomp>)opentomllibloadgetitemsinfer_target_version)rF   fpyproject_tomlconfigZinferred_target_versionr!   r!   r&   parse_pyproject_tomlp   s    (rZ   )rX   r   c              	   C   sd   |  di }| dd}|dur`z
t|W S  ty<   Y n0 z
t|W S  ttfy^   Y n0 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)rT   parse_req_python_versionr   parse_req_python_specifierr   )rX   Zproject_metadatarequires_pythonr!   r!   r&   rV      s    


rV   )r^   r   c              	   C   sJ   t | }|jd dkrdS zt|jd gW S  ttfyD   Y dS 0 dS )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)r^   versionr!   r!   r&   r\      s    r\   c                    sJ   t t| }|sdS dd tD  t| }|rF 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.)valuerP   r!   r!   r&   rM      r(   z.parse_req_python_specifier.<locals>.<dictcomp>c                    s   g | ]} | qS r!   r!   rP   Ztarget_version_mapr!   r&   r,      r(   z.parse_req_python_specifier.<locals>.<listcomp>)strip_specifier_setr   r   r-   filter)r^   specifier_setZcompatible_versionsr!   re   r&   r]      s    r]   )rh   r   c                 C   s   g }| D ]}dt |v r$|| q|jdv r`t|j}t|j |j d|j }|| q|jdkrt|j}t|j	dkrt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===.>   rj   ,c                 s   s   | ]}t |V  qd S r1   )r8   r"   r!   r!   r&   r'      r(   z&strip_specifier_set.<locals>.<genexpr>)r8   appendoperatorr   rc   r   majorminorlenr`   r   join)rh   
specifiersr$   rc   strippedr!   r!   r&   rf      s    



rf   )r   c                  C   s>   t jdkrt d } n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	~/.configrH   )	sysplatformr   homeosenvironrT   
expanduserr*   )Zuser_config_pathZconfig_rootr!   r!   r&   r@      s
    
r@   )rootr   c              
   C   s   | d }g }|  rH|jdd}| }W d   n1 s>0    Y  ztd|W S  ty } z"td| d|   W Y d}~n
d}~0 0 dS )z8Return a PathSpec matching gitignore content if present.z
.gitignorezutf-8)encodingNZgitwildmatchzCould not parse z: )r<   rQ   	readlinesr   Z
from_linesr   r   )r   Z	gitignorelinesZgfrD   r!   r!   r&   get_gitignore   s    &r   )r0   r   reportr   c              
   C   s   zd|   r| n
t |  }| }z|| }W n, ty`   |rX|| d|  Y W dS 0 W n< ty } z$|r|| d|  W Y d}~dS d}~0 0 |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_posixrb   path_ignoredOSError)r0   r   r   abspathnormalized_pathZroot_relative_pathrD   r!   r!   r&   normalize_path_maybe_ignore   s     	
r   )r0   gitignore_dictr   r   c                 C   sJ   |  D ]<\}}t| ||}|d u r( qF||r|| d  dS qdS )Nz!matches a .gitignore file contentTF)rU   r   Z
match_filer   )r0   r   r   Zgitignore_pathpatternrelative_pathr!   r!   r&   path_is_ignored  s    
r   )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   )pathsr   includeexcludeextend_excludeforce_excluder   r   verbosequietr   c                c   sF  |  sJ d| | D ]$}
t|
||}|du r6q|rHt|
||rHqd| }|
 r`|d7 }t||rx||
d qt||r||
d qt||r||
d q|
 r|duri |||
 t|
i}nd}t|
 |||||||||	d
E dH  q|
	 r|
j
dkr"t||	ds"q|r2||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 N/z(matches the --exclude regular expressionz/matches the --extend-exclude regular expressionz.matches the --force-exclude regular expression)r   r   z.ipynbT)r   r   r   r/   r   r   r   gen_python_filesiterdirr<   suffixr   r   )r   r   r   r   r   r   r   r   r   r   childr   Znew_gitignore_dictZinclude_matchr!   r!   r&   r   *  sb    



r   zcolorama.AnsiToWin32)rW   r   c                 C   s>   zddl m} W n ty&   |  Y S 0 || dddddS 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)Zcolorama.initialiser   ImportError)rW   r   r!   r!   r&   wrap_stream_for_windows~  s
    
r   )N)N)>ior|   ry   	functoolsr   pathlibr   typingr   r   r   r   r   r	   r
   r   r   r   r   Zmypy_extensionsr   Zpackaging.specifiersr   r   r   Zpackaging.versionr   r   Zpathspecr   Zpathspec.patterns.gitwildmatchr   version_inforR   r   ZtomliZblack.handle_ipynb_magicsr   Z
black.moder   Zblack.outputr   Zblack.reportr   coloramar8   r>   rE   rZ   rV   r\   r]   rf   r@   r   r   r   r   r   r   TextIOWrapperr   r!   r!   r!   r&   <module>   s   4
 
.

 
	

U