
    `N`z!                         d 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
 Zd Ze	j        fdZd Z	 	 ddZd Zd Zd Zd Zd ZdS )zyInterface to file resources.

This module provides functions for interfacing with files: opening, writing, and
querying.
    N)tokenize)errors)	py3compat)style
z
c                    g }t           j                            |           rt          j        | t           j                  rt          | d          5 }|D ]R}|                                r<|                    d          s'|                    |                                           S	 ddd           n# 1 swxY w Y   t          d |D                       rt          j        d          |S )z&Get a list of file patterns to ignore.r#Nc              3   @   K   | ]}|                     d           V  dS z./N
startswith.0es     ;lib/python3.11/site-packages/yapf/yapflib/file_resources.py	<genexpr>z._GetExcludePatternsFromFile.<locals>.<genexpr>-   s.      
7
7!1<<
7
7
7
7
7
7    z,path in .yapfignore should not start with ./)ospathisfileaccessR_OKopenstripr   appendanyr   	YapfError)filenameignore_patternsfdlines       r   _GetExcludePatternsFromFiler$   #   s$   /W^^H M")Hbg">"> M	h		 / / /$::<< 	/ 4 4 	/

 
 
.
.
.// / / / / / / / / / / / / / /
 
7
7
7
7
777 MKLLL	s   AB44B8;B8c                 `    t           j                            | d          }t          |          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.yapfignore)r   r   joinr$   )dirnameignore_files     r   GetExcludePatternsForDirr)   3   s'     Wm44+	$[	1	11r   c                 v   t           j                            |           } 	 t           j                            | t          j                  }t           j                            |          r|S t           j                            | t          j                  }	 t          |          }|5  t          j
                    }|                    |           |                    d          r|cddd           S 	 ddd           n# 1 swxY w Y   n# t          $ r Y nw xY wt           j                            | t          j                  }	 t          |          }|5  	 ddl}n"# t           $ r t#          j        d          w xY w|                    |          }|                    di                               dd          }||cddd           S 	 ddd           n# 1 swxY w Y   n# t          $ r Y nw xY w| rQt           j                            |           r2| t           j                            t           j        j                  k    rn!t           j                            |           } Nt           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.
  TyapfNr   zGtoml package is needed for using pyproject.toml as a configuration filetool)r   r   abspathr&   r   LOCAL_STYLEexistsSETUP_CONFIGr   r   ConfigParser	read_filehas_sectionIOErrorPYPROJECT_TOMLtomlImportErrorr   r   loadgetbasenamesepr'   
expanduserGLOBAL_STYLE)
r'   default_style
style_fileconfig_filer"   configr6   pyproject_toml
style_dictglobal_files
             r   GetDefaultStyleForDirrE   C   sX    GOOG$$'*'gu'899J	w~~j!!  ',,w(:;;K	b   '))f%% 		       	                  
d ',,w(<==Kb   	
++++ 	 	 	 W  	
 ;//#''3377EE
!        "                  
d   27++G44 27??27;////goog&&GU*'X ""5#566+W^^K   	sa   D (?DDD
DDG* GE G E??AGG"%G"*
G76G7c                 $    t          | ||          S )z7Return the list of files specified on the command line.)_FindPythonFiles)command_line_file_list	recursiveexcludes      r   GetCommandLineFilesrK      s    	0)W	E	EEr    Fc                     |rHt          j        | d|d          5 }|                    |           ddd           dS # 1 swxY w Y   dS t          j        |           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.
  wrL   )modeencodingnewlineN)r   open_with_encodingwriteEncodeAndWriteToStdout)r    reformatted_coderP   in_placer"   s        r   WriteReformattedCoderW      s      7		%sXr
; 
; 
; !>@hh   ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! $%566666s   >AAc                    t           dt          dt          di}| D ]}|                    t                     r|t           xx         dz  cc<   2|                    t                    r|t          xx         dz  cc<   b|                    t                    r|t          xx         dz  cc<   t	          ||j        d          pt          gd         S )z0Retrieve the line ending of the original source.r      T)keyreverse)CRLFCRLFendswithsortedr9   )linesendingsr#   s      r   
LineEndingrc      s    1b!R#'  d}}T dmmmqmmmm	r		 bkkkQkkkk	r		 bkkkQkkk
gk4
8
8
8
@RD!	DDr   c                 @   |r-t          d |D                       rt          j        d          |od |D             }g }| D ]}|dk    r|rt          ||          rt          j                            |          rh|st          j        d|z            g }t	          j        |          D ]5\  }dk    r(|r&t          |          r|                               5t          fd|D                       rQ|D ]Y}t          j        	                    |          }|rt          ||          r5t          |          r|                    |           Zfdt          t                              D             }	|	D ]]}
t          j        	                    |
          }t          ||          r|                    |           H                    |
           ^7t          j                            |          r|                    |           |S )zFind all Python files.c              3   @   K   | ]}|                     d           V  dS r   r   r   s     r   r   z#_FindPythonFiles.<locals>.<genexpr>   s.      99AQ\\$''999999r   z,path in '--exclude' should not start with ./c                 \    g | ])}|                     d t          j        j        z             *S )/)rstripr   r   r;   r   s     r   
<listcomp>z$_FindPythonFiles.<locals>.<listcomp>   s-    FFF#"344FFFr   .z2directory specified without '--recursive' flag: %sc              3   B   K   | ]}                     |          V  d S Nr   )r   r   dirpaths     r   r   z#_FindPythonFiles.<locals>.<genexpr>   s1      >>1##A&&>>>>>>r   c                 :    g | ]}                     d           S )r   )pop)r   idirnamess     r   ri   z$_FindPythonFiles.<locals>.<listcomp>   s#    CCCX\\!__CCCr   )r   r   r   	IsIgnoredr   r   isdirwalkr   r&   IsPythonFilerangelenr   )	filenamesrI   rJ   python_filesr    excluded_dirsfilelistffilepath	dirnames_r'   dir_rq   rm   s               @@r   rG   rG      sw    K9999999 K

I
J
JJFFFgFFF', $$ $$h37y7'C'C	w}}X !$ M@8KM M 	M m)+):): % %
%'8Xc>>g>)GW*E*E>


w
'
'
'
>>>>>>>>> 	
 	* 	*AW\\'1--( 8W55 (## *))) DCCCeCMM.B.BCCC	  	% 	%Ggw//$tW%% %  &&&&OOG$$$$	%%%2 
	!	! $(###	r   c                 :    |dS                       t          j        j                                        dt          j        j        z             r1 dd                               dt          j        j        z             1t           fd|D                       S )z8Return True if filename matches any patterns in exclude.NFrj      c              3      K   | ];}t          j         |                    t          j        j                            V  <d S rl   )fnmatchrh   r   r   r;   )r   r   r   s     r   r   zIsIgnored.<locals>.<genexpr>   s?      KKaW_T188BGK#8#899KKKKKKr   )lstripr   r   r;   r   r   )r   rJ   s   ` r   rr   rr      s    _5	RW[	!	!$bgk)** 8D 	bgk)** 	KKKK7KKK	K	KKr   c                    t           j                            |           d         dk    rdS 	 t          | d          5 }t	          j        |j                  d         }ddd           n# 1 swxY w Y   t          j        | d|          5 }|	                                 ddd           n# 1 swxY w Y   n%# t          $ r d	}Y nt          t          f$ r Y d
