a
    
d%                     @   s   d Z ddlZddlZ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 ddl
mZ dZdZd	Zd
d Zdd Zdd ZejfddZdd Zd"ddZdd Zdd Zdd Zdd Zd d! ZdS )#zyInterface to file resources.

This module provides functions for interfacing with files: opening, writing, and
querying.
    N)ConfigParser)detect_encoding)errors)style
z
c                 C   s   g }t j| rt | t jrt| d:}|D ]$}| r.|ds.||  q.W d   n1 sh0    Y  t	dd |D rt
d|S )z7Get a list of file patterns to ignore from .yapfignore.r#Nc                 s   s   | ]}| d V  qdS z./N
startswith.0e r   d/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/yapf/yapflib/file_resources.py	<genexpr>-       z4_GetExcludePatternsFromYapfIgnore.<locals>.<genexpr>z,path in .yapfignore should not start with ./)ospathisfileaccessR_OKopenstripr   appendanyr   	YapfError)filenameignore_patternsfdliner   r   r   !_GetExcludePatternsFromYapfIgnore$   s    .
r"   c                 C   s   g }zddl }W n ty,   tdY n0 tj| rt| tjrt	| d}|
|}W d   n1 sr0    Y  |di di dg }tdd	 |D rtd
|S )z:Get a list of file patterns to ignore from pyproject.toml.r   NHtomli package is needed for using pyproject.toml as a configuration filerbtoolZ
yapfignorer   c                 s   s   | ]}| d V  qdS r
   r   r   r   r   r   r   C   r   z7_GetExcludePatternsFromPyprojectToml.<locals>.<genexpr>z/path in pyproject.toml should not start with ./)tomliImportErrorr   r   r   r   r   r   r   r   loadgetr   )r   r   tomllibr    pyproject_tomlr   r   r   $_GetExcludePatternsFromPyprojectToml3   s*    
(
r,   c                 C   sT   g }t j| d}t j|r*|t|7 }t j| d}t j|rP|t|7 }|S )a%  Return patterns of files to exclude from ignorefile in a given directory.

  Looks for .yapfignore in the directory dirname.

  Arguments:
    dirname: (unicode) The name of the directory.

  Returns:
    A List of file patterns to exclude if ignore file is found, otherwise empty
    List.
  z.yapfignorezpyproject.toml)r   r   joinexistsr"   r,   )dirnamer   Zyapfignore_fileZpyproject_toml_filer   r   r   GetExcludePatternsForDirI   s    r0   c           
   	   C   s  t j| } t j| tj}t j|r,|S t j| tj}zt|}W n t	yZ   Y nR0 |< t
 }|| |dr|W  d   S W d   n1 s0    Y  t j| tj}zt|d}W n t	y   Y n0 |v zddl}W n ty   tdY n0 ||}|di dd}|durJ|W  d   S W d   n1 s`0    Y  | rt j| r| t jt jjkrqt j| } qt jtj}	t j|	r|	S |S )a  Return default style name for a given directory.

  Looks for .style.yapf or setup.cfg or pyproject.toml in the parent
  directories.

  Arguments:
    dirname: (unicode) The name of the directory.
    default_style: The style to return if nothing is found. Defaults to the
                   global default style ('pep8') unless otherwise specified.

  Returns:
    The filename if found, otherwise return the default style.
  ZyapfNr$   r   r#   r%   )r   r   abspathr-   r   ZLOCAL_STYLEr.   ZSETUP_CONFIGr   IOErrorr   	read_filehas_sectionZPYPROJECT_TOMLr&   r'   r   r   r(   r)   basenamesepr/   
expanduserZGLOBAL_STYLE)
r/   Zdefault_styleZ
style_fileconfig_filer    configr*   r+   Z
style_dictZglobal_filer   r   r   GetDefaultStyleForDira   sN    

0


2r:   c                 C   s   t | ||S )z7Return the list of files specified on the command line.)_FindPythonFiles)Zcommand_line_file_list	recursiveexcluder   r   r   GetCommandLineFiles   s    r>    Fc                 C   sX   |r@t j| d|d}|| W d   qT1 s40    Y  ntjj|d dS )ax  Emit the reformatted code.

  Write the reformatted code into the file, if in_place is True. Otherwise,
  write to stdout.

  Arguments:
    filename: (unicode) The name of the unformatted file.
    reformatted_code: (unicode) The reformatted code.
    encoding: (unicode) The encoding of the file.
    in_place: (bool) If True, then write the reformatted code to the file.
  wmodeencodingNzutf-8)codecsr   writesysstdoutbufferencode)r   Zreformatted_coderC   Zin_placer    r   r   r   WriteReformattedCode   s    *rJ   c                 C   s   t dtdtdi}| D ]V}|t r4|t   d7  < q|trP|t  d7  < q|tr|t  d7  < qttt tf|jddd S )z0Retrieve the line ending of the original source.r      T)keyreverse)CRLFCRLFendswithsortedr)   )linesZendingsr!   r   r   r   
LineEnding   s    


rT   c                    s|  |r t dd |D r td|o0dd |D }g }| D ]:}|dkrX|rXt||rXq:tj|r`|sxtd| g }t|D ]\ }dkr|rt|r| qnt fdd|D rq|D ]4}tj	|}|rt||rqt
|r|| qЇ fd	dtt D }	|	D ]6}
tj	|
}t||rN|| n
 |
 q$qq:tj|r:|| q:|S )
