
    ?d%                         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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y)zyInterface to file resources.

This module provides functions for interfacing with files: opening, writing, and
querying.
    N)errors)	py3compat)style
z
c                    g }t         j                  j                  |       rt        j                  | t         j                        rt        | d      5 }|D ]D  }|j                         s|j                  d      r&|j                  |j                                F 	 ddd       t        d |D              rt        j                  d      |S # 1 sw Y   2xY w)z7Get a list of file patterns to ignore from .yapfignore.r#Nc              3   >   K   | ]  }|j                  d         ywz./N
startswith.0es     =/usr/lib/python3/dist-packages/yapf/yapflib/file_resources.py	<genexpr>z4_GetExcludePatternsFromYapfIgnore.<locals>.<genexpr>*        
7!1<<
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   !_GetExcludePatternsFromYapfIgnorer$   !   s    /WW^^H"))Hbgg">	h	 / /$::< 4

 
 
.//
 
7
77KLL	/ /s   C)C;!CCc                    g }	 ddl }t        j
                  j                  |       rt	        j                  | t        j                        rt        | d      5 }|j                  |      }ddd       j                  di       j                  di       j                  dg       }t        d |D              rt        j                  d	      |S # t        $ r t        j                  d      w xY w# 1 sw Y   xY w)
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tool
yapfignorer!   c              3   >   K   | ]  }|j                  d         ywr   r   r   s     r   r   z7_GetExcludePatternsFromPyprojectToml.<locals>.<genexpr>@   r   r   z/path in pyproject.toml should not start with ./)tomliImportErrorr   r   r   r   r   r   r   r   loadgetr   )r    r!   tomllibr"   pyproject_tomls        r   $_GetExcludePatternsFromPyprojectTomlr1   0   s    / WW^^H"))Hbgg">	h	 (||B'n($(()+--0S13.558S9JB5O  
7
77NOO	 
 


	 ( (s   C C.C+.C7c                 >   g }t         j                  j                  | d      }t         j                  j                  |      r|t	        |      z  }t         j                  j                  | d      }t         j                  j                  |      r|t        |      z  }|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$   r1   )dirnamer!   yapfignore_filepyproject_toml_files       r   GetExcludePatternsForDirr8   F   s{     /GGLL-8/WW^^O$8IIOW.>?WW^^'(;<OPPO	    c                 X   t         j                  j                  |       } 	 t         j                  j                  | t        j
                        }t         j                  j                  |      r|S t         j                  j                  | t        j                        }	 t        |      }|5  t        j                         }|j                  |       |j                  d      r|cddd       S 	 ddd       t         j                  j                  | t        j                        }	 t        |d      }|5  	 ddl}|j'                  |      }|j)                  di       j)                  dd      }||cddd       S 	 ddd       | rYt         j                  j+                  |       r:| t         j                  j                  t         j                  j,                        k(  rn!t         j                  j/                  |       } t         j                  j1                  t        j2                        }	t         j                  j                  |	      r|	S |S # 1 sw Y   cxY w# t        $ r Y rw xY w# t         $ r t#        j$                  d      w xY w# 1 sw Y   xY w# t        $ r Y %w xY w)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.
  yapfNr'   r   r&   r(   )r   r   abspathr3   r   LOCAL_STYLEr4   SETUP_CONFIGr   r   ConfigParser	read_filehas_sectionIOErrorPYPROJECT_TOMLr+   r,   r   r   r-   r.   basenamesepr5   
expanduserGLOBAL_STYLE)
r5   default_style
style_fileconfig_filer"   configr/   r0   
style_dictglobal_files
             r   GetDefaultStyleForDirrN   ^   s6    GGOOG$'gu'8'89J	ww~~j! '',,w(:(:;K	b  '')f%	  & '',,w(<(<=KT"b  	$
! !b)#''377E
!  " 277++G4277??277;;//ggoog&GU 	X ""5#5#56+WW^^K 	G   
&  	$  #$ $	$   
sT   I ,8I&J 4J6I-:6JI	I*)I*-JJJ	J)(J)c                     t        | ||      S )z7Return the list of files specified on the command line.)_FindPythonFiles)command_line_file_list	recursiveexcludes      r   GetCommandLineFilesrT      s    	0)W	EEr9   c                     |r4t        j                  | d|d      5 }|j                  |       ddd       yt        j                  |       y# 1 sw Y   yxY w)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encodingnewlineN)r   open_with_encodingwriteEncodeAndWriteToStdout)r    reformatted_coderY   in_placer"   s        r   WriteReformattedCoder`      sY     		%	%sXr
; !>@hh ! ! $$%56	! !s   AAc                 v   t         dt        dt        di}| D ]w  }|j                  t               r|t         xx   dz  cc<   *|j                  t              r|t        xx   dz  cc<   Q|j                  t              sg|t        xx   dz  cc<   y t	        t        t         t        f|j
                  d      d   S )z0Retrieve the line ending of the original source.r      T)keyreverse)CRLFCRLFendswithsortedr.   )linesendingsr#   s      r   