S w xY w	 t          j        | d|          5 }|                    d          }ddd           n# 1 swxY w Y   n# t          $ r Y d
S w xY wt          j        d|          S )z)Return True if filename is a Python file.rY   z.pyTrbr   Nr
   )rO   rP   zlatin-1F   z^#!.*\bpython[23]?\b)r   r   splitextr   r   detect_encodingreadliner   rR   readUnicodeDecodeErrorr4   SyntaxErrorrematch)r    r"   rP   
first_lines       r   ru   ru      s/   Wh"e++4	h		 :)"+66q9h: : : : : : : : : : : : : : : 
	%sX
/ 
/ 
/ 24ggiii               
   HHH
;	    55			%sX
/ 
/ 
/ $24;;s##j$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
   55 
):	6	66s   B8  A)B8 )A--B8 0A-1B8 B, B8 ,B00B8 3B04B8 8CCCD# 5DD# DD# DD# #
D10D1c                     t          | d          5 }t          j        |j                  d         cddd           S # 1 swxY w Y   dS )zReturn the file's encoding.r   r   N)r   r   r   r   )r    r"   s     r   FileEncodingr     s    Hd 4r#BK0034 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4s   =AA)rL   F)__doc__r   r   r   lib2to3.pgen2r   yapf.yapflibr   r   r   r]   r^   r\   r$   r)   DEFAULT_STYLErE   rK   rW   rc   rG   rr   ru   r    r   r   <module>r      sH     				 				 " " " " " "       " " " " " "      		   2 2 2  271D > > > >BF F F #%"'7 7 7 7.
E 
E 
E- - -`L L L7 7 7>4 4 4 4 4r   