a
    ɳa^                 	   @   s  d Z ddlmZ ddlmZ ddlm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
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZzddlmZ ddlmZ W n, ey   dd	lmZ ddlmZ Y n0 ddlZdd
lm Z  ze! W n e"yR   e#Z!Y n0 dZ$dZ%dZ&dZ'e(dZ)e(dZ*e(dZ+e(dZ,e(dZ-e(dZ.e(dZ/e(dZ0e(dZ1dZ2dZ3dZ4dZ5e6e6dge6dge6g de6g de6g de6g d gZ7d!Z8d"Z9d#Z:d$gZ;d%gd&gd'gd(gd)gg d*d+Z<ej=d,krZej>?d-Z@n"ej>AeBd.pvej>?d/d0Z@ej>Ce@sej=d,krej>?d1Z@nej>Aej>?d/d2Z@d3ZDd4ZEdd7d8ZFdd9d:ZGd;d< ZHd=d> ZIeJeI d?d@ ZKejLdA ejK= eJeK G dBdC dCeMZNdDdE ZOdFdG ZPd	dKdLZQdMdN ZRdOdP ZSdQdR ZTdSdT ZUdUdV ZVd
dWdXZWdYdZ ZXd[d\ ZYd]d^ ZZdd_d`Z[ddbdcZ\ddddeZ]ddhdiZ^ddjdkZ_dldm Z`dndo Zadpdq Zbdrds Zcdtdu ZdddvdwZeddxdyZfdzd{ Zgehd|g d}ZiG d~d deMZjG dd deMZkG dd deMZlG dd delZmG dd delZnG dd delZoG dd delZpG dd delZqdddZrdd Zsdd Ztdd Zudd Zvdd Zwdd Zxdd Zydd ZzdddZ{G dd deMZ|dd Z}dd Z~dddZdd Zdd Zdd Zdd Zdd ZdddZdd ZdddZdd Zdd Zdd ZdddZddÄ ZdddńZdddǄZddɄ Zdd˄ Zddd̈́Zddτ Zddф Zddӄ ZddՄ ZdddׄZddل Zddۄ Zdd݄ Zdd߄ Zdd Zdd Zdd ZdddZdd Zdd Zdd Zdd ZG dd deMZdd Zdd Zdd ZdddZdd Zdd Zdd Zd d ZdddZG dd deMZe ZejZedkree  dS (  a  Automatically formats Python code to conform to the PEP 8 style guide.

Fixes that only need be done once can be added by adding a function of the form
"fix_<code>(source)" to this module. They should return the fixed source code.
These fixes are picked up by apply_global_fixes().

Fixes that depend on pycodestyle should be added as methods to FixPEP8. See the
class documentation for more information.

    )absolute_import)division)print_function)unicode_literalsN)ConfigParser)Error)SafeConfigParser)!STARTSWITH_INDENT_STATEMENT_REGEXz1.6.0

z^#!.*\bpython[23]?\b\s*$z%([\w.]+)\s=\slambda\s*([)(=\w,\s.]*):z#\b(not)\s+([^][)(}{]+?)\s+(in|is)\sz\b(not\s+in|is\s+not)\sz
except\s*:z^(async\s+def|def)\s.*\):z^u?r?(?P<kind>["\']{3})z# *(fmt|autopep8): *onz# *(fmt|autopep8): *off      c   ,%)r   ([{r   r   r   r   )
r   r   r   r   r   +-*///)r   r   r   r   r   r   zE226,E24,W50,W690   W503W504ZW602Zws_commaZidiomsZhas_keynerepr)ZapplyexceptZexitfuncZnumliteralsoperatorZparenreducerenamesZstandarderrorZsys_excthrowZtuple_paramsZ
xreadlines)ZE231ZE721ZW601ZW603ZW604ZW690win32z~\.pycodestyleZXDG_CONFIG_HOMEz	~/.configpycodestylez~\.pep8pep8)z	setup.cfgztox.iniz.pep8z.flake8i   rc                 C   s"   |st | |d}tj| ||ddS )z,Return opened file with a specific encoding.limit_byte_check )modeencodingnewline)detect_encodingioopen)filenamer.   r/   r,    r5   'lib/python3.9/site-packages/autopep8.pyopen_with_encoding   s
    
r7   c              
   C   s   zt | d,}ddlm} ||jd }W d   n1 s>0    Y  t| |d}|| W d   n1 st0    Y  |W S  ttt	fy   Y dS 0 dS )zReturn file encoding.rbr   tokenizeNr/   zlatin-1)
r3   lib2to3.pgen2r:   r1   readliner7   readLookupErrorSyntaxErrorUnicodeDecodeError)r4   r,   
input_filelib2to3_tokenizer/   Z	test_filer5   r5   r6   r1      s    .(r1   c                 C   s4   t | }| W  d   S 1 s&0    Y  dS )zReturn contents of file.N)r7   	readlines)r4   rB   r5   r5   r6   readlines_from_file   s    
rE   c                 c   s`   | dr,|r\tj| r\dd|fV  n0tj|r\|r\|s\|s\|  dr\d| v r\dV  dS )z6Check for missing blank lines after class declaration.def r   zE303 too many blank lines ({})z(self)r   z#E301 expected 1 blank line, found 0N)
startswithr'   ZDOCSTRING_REGEXmatchformat)logical_lineZblank_linesZblank_beforeindent_levelZprevious_logicalr5   r5   r6   extended_blank_lines   s    
rL   c           )      c   s  |d d d }d|d d d  | }|s4|dkr8dS |  d}d }	}
|dkrXtfn
tdt f}dg| }dg| }dgg}dg}i }|d d }|d g}d}d}d}d	}|D ]\}}}}}|	|d | k }|r|d | }	| o|tjtjfv}|}|r:|}t|| ||	< |tjko,|d
v }t||
 D ]*}||	 ||  }||v }|r: qfq:||
 r||||
 k}| o|dko|	|d } |r||
 r|d ||
 kr:|d
||
 fV  nd|r |s |r:|d
||
 fV  n:||
 r<|d ||
 k r<| dur:|d
||
 fV  n|sZ|r||	 dt kr|r~|s~|d
|||  fV  |||
< n| du r|d ||
< n| |tfv rn|||  t }!|dkrd|!f}"nV||
 rd||
 f}"n>|s||
 rd|!f}"n$|tkrd|!f}"n|||
< d|!f}"|dj
|" fV  ||	 rz|tjtjfvrz||
 sz|d ||
< d||d < n|tjtjfv s|dv rt||d < n\|s|	s|
s|dkrd||d d < n.|dkr ||d d  r ||
 |	 |tjkrz|dv rj|
d7 }
|d |d t||
krJ|g  ||
 |	 ||	  d7  < n|dv r:|
dkr:| p|d }#|  t|
D ]}$||$ |#krd||$< qt|D ]}%|%|#kr||%= q||
d d= |
d8 }
|
rd|||
 < t|	ddD ]&}&||& r||&  d8  <  q:qt||
d ksPJ |d |vrz|  dsz|||d < |d |d k}|r||	 ||d | < |}q|r|st||t kr|d |d d f}'|dt  }(| r|'d
|(fV  n|'d
|(fV  dS )zCOverride pycodestyle's function to provide indentation information.r   r   r   r*   N:	r-   Fz]})zE124 {}zE133 {}TzE128 {}zE123 {}ZE122ZE127ZE131ZE126ZE121z{} {})uurbbrif([{z)]}r   r   zE129 {}zE125 {})endswithDEFAULT_INDENT_SIZEr:   NLNEWLINEr'   Zexpand_indentOPreversedgetrI   unicodeCOMMENTSTRINGisspaceappendlenpoprangelistrstrip))rJ   tokensrK   hang_closingZindent_charnoqaZ	first_rowZnrowsZindent_nextrowdepthZvalid_hangsparensZ
rel_indentZ	open_rowsZhangsZindent_chancesZlast_indentindentZlast_token_multilineline	last_lineZlast_line_begins_with_multiline
token_typetextstartendr0   close_bracketZopen_rowZhangZhanging_indentZvisual_indentZone_indentederrorZprev_indentdZindidxposZdesired_indentr5   r5   r6   continued_indentation   s"   
















 








rx   rJ   c                   @   sb  e Zd ZdZdWddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dSdT Z,dUdV Z-dS )XFixPEP8a  Fix invalid code.

    Fixer methods are prefixed "fix_". The _fix_source() method looks for these
    automatically.

    The fixer method can take either one or two arguments (in addition to
    self). The first argument is "result", which is the error information from
    pycodestyle. The second argument, "logical", is required only for
    logical-line fixes.

    The fixer method can return the list of modified lines or None. An empty
    list would mean that no changes were made. None would mean that only the
    line reported in the pycodestyle error was modified. Note that the modified
    line numbers that are returned are indexed at 1. This typically would
    correspond with the line number reported in the pycodestyle error
    information.

    [fixed method list]
        - e111,e114,e115,e116
        - e121,e122,e123,e124,e125,e126,e127,e128,e129
        - e201,e202,e203
        - e211
        - e221,e222,e223,e224,e225
        - e231
        - e251,e252
        - e261,e262
        - e271,e272,e273,e274
        - e301,e302,e303,e304,e305,e306
        - e401,e402
        - e502
        - e701,e702,e703,e704
        - e711,e712,e713,e714
        - e722
        - e731
        - w291
        - w503,504

    Nc                 C   s  || _ |d u rt|| _nt|}| | _|| _td| j| _	i | _
t| jD ]8\}}|ddksz|ddkrV|| j
vrV|| j
|< qV|d u rt n|| _| j| _| j| _| j| _| j| _| j| _| j| _| j| _| j| _| j| _| j| _| j| _| j| _| j| _| j | _!| j | _"| j | _#| j$| _%| j$| _&| j$| _'| j | _(| j)| _*| j$| _+| j,| _-| j | _.| j | _/| j | _0| j1| _2|r|j3dks|j4r| j5n| j6| _7| j8| _9| j:| _;| j:| _<d S )Nr-   import r   from r   )=r4   rE   sourcer2   StringIOrD   options_get_indentwordjoinindent_wordimports	enumeratefindsetlong_line_ignore_cachefix_e112Zfix_e115_fix_reindentZfix_e121Zfix_e122Zfix_e123Zfix_e124Zfix_e126Zfix_e127Zfix_e128Zfix_e129fix_e131Zfix_e133fix_e201Zfix_e202Zfix_e203Zfix_e211fix_e271Zfix_e221Zfix_e222Zfix_e223fix_e225Zfix_e226Zfix_e227Zfix_e228Zfix_e241fix_e224Zfix_e242Zfix_e252fix_e262Zfix_e261Zfix_e272Zfix_e273Zfix_e274fix_e301Zfix_e306
aggressiveexperimentalfix_long_line_logicallyfix_long_line_physicallyZfix_e501fix_e702Zfix_e703fix_w291Zfix_w292Zfix_w293)selfr4   r~   contentsr   sioirm   r5   r5   r6   __init__  sl    


zFixPEP8.__init__c              	   C   s  zt | j\}}d}W n ttjfy2   d}Y n0 t }t|tdD ]}|d |v rZqFd|d   }t	| |rt
| |}|d d }	| j|	 }
tt|dk}|rd }|rt| j|||}|rtt|d	 d	 d |d d	 d |rqF|||}n||}|d u r2|rJ | j|	 |
kr2g }|rD|| nH|g kr~| jjdkrtd
j|d |d dtjd n||d  qF| jjdkrFtd|tjd |d  }td| j|d |d |tjd qFd S )NTFkeyrm   fix_idr   r   r   z'--->  Not fixing {error} on line {line})rt   rm   file   z--->  '{}' is not defined.infoz--->  {}:{}:{}:{}column)_find_logicalr|   r@   r:   
TokenErrorr   sorted_priority_keylowerhasattrgetattrra   _get_parameters_get_logicalrc   intersectionupdater~   verboseprintrI   sysstderraddstripr4   )r   resultslogical_startlogical_endZlogical_supportZcompleted_linesresultZfixed_methodnamefix
line_indexZoriginal_lineZis_logical_fixlogicalmodified_linesr   r5   r5   r6   _fix_source  sx    






zFixPEP8._fix_sourcec                    s  | j j| j j| j j| j jd}t|| j}| j jri }|D ]4}|d |vrXt ||d < ||d  	|d  q:t
djt||dtjd | j jr| j j\  fdd|D }| td	| j|| j jd
 | j jrtdd | jd   D }| d | j jd< d	| jS )z@Return a version of the source code with PEP 8 violations fixed.ignoreselectmax_line_lengthrg   r   rm   z$--->  {n} issue(s) to fix {progress})nprogressr   c                    s,   g | ]$}|d    kr  krn q|qS rm   r5   ).0r)   rr   rq   r5   r6   
<listcomp>q  s   zFixPEP8.fix.<locals>.<listcomp>r-   )r|   r   r   c                 s   s   | ]}| d V  qdS )r   Ncount)r   sliner5   r5   r6   	<genexpr>z  s   zFixPEP8.fix.<locals>.<genexpr>r   )r~   r   r   r   rg   _execute_pep8r|   r   r   r   r   rI   ra   r   r   
line_ranger   filter_resultsr   r   sum)r   pep8_optionsr   r   r)   r   r5   r   r6   r   \  s<    
zFixPEP8.fixc                 C   sD   t |d  d }|d d }| j| }d| |  | j|< dS )znFix a badly indented line.

        This is done by adding or removing from its initial indent only.

        r   r   rm    N)intsplitr|   lstrip)r   r   num_indent_spacesr   targetr5   r5   r6   r     s    
zFixPEP8._fix_reindentc                 C   s<   |d d }| j | }| ds(g S | j| | j |< dS )zFix under-indented comments.rm   r   #N)r|   r   rG   r   )r   r   r   r   r5   r5   r6   r     s
    
zFixPEP8.fix_e112c                 C   s@   |d d }| j | }t|}| }|dd | | j |< dS )zFix unexpected indentation.rm   r   Nr|   _get_indentationr   r   r   r   r   rl   strippedr5   r5   r6   fix_e113  s
    
zFixPEP8.fix_e113c                 C   sN   |d d }| j | }t|}| }|ds4g S |dd | | j |< dS )zFix over-indented comments.rm   r   r   N)r|   r   r   rG   r   r5   r5   r6   fix_e116  s    

zFixPEP8.fix_e116c                 C   sL   |d d }| j | }t|}|dkr*g S | }|dd | | j |< dS )zFix over-indented.rm   r   rN   Nr   r   r5   r5   r6   fix_e117  s    
zFixPEP8.fix_e117c                 C   s   t |d  d }|d d }| j| }|tt| }tt|}g }tt| j| |krd| | j|  | j|< |d|  |d8 }qJ|S )9Fix indentation undistinguish from the next logical line.r   r   rm   r   )r   r   r|   ra   r   r`   )r   r   r   r   r   spaces_to_addrl   r   r5   r5   r6   fix_e125  s    


