a
    `N`0                     @   s   d Z ddlm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dl	mZ dd	l	mZ d
Zdd Zdd ZdddZdddZdd Zdd Zdd Zedkre  dS )a  YAPF.

YAPF uses the algorithm in clang-format to figure out the "best" formatting for
Python code. It looks at the program as a series of "unwrappable lines" ---
i.e., lines which, if there were no column limit, we would place all tokens on
that line. It then uses a priority queue to figure out what the best formatting
is --- i.e., the formatting with the least penalty.

It differs from tools like autopep8 and pep8ify in that it doesn't just look for
violations of the style guide, but looks at the module as a whole, making
formatting decisions based on what's the best format for each line.

If no filenames are specified, YAPF reads the code from stdin.
    )print_functionN)tokenize)errors)file_resources)	py3compat)style)yapf_apiz0.31.0c                 C   s8  t  }|| dd }|jr0tdt dS |j}|jrHt| dS |j	rft
|jdkrf|d |j	durzt|j	nd}|js|js|jr|d g }tjjrqz|t  W q ty   Y qY q ty   Y dS 0 q|du r|jstt }dd |D }t|d |d< z.tjt d	!|d	 d
|||j"d\}}W nJ t#j$y }	 z.t%&d|	j'd d |	j'd f W Y d}	~	n
d}	~	0 0 t(d| dS t)t }
t*|j|j+|j,pg |
 }|st%&dt-|||j|j|j|j|j"|j.|j/|j0d
}|r4|js0|j/r4dS dS )a[  Main program.

  Arguments:
    argv: command-line arguments, such as sys.argv (including the program name
      in argv[0]).

  Returns:
    Zero on successful program termination, non-zero otherwise.
    With --diff: zero if there were no changes, non-zero otherwise.

  Raises:
    YapfError: if none of the supplied files were Python files.
     Nzyapf {}r   z-cannot use -l/--lines with more than one filez=cannot use --in-place or --diff flags when reading from stdinc                 S   s   g | ]}|  qS  )rstrip).0liner
   r
   ,lib/python3.9/site-packages/yapf/__init__.py
<listcomp>d       zmain.<locals>.<listcomp>
z<stdin>)filenamestyle_configlinesverifyz%s:%sz<stdout>z.Input filenames did not match any python files)r   no_local_stylein_place
print_diffr   parallelquietverbose)1_BuildParser
parse_argsversionprintformat__version__r   Z
style_help
_PrintHelpr   lenfileserror	_GetLinesr   diffsysstdinclosedappendr   Z	raw_inputEOFErrorKeyboardInterruptr   r   GetDefaultStyleForDirosgetcwdZ	removeBOMr   Z
FormatCodeZunicodejoinr   r   
TokenErrorr   	YapfErrorargsWriteReformattedCodeZGetExcludePatternsForDirZGetCommandLineFiles	recursiveZexcludeFormatFilesr   r   r   )argvparserr4   r   r   Zoriginal_sourcesourceZreformatted_source_eZ!exclude_patterns_from_ignore_filer$   changedr
   r
   r   main.   s~    


8

r>   c                 C   s   | j du r | js tt | _ t t | j  td t	t 
  D ]v\}}| D ]}td|rjdpld|dd qZt |}t|tst|trdtt|}t| d|dd t  qJdS )	zPrints the help menu.Nz[style]#  )sepz, =)r   r   r   r.   r/   r0   ZSetGlobalStyleZCreateStyleFromConfigr   sortedZHelpitems
splitlinesZGet
isinstancesetlistr1   mapstrlower)r4   optionZ	docstringr   Zoption_valuer
   r
   r   r"      s    
r"   Fc
                    s   d}
|rddl }ddl}t| t| }|j|P  f	dd| D }|j|D ]}|
| O }
qfW d   q1 s0    Y  n&| D ] }|
t	|	O }
q|
S )aF  Format a list of files.

  Arguments:
    filenames: (list of unicode) A list of files to reformat.
    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. This argument
      overrides the 'args.lines'. It can be used by third-party code (e.g.,
      IDEs) when reformatting a snippet of code.
    style_config: (string) Style name or file path.
    no_local_style: (string) If style_config is None don't search for
      directory-local style configuration.
    in_place: (bool) Modify the files in place.
    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.
    parallel: (bool) True if should format multiple files in parallel.
    quiet: (bool) True if should output nothing.
    verbose: (bool) True if should print out filenames while processing.

  Returns:
    True if the source code changed in any of the files being formatted.
  Fr   Nc                    s(   g | ] }  t|
