
    `N`*                         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dlm
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ 	 	 	 	 	 	 ddZ	 	 	 	 	 d dZd Zd!dZd ZdZdZd Zd Zd Zd Zd"dZdS )#ac  Entry points for YAPF.

The main APIs that YAPF exposes to drive the reformatting.

  FormatFile(): reformat a file.
  FormatCode(): reformat a string of code.

These APIs have some common arguments:

  style_config: (string) Either a style name or a path to a file that contains
    formatting style settings. If None is specified, use the default style
    as set in style.DEFAULT_STYLE_FACTORY
  lines: (list of tuples of integers) A list of tuples of lines, [start, end],
    that we want to format. The lines are 1-based indexed. It can be used by
    third-party code (e.g., IDEs) when reformatting a snippet of code rather
    than a whole file.
  print_diff: (bool) Instead of returning the reformatted source, return a
    diff that turns the formatted source into reformatter source.
  verify: (bool) True if reformatted code should be verified for syntax.
    N)parse)blank_line_calculator)comment_splicer)continuation_splicer)file_resources)identify_container)	py3compat)pytree_unwrapper)pytree_utils)reformatter)split_penalty)style)subtype_assignerFc                    t                       |r|rt          d          t          | |          \  }}}	t          ||| |||          \  }
}|
                    d          rJ|
                    d                              d          }|                    d |D                       |z   }
|r$|r||
k    rt          j        | |
|	|           d|	|fS |
|	|fS )a  Format a single Python file and return the formatted code.

  Arguments:
    filename: (unicode) The file to reformat.
    style_config: (string) Either a style name or a path to a file that contains
      formatting style settings. If None is specified, use the default style
      as set in style.DEFAULT_STYLE_FACTORY
    lines: (list of tuples of integers) A list of tuples of lines, [start, end],
      that we want to format. The lines are 1-based indexed. It can be used by
      third-party code (e.g., IDEs) when reformatting a snippet of code rather
      than a whole file.
    print_diff: (bool) Instead of returning the reformatted source, return a
      diff that turns the formatted source into reformatter source.
    verify: (bool) True if reformatted code should be verified for syntax.
    in_place: (bool) If True, write the reformatted code back to the file.
    logger: (io streamer) A stream to output logging.

  Returns:
    Tuple of (reformatted_code, encoding, changed). reformatted_code is None if
    the file is successfully written to (having used in_place). reformatted_code
    is a diff if print_diff is True.

  Raises:
    IOError: raised if there was an error reading the file.
    ValueError: raised if in_place and print_diff are both specified.
  z)Cannot pass both in_place and print_diff.)style_configfilenamelines
print_diffverify
c              3      K   | ]}|V  d S N .0lines     5lib/python3.11/site-packages/yapf/yapflib/yapf_api.py	<genexpr>zFormatFile.<locals>.<genexpr>g   s"      %=%=td%=%=%=%=%=%=    N)	_CheckPythonVersion
ValueErrorReadFile
FormatCoderstripsplitjoinr   WriteReformattedCode)r   r   r   r   r   in_placeloggeroriginal_sourcenewlineencodingreformatted_sourcechangeds               r   
FormatFiler/   7   s'   B  B* B
@
A
AA'/&'A'A$/7H *! ! !g t$$ H%%d++11$77E %=%=u%=%=%===G # >?.@@@)(4F*2H> > >7""	Xw	..r   	<unknown>c                 l   t                       t          j        t          j        |                     |                     d          s| dz  } 	 t          j        |           }n*# t          j        $ r}|dz   |j	        z   |_	         d}~ww xY wt          j        |           t          j        |           t          j        |           t!          j        |           t%          j        |           t)          j        |           t-          j        |          }|D ]}	|	                                 t3          |          }t5          ||           t7          j        t;          |          ||          }
| |
k    r|rdn|
dfS t=          | |
|          }|r||                                dk    fS |
dfS )a)  Format a string of Python code.

  This provides an alternative entry point to YAPF.

  Arguments:
    unformatted_source: (unicode) The code to format.
    filename: (unicode) The name of the file being reformatted.
    style_config: (string) Either a style name or a path to a file that contains
      formatting style settings. If None is specified, use the default style
      as set in style.DEFAULT_STYLE_FACTORY
    lines: (list of tuples of integers) A list of tuples of lines, [start, end],
      that we want to format. The lines are 1-based indexed. It can be used by
      third-party code (e.g., IDEs) when reformatting a snippet of code rather
      than a whole file.
    print_diff: (bool) Instead of returning the reformatted source, return a
      diff that turns the formatted source into reformatter source.
    verify: (bool) True if reformatted code should be verified for syntax.

  Returns:
    Tuple of (reformatted_source, changed). reformatted_source conforms to the
    desired formatting style. changed is True if the source changed.
  r   z: N F)r   T) r    r   SetGlobalStyleCreateStyleFromConfigendswithr   ParseCodeToTreer   
ParseErrormsgr   SpliceCommentsr   SpliceContinuationsr   AssignSubtypesr   IdentifyContainersr   ComputeSplitPenaltiesr   CalculateBlankLinesr
   UnwrapPyTreeCalculateFormattingInformation_LineRangesToSet_MarkLinesToFormatr   Reformat_SplitSemicolons_GetUnifiedDiffstrip)unformatted_sourcer   r   r   r   r   treeeuwlinesuwlr-   	code_diffs               r   r#   r#   q   s   8 u2<@@AAA		$	$T	*	* $