LineEndingrl      s    1b!R#' d}}Tdmqm	r	bkQk	r	bkQk 
T2GKK	>q	AAr9   c                 n   |r't        d |D              rt        j                  d      |xr: |D cg c].  }|j                  dt        j
                  j                  z         0 c}}g }| D ]  }|dk7  r|rt        ||      rt        j
                  j                  |      rQ|st        j                  d|z        g }t	        j                  |      D ]  \  }}dk7  r |rt        |      r|j                         -t        fd|D              rB|D ]N  }	t        j
                  j                  |	      }
|rt        |
|      r2t        |
      s>|j                  |
       P t        t        |            D cg c]  }|j                  d       }}|D ]Q  }t        j
                  j                  |      }t        ||      r|j                  |       A|j                  |       S  t        j
                  j!                  |      s|j                  |        |S c c}w c c}w )zFind all Python files.c              3   >   K   | ]  }|j                  d         ywr   r   r   s     r   r   z#_FindPythonFiles.<locals>.<genexpr>   s     9AQ\\$'9r   z,path in '--exclude' should not start with .//.z2directory specified without '--recursive' flag: %sc              3   @   K   | ]  }j                  |        y wNr   )r   r   dirpaths     r   r   z#_FindPythonFiles.<locals>.<genexpr>   s     >1##A&>s   r   )r   r   r   rstripr   r   rE   	IsIgnoredisdirwalkr   r3   IsPythonFilerangelenpopr   )	filenamesrR   rS   r   python_filesr    excluded_dirsdirnamesfilelistffilepathi	dirnames_r5   dir_rs   s                  @r   rP   rP      s   999


I
JJFgF#"34F', $$h37y7'C	ww}}X@8KM 	M m)+): %
%'8Xc>g)GW*E


w
'
>>>
 	*AWW\\'1-(8W5(#)	* /4CM.BCX\\!_C	C  	%Ggw/$tW%  &OOG$	%%%2 
	!(#I$$L 
S G< Ds   3H-H2c                 @    |y j                  t        j                  j                          j	                  dt        j                  j                  z         r2 dd   j	                  dt        j                  j                  z         r2t         fd|D              S )z8Return True if filename matches any patterns in exclude.NFrp      c              3      K   | ]A  }t        j                   |j                  t        j                  j                               C y wrr   )fnmatchrt   r   r   rE   )r   r   r   s     r   r   zIsIgnored.<locals>.<genexpr>   s-     KaW__T188BGGKK#89Ks   AA
)lstripr   r   rE   r   r   )r   rS   s   ` r   ru   ru      sq    _	RWW[[	!$bggkk)*8D 	bggkk)*	K7K	KKr9   c                 Z   t         j                  j                  |       d   dk(  ry	 t        | d      5 }t	        j
                  |j                        d   }ddd       t	        j                  | d      5 }|j                          ddd       	 t	        j                  | d|      5 }|j                  d      }ddd       t        j                  d      S # 1 sw Y   xY w# 1 sw Y   ^xY w# t        $ r d	}Y nt        t        f$ r Y y
w xY w# 1 sw Y   XxY w# t        $ r Y y
w xY w)z)Return True if filename is a Python file.rb   z.pyTr'   r   Nr	   )rX   rY   zlatin-1F   z^#!.*\bpython[23]?\b)r   r   splitextr   r   detect_encodingreadliner[   readUnicodeDecodeErrorrB   SyntaxErrorrematch)r    r"   rY   
first_lines       r   rx   rx     s&   WWh"e+	h	 ;**2;;7:h; 
	%	%sX
/ 24ggi		%	%sX
/ $24;;s#j$ 
):	66-; ;  
 H
;	  	$ $ 
 sp   C1 #C C1 7C%C1 D )D;D C"C1 %C.*C1 1D>DDDD 	D*)D*c                     t        | d      5 }t        j                  |j                        d   cddd       S # 1 sw Y   yxY w)zReturn the file's encoding.r'   r   N)r   r   r   r   )r    r"   s     r   FileEncodingr   "  s:    Hd 5r$$R[[1!45 5 5s	   "9A)rW   F)__doc__r   r   r   yapf.yapflibr   r   r   rf   rg   re   r$   r1   r8   DEFAULT_STYLErN   rT   r`   rl   rP   ru   rx   r    r9   r   <module>r      s|     	 	  " 		,0 271D1D ?DF #%"'7.
B-`L7>5r9   