zFixPEP8.fix_e125c                 C   s   t |d  d }|d d }| j| }|tt| }tt|}|| }|dkrb|dkrbd}|dkrd| | j|  | j|< n t|}| j| |d | j|< dS )r   r   r   rm   r   r   r   N)r   r   r|   ra   r   abs)r   r   r   r   r   r   Zindent_lengthoffsetr5   r5   r6   r     s    
zFixPEP8.fix_e131c                 C   s>   |d d }| j | }|d d }t||dd}|| j |< dS )zRemove extraneous whitespace.rm   r   r   r-   r   replacementNr|   fix_whitespacer   r   r   r   r   fixedr5   r5   r6   r     s    
zFixPEP8.fix_e201c                 C   sT   | j |d d  }|d d }|d| ||d dd }|| j |d d < dS )z-Remove extraneous whitespace around operator.rm   r   r   NrN   r   )r|   replace)r   r   r   r   r   r5   r5   r6   r     s     zFixPEP8.fix_e224c           	   	   C   s4  | j |d d  }|d d }|d| d ||d  }|dd|ddkr,t|t|kr,|| j |d d < |dd}zt|}W n ttjfy   Y dS 0 t|	 sdS t
t||}t|D ]B}||d  d krq|d d }|d| d ||d  }q|| j |d d < ng S dS )	z'Fix missing whitespace around operator.rm   r   r   Nr   r-   r   r   )r|   r   r   r[   generate_tokensr@   r:   r   check_syntaxr   rd   r'   Z"missing_whitespace_around_operatorrZ   r   )	r   r   r   r   r   Z
error_codetserrorser5   r5   r6   r     s2    
zFixPEP8.fix_e225c                 C   sP   |d d }| j | }|d }|d|  d ||d   }|| j |< dS )zAdd missing whitespace.rm   r   r   Nr   )r|   re   r   r   r5   r5   r6   fix_e231  s
    
$zFixPEP8.fix_e231c                 C   s   |d d }| j | }t|d d t|d }||  rB|}n |d|  ||d   }|dr|d| j |< | j |d   | j |d < |d |d gS || j |d d < dS )z,Remove whitespace around parameter '=' sign.rm   r   r   N)z=\
z=\
z=\
 	\r   )r|   minra   r   re   r   rU   )r   r   r   r   cr   r5   r5   r6   fix_e251  s    

 
zFixPEP8.fix_e251c                 C   sl   | j |d d  }|d }|d| d}||d d}|| rPd| nd }|| j |d d < dS )zFix spacing after comment hash.rm   r   r   N 	#z  # r   )r|   re   r   r   )r   r   r   r   codeZcommentr   r5   r5   r6   r   5  s    zFixPEP8.fix_e262c                 C   sJ   |d d }| j | }|d d }t||dd}||kr<g S || j |< dS )z*Fix extraneous whitespace around keywords.rm   r   r   r   r   Nr   r   r5   r5   r6   r   A  s    
zFixPEP8.fix_e271c                 C   s,   d}|| j |d d   | j |d d < dS )zAdd missing blank line.r   rm   r   N)r|   )r   r   crr5   r5   r6   r   P  s    zFixPEP8.fix_e301c                 C   sj   dt |d  d  }d}| j|d d   dkr:d}d| }|| j|d |   | j|d | < dS )	zAdd missing 2 blank lines.r   r   r*   r   rm   \r   N)r   r   r|   r   )r   r   Zadd_linenumr   r   r5   r5   r6   fix_e302U  s    zFixPEP8.fix_e302c                 C   s   t |d dd dd d }td|}d}|d d }g }||k r|dkr| j|  sd| j|< |d|  |d7 }|d8 }qB|S )	zRemove extra blank lines.r   r   r   )r   r   rm   r-   )r   r   maxr|   r   r`   )r   r   Zdelete_linenumcntrm   r   r5   r5   r6   fix_e303`  s    $


zFixPEP8.fix_e303c                 C   s(   |d d }| j |  s$d| j |< dS )z/Remove blank line following function decorator.rm   r   r-   N)r|   r   )r   r   rm   r5   r5   r6   fix_e304s  s    zFixPEP8.fix_e304c                 C   s   dt |d  d  }d}|d d }g }|dk rt|}||k r|dkr| j|  szd| j|< |d|  |d7 }|d8 }q<ndd}|dk rq| j|  }|sq|d d	krq|d8 }q|d7 }|| j|  | j|< |d|  |S )
z9Add missing 2 blank lines after end of function or class.r   r   r*   r   rm   r-   r   r   r   )r   r   r   r|   r   r`   r   )r   r   Zadd_delete_linenumr   r   r   r   rm   r5   r5   r6   fix_e305y  s2    

zFixPEP8.fix_e305c                 C   s   |d d }| j | }|d d }| ds4g S tjd|ddd }|d| d	d
 | d ||d d	 }|| j |< dS )zPut imports on separate lines.rm   r   r   importz
\bimport\b)patternstringmaxsplitr   Nz	 ,r   rz   )r|   r   rG   rer   re   )r   r   r   r   r   indentationr   r5   r5   r6   fix_e401  s$    
zFixPEP8.fix_e401c              
   C   s   t || j\}}}tddD ]P}d| j|||  }ztd| W n ttjfyf   Y qY n0  qnq|| jv r| j| |kst	| j|}|| j|  | j|< t|D ]}d| j|| < qd S )Nr   d   r-   )
get_index_offset_contentsr|   rc   r   r   r@   r:   r   r   !get_module_imports_on_top_of_file)r   r   r   r   r   r   rm   Z
mod_offsetr5   r5   r6   fix_e402  s"    

zFixPEP8.fix_e402c              	   C   s  |r0t |d dks0| j|d d   dr:| |S |d d }|d d }|d }t| j|d dd}t| j|d dd}td|}z| j|||d|d}	W n" t	t
jfy   | | Y S 0 |	rt||d D ]}
d| j|
< q|	| j|< t|d |d S g S )	:Try to make lines fit within --max-line-length characters.r   r   rm   r   r   r-   defaultr   previous_line	next_lineoriginal)ra   r|   r   rG   r   get_itemjoin_logical_liner   fix_long_liner@   r:   r   rc   )r   r   r   Zstart_line_indexZend_line_indexlogical_linesr  r  Zsingle_liner   r   r5   r5   r6   r     s8    


zFixPEP8.fix_long_line_logicallyc              	   C   s   |d d }| j | }t| j |d dd}t| j |d dd}z| j||||d}W n ttjfyp   g  Y S 0 |r|| j |< |d gS g S )r  rm   r   r-   r  r  )r|   r  r  r@   r:   r   )r   r   r   r   r  r  r   r5   r5   r6   r     s"    




z FixPEP8.fix_long_line_physicallyc              
   C   s   |||f}|| j v rg S | drP| jjrLt|| jj| d dS g S t|||| j| jj| jj| jj	| jj
d}|rt||s|S | j | d S )Nr   )rm   r   last_comment)r   r  r  r   r   r   r   r   )r   r   rG   r~   r   shorten_commentr   get_fixed_long_liner   r   r   code_almost_equalr   )r   r   r  r  r  Zcache_entryr   r5   r5   r6   r    s2    


zFixPEP8.fix_long_linec                 C   s*   t || j\}}}|dd | j|< dS )z$Remove extraneous escape of newline.r   r   N)r  r|   re   )r   r   r   _r   r5   r5   r6   fix_e502  s    
zFixPEP8.fix_e502c                 C   st   |d d }| j | }|d }|d| d t| | j ||d d }|| j |d d < |d |d d gS )z9Put colon-separated compound statement on separate lines.rm   r   r   Nr   r   )r|   r   r   r   )r   r   r   r   r   fixed_sourcer5   r5   r6   fix_e701  s    
zFixPEP8.fix_e701c                 C   s  |sg S |d }|D ]L}|d dkrd|v rt |r| jjrXtdj|d dtjd g   S q|d d	 }| j| }|	 
d
r|	d| j|< | j|d	   | j|d	 < |d	 |d gS |	 
dr|	dd | j|< |d	 gS |d d	 }|d| 	d	 }t|d ||d d  }	d}
||d d dd dkrl||d d}
|
r||
 | j|< n|d |	 | j|< |d	 gS )z=Put semicolon-separated compound statement on separate lines.r   r   ZE702rM   z8---> avoid fixing {error} with other compound statements)rt   r   rm   r   r   z
 	\;z
 	;r   r   Nr   # )r	   rH   r~   r   r   rI   r   r   r|   re   rU   r   r   )r   r   r   r  rm   r   r   r   firstsecondZinline_commentr5   r5   r6   r   %  sH    



$zFixPEP8.fix_e702c                 C   sZ   t || j\}}}t|}|rVd|dt|| j ||dd 	 | j|< dS )z'Fix multiple statements on one line defz{}
{}{}r   N)
r  r|   STARTSWITH_DEF_REGEXrH   rI   groupr   r   rr   r   r   r   r   r  r   rH   r5   r5   r6   fix_e704U  s    

zFixPEP8.fix_e704c           
      C   s   t || j\}}}|d }|t|kr*g S |d|  }||| }||d  }| dkrhd}	n| dkrzd}	ng S d||	|g| j|< dS )zFix comparison with None.r   N==is!=is notr   )r  r|   ra   re   r   r   r   )
r   r   r   r   r   right_offsetleftcenterrightZ
new_centerr5   r5   r6   fix_e711`  s    
zFixPEP8.fix_e711c           
      C   s2  t || j\}}}td|r8tjdd|dd| j|< ntd|r^tjdd|dd| j|< n|d }|t|krvg S |d	|  }||| }||d	  }d	}	| d