'(:;;DD		 
 
 
tOae#AE	

  &&&*4000!$''''---%d++++D111)$//' ) )c&&((((
5
!
!%We$$$"+w0 0 ---322!3U::,xA A A)  .ioo''2---	T	!!s   A% %B4BBc                      d} t           j        d         dk    r't           j        d         dk     rt          |           d S t           j        d         dk    r%t           j        d         dk     rt          |           d S d S )Nz-yapf is only supported for Python 2.7 or 3.4+r                  )sysversion_infoRuntimeError)errmsgs    r   r    r       s    :&aA
Q    
a
Q     r   c                    	 t          j        |           }t          j        | d|d          5 }|                                }ddd           n# 1 swxY w Y   t          j        |          }d                    d |D                       dz   }|||fS # t          $ r}|r ||            d}~wt          $ r}|r |d|             ||            d}~ww xY w)a  Read the contents of the file.

  An optional logger can be specified to emit messages to your favorite logging
  stream. If specified, then no exception is raised. This is external so that it
  can be used by third-party applications.

  Arguments:
    filename: (unicode) The name of the file.
    logger: (function) A function or lambda that takes a string and emits it.

  Returns:
    The contents of filename.

  Raises:
    IOError: raised if there was an error reading the file.
  rr2   )moder,   r+   Nr   c              3   @   K   | ]}|                     d           V  dS )z
N)r$   r   s     r   r   zReadFile.<locals>.<genexpr>   s.      ==tt{{6**======r   z@Could not parse %s! Consider excluding this file with --exclude.)	r   FileEncodingr	   open_with_encoding	readlines
LineEndingr&   IOErrorUnicodeDecodeError)r   r)   r,   fdr   line_endingsourceerrs           r   r"   r"      s]   "
*844H 
	%sXr
; 
; 
; >@llnne               !+E22KYY==u=====DF;((	 
 
 
 fSkkk		 
 
 
 fO  fSkkk	
sF   ,B AB AB A=B 
CB--C:CCc                 b    g }| D ])}|                     |                                           *|S r   )extendSplit)rJ   resuwlines      r   rD   rD      s8    
#  fJJv||~~	*r   z^#.*\byapf:\s*disable\bz^#.*\byapf:\s*enable\bc                     | dS t                      }t          |           D ]+\  }}|                    t          ||dz                        ,|S )z#Return a set of lines in the range.NrO   )setsortedupdaterange)line_rangesline_setlowhighs       r   rA   rA      s[     4UU(+&& * *ic4OOE#tax(())))	/r   c                    |r@| D ]=}|                     t          |j        |j        j        dz                        |_        >d}|t          |           k     r}| |         }|j        r
t          |j        j	        
                                          r|dz  }|t          |           k     r| |         }|j        rt          |j        j	        
                                          rmt          j        t          |j        j	        
                                                    d          d         
                                t          j                  snd|_        |dz  }|t          |           k     nHt          j        t          |j        j	        
                                t          j                  rd|_        |dz  }|t          |           k     {dS dS )z1Skip sections of code that we shouldn't reformat.rO   r   r   TN)intersectionrn   linenolastdisablelen
is_comment_DisableYAPFfirstvaluerF   _EnableYAPFresearchDISABLE_PATTERNr%   
IGNORECASE)rJ   r   ri   indexs       r   rB   rB      s   
 8 8 8 --
v{1A5
6
68 8 8fnn
 %GU^F 	fl(..00	1	1 

c'll""5>& ;v|/A/G/G/I/I#J#J 9_#\/5577==dCCBGMMOO], ,  &.
1*% c'll"" 
?FK$5$;$;$=$=r}	M	M fn	QJE! 	Gr   c                 B   t          j        t          |                     d          d                                         t           j                  pOt          j        t          |                     d          d                                         t           j                  S Nr   r   rt   )r   r   r   r%   rF   r   r   s    r   r{   r{     s|    
)OJJt$$Q'--//@ @ A
)OJJt$$R(..00"-A ABr   c                 B   t          j        t          |                     d          d                                         t           j                  pOt          j        t          |                     d          d                                         t           j                  S r   )r   r   ENABLE_PATTERNr%   rF   r   r   s    r   r~   r~   #  s|    
)NJJt$$Q'--//@ @ A
)NJJt$$R(..00"-A ABr   codec                     |                                  } |                                 }d                    t          j        | |||ddd                    dz   S )zGet a unified diff of the changes.

  Arguments:
    before: (unicode) The original source code.
    after: (unicode) The reformatted source code.
    filename: (unicode) The code's filename.

  Returns:
    The unified diff text.
  r   z
(original)z(reformatted)r2   )lineterm)
splitlinesr&   difflibunified_diff)beforeafterr   s      r   rE   rE   *  sp     &




%	





  
 
 
 r   )NNFFFN)r0   NNFFr   )r   )__doc__r   r   rS   lib2to3.pgen2r   yapf.yapflibr   r   r   r   r   r	   r
   r   r   r   r   r   r/   r#   r    r"   rD   r   r   rA   rB   r{   r~   rE   r   r   r   <module>r      s   *  				 



       . . . . . . ( ( ( ( ( ( - - - - - - ' ' ' ' ' ' + + + + + + " " " " " " ) ) ) ) ) ) % % % % % % $ $ $ $ $ $ & & & & & &       ) ) ) ) ) ) !7/ 7/ 7/ 7/v $ A" A" A" A"H! ! !%
 %
 %
 %
P   -*
 
 
  :B B BB B B     r   