qS r
   )Zsubmit_FormatFile)r   r   	executorr   r   r   r   r   r   r   r   r
   r   r      s
   
zFormatFiles.<locals>.<listcomp>)
multiprocessingconcurrent.futuresmin	cpu_countr#   ZfuturesProcessPoolExecutorZas_completedresultrN   )	filenamesr   r   r   r   r   r   r   r   r   r=   rQ   Z
concurrentworkersZfuture_formatsZfuturer   r
   rO   r   r7      s      .
r7   c	              
   C   s   |r|st d|   |du r2|s2ttj| }z$tj| |||||tj	d\}	}
}W nt t
jy } z0td| |jd d |jd f W Y d}~n4d}~0  ty } z| |_ W Y d}~n
d}~0 0 |s|s|	rt| |	|
| |S )zFormat an individual file.zReformatting %sN)r   r   r   r   r   loggerz%s:%s:%sr	   r   )r   r   r.   r/   pathdirnamer   Z
FormatFileloggingZwarningr   r2   r   r3   r4   SyntaxErrorr   r5   )r   r   r   r   r   r   r   r   r   Zreformatted_codeencodingZ
has_changer<   r
   r
   r   rN      s4    

8
rN   c                 C   sn   g }| D ]`}t tt|dd}|d dk r<td| |d |d krZtd| |t| q|S )a\  Parses the start and end lines from a line string like 'start-end'.

  Arguments:
    line_strings: (array of string) A list of strings representing a line
      range like 'start-end'.

  Returns:
    A list of tuples of the start and end line numbers.

  Raises:
    ValueError: If the line string failed to parse or was an invalid line range.
  -r	   r   zinvalid start of line range: %rz(end comes before start in line range: %r)rI   rJ   intsplitr   r3   r+   tuple)Zline_stringsr   line_stringr   r
   r
   r   r&      s    r&   c                  C   s,  t jdd} | jddddd |  }|jdd	dd
d |jddddd |jddddd |  }|jddddd |jddddddd | jddddddd | jdd d!tjtjtjf d | jd"dd#d | jd$dd%d | jd&dt jd | jd'd(dd)d | jd*d+dd,d | jd-d.d/d0 | S )1zpConstructs the parser for the command line arguments.

  Returns:
    An ArgumentParser instance for the CLI.
  zFormatter for Python code.)descriptionz-vz	--version
store_truezshow version number and exit)actionhelpz-dz--diffz#print the diff for the fixed sourcez-iz
--in-placezmake changes to files in placez-qz--quietz#output nothing and set return valuez-rz--recursivez run recursively over directoriesz-lz--linesz	START-ENDr+   Nz%range of lines to reformat, one-based)metavarrf   defaultrg   z-ez	--excludeZPATTERNz-patterns for files to exclude from formattingz--stylestorea$  specify formatting style: either a style name (for example "pep8" or "google"), or the name of a file with style settings. The default is pep8 unless a %s or %s or %s file located in the same directory as the source or one of its parent directories (for stdin, the current directory is used).z--style-helpzeshow style settings and exit; this output can be saved to .style.yapf to make your settings permanentz--no-local-stylez'don't search for local style definitionz--verifyz-pz
--parallelz^run yapf in parallel when formatting multiple files. Requires concurrent.futures in Python 2.Xz-vvz	--verbosez%print out file names while processingr$   *z-reads from stdin when no files are specified.)nargsrg   )	argparseArgumentParseradd_argumentadd_mutually_exclusive_groupr   ZLOCAL_STYLEZSETUP_CONFIGZPYPROJECT_TOMLSUPPRESS)r9   Zdiff_inplace_quiet_groupZlines_recursive_groupr
   r
   r   r     s    	r   c               
   C   sb   zt tt j W nH tjy\ }  z.t jdt|  d  t d W Y d } ~ n
d } ~ 0 0 d S )Nzyapf: r   r	   )	r(   exitr>   r8   r   r3   stderrwriterK   )r<   r
   r
   r   run_mainh  s
    ru   __main__)NFFFFFFF)NFFFFFF)__doc__Z
__future__r   rm   r\   r/   r(   Zlib2to3.pgen2r   Zyapf.yapflibr   r   r   r   r   r!   r>   r"   r7   rN   r&   r   ru   __name__r
   r
   r
   r   <module>   sH   ^        
6       
&X