krtd|rtjdd|dd}	n*| dkrtd|rtjdd|dd}	|	d	u r
g S |	d 	 r d|	 }	||	 | j|< d	S )z.Fix (trivial case of) comparison with boolean.z^\s*if [\w."\'\[\]]+ == False:$zif ([\w."\'\[\]]+) == False:z
if not \1:r   r   z^\s*if [\w."\'\[\]]+ != True:$zif ([\w."\'\[\]]+) != True:r   Nr&  z\bTrue\bz
\bTrue\b *r-   r(  z	\bFalse\bz\bFalse\b *r   r   )
r  r|   r  rH   subra   re   r   r   isalnum)
r   r   r   r   r   r*  r+  r,  r-  Z	new_rightr5   r5   r6   fix_e712v  s:    

zFixPEP8.fix_e712c              	   C   s  t || j\}}}|d| }||d }t|}d\}}|rp|d}| }d|d| d||d }t|}	|	r|	ddkr|	d}
d|d|
 |	d|	d|	d||	 d |}|r|| }
|| d	 }d|d|
 d
||d }|| j|< dS )z+Fix (trivial case of) non-membership check.Nr   r   r   {}{} {}inr   {5}{0}{1} {2} {3} {4}r   r   znot in)	r  r|   COMPARE_NEGATIVE_REGEX_THROUGHsearchrq   rr   rI   COMPARE_NEGATIVE_REGEXr#  )r   r   r   r   r   before_targetZmatch_notinZnotin_pos_startZnotin_pos_endrH   	pos_start
new_targetpos_endr5   r5   r6   fix_e713  s8    




zFixPEP8.fix_e713c              	   C   s  t || j\}}}|d| }||d }t|}d\}}|rp|d}| }d|d| d||d }t|}	|	r|	d	dr|	d}
d|d|
 |	d	|	d|	d||	 d |}|r|| }
|| d
 }d|d|
 d||d }|| j|< dS )z,Fix object identity should be 'is not' case.Nr2  r   r3  r4  r   r'  r5  r   r   r)  )
r  r|   r6  r7  rq   rr   rI   r8  r#  rG   )r   r   r   r   r   r9  Zmatch_isnotZisnot_pos_startZisnot_pos_endrH   r:  r;  r<  r5   r5   r6   fix_e714  s8    




zFixPEP8.fix_e714c                 C   sT   t || j\}}}t|}|rPd|d|d d  d|| d | j|< dS )zfix bare except{}{}{}Nr   r   zexcept BaseException:)r  r|   BARE_EXCEPT_REGEXr7  rI   rr   r$  r5   r5   r6   fix_e722  s    

zFixPEP8.fix_e722c                 C   sh   t || j\}}}t|}|rd| }d|d|d |d|d||d  | j|< dS )z,Fix do not assign a lambda expression check.z{}def {}({}): return {}Nr   r   r   )	r  r|   LAMBDA_REGEXr7  rr   rI   rq   r#  r   )r   r   r   r  r   rH   rr   r5   r5   r6   fix_e731  s    

 zFixPEP8.fix_e731c                 C   s0   | j |d d   }|d | j |d d < dS )zRemove trailing whitespace.rm   r   r   N)r|   re   )r   r   Z
fixed_liner5   r5   r6   r     s    zFixPEP8.fix_w291c                 C   sZ   d}t | jD ]}| }|r$ q.q|d7 }qt| j}| jd||  | _tdd| S )zRemove trailing blank lines.r   r   N)rZ   r|   re   ra   rc   )r   r  Zblank_countrm   Zoriginal_lengthr5   r5   r6   fix_w391  s    

zFixPEP8.fix_w391c              
   C   s  t || j\}}}| d }zt|}W n ttjfyD   Y d S 0 t|d d |s\d S d}d}d}	tdD ]}
||
 dk r q
||
 d }|dk st	| j|kr q
|d }| j| 
 }|s|r|d dkr|	d7 }	qpd}ztd| j|| }W n ttjfy"   Y qpY n0 d}g }t|D ]2\}}|d tjtjfv r4|| |d7 }q4|dkr||d	 d  }n|}g }|D ]p}|d tjtjfv r|d8 }|dkr qtj|d kr|r|d tjkr|d
 d } q|}q q
qp||}
|d |	 }d|d |
 ||
t	| d  
 | j|< t| j|| }| j| }||}|rd|d | |||d d  | j|< nf|d | drd|d |d  |||d d  | j|< n$d|d | |||d  | j|< d S )Nr   F   r   r   Tr-   r   r   {}{}z{} {} {}r3  z{} {}{})r  r|   r   r   r@   r:   r   _is_binary_operatorrc   ra   r   r   r   rX   rW   r`   r]   indexrI   find_newlinerU   )r   r   r   r  r   Zone_string_tokenr   comment_indexZfound_not_comment_only_lineZcomment_only_linenumr   Z
from_indexto_indexZ
strip_linenewline_countnewline_indexrI  tttsoldZfix_target_linenlZbefore_lineZblr5   r5   r6   fix_w503  s    





&
 


zFixPEP8.fix_w503c              
   C   s  t || j\}}}d}d }tddD ]}|| }ztd| j|| }	W n ttjfyj   Y q$Y n0 d}
g }t|	D ]\}}t	|d |d r|d d dkr|d d dkr|d d |d d f}q||d tj
kr,|d dv r,|d d dkrR|d d dkrR|d d |d d f}q||d tjtjfv r||| |
d7 }
q||
dkrp|	d |d  }n|	}g }|D ]2}tj|d kr|r|d \}} q|}q| qq$|sd S ||d |d  }|r|dkrd	|d |d   ||d  | j|< n@d
|d |d   ||d d   ||d d  | j|< | j|d  }d}td|}|r| d }d|d | |||d  | j|d < d S )Nr   r      r-   r   r   andorrF  rG  r?  z\s*r3  )r  r|   rc   r   r   r@   r:   r   r   rH  NAMErX   rW   r`   r]   rI   re   r   r  rH   span)r   r   r   r  r   rK  Zoperator_positionr   rL  r   rM  rN  rI  rO  rP  rQ  Zcomment_rowZtarget_operatorr  Znext_line_indentmr5   r5   r6   fix_w504O  sn    

 $





zFixPEP8.fix_w504c                 C   s@   t || j\}}}d|d |d  ||d d  | j|< d S )Nz{}\{}r   )r  r|   rI   )r   r   r   r   r   r5   r5   r6   fix_w605  s    
zFixPEP8.fix_w605)NN).__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r
  r   r   r  r  r  r   r%  r.  r1  r=  r>  rA  rC  r   rD  rS  r[  r\  r5   r5   r5   r6   ry     sZ   )  
>C$!#0& 
L:ry   c                    s  dd }dd   fdd}d}d}d	}t t| }|D ]J\}|st }	|	d
urd}|	d}|	 |	j  }
|
dd
 |kr8d}q8|r dd
 |kr8d}q8 sq8n	drq8	dr||krq8|  S 	dr8||kr q8|t
|fg|\}}|r0|| d   S |  S tjrN|  S tfdd|D rjq8q8|r||  S |  S q8dS )zreturn import or from keyword position

    example:
      > 0: import sys
        1: import os
        2:
        3: def function():
    c                 S   sP   | d dv r| dd  } | r4| d dv r4| dd  } | oN| d dkpN| d dkS )Nr   ZuUbBr   ZrR"'r5   r   r5   r5   r6   is_string_literal  s
    z<get_module_imports_on_top_of_file.<locals>.is_string_literalc                 S   s6   t | }|jD ] }t|t jr|jdkr dS qdS )N
__future__TF)astparsebody
isinstanceZ
ImportFrommodule)rm   Znodesr   r5   r5   r6   is_future_import  s
    

z;get_module_imports_on_top_of_file.<locals>.is_future_importc              
      sp   d}d}| D ]Z\}}|  dD ]:}|| }z ||fW     S  tyZ   Y q"Y q"0 q"|d7 }qd|fS )Nr   r-   Tr   F)r   