zFind all Python files.c                 s   s   | ]}| d V  qdS r
   r   r   r   r   r   r      r   z#_FindPythonFiles.<locals>.<genexpr>z,path in '--exclude' should not start with ./c                 S   s   g | ]}| d tjj qS )/)rstripr   r   r6   r   r   r   r   
<listcomp>   r   z$_FindPythonFiles.<locals>.<listcomp>.z2directory specified without '--recursive' flag: %sc                 3   s   | ]}  |V  qd S Nr   r   )dirpathr   r   r      r   c                    s   g | ]}  d qS )r   )pop)r   i)dirnamesr   r   rW      r   )r   r   r   	IsIgnoredr   r   isdirwalkr   r-   IsPythonFilerangelenr   )	filenamesr<   r=   Zpython_filesr   Zexcluded_dirsfilelistffilepathZ	dirnames_r/   Zdir_r   )r]   rZ   r   r;      sD    


r;   c                    sP   |du rdS   tjj  dtjj r: dd  qt fdd|D S )z8Return True if filename matches any patterns in exclude.NFrX      c                 3   s$   | ]}t   |tjjV  qd S rY   )fnmatchrV   r   r   r6   r   r   r   r   r     r   zIsIgnored.<locals>.<genexpr>)lstripr   r   r6   r   r   )r   r=   r   rj   r   r^      s    r^   c              	   C   s  t j| d dkrdS ztt| d}t|jd }W d   n1 sH0    Y  tj| d|d}|  W d   n1 s0    Y  W n, ty   d	}Y n t	t
fy   Y d
S 0 z>tj| d|d}|d}W d   n1 s0    Y  W n t	y   Y d
S 0 td|S )z)Return True if filename is a Python file.rK   z.pyTr$   r   Nr   rA   zlatin-1F   z^#!.*\bpython[23]?\b)r   r   splitextr   r   readlinerD   readUnicodeDecodeErrorr2   SyntaxErrorrematch)r   r    rC   
first_liner   r   r   ra     s"    ,*,ra   c                 C   s<   t | d}t|jd W  d   S 1 s.0    Y  dS )zReturn the file's encoding.r$   r   N)r   r   rn   )r   r    r   r   r   FileEncoding"  s    ru   )r?   F)__doc__rD   ri   r   rr   rF   configparserr   tokenizer   Zyapf.yapflibr   r   rO   rP   rN   r"   r,   r0   ZDEFAULT_STYLEr:   r>   rJ   rT   r;   r^   ra   ru   r   r   r   r   <module>   s2   B  
0
