o
    b                      @   s  d dl mZ d dlZd dlZd dlmZ d dl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 d dlZd dlmZ d dlmZ d d	lmZ erZd dlZe d
ee defddZ deedf dee fddZ!dede	eef fddZ"e defddZ#e dedefddZ$dedededee fddZ%dedeee  de&fd d!Z'd"e
e ded#ee d$ee d%eee  d&eee  ded'ee d(e&d)e&dee fd*d+Z(d,ej)deej)d-f fd.d/Z*dS )0    )	lru_cacheN)Path)AnyDictIterableIteratorListOptionalPatternSequenceTupleUnionTYPE_CHECKING)PathSpec)GitWildMatchPatternError)err)Report)"jupyter_dependencies_are_installedsrcsreturnc                 C   s   | st t  g} dd | D }dd |D }ttjdd |D  dd d}|g|jR D ] }|d	  r<|  S |d
 	 rF|  S |d 
 rP|  S q0|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.
    c                 S   s   g | ]}t t  | qS  )r   cwdresolve).0srcr   r   J/var/www/secure340b-portal/env/lib/python3.10/site-packages/black/files.py
<listcomp>-   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   1   s    c                 s   s    | ]}t |V  qd S N)set)r   r   r   r   r   	<genexpr>6   s    z$find_project_root.<locals>.<genexpr>c                 S   s   | j S r!   )parts)r    r   r   r   <lambda>7   s    z#find_project_root.<locals>.<lambda>)keyz.gitz.hgpyproject.toml)strr   r   r   maxr"   intersectionr   existsr   is_file)r   Z	path_srcsZsrc_parentsZcommon_base	directoryr   r   r   find_project_root    s&   
r.   path_search_start.c              
   C   st   t | }|d }| rt|S zt }| rt|W S dW S  ty9 } 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 )r.   r,   r(   find_user_pyproject_tomlPermissionErrorr   )r/   Zpath_project_rootZpath_pyproject_tomlZpath_user_pyproject_tomler   r   r   find_pyproject_tomlG   s    r3   path_configc                 C   s\   t | dd}t|}W d   n1 sw   Y  |di di }dd | D S )zParse a pyproject toml file, pulling out relevant parts for Black

    If parsing fails, will raise a tomli.TOMLDecodeError
    utf8encodingNZtoolblackc                 S   s&   i | ]\}}| d d dd|qS )z-- -_)replace)r   kvr   r   r   
<dictcomp>c   s   & z(parse_pyproject_toml.<locals>.<dictcomp>)opentomliloadgetitems)r4   fpyproject_tomlconfigr   r   r   parse_pyproject_toml[   s
   rH   c                  C   sD   t jdkrt d } |  S tjdd}t| d } |  S )zReturn 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.
    win32z.blackXDG_CONFIG_HOMEz	~/.configr8   )	sysplatformr   homeosenvironrC   
expanduserr   )user_config_pathZconfig_rootr   r   r   r0   f   s   
r0   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-8r6   NZgitwildmatchzCould not parse z: )r,   r@   	readlinesr   Z
from_linesr   r   )rR   	gitignorelinesZgfr2   r   r   r   get_gitignorev   s   
rV   r    reportc              
   C   s   z|   r| nt |  }| | }W |S  ty4 } z|| d|  W Y d}~dS d}~w tyK   | 	 rJ|| d|  Y dS  w )zrNormalize `path`. May return `None` if `path` was ignored.

    `report` is where "path ignored" output goes.
    zcannot be read because Nz'is a symbolic link that points outside )
is_absoluter   r   r   relative_toas_posixOSErrorpath_ignored
ValueError
is_symlink)r    rR   rW   abspathnormalized_pathr2   r   r   r   normalize_path_maybe_ignore   s   ra   r`   patternc                 C   s$   |r| | nd }t|o|dS )Nr   )searchboolgroup)r`   rb   matchr   r   r   path_is_excluded   s   rg   pathsincludeexcludeextend_excludeforce_excluderT   verbosequietc                c   s<   |  sJ d| | D ]}
t|
||}|du rq|dur+||r+||
d qd| }|
 r7|d7 }t||rC||
d qt||rO||
d qt||r[||
d q|
 r}t|
 |||||||durs|t|
 nd||	d
E dH  q|
	 r|
j
d	krt||	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 Nz#matches the .gitignore file content/z(matches the --exclude regular expressionz/matches the --extend-exclude regular expressionz.matches the --force-exclude regular expression)rm   rn   z.ipynbT)rX   ra   Z
match_filer\   r   rg   gen_python_filesiterdirrV   r,   suffixr   rc   )rh   rR   ri   rj   rk   rl   rW   rT   rm   rn   childr`   Zinclude_matchr   r   r   rp      s\   


rp   rE   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)Zcolorama.initialisert   ImportError)rE   rt   r   r   r   wrap_stream_for_windows   s   rz   )+	functoolsr   iorN   pathlibr   rK   typingr   r   r   r   r   r	   r
   r   r   r   r   Zpathspecr   Zpathspec.patterns.gitwildmatchr   rA   Zblack.outputr   Zblack.reportr   Zblack.handle_ipynb_magicsr   coloramar(   r.   r3   rH   r0   rV   ra   rd   rg   rp   TextIOWrapperrz   r   r   r   r   <module>   s    4&






M