splitlinesr@   )r|   r   rm   r  r  Z	line_part)rj  r5   r6   has_future_import  s    
z<get_module_imports_on_top_of_file.<locals>.has_future_import)tryr!   elsefinallyF"""NTkindrF  r   rz   r{   r   c                 3   s   | ]}  |V  qd S NrG   )r   kwr   r5   r6   r         z4get_module_imports_on_top_of_file.<locals>.<genexpr>r   )iterr   DOCSTRING_START_REGEXrH   r   r#  rr   endposre   rG   	itertoolschainr'   ZDUNDER_REGEXany)r|   Zimport_line_indexrc  rl  Zallowed_try_keywordsZin_docstringZdocstring_kindZsource_streamr   rZ  ZremainZhitr   r5   )rj  rm   r6   r	    s\    	





r	  c                 C   s"   | d d }|| d d || fS )z2Return (line_index, column_offset, line_contents).rm   r   r   r5   )r   r|   r   r5   r5   r6   r    s
    
r      O   Fc              
      s   t | }| t|d }	|	 |	ks(J |  dr:J tt|	}
t|
|	|| |d}ttt|	| |g fddd}|dkrt
dd	g| d	g ttjd
d |r|d }t|t|krdS |S dS )zBreak up long line and return result.

    Do this by generating multiple reformatted candidates and then
    ranking the candidates to heuristically select the best option.

    Nr   )r   r   r  c                    s   t |  dS )N)r   )line_shortening_rank)xr   r   r   r5   r6   <lambda>	  s
   z%get_fixed_long_line.<locals>.<lambda>r   r   zP-------------------------------------------------------------------------------
r-   zutf-8r   r   )r   ra   r   rG   rd   r   shorten_liner   r   unionr   r   wrap_outputr   r   longest_line_length)r   r  r  r   r   r   r   r   rl   r|   rf   Z
candidatesZbest_candidater5   r  r6   r    s4    		
r  c                 C   s&   t | dkrdS tdd |  D S )zReturn length of longest line.r   c                 s   s   | ]}t |V  qd S rr  ra   r   rm   r5   r5   r6   r   !  ru  z&longest_line_length.<locals>.<genexpr>)ra   r   rk  r   r5   r5   r6   r    s    r  c                 C   s    t | }|tt|   d S )z/Return single line based on logical line input.r   )r   untokenize_without_newlinesr   r   )rJ   r  r5   r5   r6   r  $  s    
r  c           
      C   s   d}d}d}| D ]j}|d }|d \}}|d \}}	||kr@d}||ksP|dkrb| dsb|d7 }|dkrr||7 }|}|	}q| S )	z#Return source code based on tokens.r-   r   r*   r   r   r   r   r   )rU   re   )
rf   rp   Zlast_rowZlast_columnrO  token_string	start_rowstart_columnend_row
end_columnr5   r5   r6   r  ,  s*    r  c                 C   s   g }g }d}d}t d| D ]}|d tjtjtjtjtjfv rDq|s|d tjtj	fv rd}|
|d d d |d d f q|r|s|
|d d d |d d f d}|d tjkr|d dv r|d7 }q|d d	v r|d8 }q||fS )
NTr   r-   r   r   r   FrT   }]))r   r   r:   r]   DEDENTINDENTrW   	ENDMARKERrX   SEMIr`   rY   )source_linesr   r   Zlast_newlinerk   rO  r5   r5   r6   r   H  s.    ""

r   c                 C   s   |d d }|d d }d}d}t dt|dD ]H}|s<J || }	|	d |ksh|	d |kr0|	d |kr0|	}|| } qzq0|du rdS | |d |d d  }
|||
fS )zdReturn the logical line corresponding to the result.

    Assumes input is already E702-clean.

    rm   r   r   Nr   )rc   ra   )r  r   r   r   ri   colZlsler   r  r  r5   r5   r6   r   b  s    $r   c                 C   s(   d|  krt | k r$n n| | S |S Nr   r  )itemsrI  r  r5   r5   r6   r  y  s    r  c                 C   s   t | }||S )zReindent all lines.)
Reindenterrun)r|   indent_sizeZ
reindenterr5   r5   r6   reindent  s    r  c                 C   sd   t | }t |}t|t|kr$dS t|D ]2\}}d||  d||  kr, dS q,dS )zZReturn True if code is similar.

    Ignore whitespace when comparing specific line.

    Fr-   T)split_and_strip_non_empty_linesra   r   r   r   )arQ   Zsplit_aZsplit_brI  r  r5   r5   r6   r    s    $r  c                 C   s   dd |   D S )z=Return lines split by newline.

    Ignore empty lines.

    c                 S   s   g | ]}|  r|  qS r5   r   r  r5   r5   r6   r     ru  z3split_and_strip_non_empty_lines.<locals>.<listcomp>)rk  )rp   r5   r5   r6   r    s    r  c           	      C   s  d| vr| S t | ddtt| B }g }t| }t| ddD ]\}}| dr||vrt	
|st|}| }t|dkrtdd t|D }|d| ddks|d  s|d  s|d d	vr| dsd
|d }|||  qB|| qBd|S )zFormat block comments.r   Tinclude_docstringsr   rq   c                 s   s   | ]\}}|d kr|V  qdS )r   Nr5   )r   rI  r   r5   r5   r6   r     s   
zfix_e265.<locals>.<genexpr>Nz:!r  z# 	r-   )multiline_string_linesr   commented_out_code_linesr2   r}   r   rD   r   rG   r'   rh   r   ra   nextr   r0  r_   re   rU   r`   r   )	r|   r   Zignored_line_numbersZfixed_linesr   line_numberrm   r  rw   r5   r5   r6   fix_e265  sF    




r  r-   c                 C   s   | o|  d| k}|r | d }n| }ddlm} zt|||d}W n" |jjtttfyd   |  Y S 0 |r~||v r~|| vr~| S |r| dS |S )zjReturn refactored code using lib2to3.

    Skip if ignore string is produced in the refactored code.

    r   r   r   )pgen2)fixer_namesr4   )	re   Zlib2to3r  refactor_with_2to3rf  Z
ParseErrorr@   rA   UnicodeEncodeError)r|   r  r   r4   Znot_found_end_of_file_newlineZinput_sourcer  Znew_textr5   r5   r6   refactor  s,    




r  c                 C   sT   t  }t D ]@\}}t|| |dr|rBtd|| tjd |t |O }q|S )Nr   r   --->  Applying {} fix for {}r   )	r   CODE_TO_2TO3r  
code_matchr   rI   upperr   r   )r   r   wherer   fixesr   r   r5   r5   r6   code_to_2to3  s    r  Tglobalc                 C   s2   |s| S |pg }|pg }t | t||||d|dS )z*Fix various deprecated code (via lib2to3).)r   r   r  r   r4   )r  r  )r|   r   r   r   r4   r  r   r5   r5   r6   fix_2to3  s    r  c                 C   s   |s| S t | dgddS )z)Fix deprecated form of raising exception.raisewith_traceback)r   )r  )r|   r   r5   r5   r6   fix_w602  s    r  c                 C   s   t | trJ tt}| D ]V}|tr<|t  d7  < q|trX|t  d7  < q|tr|t  d7  < qt	||j
ddptgd S )zKReturn type of newline used in source.

    Input is a list of lines.

    r   T)r   reverser   )rh  r\   collectionsdefaultdictr   rU   CRLFCRLFr   r[   )r|   Zcounterrm   r5   r5   r6   rJ    s    



rJ  c              	   C   sN   d}z,t | D ]}|d tjkr|d } q.qW n ttjfyH   Y n0 |S )zReturn indentation type.r|  r   r   )r   tokenr  r@   r:   r   )r|   r   rO  r5   r5   r6   r   '  s    
r   c                 C   s,   |   r(t| t|   }| d| S dS )zReturn leading whitespace.Nr-   )r   ra   r   )rm   Znon_whitespace_indexr5   r5   r6   r   4  s    r   c                 C   sX   d}t j| |d| d| |d}d}|D ]*}||7 }|r(||s(||d | 7 }q(|S )z0Return text of unified diff between old and new.r   z	original/zfixed/)Zlinetermr-   z\ No newline at end of file)difflibZunified_diffrU   )rQ  newr4   r0   diffrp   rm   r5   r5   r6   get_diff_text=  s    r  c                 C   sv   g d}d}dg}| d   }z||W S  typ   z||| d W  Y S  tyj   | Y  Y S 0 Y n0 dS )z~Key for sorting PEP8 results.

    Global fixes should be done first. This is important for things like
    indentation.

    )Ze701Ze702Ze225Ze231Ze201Ze262i'  e501r   r   N)r   rI  
ValueError)Zpep8_resultpriorityZmiddle_indexZlowest_priorityr   r5   r5   r6   r   Q  s    r   c              	   c   s|   t | |||||dD ]
}|V  q|rXtD ].}	t| ||||	|d}
|
dur(|
|kr(|
V  q(|rxt| |||dD ]
}
|
V  qldS )zJSeparate line at OPERATOR.

    Multiple candidates will be yielded.

    )rf   r|   r  r   r   r  )rf   r|   r  r   key_token_stringsr   N)rf   r|   r  r   )_shorten_lineSHORTEN_OPERATOR_GROUPS_shorten_line_at_tokens_shorten_line_at_tokens_new)rf   r|   r  r   r   r   r   r  	candidater  Z	shortenedr5   r5   r6   r  t  s8    

r  c                 c   s  t | D ]\}}}}	|tjkrt|st|s||d d   ds|d| }
||d }||  d | |
  d V  q|tjkr|dkr|tj	ksJ |d|	 }
|}|

 dr||	d  drnB|

 dr||7 }n*d|
v r|dd|
d  7 }n||7 }|||	d   }| r| d	rPq| d
rbq|

 drtq|dv r|
d d | }n|
d | }t|rt|n|r|| V  qdS )zSeparate line at OPERATOR.

    The input is expected to be free of newlines except for inside multiline
    strings and at the end.

    Multiple candidates will be yielded.

    r   N)rh   zpragma:zpylint:r   =r   r   r   r   r   .z+-*/z \)token_offsetsr:   r]   is_probably_part_of_multiliner   r   rG   r  rY   r  re   rU   r   r   r   normalize_multiline)rf   r|   r  r   r   r  ro   r  start_offset
end_offsetr   r!  Zsecond_indentr   r5   r5   r6   r    sp    



r  c                 C   s   | t jks|dv o|dvS )NrU  z()[]{},:.;@=%~)r:   rY   )ro   rp   r5   r5   r6   rH    s    rH  Token)ro   r  sposeposrm   c                   @   s   e Zd ZdZG dd deZG dd deZG dd deZdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zd0ddZdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/S )1ReformattedLineszThe reflowed lines of atoms.

    Each part of the line is represented as an "atom." They can be moved
    around when need be to get the optimal formatting.

    c                   @   s,   e Zd ZdZdd Zdd Zedd ZdS )	zReformattedLines._Indentz,Represent an indentation in the atom stream.c                 C   s
   || _ d S rr  Z_indent_amtr   
indent_amtr5   r5   r6   r     s    z!ReformattedLines._Indent.__init__c                 C   s
   d| j  S Nr   r  r   r5   r5   r6   emit  s    zReformattedLines._Indent.emitc                 C   s   | j S rr  r  r  r5   r5   r6   size  s    zReformattedLines._Indent.sizeN)r]  r^  r_  r`  r   r  propertyr  r5   r5   r5   r6   _Indent  s
   r  c                   @   s$   e Zd ZdZdd Zedd ZdS )zReformattedLines._Spacez%Represent a space in the atom stream.c                 C   s   dS r  r5   r  r5   r5   r6   r    s    zReformattedLines._Space.emitc                 C   s   dS Nr   r5   r  r5   r5   r6   r  
  s    zReformattedLines._Space.sizeNr]  r^  r_  r`  r  r  r  r5   r5   r5   r6   _Space  s   r  c                   @   s$   e Zd ZdZdd Zedd ZdS )zReformattedLines._LineBreakz*Represent a line break in the atom stream.c                 C   s   dS )Nr   r5   r  r5   r5   r6   r    s    z ReformattedLines._LineBreak.emitc                 C   s   dS r  r5   r  r5   r5   r6   r    s    z ReformattedLines._LineBreak.sizeNr  r5   r5   r5   r6   
_LineBreak  s   r  c                 C   s"   || _ g | _d| _d | _d | _d S r  )_max_line_length_lines_bracket_depth
_prev_item_prev_prev_item)r   r   r5   r5   r6   r     s
    zReformattedLines.__init__c                 C   s   |   S rr  )r  r  r5   r5   r6   __repr__   s    zReformattedLines.__repr__c                 C   s,   t |tr| || d S | ||| d S rr  )rh  Atom	_add_item_add_container)r   objr  break_after_open_bracketr5   r5   r6   r   &  s    
zReformattedLines.addc                 C   s   d}t | jdkrTt| jd | jr,|d8 }t | jdkrTt| jd | jrT|d8 }|dkrv| j|   |d8 }qT| j| d S )Nr   r   r*   r   )ra   r  rh  r  r`   )r   itemZ
num_spacesr5   r5   r6   add_comment-  s    
zReformattedLines.add_commentc                 C   s   | j | | d S rr  )r  r`   r  r  r5   r5   r6   
add_indent;  s    zReformattedLines.add_indentc                 C   s"   | j |   | t| d S rr  )r  r`   r  r  ra   )r   rl   r5   r5   r6   add_line_break>  s    zReformattedLines.add_line_breakc                 C   s.   | j ||   | j |d | | d S r  )r  insertr  r  )r   rI  r  r5   r5   r6   add_line_break_atB  s    z"ReformattedLines.add_line_break_atFc                 C   s8  | j r"t| j d | j| j| jfr&d S t| j}| jr@t| jnd}| jjsd| jj	sd| jj
sd| jjr|d dvs$|d dkr|s$|dkr4|d dkr4|dkr4|d d	kr4|d d
v r|d dvs$|d dv s$|r|dks$| jr4|dvr4| jj
s| jjs| jj	r4|dv r4| j |   d S )Nr*   r-   r   z	([{.,:}])r  fromr  r   rM   r  z.,}])z:,z+-)r   r   r   r   r   r   z**r4  )r  rh  r  r  r  r\   r  r  
is_keyword	is_stringis_name	is_numberr`   )r   Z	curr_textequal	prev_textZprev_prev_textr5   r5   r6   add_space_if_neededF  sn    









!z$ReformattedLines.add_space_if_neededc                 C   s   | j S )z(Return the previous non-whitespace item.)r  r  r5   r5   r6   previous_itemt  s    zReformattedLines.previous_itemc                 C   s   |   | | jkS rr  )current_sizer  )r   Zitem_extentr5   r5   r6   fits_on_current_linex  s    z%ReformattedLines.fits_on_current_linec                 C   s2   d}t | jD ]}||j7 }t|| jr q.q|S )z3The size of the current line minus the indentation.r   )rZ   r  r  rh  r  )r   r  r  r5   r5   r6   r  {  s    
zReformattedLines.current_sizec                 C   s   | j ot| j d | j| jfS )Nr*   )r  rh  r  r  r  r5   r5   r6   
line_empty  s
    

zReformattedLines.line_emptyc                 C   s<   d}| j D ]$}t|| jr"| }|| 7 }q
| d S )Nr-   r   )r  rh  r  re   r  )r   r  r  r5   r5   r6   r    s    
zReformattedLines.emitc                 C   s  | j r6| j jr6|jr6| j|   | j| | t|}| jrl| jrl| || |dv r| 	|| nJ| jr| 
 s| t|r| | n"| j|   | j| | | j| || j  | _ | _|dv r|  jd7  _n(|dv r|  jd8  _| jdksJ dS )zAdd an item to the line.

        Reflow the line to get the best formatting after the item is
        inserted. The bracket depth indicates if the item is being
        inserted inside of a container or not.

        z.,)]}rT   r   r  r   N)r  r  r  r`   r  r  r\   r  &_prevent_default_initializer_splitting_split_after_delimiterr  r  ra   _enforce_spacer  )r   r  r  	item_textr5   r5   r6   r    s(    
zReformattedLines._add_itemc                 C   s   |d }t | jdkr|  s| |j| j d st |d dkrZ| jjrZd}|d }q|slt | jdvr| j| 	  | j| 
| d	}n|  d }d	}t|ttfr|}|j| d
| |d d S )Nr   r  r   r   r   Tr   rT   Fr   )r  )r\   r  r  r  r  r  r  r  r`   r  r  r  rh  ListComprehensionIfExpressionreflow)r   	containerr  r  Zactual_indentr5   r5   r6   r    s4    
zReformattedLines._add_containerc                 C   s   t |dkr|   dS | jr2| jr2t | jdkr6dS |   | j| j}t| j|d  | jsr| |j	d rvdS t| j|d  | j
r| j|d = | | j| j| dS )a  Prevent splitting between a default initializer.

        When there is a default initializer, it's best to keep it all on
        the same line. It's nicer and more readable, even if it goes
        over the maximum allowable line length. This goes back along the
        current line to determine if we have a default initializer, and,
        if so, to remove extraneous whitespaces and add a line
        break/indent before it if needed.

        r  Nr   )r\   _delete_whitespacer  r  r  rI  rh  r  r  r  r  r  )r   r  r  Zprev_prev_indexr5   r5   r6   r    s&    z7ReformattedLines._prevent_default_initializer_splittingc                 C   s   |    | |jrdS d}t| jD ]J}|rDt|tr>|jsD qrnd}t|| jrX|}t|| j	| j
fr& dS q&|szdS | | j|| dS )z&Split the line only after a delimiter.N)r  r  r  rZ   r  rh  r  is_colonr  r  r  r  rI  )r   r  r  Z
last_spaceZcurrent_itemr5   r5   r6   r  
	  s(    z'ReformattedLines._split_after_delimiterc                 C   s   t | jd | j| j| jfr dS | js*dS t|}t| j}|dkrL|dksl|dkr\|dksl|dkr||dkr|| j|   dS )zEnforce a space in certain situations.

        There are cases where we will want a space where normally we
        wouldn't put one. This just enforces the addition of a space.

        r*   Nr  r  r   r   )rh  r  r  r  r  r  r\   r`   )r   r  r  r  r5   r5   r6   r  %	  s*    

zReformattedLines._enforce_spacec                 C   s*   t | jd | j| j| jfr&| jd= q dS )z/Delete all whitespace from the end of the line.r*   N)rh  r  r  r  r  r  r5   r5   r6   r  ?	  s    z#ReformattedLines._delete_whitespaceN)F)r]  r^  r_  r`  objectr  r  r  r   r  r   r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r5   r5   r5   r6   r    s.   

.
)$'r  c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	d
Zdd Ze	dd Z
e	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd ZdS )r  z3The smallest unbreakable unit that can be reflowed.c                 C   s
   || _ d S rr  )_atom)r   Zatomr5   r5   r6   r   J	  s    zAtom.__init__c                 C   s   | j jS rr  r  r  r  r5   r5   r6   r  M	  s    zAtom.__repr__c                 C   s   | j S rr  )r  r  r5   r5   r6   __len__P	  s    zAtom.__len__Fc           	      C   s   | j jtjkr||  d S |r$|n| j}| j jdvr>|d7 }| }|s||s|rh|| jd s|	 s| j
s|r|jrt| dks|| n|t|  || t|| d S )Nz,:([{}])r   r   )r  ro   r:   r]   r  r  r  r  r  r  r  r  r\   r  r   r   ra   )	r   reflowed_linescontinued_indentextentr  is_list_comp_or_if_exprnext_is_dotZ
total_size	prev_itemr5   r5   r6   r  S	  s<    

zAtom.reflowc                 C   s   |   S rr  )r  r  r5   r5   r6   r  w	  s    z	Atom.emitc                 C   s   t | jjS rr  )keyword	iskeywordr  r  r  r5   r5   r6   r  z	  s    zAtom.is_keywordc                 C   s   | j jtjkS rr  )r  ro   r:   r^   r  r5   r5   r6   r  ~	  s    zAtom.is_stringc                 C   s   | j jtjkS rr  )r  ro   r:   rX  r  r5   r5   r6   r  	  s    zAtom.is_namec                 C   s   | j jtjkS rr  )r  ro   r:   NUMBERr  r5   r5   r6   r  	  s    zAtom.is_numberc                 C   s   | j jdkS )Nr   r  r  r5   r5   r6   is_comma	  s    zAtom.is_commac                 C   s   | j jdkS )NrM   r  r  r5   r5   r6   r  	  s    zAtom.is_colonc                 C   s   t | jjS rr  )ra   r  r  r  r5   r5   r6   r  	  s    z	Atom.sizeN)FFF)r]  r^  r_  r`  r   r  r  r  r  r  r  r  r  r  r  r  r  r5   r5   r5   r6   r  F	  s.      
$





r  c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd ddZdd Z	e
dd Ze
dd Ze
dd Ze
dd Ze
dd Ze
dd Ze
dd Ze
dd ZdS )!	Containerz#Base class for all container types.c                 C   s
   || _ d S rr  _items)r   r  r5   r5   r6   r   	  s    zContainer.__init__c                 C   sz   d}d}| j D ]f}|jr"|d7 }nL|jr2|d7 }n<t|}|rf|s^|tdsf|tdsf|d7 }||7 }|j}q|S )Nr-   F, z: z
([{,.:}]) z	([{,.:}])r   )r  r  r  r\   rU   tuplerG   r  )r   r  Zlast_was_keywordr  Zitem_stringr5   r5   r6   r  	  s(    


zContainer.__repr__c                 c   s   | j D ]
}|V  qd S rr  r  )r   elementr5   r5   r6   __iter__	  s    
zContainer.__iter__c                 C   s
   | j | S rr  r  )r   rv   r5   r5   r6   __getitem__	  s    zContainer.__getitem__Fc           
      C   sx  d}t | jD ]b\}}t| j|d }t|trt| ttf}|j||| |||o`t	|dkd |rz|j
rz|| d}n"||t|| t|ttf }|r|dkrt	|| jkr|rt	|| jkrt| jdkst|ts|| d}qt| j|d }	t	|dvr|rt|tst	|d	kr|	rt|	trHt	|d
kr| s|| |d d s|| qd S )NFr   r  )r  r  r   r   r   )r  r   r4  rM   not)r   r  r  rh  r  r	  r
  r  _get_extentr\   r  r  r   ra   open_bracketrs   r  r  r  )
r   r  r  r  Zlast_was_containerrI  r  Z	next_itemr  Znext_next_itemr5   r5   r6   r  	  sv    








zContainer.reflowc                 C   s   d}t | j|d }|o"t|dk}|t| jk rt | j|}|d7 }t|ttfrVqt|tr|r|jr|rx|d7 }n
||j	7 }|}q$nt|dvr|js|j
sqt|dkrd}||j	7 }|}q$|S )zbThe extent of the full element.

        E.g., the length of a function call or keyword.

        r   r   r  )r  r  rM   r%  T)r  r  r\   ra   rh  r	  r
  r  r  r  r  )r   rI  r  r  Zseen_dotr  r5   r5   r6   r&  	  s4    




zContainer._get_extentc                 C   s   dS NFr5   r  r5   r5   r6   r  
  s    zContainer.is_stringc                 C   s   t |  S rr  )ra   r  r  r5   r5   r6   r  
  s    zContainer.sizec                 C   s   dS r(  r5   r  r5   r5   r6   r  
  s    zContainer.is_keywordc                 C   s   dS r(  r5   r  r5   r5   r6   r  
  s    zContainer.is_namec                 C   s   dS r(  r5   r  r5   r5   r6   r   
  s    zContainer.is_commac                 C   s   dS r(  r5   r  r5   r5   r6   r  $
  s    zContainer.is_colonc                 C   s   d S rr  r5   r  r5   r5   r6   r'  (
  s    zContainer.open_bracketc                 C   s   d S rr  r5   r  r5   r5   r6   rs   ,
  s    zContainer.close_bracketN)F)r]  r^  r_  r`  r   r  r#  r$  r  r&  r  r  r  r  r  r  r  r'  rs   r5   r5   r5   r6   r  	  s0    
/%






r  c                   @   s(   e Zd ZdZedd Zedd ZdS )Tuplez'A high-level representation of a tuple.c                 C   s   dS )Nr   r5   r  r5   r5   r6   r'  5
  s    zTuple.open_bracketc                 C   s   dS )Nr   r5   r  r5   r5   r6   rs   9
  s    zTuple.close_bracketNr]  r^  r_  r`  r  r'  rs   r5   r5   r5   r6   r)  1
  s
   
r)  c                   @   s(   e Zd ZdZedd Zedd ZdS )Listz&A high-level representation of a list.c                 C   s   dS )Nr   r5   r  r5   r5   r6   r'  B
  s    zList.open_bracketc                 C   s   dS )N]r5   r  r5   r5   r6   rs   F
  s    zList.close_bracketNr*  r5   r5   r5   r6   r+  >
  s
   
r+  c                   @   s(   e Zd ZdZedd Zedd ZdS )	DictOrSetz3A high-level representation of a dictionary or set.c                 C   s   dS )Nr   r5   r  r5   r5   r6   r'  O
  s    zDictOrSet.open_bracketc                 C   s   dS )N}r5   r  r5   r5   r6   rs   S
  s    zDictOrSet.close_bracketNr*  r5   r5   r5   r6   r-  K
  s
   
r-  c                   @   s   e Zd ZdZedd ZdS )r	  z4A high-level representation of a list comprehension.c                 C   s,   d}| j D ]}t|tr q(||j7 }q
|S r  )r  rh  r
  r  )r   lengthr  r5   r5   r6   r  \
  s    

zListComprehension.sizeN)r]  r^  r_  r`  r  r  r5   r5   r5   r6   r	  X
  s   r	  c                   @   s   e Zd ZdZdS )r
  z0A high-level representation of an if-expression.N)r]  r^  r_  r`  r5   r5   r5   r6   r
  f
  s   r
  c                 C   sT  t t| |  g}|d7 }t| }||k rPt| |  }|jdv r|dkrZt||d fS |dkrrt||d fS |t | |jdkrt||fS |jdkrt||fS |jdkrt	||fS n|jdv rt
| |\}}|| n^|jdkrt
| |d\}}|| n6|jdkr8t
| |d\}}|| n|t | |d7 }q"d	S )
z9Parse a high-level container, such as a list, tuple, etc.r   z,)]}forrS   r   r,  r.  rT   )NN)r  r  ra   r  r	  r
  r`   r)  r+  r-  _parse_container)rf   rI  Z	for_or_ifr  
num_tokenstokr  r5   r5   r6   r1  k
  s:    






r1  c                 C   s   d}g }t | }||k rt| |  }|jtjks4J |jtjkrBq|jdv rnt| |\}}|sbdS |	| n|	t
| |d7 }q|S )zoParse the tokens.

    This converts the tokens into a form where we can manipulate them
    more easily.

    r   rT   Nr   )ra   r  ro   r  r  r:   rX   r  r1  r`   r  )rf   rI  parsed_tokensr2  r3  r  r5   r5   r6   _parse_tokens
  s     

r5  c                 C   s   t | d dkr|dt  }n|dt  }| }t|}|t|d |st| d}t| d}|r|rt |d dkrt|t| d t|krdS | D ]N}	|jt |	d	d
 |}
|rt|	t	rd}d|
 d  }|	||| |
}q| S )z'Reflow the lines so that it looks nice.r   defz  r   r   r   r   NT)r  F)r\   rV   r  r  ra   r   r  r   rh  r  r  r  r  )r4  r  r   start_on_prefix_liner  r  linesZfirst_tokenZsecond_tokenr  Zsave_continued_indentr5   r5   r6   _reflow_lines
  s6    

r9  c                 c   sn   || V  t | }|rjt|||dd}|r@tt| r@|V  t|||dd}|rjtt| rj|V  dS )zShorten the line taking its length into account.

    The input is expected to be free of newlines except for inside
    multiline strings and at the end.

    T)r7  FN)r5  r9  r   r  r   )rf   r|   r  r   r4  r   r5   r5   r6   r  
  s    

r  c                 C   s  g }t t| D ]\}}|\}	}
}}|	tjks2J |
|v rdddd|
}|rt| |d ddgdd |kst| |d ddgdd |krq|dkr|
d	kr| |d  d d
v rq|t|d k r|| qt| |d }|	tj	kr|r|d tj	kr|| qd}d}t
||D ]P}|rT|d| | 7 }dD ]}||r6||7 }q6n|}|rbJ |}q|dusxJ t|dkrt|n|r|| S dS )zSeparate line by breaking at tokens in key_token_strings.

    The input is expected to be free of newlines except for inside
    multiline strings and at the end.

    r   r,  r.  r   r   r   r   Nr  r   r   z,(%[r   r   rT   )r   r  r  r  r[   r  ra   r`   r:   r^   split_at_offsetsrU   r   r  )rf   r|   r  r   r  r   offsetsrI  Z_tro   r  r  r  Zunwanted_next_tokenZprevious_tokencurrent_indentr   rm   symbolr5   r5   r6   r    s    
r  c                 c   s   d}d}d}| D ]j}|d }|d }|d \}}|d \}	}
||7 }||krT||8 }|}|t |7 }||||fV  |	}|
}qdS )zYield tokens and offsets.r   r   r   r   Nr  )rf   r  Zprevious_end_rowZprevious_end_columnrO  ro   r  r  r  r  r  r  r5   r5   r6   r  \  s(    r  c                 C   sl   |  dr |  dr | d S |  dr2d|  S |  drD| d S |  drV| d S |  d	rh| d S | S )
zrNormalize multiline-related code that will cause syntax error.

    This is for purposes of checking syntax.

    rF   rM   z passzreturn z	def _(): @zdef _(): passzclass )zif zelif for zwhile )rG   re   rU   r   r5   r5   r6   r  {  s    



r  c                 C   s>   | d|  d}| |d d}|dr2| S || | S )z3Replace whitespace at offset and return fixed line.Nr   r   )re   r   rG   )rm   r   r   r+  r-  r5   r5   r6   r     s
    
r   c                    s>   G  fdddt j t jd| d| }|  |j S )z,Execute pycodestyle via python method calls.c                       s8   e Zd ZdZ fddZ fddZdd Z  ZS )z"_execute_pep8.<locals>.QuietReportz'Version of checker that does not print.c                    s   t  | | g | _d S rr  )superr    _QuietReport__full_error_results)r   r~   QuietReport	__class__r5   r6   r     s    z+_execute_pep8.<locals>.QuietReport.__init__c                    s8   t  | ||||}|r4| j|||d |d dS )zCollect errors.r   )r   rm   r   r   N)rA  rt   rB  r`   )r   r  r   rp   checkr   rC  r5   r6   rt     s    z(_execute_pep8.<locals>.QuietReport.errorc                 S   s   | j S )zReturn error results in detail.

            Results are in the form of a list of dictionaries. Each
            dictionary contains 'id', 'line', 'column', and 'info'.

            )rB  r  r5   r5   r6   full_error_results  s    z5_execute_pep8.<locals>.QuietReport.full_error_results)r]  r^  r_  r`  r   rt   rG  __classcell__r5   rD  )rE  r6   rD    s   rD  r-   )r8  Zreporter)r-   )r'   Z
BaseReportZCheckerZ	check_allreportrG  )r   r|   Zcheckerr5   rI  r6   r     s    r   c                 C   s&   |r|  dtt d S |  dS )Nz 	r   )r   re   r  r  )rm   with_rstripr5   r5   r6   _remove_leading_and_normalize  s    rL  c                   @   s,   e Zd ZdZdd ZefddZdd ZdS )	r  zReindents badly-indented code to uniformly use four-space indentation.

    Released to the public domain, by Tim Peters, 03 October 2000.

    c                 C   s   t |}| }t|| _g | _t|ddD ]J\}}|| jv rN| j| q.|t|k}| jt	|
 t||  q.| jdd  d| _|| _d S )Nr   r  r   )r2   r}   rD   r  string_content_line_numbersr8  r   r`   ra   r   
expandtabsrL  r  rI  
input_text)r   rO  r   r  r  rm   rK  r5   r5   r6   r     s     



zReindenter.__init__c              	   C   s  |dk r| j S ztt| j}W n ttjfy@   | j  Y S 0 | j}|t	|df i }g }|d d }|
|d|  tt	|d D ]}|| \}}||d  d }	t|| }
|| }|dk r|
r||
d}|dk r@t|d t	|d D ]<}|| \}}|dkr|
t|| kr6|| } q@q|dk rt|d ddD ]B}|| \}}|dkrZ|
t||d   t||  } qqZ|dk r|
}nd}|dksJ |||
< ||
 }|dks|
dkr|
|||	  qt|||	 |dD ]x\}}|| jv r,|| nV|dkr`|dkrL|| n|d| |  n"tt|| }|||d  qqd|S )	zcFix indentation and return modified line numbers.

        Line numbers are indexed at 1.

        r   r   r*   r  r   r   Nr-   )rO  _reindent_statsr:   r   getliner@   r   r8  r`   ra   extendrc   _leading_space_countr[   r   rM  r   r   )r   r  statsr8  Z	have2wantZafterr   ZthisstmtZ	thislevelZnextstmtZhaveZwantjZjlineZjlevelr  r  rm   remover5   r5   r6   r    sr    











zReindenter.runc                 C   s4   | j t| jkrd}n| j| j  }|  j d7  _ |S )zLine-getter for tokenize.r-   r   )rI  ra   r8  )r   rm   r5   r5   r6   rQ  ;  s
    zReindenter.getlineN)r]  r^  r_  r`  r   rV   r  rQ  r5   r5   r5   r6   r    s   Rr  c                 C   s   d}d}g }| D ]}|d }|d d }|d }|t jkr@d}q|t jkrXd}|d7 }q|t jkrpd}|d8 }q|t jkr|r||df q|t jkrq|rd}|r|||f q|S )zReturn list of (lineno, indentlevel) pairs.

    One for each stmt and comment line. indentlevel is -1 for comment
    lines, as a signal that tokenize doesn't know what to do about them;
    indeed, they're our headache!

    r   r   r   r   r*   )r:   rX   r  r  r]   r`   rW   )rf   Z	find_stmtlevelrT  rO  ro   r   rm   r5   r5   r6   rP  E  s2    






rP  c                 C   s*   d}|t | k r&| | dkr&|d7 }q|S )z(Return number of leading spaces in line.r   r   r   r  )rm   r   r5   r5   r6   rS  x  s    
rS  c                 C   sd   ddl m} dd |D }|||d}ddlm} zt|j| |dW S  |jy^   |  Y S 0 dS )	zQUse lib2to3 to refactor the source.

    Return the refactored source code.

    r   )RefactoringToolc                 S   s   g | ]}d | qS )zlib2to3.fixes.fix_r5   r   namer5   r5   r6   r     ru  z&refactor_with_2to3.<locals>.<listcomp>)r  Zexplicitr9   )rZ  N)Zlib2to3.refactorrX  r<   r:   r\   Zrefactor_stringr   )Zsource_textr  r4   rX  ZfixerstoolrC   r5   r5   r6   r    s    r  c              
   C   s2   zt | ddddW S  tttfy,   Y dS 0 dS )zReturn True if syntax is okay.z<string>execT)dont_inheritFN)compiler@   	TypeErrorr  r  r5   r5   r6   r     s    r   c                    s   t t|  }|sg S |d  }ddittd dD ]$\}}| }||krZ qd||< q>fdd fdd|D S )	zA wrapper around 're.finditer' to find line numbers.

    Returns a list of line numbers where pattern was found in contents.
    r*   r   z\nr   c                    s   | dd|  } | S )zuGet the line number of string in a files contents.

        Failing to find the newline is OK, -1 maps to 0

        r   r   )rfindrq   )rH   r   Znewline_offset)newline_offsetsr5   r6   get_line_num  s    z,find_with_line_numbers.<locals>.get_line_numc                    s   g | ]}| d  qS )r   r5   )r   rH   )r   rb  r5   r6   r     ru  z*find_with_line_numbers.<locals>.<listcomp>)rd   r  finditerrq   r   )r  r   matchesrr   Zline_numrZ  r   r5   )r   rb  ra  r6   find_with_line_numbers  s    
	re  c                 C   s   t t| }t t| }ttd| d }i }|D ]}d||< q0|D ]}d||< qBg }d}d}t| D ]D\}	}
|
du r|du r|	}d}qh|
du rh|du rh|||	f d}qh|du r|||f |S )zReturns a list of tuples representing the disabled ranges.

    If disabled and no re-enable will disable for rest of file.

    r   r   TFN)	re  ENABLE_REGEXDISABLE_REGEXra   r  findallr   r  r`   )r|   Zenable_line_numsZdisable_line_numsZtotal_linesZenable_commandsZnumdisabled_rangesZcurrently_enabledZdisabled_startrm   Zcommanded_enabledr5   r5   r6   get_disabled_ranges  s*    



rj  c                 C   s<   | d }|D ]*}|d |  kr,|d krn q dS qdS )z;Filter out reports based on tuple of disabled ranges.

    rm   r   r   FTr5   )r   ri  rm   Zdisabled_ranger5   r5   r6   filter_disabled_results  s
     rk  c           	      #   s*  t | dd}t | dd}t| }t|   r> fdd|D }tdd |D }|D ]}|d  }|d	 |v r||d
r|qT|d	 |v r|dv rqT|s|d	 d |v r|drqT|dkr|drqT|dkr|drqT|dkr|drqT|d	 |v r
|dr
qT|r|drqT|V  qTdS )z|Filter out spurious reports from pycodestyle.

    If aggressive is True, we allow possibly unsafe fixes (E711, E712).

    Fr  Tc                    s   g | ]}t | r|qS r5   )rk  r   r   ri  r5   r6   r     s   z"filter_results.<locals>.<listcomp>c                 s   s   | ]}|d    dkV  qdS )r   Ze901N)r   rl  r5   r5   r6   r     ru  z!filter_results.<locals>.<genexpr>r   rm   )e1r  Zw191)r  r   )Zw29Zw39r   )Ze711Ze72Zw6)Ze712Ze713Ze714r   Ze704)Ze26r  )rn  Ze7N)r  r  rj  r{  r   rG   )	r|   r   r   Z!non_docstring_string_line_numbersZall_string_line_numbersZcommented_out_code_line_numbersZhas_e901r)   Zissue_idr5   rm  r6   r     sP    





r   c              	   C   s   t  }d}zpt| D ]b}|d }|d d }|d d }|tjkrr||krr|sX|tjkrr|t td| d| O }|}qW n ttjfy   Y n0 |S )zReturn line numbers that are within multiline strings.

    The line numbers are indexed at 1.

    Docstrings are ignored.

    r-   r   r   r   r   )r   r   r:   r^   r  rc   r@   r   )r|   r  line_numbersZprevious_token_typerO  ro   r  r  r5   r5   r6   r  .  s"    
r  c              	   C   s   g }zt | D ]}|d }|d }|d d }|d }| dsFq|tjkr|d }t @ tjdt	d d|v rd|vrt
|r|| W d	   q1 s0    Y  qW n ttjfy   Y n0 |S )
zReturn line numbers of comments that are likely code.

    Commented-out code is bad practice, but modifying it just adds even
    more clutter.

    r   r   r   r   r   r   )categoryr   N)r   r   rG   r:   r]   r   warningscatch_warningsfilterwarningsSyntaxWarningr   r`   r@   r   )r|   ro  rO  ro   r  r  rm   Zstripped_liner5   r5   r6   r  N  s.    

.r  c                 C   s   t | |ksJ |  } t| d }t|t |d }d}t | t | | d  |krt| d  st| d| d S |rtd| rtj| 	d|||d	d	d
}d
|d S | d S )zReturn trimmed or split long comment line.

    If there are no comments immediately following it, do a text wrap.
    Doing this wrapping on all comments in general would lead to jagged
    comment text.

    r  H   rE  r*   Nr   z\s*#+\s*\w+r   F)initial_indentsubsequent_indentwidthZbreak_long_wordsZbreak_on_hyphens)ra   re   r   r   r0  r  rH   textwrapwrapr   r   )rm   r   r  r  ZMIN_CHARACTER_REPEATZsplit_linesr5   r5   r6   r  u  s,    

r  c                    sB    fdd| D }|r>| d | d  dkr>|d  d|d< |S )zcReturn fixed line endings.

    All lines will be modified to use the most common line ending.
    c                    s   g | ]}| d   qS )
re   r  r0   r5   r6   r     ru  z*normalize_line_endings.<locals>.<listcomp>r*   r{  r|  )r8  r0   rm   r5   r}  r6   normalize_line_endings  s    r~  c                 C   s   | | p|  |S rr  rs  )r  rQ   r5   r5   r6   mutual_startswith  s    r  c                 C   s   |r>t |trJ dd |D D ]}t|  | r  dS q |rt |trPJ dd |D D ]}t|  | r^ dS q^dS dS )Nc                 S   s   g | ]}|  qS r5   r  r   r   r5   r5   r6   r     ru  zcode_match.<locals>.<listcomp>Fc                 S   s   g | ]}|  qS r5   r  r  r5   r5   r6   r     ru  T)rh  r\   r  r   )r   r   r   Zignored_codeZselected_coder5   r5   r6   r    s    r  c                 C   s>   t ||}t| ts$| |p t } t| }t| |dS )zgReturn fixed source code.

    "encoding" will be used to decode "source" if it is a byte string.

    r~   )	_get_optionsrh  r\   decodeget_encodingr2   r}   	fix_linesrD   )r|   r~   r/   apply_configr   r5   r5   r6   fix_code  s
    


r  c                 C   s   | st dg|dS t| trt dg|d}|  D ]d\}}t||sRtd|tt||}t|t	t
fst|t	t
frtd|t||| q2n| }|S )zReturn parsed options.r-   r  zNo such option '{}'z"Option '{}' should not be a string)
parse_argsrh  dictr  r   r  rI   typer   strr\   setattr)Zraw_optionsr  r~   rZ  valueZexpected_typer5   r5   r6   r    s     

r  c                 C   s   t | }dt| d}t }|jr*|}nP|j|j|j|jd}t	
|}| }	t||	}
dd |
D }t||||d}d}t }t||vr|jdkr||jkrq|d7 }|t| t|}t||||d	}| }qt	
|}dt| |S )
zReturn fixed source code.r-   r   r   c                 S   s    h | ]}|d  t v r|d  qS )r   )"SELECTED_GLOBAL_FIXED_METHOD_CODESrl  r5   r5   r6   	<setcomp>  s   zfix_lines.<locals>.<setcomp>)r4   codesr   r   )r   r   )rJ  r   r~  r   r   r   r   r   rg   r2   r}   rD   r   apply_global_fixeshashZpep8_passesr   copyry   r   )r  r~   r4   Zoriginal_newlineZ
tmp_sourceZprevious_hashesr  r   r   r   r   r  Zpassesr   r   r5   r5   r6   r    sH    




r  c                 C   sd  |st | g|d}t| }|}|js.|js.|r6t| }|rJtt||d}t||| d}|jrt	|}|
 }t||| }|r|| |  n|jdkr||}|S |jrHd| }	| }
|r|d dd nd}|dd }|	|
ks||krDt| d|d}|| W d    n1 s60    Y  |S d S |r`|| |  |S )	Nr  r;   r  r   r-   r*   r   w)r  rE   in_placer  r1   LineEndingWrapperr  r  r2   r}   rD   r  writeflushjobsencoder   rk  r   r7   )r4   r~   outputr  original_sourcer  r/   r  r  r  r   Zoriginal_source_last_lineZfixed_source_last_linefpr5   r5   r6   fix_file  sH    





*
r  c                  c   sT   t t  D ]@} t| rt| }|dd dgkr8qt| }|r|| fV  qdS )z'Yield multiple (code, function) tuples.Nr   r|   )rd   globalsvaluesinspectZ
isfunctionr   extract_code_from_function)function	argumentsr   r5   r5   r6   global_fixesD  s    
r  c                 C   s>   t jjdkr,t| r| j} tt| jS t	| d S d S )Nr   r   )
r   version_infomajorr  Zismethod__func__rd   Z	signature
parametersZ
getargspec)r  r5   r5   r6   r   Q  s
    
r   c              	      s   |du rg }t  fdddD r0t|  jd} t D ]b\}}| tv rX| |vrXq6t| j jdr6 j	rt
d|| tjd ||  jd	} q6t|  j j j|| j	d
} | S )zRun global fixes on source code.

    These are fixes that only need be done once (unlike those in
    FixPEP8, which are dependent on pycodestyle).

    Nc                 3   s    | ]}t | j jd V  qdS )r  N)r  r   r   r   r   r  r5   r6   r   i  s   z%apply_global_fixes.<locals>.<genexpr>)E101ZE111)r  r  r  r   )r   )r   r   r   r4   r  r   )r{  r  r  r  r  r  r  r   r   r   r   rI   r   r   r   r  )r|   r~   r  r4   r  r   r  r5   r  r6   r  _  s@    
r  c                 C   sV   | j dsdS tdd| j }|s(dS zt|dd  W n tyP   Y dS 0 |S )z Return code handled by function.r   Nz^fix_r-   r   )r]  rG   r  r/  r   r  )r  r   r5   r5   r6   r    s    r  c                  C   s   d tjg} d| S )Nzpycodestyle: {}r   )rI   r'   __version__r   )Zpackagesr5   r5   r6   _get_package_version  s    r  c               	   C   s  t jttdd} | jdddtt d | jddd	d
dd | jddddd | jddddd | jddtdtd | jdddd | jddddd | jddt	d d!d"d# | jd$d%d d&t	d'd( | jd)d*d	d
d+d | jd,dd-d | jd.d/d0d1 | jd2dd3d | jd4d5d6d7t
d | jd8d5d6d9d | jd:d d;t	d<d( | jd=d>d?d@t	dAdBdC | jdDtt	t jdE | jdFddGd | jdHddId | jdJdKdLdM | S )NzReturn command-line parser.autopep8)descriptionprogz	--versionversionz%(prog)s {} ({}))actionr  z-vz	--verboser   r   zCprint verbose messages; multiple -v result in more verbose messages)r  r  helpz-dz--diff
store_truez#print the diff for the fixed source)r  r  z-iz
--in-placezmake changes to files in placez--global-configr4   zapath to a global pep8 config file; if this file does not exist then this is ignored (default: {}))metavarr  r  z--ignore-local-configzdon't look for and apply local config files; if not passed, defaults are updated with any config files in the project's root directoryz-rz--recursivezHrun recursively over directories; must be used with --in-place or --diffz-jz--jobsr   r   z@number of parallel jobs; match CPU count if value is less than 1)r  r  r  r  z-pz--pep8-passesr*   z<maximum number of additional pep8 passes (default: infinite))r  r  r  r  z-az--aggressivezLenable non-whitespace changes; multiple -a result in more aggressive changesz--experimentalzenable experimental fixesz	--excludeZglobszCexclude file/directory names that match these comma-separated globs)r  r  z--list-fixesz3list codes for fixes; used by --ignore and --selectz--ignorer   r-   z.do not fix these errors/warnings (default: {})z--selectz*fix only these errors/warnings (e.g. E4,W)z--max-line-lengthr}  z6set maximum allowed line length (default: %(default)s)z--line-rangez--rangerm   Nr   zlonly fix errors found within this inclusive range of line numbers (e.g. 1 99); line numbers are indexed at 1)r  r  r  nargsr  z--indent-size)r  r  r  z--hang-closingz)hang-closing option passed to pycodestylez--exit-codezchange to behavior of exit code. default behavior of return value, 0 is no differences, 1 is error exit. return 2 when add this option. 2 is exists differences.filesr   z&files to format or '-' for standard in)r  r  )argparseArgumentParserdocstring_summaryr`  add_argumentrI   r  r  DEFAULT_CONFIGr   DEFAULT_IGNORErV   SUPPRESS)parserr5   r5   r6   create_parser  s    








r  c                    s   t  }d}t fddtD r$d}d|v }d|v } D ]}|dkr|r\|r\|h d q|rp|h d	 q|h d
 q8|dv r|r|r|dh q|r|ddh q|ddh q8|dv r|s8|| q8|S )zexpand to individual E/W codesFc                 3   s$   | ] t  fd dD V  qdS )c                 3   s   | ]}  |V  qd S rr  rs  r  Zconflicting_coder5   r6   r     s   z*_expand_codes.<locals>.<genexpr>.<genexpr>N)r{  r   r  r  r6   r     s   z _expand_codes.<locals>.<genexpr>Tr   r   W>   W6W2W1W3W505>   r  r   r  r  r  r  >   r  r  r   r  r  r  )ZW5ZW50r  r   )r   allCONFLICTING_CODESr   r   )r  Zignore_codesretZis_conflictZis_ignore_w503Zis_ignore_w504r   r5   r  r6   _expand_codes  s0    r  c                    s  t  }||   js( js(|td dd  jD  _|rt |}zt |}W n tyl   d}Y n0 |rv|}||  dd  jD  _d jv rt	 jdkr|td  j
r|td	  jr|td
  jr|td t	 jdkr js j
s|td  jr< js< j
s<|td  jrX j
rX|td  jdkrp|td  jdkr|td  jrtt j jrt jng  _ jrt j _t fddtD r jt n& js jr
h d _n
tt _ jr*t j _ni  _ jdk rNddl}|  _ jdkrv jsv j
sv|td  jr jd dkr|td  jd  jd kr|td  S )zParse command-line options.zincorrect number of argumentsc                 S   s   g | ]}t |qS r5   decode_filenamerY  r5   r5   r6   r     ru  zparse_args.<locals>.<listcomp>Nc                 S   s   g | ]}t |qS r5   r  rY  r5   r5   r6   r     ru  r   r   z"cannot mix stdin and regular filesz)--diff cannot be used with standard inputz---in-place cannot be used with standard inputz.--recursive cannot be used with standard inputz^autopep8 only takes one filename as argument unless the "--in-place" or "--diff" args are usedz2--recursive must be used with --in-place or --diffz,--in-place and --diff are mutually exclusiver   z(--max-line-length must be greater than 0z$--indent-size must be greater than 0c                 3   s(   | ]  t  fd djD  V  qdS )c                 3   s   | ]}  |V  qd S rr  rs  )r   Zignore_coder  r5   r6   r   a  s   z'parse_args.<locals>.<genexpr>.<genexpr>N)r{  r   r  argsr  r6   r   `  s   zparse_args.<locals>.<genexpr>>   r  r  r  r  Ez!parallel jobs requires --in-placez --range must be positive numberszAFirst value of --range should be less than or equal to the second)r  r  r  
list_fixesexitEXIT_CODE_ARGPARSE_ERRORread_configread_pyproject_toml	Exceptionra   r  r  	recursiver   r  r   r  _split_comma_separatedr   r  r  r   r   r  excluder  multiprocessing	cpu_countr   )r  r  r  Zparser_with_pyproject_tomlr  r5   r  r6   r  
  s    




 


r  c                 c   s   |  |D ]r\}}|ddd}||s0q
|| }|tu rN| ||}n"|tu rd| ||}n| ||}|||fV  q
d S )Nr   r  )r  r   r   r[   r   ZgetintboolZ
getboolean)configsectionoption_listkr  norm_optZopt_typer  r5   r5   r6   _get_normalize_options  s    
r  c           
         s   t  }z|| j | jsj| jo4tjtj| j  }|rj| fddt	D rXqjtj
 \ }q:i }dd |jD }dD ]H}||sqt|||D ]*\}}}	| jrtd|||	 |	||< qq|jf i | W n ty   Y n0 |S )z5Read both user configuration and local configuration.c                    s   g | ]}t j |qS r5   )ospathr   )r   fnparentr5   r6   r     s   zread_config.<locals>.<listcomp>c                 S   s    i | ]}|j |jpt|jqS r5   destr  r  r   or5   r5   r6   
<dictcomp>  s   zread_config.<locals>.<dictcomp>)r(   r'   Zflake8z+enable config: section={}, key={}, value={})r   r>   global_configignore_local_configr  r  r  abspathcommonprefixPROJECT_CONFIGr   _actionsZhas_sectionr  r   r   rI   set_defaultsr   )
r  r  r  taildefaultsr  r  r  r  r  r5   r  r6   r    s@    
r  c                 C   s  ddl }d}tj| jrNt| j}||}W d   n1 sD0    Y  | js| jontj	tj
| j }}|rtj|d}tj|rt|(}||}W d   qW d   n1 s0    Y  tj|\}}qt|sdS |di ddu rdS |dd}i }dd |jD }	d}
| D ]x\}}|d	d	d
}|	|sbq8t|ttfv r||
v rd|}n|}| jrtd|| |||< q8|r|jf i | |S )z+Read pyproject.toml and load configuration.r   Nzpyproject.tomlr[  r  c                 S   s    i | ]}|j |jpt|jqS r5   r  r  r5   r5   r6   r    s   z'read_pyproject_toml.<locals>.<dictcomp>)r   r   r   r  r   z.enable pyproject.toml config: key={}, value={})tomlr  r  existsr  r3   loadr  r  r  r  r   r   r[   r  r  r   r   r  rd   r!  r   r   rI   r  )r  r  r  r  r  r  r  Zpyproject_tomlr  r  ZTUPLED_OPTIONSr  vr  r  r5   r5   r6   r    sR    (

,r  c                 C   s   dd |  dD S )zReturn a set of strings.c                 S   s   h | ]}|  r|  qS r5   r  )r   rp   r5   r5   r6   r    ru  z)_split_comma_separated.<locals>.<setcomp>r   r   )r  r5   r5   r6   r    s    r  c                 C   s   t | tr| S | t S )zReturn Unicode filename.)rh  r\   r  r   getfilesystemencodingr  r5   r5   r6   r     s    
r  c               
   c   s   dt tjfV  tdddd} t| D ]>}td|}|r&|d t	ddt t
| |jfV  q&tt D ]6\}}| d	t| d  t	ddt |jfV  qpttD ]2}| d	t| d  t	ddt tjfV  qdS )
zYield pep8 error codes that autopep8 fixes.

    Each item we yield is a tuple of the code followed by its
    description.

    r  Nr-   )r4   r~   r   zfix_([ew][0-9][0-9][0-9])r   z\s+r   r   )r  r  r`  ry   dirr  rH   r#  r  r/  r   r   r  ra   r  r  )instanceZ	attributer   r  r5   r5   r6   supported_fixes  s$    r  c                 C   s   | r|  dd S dS )zReturn summary of docstring.r   r   r-   r  )Z	docstringr5   r5   r6   r  $  s    r  c                    s  |   sdS d}|  d}d |d  ds|d  d dvrdD ]N\}}|d |}|d |}	|dkrL|	dk s|	|d krLt d|  qLt fdd	|D }
|d
td|
|  7 }|t|7 }|dtdd	 |D  7 }dddd	|d d }t|dkr8|r8|d  |r8|d7 }t
|D ]f\}}|  }|drbq@dD ],}||r~|d7 }||krf|d7 }qf|drd|v r|d7 }|drht|t|kr|d7 }|dr|dd  dr|d7 }|drBt|dkrB|d  s:|d dv rB|d7 }|drV|d7 }t|rh|d7 }td|dr|d7 }td |dr|d7 }|d!r|d8 }|d"r|d#8 }|d$rdt|}|d7 }|t|k r>|t|| 7 }||  dr|}q>|| d$s2q>|d7 }q||k rR|d%7 }n||r^dnd7 }d|v r|d&r|d%7 }|d&r|d7 }|d%t| 7 }q@td|S )'zDReturn rank of candidate.

    This is for sorting candidates.

    r   r   r   r*   rT   z()z[]z{}r   c                 3   s   | ]} t |  V  qd S rr  )ra   r   r   r  r   r5   r6   r   D  ru  z'line_shortening_rank.<locals>.<genexpr>r   r   c                 s   s   | ]}t |V  qd S rr  r  r  r5   r5   r6   r   K  ru  r   r,  r.  r:     )r  r   r   r   r   r  i  z': )r   r   r   r  r   Nr   r   r  i,  r  z.*[(\[{]\s*[\-\+~]$z\ z.*lambda\s*\*$r   r@  2   r   
   rM   )r   re   r   r   rG   r   r   ra   standard_deviationr[   r   rU   r0  has_arithmetic_operatorr  rH   count_unbalanced_brackets)r  r   r   r   Zrankr8  openingclosingZopening_locZclosing_locZcurrent_longestZbad_staring_symbollinenoZcurrent_lineZ	bad_startZ	total_lenr5   r  r6   r~  )  s    







r~  c                    sB   t | } | sdS t| t|   t fdd| D t|  d S )zReturn standard deviation.r   c                 3   s   | ]}|  d  V  qdS )r   Nr5   )r   r   Zmeanr5   r6   r     ru  z%standard_deviation.<locals>.<genexpr>g      ?)rd   r   ra   )Znumbersr5   r  r6   r    s    r  c                 C   s   t jD ]}|| v r dS qdS )z6Return True if line contains any arithmetic operators.TF)r'   ZARITHMETIC_OP)rm   r"   r5   r5   r6   r	    s    
r	  c                 C   s2   d}dD ]$\}}|t | || | 7 }q|S )z/Return number of unmatched open/close brackets.r   r  )r   r   )rm   r   r  r  r5   r5   r6   r
    s    r
  c                 C   s^   g }d}d}t |D ]2}|t| k rB||krB|| ||   |}q|| |d  |S )z9Split line at offsets.

    Return list of strings.

    r   N)r   ra   r`   r   )rm   r<  r   Zprevious_offsetZcurrent_offsetr5   r5   r6   r;    s    r;  c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r  zReplace line endings to work with sys.stdout.

    It seems that sys.stdout expects only '\n' as the line ending, no matter
    the platform. Otherwise, we get repeated line endings.

    c                 C   s
   || _ d S rr  )_LineEndingWrapper__output)r   r  r5   r5   r6   r     s    zLineEndingWrapper.__init__c                 C   s    | j |dddd d S )Nr   r   r
   )r  r  r   )r   sr5   r5   r6   r    s    zLineEndingWrapper.writec                 C   s   | j   d S rr  )r  r  r  r5   r5   r6   r    s    zLineEndingWrapper.flushN)r]  r^  r_  r`  r   r  r  r5   r5   r5   r6   r    s   r  c                 C   sd   t j| }|drdS |D ](}t||r4 dS t| |r dS qt j| s`t| s`dS dS )z4Return True if file is okay for modifying/recursing.r  FT)r  r  basenamerG   fnmatchisdiris_python_file)r4   r  	base_namer  r5   r5   r6   
match_file   s    
r  c                 #   s   | r|  d}|rjtj|rjt|D ]>\}}|  fdd|D 7 }  fdd|D |dd< q(q d} D ]}t||rrd} qqr|s |V  q dS )zYield filenames.r   c                    s.   g | ]&}t tj| rtj|qS r5   r  r  r  r   )r   fr  rootr5   r6   r     s   zfind_files.<locals>.<listcomp>c                    s$   g | ]}t tj| r|qS r5   r  )r   ru   r  r5   r6   r     s   NFT)rb   r  r  r  walkr  )	filenamesr  r  rZ  ZdirectoriesZchildrenZis_exclude_matchr  r5   r  r6   
find_files  s    
 r  c              
   C   sl   | d j r"td| d tjd z
t|  W S  tyf } z"tt|tjd |W Y d}~n
d}~0 0 dS )z>Helper function for optionally running fix_file() in parallel.r   z	[file:{}]r   r   N)r   r   rI   r   r   r  IOErrorr\   )r  rt   r5   r5   r6   	_fix_file)  s    

r  c                 C   s*  g }t | |j|j} |jdkrddl}||j}g }| D ] }|t||ff}|| q:|	  |
  |jr|D ]"}	tj|	   tj  qv|dd |D  nv| D ]p}t|||f}|du rq|jr|dkr|| q|jr|| qt|}
d
|
 | kr|| q|S )z?Fix list of files.

    Optionally fix files recursively.

    r   r   Nc                 S   s   g | ]}|d ur|  qS rr  )r[   r  r5   r5   r6   r   I  ru  z&fix_multiple_files.<locals>.<listcomp>r-   )r  r  r  r  r  ZPoolZapply_asyncr  r`   closer   r  r   stdoutr  r[   r  r  rR  r  rE   rk  )r  r~   r  r   r  ZpoolZretsrZ  r  r)   r  r5   r5   r6   fix_multiple_files4  s:    
r"  c              	   C   s   |  drdS z\t| td<}|t}|s>W d   W dS | d }W d   n1 s^0    Y  W n ttfy   Y dS 0 t|sdS dS )z'Return True if filename is Python file.z.pyTr+   NFr   )	rU   r7   MAX_PYTHON_FILE_DETECTION_BYTESr>   rk  r  
IndexErrorPYTHON_SHEBANG_REGEXrH   )r4   r  rp   Z
first_liner5   r5   r6   r  [  s"    

.
r  c                 C   s   d| v pd| v p|   dS )zReturn True if line is likely part of a multiline string.

    When multiline strings are involved, pep8 reports the error as being
    at the start of the multiline string, which doesn't work for us.

    rp  z'''r   )re   rU   r   r5   r5   r6   r  q  s
    r  c                 C   s   t |t| dr| jn| S )z&Return output with specified encoding.buffer)codecs	getwriterr   r&  )r  r/   r5   r5   r6   r    s
    r  c                   C   s   t  pt S )zReturn preferred encoding.)localegetpreferredencodingr   getdefaultencodingr5   r5   r5   r6   r    s    r  c           
      C   s  | du rt j} zttjtj W n ty4   Y n0 zJt| dd |d}|jrtt	 D ]\}}t
dj||d q^tW S |jdgkr|jrJ t jjpt }t j }t|||d}tt j|d| t|t|kr|jrtW S n|js|jrtt|j|_n t|jdks"J |jr.J t|j|t j}|jrZt dd	 |D }	nt d
d	 |D }	|jr|	rtW S W n. t!y   t" Y S  t#y   t" Y S 0 dS )zCommand-line entry.Nr   r  z{code} - {description})r   r  r   r;   c                 S   s   g | ]}t |d kqS )r   r  r   r  r5   r5   r6   r     ru  zmain.<locals>.<listcomp>c                 S   s   g | ]}|d uqS rr  r5   r,  r5   r5   r6   r     ru  )$r   argvsignalSIGPIPESIG_DFLAttributeErrorr  r  r   r  r   rI   EXIT_CODE_OKr  r  stdinr/   r  r>   r  r  r!  r  r  Z	exit_codeEXIT_CODE_EXISTS_DIFFr  rd   r   ra   r  r"  r{  r  EXIT_CODE_ERRORKeyboardInterrupt)
r-  r  r  r   r  r/   Z
read_stdinZfixed_stdinr   r  r5   r5   r6   main  sJ    



r7  c                   @   s    e Zd ZdZdd Zdd ZdS )CachedTokenizerzzA one-element cache around tokenize.generate_tokens().

    Original code written by Ned Batchelder, in coverage.py.

    c                 C   s   d | _ d | _d S rr  )	last_textlast_tokensr  r5   r5   r6   r     s    zCachedTokenizer.__init__c                 C   s2   || j kr,t|}tt|j| _|| _ | jS )z*A stand-in for tokenize.generate_tokens().)r9  r2   r}   rd   r:   r   r=   r:  )r   rp   Z	string_ior5   r5   r6   r     s    


zCachedTokenizer.generate_tokensN)r]  r^  r_  r`  r   r   r5   r5   r5   r6   r8    s   r8  __main__)r)   Nr*   )r*   )r|  r}  FFF)N)F)Nr-   )r-   F)TNNr-   r  F)T)FFr-   )Fr-   )N)T)r-   )F)F)NNF)r-   )NNF)r  r-   N)F)F)N)NT)r`  rd  r   r   r   r   r  r'  r  r  r  r  r  r2   ry  r  r)  r  r  r.  r   ry  r  r:   rq  re  Zconfigparserr   r   r   ImportErrorr'   r	   r\   	NameErrorr  r  r  r  r  r^  r%  rB  r8  r6  r@  r"  rw  rf  rg  r2  r5  r4  r  	frozensetr  r  rV   r  r  r  platformr  
expanduserr  r   getenvr  r  r#  r7   r1   rE   rL   Zregister_checkrx   Z_checksr  ry   r	  r  r  r  r  r  r   r   r  r  r  r  r  r  r  r  r  rJ  r   r   r  r   r  r  rH  
namedtupler  r  r  r  r)  r+  r-  r	  r
  r1  r5  r9  r  r  r  r  r   r   rL  r  rP  rS  r  r   re  rj  rk  r   r  r  r  r~  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r~  r  r	  r
  r;  r  r  r  r  r"  r  r  r  r  r7  r8  Z_cached_tokenizerr   r]  r  r5   r5   r5   r6   <module>2   s  















	
 @
       eU	 
0	*! 
	$
'
E  _Q ;!*O&~3!"F '#6/ 
&I& &6
 
		'8