
    We*`                    .	   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ddlmZmZ ddlZddlmZ d	Z d
Z!dZ"dZ# ejH                  d      Z% ejH                  d      Z& ejH                  d      Z' ejH                  d      Z( ejH                  d      Z) ejH                  d      Z* ejH                  d      Z+ ejH                  d      Z, ejH                  d      Z-dZ.dZ/dZ0dZ1 e2 e2dg       e2dg       e2g d       e2g d       e2g d       e2g d      g      Z3dZ4d Z5d!Z6d"gd#gg d$d%Z7ejp                  d&k(  rejr                  ju                  d'      Z;nIejr                  jy                   ejz                  d(      xs ejr                  ju                  d)      d*      Z;ejr                  j}                  e;      s`ejp                  d&k(  rejr                  ju                  d+      Z;n5ejr                  jy                  ejr                  ju                  d)      d,      Z;d-Z?d.Z@dd/ZAdd0ZBd1 ZCd2 ZD ej                  eD       d3 ZFej                  d4   ej                  =  ej                  eF        G d5 d6eH      ZId7 ZJd8 ZK	 	 dd9ZLd: ZMd; ZNd< ZOd= ZPd> ZQdd?ZRdd@ZSdA ZTdB ZUddCZVddDZW	 	 ddEZXdF ZYdG ZZdH Z[dI Z\dJ Z]	 ddKZ^	 ddLZ_dM Z` ej                  dNg dO      Zb G dP dQeH      Zc G dR dSeH      Zd G dT dUeH      Ze G dV dWee      Zf G dX dYee      Zg G dZ d[ee      Zh G d\ d]ee      Zi G d^ d_ee      Zjdd`Zkda Zldb Zmdc Zndd Zode Zpdf Zqdg Zrdh ZsddiZt G dj dkeH      Zudl Zvdm ZwddnZxdo Zydp Zzdq Z{dr Z|ds Z}ddtZ~du ZddvZdw Zdx Zdy ZddzZd{ Zdd|Zdd}Zd~ Zd Z	 	 ddZd Zd Zd Zd ZddZd Zd Zd Zd Zd Zd Zd Z	 ddZd Zd Zd Zd Z G d deH      Zd Zd Zd ZddZd Zd Zd Zd ZddZ G d deH      Z e       ZejP                  Zedk(  r ejT                   e              yy)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)!STARTSWITH_INDENT_STATEMENT_REGEXz2.0.4

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W504ws_commaidioms)applyexceptexitfuncnumliteralsoperatorparenreducerenamesstandarderrorsys_excthrowtuple_params
xreadlines)E231E721W690win32z~\.pycodestyleXDG_CONFIG_HOMEz	~/.configpycodestylez~\.pep8pep8)z	setup.cfgztox.iniz.pep8z.flake8i   c                 R    |st        | |      }t        j                  | ||d      S )z,Return opened file with a specific encoding.limit_byte_check )modeencodingnewline)detect_encodingioopen)filenamer9   r:   r7   s       (lib/python3.12/site-packages/autopep8.pyopen_with_encodingrA      s,    "8>NO778$     c                 .   	 t        | d      5 }ddlm} |j                  |j                        d   }ddd       t        |       5 }|j                  |       ddd       |S # 1 sw Y   2xY w# 1 sw Y   |S xY w# t        t        t        f$ r Y yw xY w)zReturn file encoding.rbr   tokenizeNr:   zlatin-1)
r>   lib2to3.pgen2rF   r<   readlinerA   readLookupErrorSyntaxErrorUnicodeDecodeError)r?   r7   
input_filelib2to3_tokenizer:   	test_files         r@   r<   r<      s    
(D!ZB'77
8K8KLQOH "  8<	NN+, =  "! = &89 sE   A= %A$A= A0	A= $A-)A= 0A:5A= :A= =BBc                 d    t        |       5 }|j                         cddd       S # 1 sw Y   yxY w)zReturn contents of file.N)rA   	readlines)r?   rN   s     r@   readlines_from_filerS      s$    	H	%##% 
&	%	%s   &/c              #   (  K   |j                  d      r9|r6t        j                  j                  |       rddj	                  |      f yyyt        j                  j                  |      r%|r"|s|s| j                  d      r
d| v rd yyyyyyyw)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)
startswithr3   DOCSTRING_REGEXmatchformat)logical_lineblank_linesblank_beforeindent_levelprevious_logicals        r@   extended_blank_linesr_      s      ""6*;66<<\J6==kJKK K;		$	$	*	*+;	< ##V-|#<< $ .    
=s   BBc              #   L
  K   |d   d   d   }d|d   d   d   z   |z
  }|s|dk(  ry| j                  d      }dx}	}
|dk7  rt        fnt        dt        z  f}dg|z  }dg|z  }dgg}dg}i }|d   d   }|d   g}d}d}d}d	}|D ]/  \  }}}}}|	|d   |z
  k  }|r1|d   |z
  }	| xr" |t        j                  t        j                  fv}|}|r|}t        j                  |      |z
  ||	<   |t        j                  k(  xr |d
v }t        ||
         D ]  }||	   ||   z
  }||v }|s n ||
   r||
   k(  }| xr dkD  xr |j                  |d         } |r*||
   r%|d   ||
   k7  r|dj                  ||
         f n|rs|r|dj                  ||
         f n||
   r(|d   ||
   k  r| dur|dj                  ||
         f ns|r4||	   dt        z  k(  r%|r|s|dj                  ||   z         f ||
<   nt| du r	|d   ||
<   ng| |t        fv rn\||   z   t        z   }!dk  rd|!f}"n0||
   rd||
   f}"n#|s
||
   rd|!f}"n|t        kD  rd|!f}"n	|||
<   d|!f}"| dj                  |" f ||	   r8|t        j                  t        j                  fvr||
   s|d   ||
<   d||d   <   ny|t        j                  t        j                  fv s|dv rt        ||d   <   nF|s|	s|
s|dk(  rd||d   dz   <   n/|dk(  r*||d   d j                         r||
   j                  |	       |t        j                  k(  rQ|dv rh|
dz  }
|j                  d       |j                  d       t!        |      |
k(  r|j                  g        ||
   j                  |	       ||	xx   dz  cc<   n|dv r|
dkD  r|j#                         xs |d   }#|j#                          t%        |
      D ]  }$||$   |#kD  sd||$<    t'        |      D ]  }%|%|#k\  s	||%=  ||
dz   d= |
dz  }
|
rd|||
   <   t%        |	dd      D ]  }&||&   s	||&xx   dz  cc<    n t!        |      |
dz   k(  sJ |d   |vr'|j)                         j                  d      s|||d   <   |d   |d   k7  }|r||	   ||d   |z
  <   |}2 |rj|sgt        j                  |      |t        z   k(  rGd   |d   dz   f}'|dt        z  z   }( r|'dj                  |(      f y|'dj                  |(      f yyyyw)zCOverride pycodestyle's function to provide indentation information.r   r   r   N:	r8   Fz]})zE124 {}zE133 {}TzE128 {}zE123 {}E122E127E131E126E121z{} {})uurbbrif([{z)]}r   r   zE129 {}zE125 {})endswithDEFAULT_INDENT_SIZErF   NLNEWLINEr3   expand_indentOPreversedgetrY   strCOMMENTSTRINGisspaceappendlenpoprangelistrstrip))rZ   tokensr]   hang_closingindent_charnoqa	first_rownrowsindent_nextrowdepthvalid_hangsparens
rel_indent	open_rowshangsindent_chanceslast_indentindentlast_token_multilineline	last_linelast_line_begins_with_multiline
token_typetextstartendr;   close_bracketopen_rowhanghanging_indentvisual_indentone_indentederrorprev_indentdindidxposdesired_indents)                                            r@   continued_indentationr      s     q	!QIr
1a  9,Euz '',KOC% $ 
%8%&)<%<%>  S5[F uJ IFE N)A,K!nFDI&+#.4*
D%da9,,(Y&C// I!(++x7G7G)HH .B+K *77=LJsO (8;;6H45=M %Yu%56!#H)==!%!4!	 7
 U|"&%,"6!.. :4!8 :+//a9  8ve}, )"2"26%="ABBt )"2"26%="ABB58fUm#; , )"2"26%="ABB c?a*=&==  )"2"2<3=h3G4H #I J J#e$& %au4+- ,z(/C C 3!4 19#\2EE]#VE]3E&5<#\2E//#\2E#'E%L#\2Engnne455 3Kx{{H,<,<==u!!HF5M'+N58$X__h.>.>??,,'*N58$  Eddl)-N3q6A:&S[T#a&']224e##C( $u}
a T"y>U*$$R(% '',sq 519$jjl<k!n		uAay;.$%q	 &  /Ck)*3/ 0 eaij)
48N6%=1 b"-Cc{sq( . v;%!)+++a.$$&//4 ,0uQx( %aCF 2-7_Js1v	)*	O /5T 	+!!$'<:M+MMQxQ'%,?(??	((899	((899 	N , 	s    D%T$(KT$9T$1T$CT$rZ   c                      e Zd ZdZ	 	 d/dZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d  Z!d! Z"d" Z#d# Z$d$ Z%d% Z&d& Z'd' Z(d( Z)d) Z*d* Z+d+ Z,d, Z-d- Z.d. Z/y)0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,e275
        - e301,e302,e303,e304,e305,e306
        - e401,e402
        - e502
        - e701,e702,e703,e704
        - e711,e712,e713,e714
        - e722
        - e731
        - w291
        - w503,504

    Nc                 z   || _         |t        |      | _        n*t        j                  |      }|j                         | _        || _        t        dj                  | j                              | _	        i | _
        t        | j                        D ]L  \  }}|j                  d      dk(  s|j                  d      dk(  s/|| j                  vs>|| j                  |<   N |
t               n|| _        | j                  | _        | j"                  | _        | j"                  | _        | j"                  | _        | j"                  | _        | j"                  | _        | j"                  | _        | j"                  | _        | j"                  | _        | j4                  | _        | j8                  | _        | j8                  | _        | j8                  | _        | j@                  | _!        | j@                  | _"        | j@                  | _#        | jH                  | _%        | jH                  | _&        | jH                  | _'        | j@                  | _(        | jR                  | _*        | jH                  | _+        | jX                  | _-        | j@                  | _.        | j@                  | _/        | j@                  | _0        | j@                  | _1        | jd                  | _3        |r'|jh                  dk\  s|jj                  r| jl                  n| jn                  | _8        | jr                  | _:        | jv                  | _<        | jv                  | _=        y )Nr8   import r   from r   )>r?   rS   sourcer=   StringIOrR   options_get_indentwordjoinindent_wordimports	enumeratefindsetlong_line_ignore_cachefix_e112fix_e115_fix_reindentfix_e121fix_e122fix_e123fix_e124fix_e126fix_e127fix_e128fix_e129fix_e131fix_e133fix_e201fix_e202fix_e203fix_e211fix_e271fix_e221fix_e222fix_e223fix_e225fix_e226fix_e227fix_e228fix_e241fix_e224fix_e242fix_e252fix_e262fix_e261fix_e272fix_e273fix_e274fix_e275fix_e301fix_e306
aggressiveexperimentalfix_long_line_logicallyfix_long_line_physicallyfix_e501fix_e702fix_e703fix_w291fix_w292fix_w293)selfr?   r   contentsr   sioir   s           r@   __init__zFixPEP8.__init__  si    !-h7DK++h'C--/DK*2774;;+?@  -GAt		)$)TYYw-?1-D,%&T"	 . ,3CE' 	# **************** ++q0G4H4H (()) 	 rB   c           	         	 t        | j                        \  }}d}t               }t        |t              D ]  }|d   |v rd|d   j                         z   }t        | |      r<t        | |      }|d   dz
  }	| j                  |	   }
t        t        |            dkD  }|r_d }|rQt        | j                  |      }|r7t        t        |d	   d	   dz   |d   d	   dz               j                  |      r |||      }n ||      }||rJ | j                  |	   |
k(  rg }|r|j!                  |       |g k(  rO| j"                  j$                  dk\  s!t'        d
j)                  |d   |d         t*        j,                         U|j/                  |d          k| j"                  j$                  dk\  st'        dj)                  |      t*        j,                         |d   j1                         }t'        dj)                  | j2                  |d   |d   |      t*        j,                          y # t        t        j                  f$ r d}Y =w xY w)NTFkeyr   fix_idr   r   r   z'--->  Not fixing {error} on line {line})r   r   file   z--->  '{}' is not defined.infoz--->  {}:{}:{}:{}column)_find_logicalr   rL   rF   
TokenErrorr   sorted_priority_keylowerhasattrgetattrr|   _get_parameters_get_logicalr~   intersectionupdater   verboseprintrY   sysstderraddstripr?   )r   resultslogical_startlogical_endlogical_supportcompleted_linesresultfixed_methodnamefix
line_indexoriginal_lineis_logical_fixlogicalmodified_linesr   s                  r@   _fix_sourcezFixPEP8._fix_source
  s`   	$+8+E(]K"O %W-88Ff~0%t(:(:(<<t-.d$45#F^a/
 $J 7!$_S%9!:Q!>!"G&".t{{/5/</:#< #s5#AJqMA-#AJqMA-,/ (00< /11(1 %%(%9N%([N!)---{{:.-?)+!#**>:#r)||++q0ELL&,Tl M I!$-
 $''v7<<''1,4;;<LM ZZ) "&>//1D-44T]]5;F^5;H5E59;  #zz	+k 9	 X001 	$#O	$s   H; ;IIc                    | j                   j                  | j                   j                  | j                   j                  | j                   j                  d}t        || j                        }| j                   j                  roi }|D ]3  }|d   |vrt               ||d   <   ||d      j                  |d          5 t        dj                  t        |      |      t        j                         | j                   j                  r9| j                   j                  \  }}|D cg c]  }||d   cxk  r|k  rn n| }}| j!                  t#        dj%                  | j                        || j                   j&                               | j                   j                  rAt)        d	 | j                  d
z
   D              }||z   d
z
  | j                   j                  d
<   dj%                  | j                        S c c}w )z@Return a version of the source code with PEP 8 violations fixed.)ignoreselectmax_line_lengthr   r   r   z$--->  {n} issue(s) to fix {progress})nprogressr   r8   )r   r  r   c              3   >   K   | ]  }|j                  d         yw)r   Ncount).0slines     r@   	<genexpr>zFixPEP8.fix.<locals>.<genexpr>k  s"      A%?E D)%?s   r   )r   r  r  r  r   _execute_pep8r   r  r   r  r  rY   r|   r  r  
line_ranger  filter_resultsr   r   sum)r   pep8_optionsr  r  rr   r   r  s           r@   r  zFixPEP8.fixM  s    ll))ll))#||;; LL55	
  dkk:<<HT7(*(+HQtW%4!%%ai0  8??g, @ 39<E <<""00JE3") 5'Q1V933 'G 5 	rwwt{{/C0737<<3J3JL 	M <<"" A%)[[3%?A AE).):DLL##A&wwt{{##5s   &G>c                     t        |d   j                         d         }|d   dz
  }| j                  |   }d|z  |j                         z   | j                  |<   y)znFix a badly indented line.

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

        r   r   r    N)intsplitr   lstrip)r   r  num_indent_spacesr  targets        r@   r   zFixPEP8._fix_reindentq  s\      v 4 4 6q 9:F^a'
Z("%(9"9FMMO"KJrB   c                     |d   dz
  }| j                   |   }|j                         j                  d      sg S | j                  |z   | j                   |<   y)zFix under-indented comments.r   r   #N)r   r+  rV   r   )r   r  r  r-  s       r@   r   zFixPEP8.fix_e112}  sP    F^a'
Z(}}))#.I"&"2"2V";JrB   c                     |d   dz
  }| j                   |   }t        |      }|j                         }|dd |z   | j                   |<   y)zFix unexpected indentation.r   r   Nr   _get_indentationr+  r   r  r  r-  r   strippeds         r@   fix_e113zFixPEP8.fix_e113  sL    F^a'
Z(!&)==?"(*x"7JrB   c                     |d   dz
  }| j                   |   }t        |      }|j                         }|j                  d      sg S |dd |z   | j                   |<   y)zFix over-indented comments.r   r   r/  N)r   r2  r+  rV   r3  s         r@   fix_e116zFixPEP8.fix_e116  s_    F^a'
Z(!&)==?""3'I"(*x"7JrB   c                     |d   dz
  }| j                   |   }t        |      }|dk(  rg S |j                         }|dd |z   | j                   |<   y)zFix over-indented.r   r   rc   Nr1  r3  s         r@   fix_e117zFixPEP8.fix_e117  sX    F^a'
Z(!&)T>I==?"(*x"7JrB   c                    t        |d   j                         d         }|d   dz
  }| j                  |   }|t        t	        |            z
  }t        t	        |            }g }t        t	        | j                  |               |k\  r`d|z  | j                  |   z   | j                  |<   |j                  d|z          |dz  }t        t	        | j                  |               |k\  r`|S )9Fix indentation undistinguish from the next logical line.r   r   r   r(  )r)  r*  r   r|   r2  r{   )r   r  r,  r  r-  spaces_to_addr   r  s           r@   fix_e125zFixPEP8.fix_e125  s    v 4 4 6q 9:F^a'
Z()C0@0H,II%f-."4;;z#:;<F'*]':'+{{:'>(?DKK
#!!!j.1!OJ	 "4;;z#:;<F rB   c                    t        |d   j                         d         }|d   dz
  }| j                  |   }|t        t	        |            z
  }t        t	        |            }||z
  }|dk(  r|dk(  rd}|dk\  r#d|z  | j                  |   z   | j                  |<   yt        |      }| j                  |   |d | j                  |<   y)r;  r   r   r   r   r   r(  N)r)  r*  r   r|   r2  abs)r   r  r,  r  r-  r<  indent_lengthoffsets           r@   r   zFixPEP8.fix_e131  s    v 4 4 6q 9:F^a'
Z()C0@0H,II,V45)M9!mq&8MA'*]':'+{{:'>(?DKK
# 'F&*kk*&=fg&FDKK
#rB   c                 |    |d   dz
  }| j                   |   }|d   dz
  }t        ||d      }|| j                   |<   y)zRemove extraneous whitespace.r   r   r   r8   rA  replacementNr   fix_whitespacer   r  r  r-  rA  fixeds         r@   r   zFixPEP8.fix_e201  sO    F^a'
Z(!A%v&,+-/ #(JrB   c                     | j                   |d   dz
     }|d   dz
  }|d| ||d j                  dd      z   }|| j                   |d   dz
  <   y)z-Remove extraneous whitespace around operator.r   r   r   Nrc   r(  )r   replace)r   r  r-  rA  rH  s        r@   r   zFixPEP8.fix_e224  sb    VF^a/0!A%w&/"9"9$"DD*/F6NQ&'rB   c                    | j                   |d   dz
     }|d   dz
  }|d| dz   ||d z   }|j                  dd      |j                  dd      k(  rt        |      t        |      k(  r|| j                   |d   dz
  <   |j                  dd      }	 t	        |      }t        |j                               sy	 t        j                  }t         |||            }t        |      D ]2  }	||	d   j!                         d   k7  r|	d   d   }|d| dz   ||d z   }4 || j                   |d   dz
  <   yg S # t
        t        j                  f$ r Y yw xY w# t        $ r t        j                  }Y w xY w)	z'Fix missing whitespace around operator.r   r   r   Nr(  r8   r   r   )r   rJ  r2  rv   generate_tokensrL   rF   r   check_syntaxr+  r3   "missing_whitespace_around_operatorAttributeErrormissing_whitespacer   ru   r*  )
r   r  r-  rA  rH  
error_codets_missing_whitespaceerrorses
             r@   r   zFixPEP8.fix_e225  s   VF^a/0!A%w#%vw7
 MM#r"fnnS"&==U#'7'??.3DKKv*+D!,J$U+  /EBB $ -eR89Ff%1a01agv,uVW~=	 &
 /4DKKv*+I'  !4!45  " E&1&D&D#Es$   D0 6E 0EEE+*E+c                     |d   dz
  }| j                   |   }|d   }|d| j                         dz   ||d j                         z   }|| j                   |<   y)zAdd missing whitespace.r   r   r   Nr(  )r   r   r+  rG  s         r@   fix_e231zFixPEP8.fix_e231	  sc    F^a'
Z(!w&&(3.1G1G1II"'JrB   c                    |d   dz
  }| j                   |   }t        |d   dz
  t        |      dz
        }||   j                         r|}n'|d| j	                         ||d j                         z   }|j                  d      rX|j	                  d      | j                   |<   | j                   |dz      j                         | j                   |dz   <   |dz   |dz   gS || j                   |d   dz
  <   y)z,Remove whitespace around parameter '=' sign.r   r   r   N)z=\
z=\
z=\
 	\r   )r   minr|   r  r   r+  ro   )r   r  r  r-  crH  s         r@   fix_e251zFixPEP8.fix_e251  s    F^a'
Z(
 x 1$Fa! !9??E2AJ%%'&**;*;*==E >>78&+ll;&?DKK
#*.++j1n*E*L*L*NDKK
Q'NJN33*/F6NQ&'rB   c                     | j                   |d   dz
     }|d   }|d| j                  d      }||d j                  d      }||j                         rd|z   ndz   }|| j                   |d   dz
  <   y)z&Fix spacing after inline comment hash.r   r   r   N 	#z  # r   )r   r   r+  r  )r   r  r-  rA  codecommentrH  s          r@   r   zFixPEP8.fix_e262,  s    VF^a/0!gv%%f-/((0GMMO(F*/F6NQ&'rB   c                 V   | j                   |d   dz
     }t        |      }|j                  d      }t        d t	        |      D              }|d| }||d j                  d      }|j                  d      ry||z   |j                         rd|z   ndz   }|| j                   |d   dz
  <   y)	z%Fix spacing after block comment hash.r   r   z 	c              3   2   K   | ]  \  }}|d k7  s|  yw)r/  N )r  indexr[  s      r@   r   z#FixPEP8.fix_e265.<locals>.<genexpr>>  s     F/heQQ#XE/s   N!r(  r   )r   r2  r+  nextr   rV   r  )	r   r  r-  r   r   r   hashesr`  rH  s	            r@   fix_e265zFixPEP8.fix_e2658  s    VF^a/0!&)}}U#F)D/FGdsst*##E* c"GMMO3=N*/F6NQ&'rB   c                     | j                   |d   dz
     }|j                         j                  d      ryt        |      }|dz   |j	                  d      z   }|| j                   |d   dz
  <   y)z"Fix too many block comment hashes.r   r   r/  N# z# 	)r   r  ro   r2  r+  )r   r  r-  indentationrH  s        r@   fix_e266zFixPEP8.fix_e266J  sk    VF^a/0 <<>""3'&v.d"V]]6%::*/F6NQ&'rB   c                     |d   dz
  }| j                   |   }|d   dz
  }t        ||d      }||k(  rg S || j                   |<   y)z*Fix extraneous whitespace around keywords.r   r   r   r(  rC  NrE  rG  s         r@   r   zFixPEP8.fix_e271W  s[    F^a'
Z(!A%v&,+.0 F?I&+DKK
#rB   c                 ^    d}|| j                   |d   dz
     z   | j                   |d   dz
  <   y)zAdd missing blank line.r   r   r   N)r   )r   r  crs      r@   r   zFixPEP8.fix_e301f  s5    *,t{{6&>A;M/N*NF6NQ&'rB   c                     dt        |d   j                         d         z
  }d}| j                  |d   dz
     j                         dk(  rd}d|z  }|| j                  |d   |z
     z   | j                  |d   |z
  <   y)	zAdd missing 2 blank lines.r   r   ra   r   r   \r   N)r)  r*  r   r  )r   r  add_linenumrA  ro  s        r@   fix_e302zFixPEP8.fix_e302k  s    #fVn224R899;;vf~)*002d:FKVF^f455 	F6NV+,rB   c                 ^   t        |d   j                  d      d   j                  d      d         dz
  }t        d|      }d}|d   dz
  }g }||k  rZ|dk\  rU| j                  |   j	                         s(d| j                  |<   |j                  d|z          |dz  }|dz  }||k  r|dk\  rU|S )	zRemove extra blank lines.r   r   r   )r   r   r   r8   )r)  r*  maxr   r  r{   )r   r  delete_linenumcntr   r  s         r@   fix_e303zFixPEP8.fix_e303v  s    VF^11#6q9??DQGH1LQ/ f~!N"tqy;;t$**,$&D!%%a$h/qAID N"tqy rB   c                 n    |d   dz
  }| j                   |   j                         sd| j                   |<   yy)z/Remove blank line following function decorator.r   r   r8   N)r   r  )r   r  r   s      r@   fix_e304zFixPEP8.fix_e304  s8    f~!{{4 &&( "DKK )rB   c                 $   dt        |d   j                         d         z
  }d}|d   dz
  }g }|dk  rlt        |      }||k  rZ|dk\  rU| j                  |   j	                         s(d| j                  |<   |j                  d|z          |dz  }|dz  }||k  r|dk\  rU|S d}	 |dk  rn/| j                  |   j                         }|sn|d   d	k7  rn|dz  }5|dz  }|| j                  |   z   | j                  |<   |j                  d|z          |S )
z9Add missing 2 blank lines after end of function or class.r   r   ra   r   r   r8   r   r   r/  )r)  r*  r?  r   r  r{   r+  )r   r  add_delete_linenumrx  rA  r  ro  r   s           r@   fix_e305zFixPEP8.fix_e305  sK   VF^%9%9%;B%?!@@!#!!$%7!8**v{{{6*002*,DKK'"))!f*51HC! **v{,  BA:{{6*1137c>!  aKF"$t{{6':":DKK!!!f*-rB   c                 <   |d   dz
  }| j                   |   }|d   dz
  }|j                         j                  d      sg S t        j                  d|d      d   }|d| j                  d	      d
z   |z   dz   ||d j                  d	      z   }|| j                   |<   y)zPut imports on separate lines.r   r   r   importz
\bimport\b)patternstringmaxsplitr   Nz	 ,r   r   )r   r+  rV   rer*  r   )r   r  r  r-  rA  rk  rH  s          r@   fix_e401zFixPEP8.fix_e401  s    F^a'
Z(!A%}}))(3Ihh}&,q::;=''/$6()+1&'?+A+A&+IJ"'JrB   c                     t        || j                        \  }}}t        dd      D ]?  }dj                  | j                  |||z          }	 t	        dj                  |              n || j                  v r| j                  |   |k7  s5t        | j                  |      }| j                  |   z   | j                  |<   t              D ]  }d| j                  ||z   <    y # t
        t        j                  f$ r Y w xY w)Nr   d   r8   )
get_index_offset_contentsr   r~   r   rL  rL   rF   r   r   !get_module_imports_on_top_of_file)r   r  r  rA  r-  r   r   
mod_offsets           r@   fix_e402zFixPEP8.fix_e402  s    '@AE(N$VVq#A774;;z*Q,?@D.   $,,&4<<+?:+M:4;;;EGJ&*T[[-D&DDKK
#AhF-/DKK
6)*   !4!45 s   C!!C=<C=c                    |rCt        |d         dk(  s2| j                  |d   dz
     j                         j                  d      r| j	                  |      S |d   d   }|d   d   }|d   }t        | j                  |dz
  d      }t        | j                  |dz   d      }t        dj                  |            }	 | j                  |||dj                  |            }	|	rDt        ||dz         D ]  }
d| j                  |
<    |	| j                  |<   t        |dz   |dz         S g S # t        t        j                  f$ r | j	                  |      cY S w xY w)	:Try to make lines fit within --max-line-length characters.r   r   r   r/  r   r8   defaultr-  previous_line	next_lineoriginal)r|   r   r+  rV   r   get_itemjoin_logical_liner   fix_long_linerL   rF   r   r~   )r   r  r  start_line_indexend_line_indexlogical_linesr  r  single_linerH  r  s              r@   r   zFixPEP8.fix_long_line_logically  sj    
Oq KKv*+224??D0088"1:a= A
 .>.BBOT[[.1*<bI	'(>?	9&&"+#/	 ' 1E #$4nq6HI
*,J' J,1DKK())A-~/ABB	 X001 	90088	9s   =$D) )*EEc                 <   |d   dz
  }| j                   |   }t        | j                   |dz
  d      }t        | j                   |dz   d      }	 | j                  ||||      }|r|| j                   |<   |dz   gS g S # t        t        j
                  f$ r g cY S w xY w)r  r   r   r8   r  r  )r   r  r  rL   rF   r   )r   r  r  r-  r  r  rH  s          r@   r   z FixPEP8.fix_long_line_physically  s    F^a'
Z( j1nbIT[[*q."E		&&+#	 ' !E &+DKK
#N##	 X001 	I	s   A= =BBc           
      L   |||f}|| j                   v rg S |j                         j                  d      rX| j                  j                  r@t        || j                  j                  |j                         j                  d             S g S t        |||| j                  | j                  j                  | j                  j                  | j                  j                  | j                  j                        }|rt        ||      s|S | j                   j                  |       y )Nr/  )r   r  last_comment)r-  r  r  r   r  r   r   r  )r   r+  rV   r   r   shorten_commentr  get_fixed_long_liner   r   r  code_almost_equalr  )r   r-  r  r  r  cache_entryrH  s          r@   r  zFixPEP8.fix_long_line
  s    }i8$555I==?%%c*||&&&$(LL$@$@%.%5%5%7%B%B3%G!GI I I#'(( LL88||..22LL((* *8U;L##''4rB   c                 z    t        || j                        \  }}}|j                  d      dz   | j                  |<   y)z$Remove extraneous escape of newline.rY  r   N)r  r   r   )r   r  r  _r-  s        r@   fix_e502zFixPEP8.fix_e502)  s9    ";F<@KK#IQ"(--"<t"CJrB   c                     |d   dz
  }| j                   |   }|d   }|d| dz   t        |      z   | j                  z   ||d j                  d      z   }|| j                   |d   dz
  <   |d   |d   dz   gS )z9Put colon-separated compound statement on separate lines.r   r   r   Nr   rY  )r   r2  r   r+  )r   r  r  r-  r[  fixed_sources         r@   fix_e701zFixPEP8.fix_e701/  s    F^a'
Z(8r
T)(01373C3CDqr
))+67 +7F6NQ&'vv 233rB   c                 2   |sg S |d   }|D ]m  }|d   dk(  sd|v st        j                  |      s'| j                  j                  r.t	        dj                  |d         t        j                         g c S  |d   d	z
  }| j                  |   }|j                         j                  d
      rX|j                  d      | j                  |<   | j                  |d	z      j                         | j                  |d	z   <   |d	z   |dz   gS |j                         j                  d      r'|j                  d      dz   | j                  |<   |d	z   gS |d   d	z
  }|d| j                  d      j                         }t        |d         ||d j                  d      j                         z   }	d}
||d j                  d      j                         dd dk(  r||d j                  d      }
|
r||
z   | j                  |<   n|dz   |	z   | j                  |<   |d	z   gS )z=Put semicolon-separated compound statement on separate lines.r   r   E702rb   z8---> avoid fixing {error} with other compound statements)r   r   r   r   rq  z
 	\;z
 	;r   r   Nr   rj  )r	   rX   r   r  r  rY   r  r  r   r   ro   r+  r2  )r   r  r  r  r   r  r-  rA  firstsecondinline_comments              r@   r   zFixPEP8.fix_e702;  s   I
 "Dt&3$;9??E<<''44:FF4N ZZ
 	 " F^a'
Z(==?##D)&,mmK&@DKK
#*.++j1n*E*L*L*NDKK
Q'NJN33==?##C(&,mmJ&?$&FDKK
#N##!A%w&&s+224"=#34/((-4467 &'?!!#&--/3t;#FG_33C8N&+n&<DKK
#&+dlV&;DKK
#QrB   c                 2   t        || j                        \  }}}t        j                  |      }|rfdj	                  |j                  d      t        |      | j                  z   ||j                  d      d j                               | j                  |<   yy)z'Fix multiple statements on one line defz{}
{}{}r   N)
r  r   STARTSWITH_DEF_REGEXrX   rY   groupr2  r   r   r+  r   r  r  r  r-  rX   s         r@   fix_e704zFixPEP8.fix_e704k  s    ";F<@KK#IQ$**62&0&7&7A (4+;+;;uyy|}%,,.'0DKK
# rB   c                 V   t        || j                        \  }}}|dz   }|t        |      k\  rg S |d| j                         }||| }||d j	                         }|j                         dk(  rd}	n|j                         dk(  rd}	ng S dj                  ||	|g      | j                  |<   y)zFix comparison with None.r   N==is!=is notr(  )r  r   r|   r   r+  r  r   )
r   r  r  rA  r-  right_offsetleftcenterright
new_centers
             r@   fix_e711zFixPEP8.fix_e711v  s    '@AE(N$VV z3v;&Igv%%'|,|}%,,.<<>T!J\\^t#!JI"%((D*e+D"EJrB   c                    t        || j                        \  }}}t        j                  d|      r't        j                  dd|d      | j                  |<   y	t        j                  d|      r't        j                  dd|d      | j                  |<   y	|dz   }|t        |      k\  rg S |d	| j                         }||| }||d	 j                         }d	}	|j                         d
k(  r0t        j                  d|      r\t        j                  dd|d      }	nB|j                         dk(  r/t        j                  d|      rt        j                  dd|d      }	|	g S |	d   j                         rd|	z   }	||	z   | j                  |<   y	)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 *r8   r  z	\bFalse\bz\bFalse\b *r   r(  )
r  r   r  rX   subr|   r   r+  r  isalnum)
r   r  r  rA  r-  r  r  r  r  	new_rights
             r@   fix_e712zFixPEP8.fix_e712  sp   '@AE(N$VV 886?&(ff-L-:F!'MDKK
#XX7@&(ff-K-:F!'MDKK
# "A:Ls6{*	'6?))+DF<0F<=)002E I||~%88K/ "}b%q II4'88L%0 "~r5 JI 	|##%)O	&*Y&6DKK
#rB   c           	         t        || j                        \  }}}|d| }||d }t        j                  |      }d\  }}|r:|j	                  d      }|j                         }dj                  |d| d||d       }t        j                  |      }	|	r|	j                  d      dk(  r|	j	                  d      }
dj                  |d|
 |	j                  d      |	j                  d      |	j                  d      ||	j                         d |      }|r&||z   }
||z   d	z
  }dj                  |d|
 d
||d       }|| j                  |<   yyy)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searchr   r   rY   COMPARE_NEGATIVE_REGEXr  )r   r  r  rA  r-  before_targetmatch_notinnotin_pos_startnotin_pos_endrX   	pos_start
new_targetpos_ends                r@   fix_e713zFixPEP8.fix_e713  sn   '@AE(N$VV w4;;FC)-&)//2O'OO-M%%'($}~0FHF '--f5{{1~%!KKN	4;;:I&AAKKNF599;<$8-I
  /& 8I+f4q8G!*!1!1":I.*WX:N"PJ*4J' & rB   c           	         t        || j                        \  }}}|d| }||d }t        j                  |      }d\  }}|r:|j	                  d      }|j                         }dj                  |d| d||d       }t        j                  |      }	|	r|	j                  d      j                  d      r|	j	                  d      }
dj                  |d|
 |	j                  d	      |	j                  d      |	j                  d      ||	j                         d |      }|r&||z   }
||z   d
z
  }dj                  |d|
 d||d       }|| j                  |<   yyy)z,Fix object identity should be 'is not' case.Nr  r   r  r  r   r  r  r   r   r  )
r  r   r  r  r   r   rY   r  r  rV   )r   r  r  rA  r-  r  match_isnotisnot_pos_startisnot_pos_endrX   r  r  r  s                r@   fix_e714zFixPEP8.fix_e714  st   '@AE(N$VV w4;;FC)-&)//2O'OO-M%%'($}~0FHF '--f5{{1~((.!KKN	4;;:I&AAKKNF599;<$8-I
  /& 8I+f4q8G!*!1!1":I.*WX:N"PJ*4J' / rB   c                     t        || j                        \  }}}t        j                  |      }|r;dj	                  |d|d   dz
   d||j                         d       | j                  |<   yy)zfix bare except{}{}{}Nr   r   zexcept BaseException:)r  r   BARE_EXCEPT_REGEXr  rY   r   r  s         r@   fix_e722zFixPEP8.fix_e722  st    ";F<@KK#IQ!((0&.oo,x(1,-/Fuyy{|$'&DKK
# rB   c                 L   t        || j                        \  }}}t        j                  |      }|rs|j	                         }dj                  |d|j                  d       |j                  d      |j                  d      ||d j                               | j                  |<   yy)z,Fix do not assign a lambda expression check.z{}def {}({}): return {}Nr   r   r   )	r  r   LAMBDA_REGEXr  r   rY   r   r  r+  )r   r  r  r  r-  rX   r   s          r@   fix_e731zFixPEP8.fix_e731  s    ";F<@KK#IQ##F+))+C&?&F&FA'QQst##%''DKK
# rB   c                 z    | j                   |d   dz
     j                         }|dz   | j                   |d   dz
  <   y)zRemove trailing whitespace.r   r   r   N)r   r   )r   r  
fixed_lines      r@   r   zFixPEP8.fix_w291  s>    [[!!34;;=
*4t*;F6NQ&'rB   c                     d}t        | j                        D ]  }|j                         }|r n|dz  } t        | j                        }| j                  d||z
   | _        t	        dd|z         S )zRemove trailing blank lines.r   r   N)ru   r   r   r|   r~   )r   r  blank_countr   original_lengths        r@   fix_w391zFixPEP8.fix_w391  so    T[[)D;;=Dq  * dkk*kk"@?[#@AQO+,,rB   c                 `   t        || j                        \  }}}|j                         d   }	 t        |      }t        |d   d   |      sy d}d}d}	t        d      D ]k  }
||
z
  dk  r n`||
z
  dz
  }|dk  st        | j                        |k  r n8|dz   }| j                  |   j                         }|s|r|d   dk(  r|	dz  }	kd}	 t        dj                  | j                  ||             }d}g }t        |      D ]A  \  }}|d   t
        j                  t
        j                  fv s,|j                  |       |dz  }C |dkD  r	||d	   d  }n|}g }|D ]m  }|d   t
        j                  t
        j                  fv r|dz  }|dk  r n<t
        j                   |d   k(  r"|r |d   t
        j                  k7  r
|d
   d   } n|}o  n |j#                  |      }
|dz
  |	z
  }dj%                  |d |
 ||
t        |      z   d  j                               | j                  |<   t'        | j                  ||       }| j                  |   }|j#                  |      }|r*dj%                  |d | |||dz   d        | j                  |<   y |d | j)                  d      r-dj%                  |d |dz
   |||dz
  d        | j                  |<   y dj%                  |d | |||d        | j                  |<   y # t        t
        j                  f$ r Y y w xY w# t        t
        j                  f$ r Y w xY w)Nr   F   r   r/  Tr8   r   r   {}{}z{} {} {}r  z{} {}{})r  r   r*  rL  rL   rF   r   _is_binary_operatorr~   r|   r+  r   r   rr   rq   r{   rx   rd  rY   find_newlinero   )r   r  r  r  r-  one_string_tokenrR  comment_indexfound_not_comment_only_linecomment_only_linenumr   
from_indexto_index
strip_linenewline_countnewline_indexrd  tttsoldfix_target_linenlbefore_linebls                           r@   fix_w503zFixPEP8.fix_w503  s   ";F<@KK#IQ!<<>!,	 !12B #2a58-=>&+# qAQ!##a!+JA~T[[!1Z!?!A~HZ0779J/z!}3$)$*.'$RWWT[[H-M%NO MM%bMqQ4H,,hkk::!((/!Q&M * q r*+,CQ4H,,hkk::!Q&M A%##qt+A(++8M$'F1IM  Q R LL)*$q.+??"(--2AJq3'7#889:AAC#EJ$++ojABkk/2r"+5+<+<N]+-=MA-./,1DKK( 3B((- 09/?/?A&(8+bde:L0NO, 09/?/?$&6BC8H0JO,G X001 		4  !4!45 s#   K1 'L1LLL-,L-c                 &   t        || j                        \  }}}d}d }t        dd      D ]A  }||z   }	 t        dj	                  | j                  ||             }	d}
g }t        |	      D ]  \  }}t        |d   |d         r)|d   d   dk(  s$|d   d   dk(  s0|d   d   |d   d   f}A|d   t        j                  k(  r0|d   dv r)|d   d   dk(  sj|d   d   dk(  sv|d   d   |d   d   f}|d   t        j                  t        j                  fv s|j                  |       |
dz  }
 |
dkD  r	|	d |d    }n|	}g }|D ]&  }t        j                  |d   k(  r|r
|d   \  }} n|}(  n |sy ||d   |d    }|r<dk(  r7d	j                  |d |d    j!                         ||d        | j                  |<   nNd
j                  |d |d    j!                         ||d   d  j#                         ||d   d        | j                  |<   | j                  |dz      }d}t%        j&                  d|      }|r|j)                         d   }dj                  |d | |||d        | j                  |dz   <   y # t
        t        j                  f$ r Y dw xY w)Nr   r      r8   r   r   andorr  r  r  z\s*r  )r  r   r~   rL  r   rL   rF   r   r   r  NAMErr   rq   r{   rx   rY   r   r+  r  rX   span)r   r  r  r  r-  r  operator_positionr   r  rR  r  r  rd  r  r  r  comment_rowtarget_operatorr  next_line_indentms                        r@   fix_w504zFixPEP8.fix_w504e  s
   ";F<@KK#IQ  q!A!A~H$RWWT[[H-M%NO MM%bMq&qtQqT2tAw!|!Q1-.qT!Wad1g,>)qTX]]*qt}/DtAw!|!Q1-.qT!Wad1g,>)qTh..<<!((/!Q&M * q +-+,C##qt+14Q.K	 
 9 : ! !21!56G6JK[A-&,mm,)!,-446}~&'(DKK
# '/oo,)!,-446(+,-446(+,-'/DKK
#
 KK
Q/	HHVY' vvx{&/&6&6''(/&'('*JN#Y  !4!45 s   'I33JJc                     t        || j                        \  }}}dj                  |d |dz    ||dz   d        | j                  |<   y )Nz{}\{}r   )r  r   rY   )r   r  r  rA  r-  s        r@   fix_w605zFixPEP8.fix_w605  sO    '@AE(N$VV"*//;FQJ
!4#6JrB   NN)0__name__
__module____qualname____doc__r   r  r  r   r   r5  r7  r9  r=  r   r   r   r   rW  r\  r   rh  rl  r   r   rs  ry  r{  r~  r  r  r   r   r  r  r  r   r  r  r  r  r  r  r  r   r  r  r  r
  rc  rB   r@   r   r     s    %R (,=&~A+F"$H
L	<888$G(
(0#J(06
00$0,O
	
&#B(0"!F.>D
4. `	0F,$7L5@5>&	'<
-JJX8*t6rB   r   c                    d }d fd}d}d}d}t        t        |             }|D ]_  \  }|snt        j                  j	                               }	|	Id}|	j                  d	      }|	j                         |	j                   j                         }
|
d
d |k(  rd}w|rj                         d
d |k(  rd}j                         sj                  d      rj                  d      r
||k(  r|c S j                  d      r8||k(  r |t        j                  |fg|            \  }}|r
||z   dz   c S |c S t        j                  j                        r|c S t        fd|D              rR |      r|c S |c S  y)zreturn import or from keyword position

    example:
      > 0: import sys
        1: import os
        2:
        3: def function():
    c                 b    | d   dv r| dd  } | r| d   dv r| dd  } | xr | d   dk(  xs | d   dk(  S )Nr   uUbBr   rR"'rc  r   s    r@   is_string_literalz<get_module_imports_on_top_of_file.<locals>.is_string_literal  sQ    7f8DDGtO8D:aC947c>:rB   c                     t        j                  |       }|j                  D ].  }t        |t         j                        s|j
                  dk(  s. y y)N
__future__TF)astparsebody
isinstance
ImportFrommodule)r   nodesr  s      r@   is_future_importz;get_module_imports_on_top_of_file.<locals>.is_future_import  s>    		$A!S^^,\1I  rB   c                     d}d}| D ]A  \  }}|j                         j                  d      D ]  }||z   }	  |      |fc c S  |dz  }C d|fS # t        $ r Y .w xY w)Nr   r8   Tr   F)r  
splitlinesrL   )r   rA  r   r  r  	line_partr!  s         r@   has_future_importz<get_module_imports_on_top_of_file.<locals>.has_future_import  s    "LAy&__.99$?	i'+D1699 @ aKF # f} # s   	A	AA)tryr"   elsefinallyF"""NTkindr  r/  r   r   r   c              3   @   K   | ]  }j                  |        y wNrV   )r  kwr   s     r@   r   z4get_module_imports_on_top_of_file.<locals>.<genexpr>  s     D/C$/C   r   )iterr   DOCSTRING_START_REGEXrX   r+  r  r   endposr   rV   	itertoolschainr3   DUNDER_REGEXany)r   import_line_indexr  r%  allowed_try_keywordsin_docstringdocstring_kindsource_streamrx  r  remainhitrA  r!  r   s                @@r@   r  r    s   ; @LN6*+M"	T%++DKKM:A}#!"aeegqxx0779"#;.0#(L{{}RS!^3${{}__S!??9%''J__W%''+#t}=KC V|a''J%%++D1JD/CDDt$JJS #T rB   c                 .    | d   dz
  }|| d   dz
  ||   fS )z2Return (line_index, column_offset, line_contents).r   r   r   rc  )r  r   r  s      r@   r  r    s2    !#J8q :   rB   c           
      $   t        |       }| t        |      d }	|	j                         |	k(  sJ | j                         j                  d      rJ t	        t        |	            }
t        |
|	|||      }t        t        t        |      j                  | |g            fd      }|dk\  r<t        dj                  dg|z   dgz         t        t        j                  d	      
       |r|d   }t        |      t        |      kD  ry|S y)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                 "    t        |       S )N)r   )line_shortening_rank)xr   r   r  s    r@   <lambda>z%get_fixed_long_line.<locals>.<lambda>  s    *%	'rB   r   r   zP-------------------------------------------------------------------------------
r8   zutf-8r   r   )r2  r|   r+  rV   r   rL  shorten_liner   r   unionr  r   wrap_outputr  r  longest_line_length)r-  r  r  r   r  r   r   r  r   r   r   
candidatesbest_candidates      `` `      r@   r  r    s    f%FCKL!F==?f$$$}}))#... /&)*F!#%J s:$$fh%789'(J !|$$bTJ%6"%=>szz73	5 #A ~.1DX1NN rB   c                 `    t        |       dk(  ryt        d | j                         D              S )zReturn length of longest line.r   c              3   2   K   | ]  }t        |        y wr,  r|   r  r   s     r@   r   z&longest_line_length.<locals>.<genexpr>7  s     7%6Ts4y%6   )r|   rv  r#  r_  s    r@   rG  rG  3  s(    
4yA~7T__%6777rB   c                 h    t        |       }|t        t        | j                                     z   dz   S )z/Return single line based on logical line input.r   )r2  untokenize_without_newlinesrL  r+  )rZ   rk  s     r@   r  r  :  s:    "<0K4++-.0 0267 7rB   c                     d}d}d}| D ]L  }|d   }|d   \  }}|d   \  }}	||kD  rd}||kD  s|dk(  r|j                  d      s|dz  }|dk7  r||z  }|}|	}N |j                         S )	z#Return source code based on tokens.r8   r   ra   r   r   r   r   r(  )ro   r   )
r   r   last_rowlast_columnr  token_string	start_rowstart_columnend_row
end_columns
             r@   rQ  rQ  B  s    DHKt$%aD!L !!*xKK'<4+?c"CKD4L D # & ;;=rB   c                 H   g }g }d}d}t        dj                  |             D ]  }|d   t        j                  t        j                  t        j
                  t        j                  t        j                  fv rV|sJ|d   t        j                  t        j                  fv r%d}|j                  |d   d   dz
  |d   d   f       |r&|s$|j                  |d   d   dz
  |d   d   f       d}|d   t        j                  k(  s|d   dv r|dz  }|d   d	v s|dz  } ||fS )
NTr   r8   r   r   r   Frn   }]))rL  r   rF   rx   DEDENTINDENTrq   	ENDMARKERrr   SEMIr{   rt   )source_linesr  r	  last_newliner   r  s         r@   r   r   ^  s*   MKLFRWW\23Q4H$$hooOOX[[&&( ( !A$8#3#3X]]"CCL!Q!QqT!W56  !A$q'A+qtAw!78 LQ48;;tu}!1!! 4" ;''rB   c                     |d   dz
  }|d   dz
  }d}d}t        dt        |      d      D ]-  }|sJ ||   }	|	d   |kD  s|	d   |k(  s|	d   |kD  s&|	}||   } n |y| |d   |d   dz    }
|||
fS )zdReturn the logical line corresponding to the result.

    Assumes input is already E702-clean.

    r   r   r   Nr   )r~   r|   )r`  r  r  r	  r   collsler   rB  r  s              r@   r   r   x  s     .1
C

Q
C	B	B1c-(!,{NQ4#:!A$#+!A$*Bq!B - 
zBqE"Q%!),Hr8rB   c                 >    d|cxk  rt        |       k  r| |   S  |S |S Nr   rL  )itemsrd  r  s      r@   r  r    s-    ECJU|  N7NrB   c                 <    t        | |      }|j                  |      S )zReindent all lines.)
Reindenterrun)r   indent_size
leave_tabs
reindenters       r@   reindentro    s    FJ/J>>+&&rB   c                    t        |       }t        |      }t        |      t        |      k7  ryt        |      D ]L  \  }}dj                  ||   j	                               dj                  ||   j	                               k7  sL y y)zZReturn True if code is similar.

    Ignore whitespace when comparing specific line.

    Fr8   T)split_and_strip_non_empty_linesr|   r   r   r*  )ark   split_asplit_brd  r  s         r@   r  r    sz     .a0G-a0G
7|s7|#(
7775>'')*bgggen6J6J6L.MM ) rB   c                     | j                         D cg c]#  }|j                         s|j                         % c}S c c}w )z=Return lines split by newline.

    Ignore empty lines.

    )r#  r  )r   r   s     r@   rq  rq    s3     &*__%6G%6T$**,DJJL%6GGGs   >>c                    | xr | j                  d      | k(  }|r| dz   }n| }ddlm} 	 t        |||      }|r
||v r|| vr| S |r|j                  d      S |S # |j                  j
                  t        t        t        f$ r | cY S w xY w)zjReturn refactored code using lib2to3.

    Skip if ignore string is produced in the refactored code.

    r   r   r   )pgen2)fixer_namesr?   )	r   lib2to3rw  refactor_with_2to3r  
ParseErrorrL   rM   UnicodeEncodeError)r   rx  r  r?   not_found_end_of_file_newlineinput_sourcerw  new_texts           r@   refactorr    s     %+$Nv}}V/D/N!$}%l2=/79 X&"6M$v&&O KK""   	s   A +BBc                    t               }t        j                         D ]]  \  }}t        || |      s|r9t	        dj                  ||j                               t        j                         |t        |      z  }_ |S )Nr  r  --->  Applying {} fix for {}r   )	r   CODE_TO_2TO3rh  
code_matchr  rY   upperr  r  )r  r  wherer  fixesr_  r  s          r@   code_to_2to3r    so    EE!'')	cd6&94;;E<@JJLJ::' SXE * LrB   c           	      X    |s| S |xs g }|xs g }t        | t        ||||      |      S )z*Fix various deprecated code (via lib2to3).)r  r  r  r  r?   )r  r  )r   r   r  r  r?   r  r  s          r@   fix_2to3r    sE     \rF\rFF (.',)02 &' 'rB   c                    t        | t              rJ t        j                  t              }| D ]w  }|j                  t              r|t        xx   dz  cc<   *|j                  t              r|t        xx   dz  cc<   Q|j                  t              sg|t        xx   dz  cc<   y t        ||j                  d      xs t        gd   S )zKReturn type of newline used in source.

    Input is a list of lines.

    r   T)r   reverser   )r  rw   collectionsdefaultdictr)  ro   CRLFCRLFr   rv   )r   counterr   s      r@   r  r    s     &#&&&%%c*G==DMQM]]2BK1K]]2BK1K  7T:BrdAFFrB   c                     d}	 t        |       D ]   }|d   t        j                  k(  s|d   } |S  	 |S # t        t        j
                  f$ r Y |S w xY w)zReturn indentation type.    r   r   )rL  tokenr]  rL   rF   r   )r   r   r  s      r@   r   r     sl    K (Atu||#d  )  ,,- s   $5 5 5 AAc                 x    | j                         r*t        |       t        | j                               z
  }| d| S y)zReturn leading whitespace.Nr8   )r  r|   r+  )r   non_whitespace_indexs     r@   r2  r2    s7    zz|"4y3t{{}+==))**rB   c                     d}t        j                  | |d|z   d|z   |      }d}|D ]'  }||z  }|s|j                  |      r||dz   |z   z  }) |S )z0Return text of unified diff between old and new.r   z	original/zfixed/)linetermr8   z\ No newline at end of file)difflibunified_diffro   )r  newr?   r;   diffr   r   s          r@   get_diff_textr  !  su    GSh8	D D g.G<<wFFD  KrB   c                     g d}d}dg}| d   j                         }	 |j                  |      S # t        $ r. 	 ||j                  |      z   dz   cY S # t        $ r |cY cY S w xY ww xY w)z~Key for sorting PEP8 results.

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

    )e701e702e225e231e201e262i'  e501r   r   )r   rd  
ValueError)pep8_resultprioritymiddle_indexlowest_priorityr   s        r@   r   r   5  s    H L 	O
 d

!
!
#C ~~c""  	 /"7"7"<<q@@ 	 	  s,   / 	A&AA&A"A&!A""A&c           	   #      K   t        | |||||      D ]  }|  |r)t        D ]   }	t        | ||||	|      }
|
|
|k7  s|
 " |rt        | |||      D ]  }
|
  yyw)zJSeparate line at OPERATOR.

    Multiple candidates will be yielded.

    )r   r   rk  r   r   r  )r   r   rk  r   key_token_stringsr   N)r   r   rk  r  )_shorten_lineSHORTEN_OPERATOR_GROUPS_shorten_line_at_tokens_shorten_line_at_tokens_new)r   r   rk  r   r  r   r   r  	candidater  	shorteneds              r@   rD  rD  X  s      #&*0/:/:.81>@	 @ !8/''"3%'I $f)< "9 4' /	1I O1 s   9A$A$"A$c              #   l  K   t        |       D ]!  \  }}}}	|t        j                  k(  rt        |      sxt        |      sm||dz   d j	                         j                         j                  d      s:|d| }
||d }||j	                         z   dz   |z   |
j	                         z   dz    |t        j                  k(  s|dk7  s|t        j                  k7  sJ |d|	 }
|}|
j                         j                  d      r#||	d j                         j                  d      rnI|
j                         j                  d      r||z  }n$d|
v r|dd|
j                  d      z   z  z  }n||z  }|||	d j                         z   }|j	                         r|j                         j                  d	      r|j                         j                  d
      r|
j                         j                  d      r|dv r|
dz   dz   |z   }n|
dz   |z   }t        |rt        |      n|      s||z    $ yw)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)r   zpragma:zpylint:r   =r   ru  r(  r/  r   .z+-*/z \)token_offsetsrF   rx   is_probably_part_of_multiliner  r   rV   r  rt   r]  r   ro   r+  r   rM  normalize_multiline)r   r   rk  r   r   r  r   rU  start_offset
end_offsetr  r  second_indentrH  s                 r@   r  r  ~  s6     &f-				 (***-m<-f5|a'()//1779DD.0 =L)ELM*F/$6!&/156 7588#(; ---;J'E'M'',:;'..0;;C@((-,EJJsO(;!<<,#fZ[&9&@&@&BBFLLN**3/ }}))#.||~&&s+v%,v5v- ) 06/46!E))k .s   B>H4H4E H4)H4c                 @    | t         j                  k(  xs |dv xr |dvS )Nr  z()[]{},:.;@=%~)rF   rt   )r   r   s     r@   r  r    s+    8;;&?$-*? )((*rB   Token)r   rU  sposeposr   c                       e Zd ZdZ G d de      Z G d de      Z G d de      Zd Zd	 Z	d
 Z
d Zd Zd Zd ZddZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)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                   ,    e Zd ZdZd Zd Zed        Zy)ReformattedLines._Indentz,Represent an indentation in the atom stream.c                     || _         y r,  _indent_amtr   
indent_amts     r@   r   z!ReformattedLines._Indent.__init__  s
    )DrB   c                      d| j                   z  S Nr(  r  r   s    r@   emitzReformattedLines._Indent.emit  s    ))))rB   c                     | j                   S r,  r  r  s    r@   sizezReformattedLines._Indent.size  s    ###rB   N)r  r  r  r  r   r  propertyr  rc  rB   r@   _Indentr    s#    :	*	* 
	$ 
	$rB   r  c                   &    e Zd ZdZd Zed        Zy)ReformattedLines._Spacez%Represent a space in the atom stream.c                      yr  rc  r  s    r@   r  zReformattedLines._Space.emit  s    rB   c                      yNr   rc  r  s    r@   r  zReformattedLines._Space.size      rB   Nr  r  r  r  r  r  r  rc  rB   r@   _Spacer    s    3	 
	 
	rB   r  c                   &    e Zd ZdZd Zed        Zy)ReformattedLines._LineBreakz*Represent a line break in the atom stream.c                      y)Nr   rc  r  s    r@   r  z ReformattedLines._LineBreak.emit  s    rB   c                      yrg  rc  r  s    r@   r  z ReformattedLines._LineBreak.size  r  rB   Nr  rc  rB   r@   
_LineBreakr    s    8	 
	 
	rB   r  c                 J    || _         g | _        d| _        d | _        d | _        y rg  )_max_line_length_lines_bracket_depth
_prev_item_prev_prev_item)r   r  s     r@   r   zReformattedLines.__init__  s(     /#rB   c                 "    | j                         S r,  )r  r  s    r@   __repr__zReformattedLines.__repr__  s    yy{rB   c                 p    t        |t              r| j                  ||       y | j                  |||       y r,  )r  Atom	_add_item_add_container)r   objr  break_after_open_brackets       r@   r  zReformattedLines.add
  s0    c4 NN3
+C-EFrB   c                    d}t        | j                        dkD  rht        | j                  d   | j                        r|dz  }t        | j                        dkD  r(t        | j                  d   | j                        r|dz  }|dkD  r4| j                  j	                  | j                                |dz  }|dkD  r4| j                  j	                  |       y )Nr   r   ra   r   )r|   r  r  r  r{   )r   item
num_spacess      r@   add_commentzReformattedLines.add_comment  s    
t{{a$++b/4;;7a
4;;!#dkk"ot{{;!OJ1nKKt{{}-!OJ 1n 	4 rB   c                 X    | j                   j                  | j                  |             y r,  )r  r{   r  r  s     r@   
add_indentzReformattedLines.add_indent  s    4<<
34rB   c                     | j                   j                  | j                                | j                  t	        |             y r,  )r  r{   r  r  r|   )r   r   s     r@   add_line_breakzReformattedLines.add_line_break"  s+    4??,-F$rB   c                     | j                   j                  || j                                | j                   j                  |dz   | j                  |             y r  )r  insertr  r  )r   rd  r  s      r@   add_line_break_atz"ReformattedLines.add_line_break_at&  s>    5$//"34519dll:&>?rB   c                 N   | j                   r:t        | j                   d   | j                  | j                  | j                  f      ry t        | j                        }| j                  rt        | j                        nd}| j                  j                  sB| j                  j                  s,| j                  j                  s| j                  j                  r|d   dvs|d   dk(  r|s|dk7  r|d   dk7  r|dk7  r|d   d	k7  r|d   d
v r|d   dvsd|d   dv s]|r|dk(  sV| j                  rw|dvrr| j                  j                  s,| j                  j                  s| j                  j                  r/|dv r*| j                   j                  | j	                                y y y y y y y y y )Nra   r8   r   z	([{.,:}])r  fromr  r  rb   r[  z.,}])z:,z+-)r   r   r   r   r   r   z**r  )r  r  r  r  r  rw   r  r  
is_keyword	is_stringis_name	is_numberr{   )r   	curr_textequal	prev_textprev_prev_texts        r@   add_space_if_neededz$ReformattedLines.add_space_if_needed*  s   zB$//4<<!M O (	)-)=)=C$$%2 	 oo((DOO,E,Eoo%%)B)Bl+-|s"u &9R=C+?8# q\S  }%)A,g*E mt# c) $$$&&&..&&00&&00HHKKt{{}- I 1 ' % ! $ ,@&rB   c                     | j                   S )z(Return the previous non-whitespace item.)r  r  s    r@   previous_itemzReformattedLines.previous_itemX  s    rB   c                 B    | j                         |z   | j                  k  S r,  )current_sizer  )r   item_extents     r@   fits_on_current_linez%ReformattedLines.fits_on_current_line\  s!      "[0D4I4IIIrB   c                     d}t        | j                        D ]*  }||j                  z  }t        || j                        s) |S  |S )z3The size of the current line minus the indentation.r   )ru   r  r  r  r  )r   r  r  s      r@   r	  zReformattedLines.current_size_  sE    T[[)DDIID$0 *
 rB   c                 |    | j                   xr/ t        | j                   d   | j                  | j                  f      S )Nra   )r  r  r  r  r  s    r@   
line_emptyzReformattedLines.line_emptyi  s5     <4;;r? OOT\\:<	=rB   c                     d}| j                   D ];  }t        || j                        r|j                         }||j	                         z  }= |j                         dz   S )Nr8   r   )r  r  r  r   r  )r   r  r  s      r@   r  zReformattedLines.emitn  sQ    KKD$0diik!F  
 }}%%rB   c                    | j                   ru| j                   j                  r_|j                  rS| j                  j                  | j	                                | j                  j                  | j                  |             t        |      }| j                  r5| j                  r)| j                  ||       |dv r| j                  ||       n| j                  r| j                         s| j                  t        |            r| j                  |       nS| j                  j                  | j	                                | j                  j                  | j                  |             | j                  j                  |       || j                   c| _         | _        |dv r| xj                  dz  c_        y|dv r'| xj                  dz  c_        | j                  dk\  sJ yy)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.,)]}rn   r   r[  r   N)r  r  r  r{   r  r  rw   r  &_prevent_default_initializer_splitting_split_after_delimiterr  r  r|   _enforce_spacer  )r   r  r  	item_texts       r@   r  zReformattedLines._add_itemz  si    ??t88T^^KKt01KKt||J78I	;;4..77jIG#++D*=[[!2((Y8##D) ""4??#45""4<<
#;<4 04doo--1$%1$&&!+++  rB   c                 x   |dz   }t        | j                        dk7  r| j                         s| j                  |j                  | j
                  z   dz         st        |      d   dk(  r| j                  j                  rd}|dz   }n|st        | j                        dvrk| j                  j                  | j                                | j                  j                  | j                  |             d	}n| j                         dz   }d	}t        |t        t        f      r|}|j                  | d
|z  |       y )Nr   r  r   r   r   Tr   rn   Fr(  )r  )rw   r  r  r  r  r  r  r  r{   r  r  r	  r  ListComprehensionIfExpressionreflow)r   	containerr  r  actual_indents        r@   r  zReformattedLines._add_container  s   "Q  C'!))!4!44q8: 9~a C'DOO,C,C+/( *Q(DOO$E1
 ""4??#45""4<<
#;<+0( --/!3M',$i"3\!BC&M 	s]22J 	 	LrB   c                 n   t        |      dk(  r| j                          y| j                  r$| j                  rt        | j                        dk7  ry| j                          | j                  j                  | j                        }t        | j                  |dz
     | j                        s| j                  |j                  dz         ryt        | j                  |dz
     | j                        r| j                  |dz
  = | j                  | j                  j                  | j                        |       y)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   )rw   _delete_whitespacer  r  r  rd  r  r  r  r  r  r  )r   r  r  prev_prev_indexs       r@   r  z7ReformattedLines._prevent_default_initializer_splitting  s     t9 ##%t';';DOO$+!++++D,@,@A t{{?Q#67F%%dii!m4  dkk/A"56DOa/0t{{001E1EF)	+rB   c                    | j                          | j                  |j                        ryd}t        | j                        D ]`  }|rt        |t              r|j                  s n@d}t        || j                        r|}t        || j                  | j                  f      s` y |sy| j                  | j                  j                  |      |       y)z&Split the line only after a delimiter.N)r  r  r  ru   r  r  r  is_colonr  r  r  r  rd  )r   r  r  
last_spacecurrent_items        r@   r  z'ReformattedLines._split_after_delimiter  s    !$$TYY/
$T[[1Ld3!**!
,4)
,$,,(GH 2 t{{00<jIrB   c                 f   t        | j                  d   | j                  | j                  | j                  f      ry| j
                  syt        |      }t        | j
                        }|dk(  r|dk(  s|dk(  r|dk(  s
|dk(  r0|dk(  r*| j                  j                  | j                                yyy)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.

        ra   Nr  r  r  r   )r  r  r  r  r  r  rw   r{   )r   r  r  r  s       r@   r  zReformattedLines._enforce_space		  s     dkk"o{{DOOT\\BDI	(	
 #)v"5("yC'7#)x"7KKt{{}- #8rB   c                 
   t        | j                  d   | j                  | j                  | j                  f      rI| j                  d= t        | j                  d   | j                  | j                  | j                  f      rHyy)z/Delete all whitespace from the end of the line.ra   N)r  r  r  r  r  r  s    r@   r  z#ReformattedLines._delete_whitespace#	  sc    R4;;+/<<+9 :B R4;;+/<<+9 :rB   N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  rc  rB   r@   r  r    s    $& $	 		V 	$G!5%@,.\J=
&',R"LH%+NJ6.4 rB   r  c                       e Zd ZdZd Zd Zd Z	 	 	 d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y)r  z3The smallest unbreakable unit that can be reflowed.c                     || _         y r,  )_atom)r   atoms     r@   r   zAtom.__init__.	  s	    
rB   c                 .    | j                   j                  S r,  r(  rU  r  s    r@   r  zAtom.__repr__1	  s    zz&&&rB   c                     | j                   S r,  )r  r  s    r@   __len__zAtom.__len__4	  s    yyrB   c                 V   | j                   j                  t        j                  k(  r|j	                  |        y |r|n| j
                  }| j                   j                  dvr|dz  }|j                         }|s{|j                  |      sj|r|j                  | j
                  dz         sJ|j                         s:| j                  s.|r|j                  rt        |       dk(  s|j                  |       n|j                  t        |              |j                  | t!        |      |       y )Nz,:([{}])r   r   )r(  r   rF   rx   r  r  rU  r  r  r  r  r  rw   r  r  r  r|   )	r   reflowed_linescontinued_indentextentr  is_list_comp_or_if_exprnext_is_dot
total_size	prev_items	            r@   r  zAtom.reflow7	  s     ::  H$4$44&&t,%V499
::""*4!OJ"002	'33J?44TYY]C))+y00Tc! ))*:;..s4y94%5!63	5rB   c                 "    | j                         S r,  )r  r  s    r@   r  z	Atom.emit[	  s    }}rB   c                 T    t        j                  | j                  j                        S r,  )keyword	iskeywordr(  rU  r  s    r@   r  zAtom.is_keyword^	  s      !8!899rB   c                 P    | j                   j                  t        j                  k(  S r,  )r(  r   rF   ry   r  s    r@   r  zAtom.is_stringb	      zz$$77rB   c                 P    | j                   j                  t        j                  k(  S r,  )r(  r   rF   r  r  s    r@   r  zAtom.is_namef	  s    zz$$55rB   c                 P    | j                   j                  t        j                  k(  S r,  )r(  r   rF   NUMBERr  s    r@   r   zAtom.is_numberj	  r;  rB   c                 4    | j                   j                  dk(  S )Nr   r+  r  s    r@   is_commazAtom.is_comman	      zz&&#--rB   c                 4    | j                   j                  dk(  S )Nrb   r+  r  s    r@   r  zAtom.is_colonr	  rA  rB   c                 @    t        | j                  j                        S r,  )r|   r(  rU  r  s    r@   r  z	Atom.sizev	  s    4::**++rB   N)FFF)r  r  r  r  r   r  r-  r  r  r  r  r  r  r   r@  r  r  rc  rB   r@   r  r  *	  s    ='
 "' %	"5H : : 8 8 6 6 8 8 . . . . , ,rB   r  c                       e Zd ZdZd Zd Zd Zd Z	 d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        Zy)	Containerz#Base class for all container types.c                     || _         y r,  _items)r   rh  s     r@   r   zContainer.__init__	  s	    rB   c                 *   d}d}| j                   D ]  }|j                  r|dz  }n_|j                  r|dz  }nMt        |      }|r;|s4|j	                  t        d            s|j                  t        d            s|dz  }||z  }|j                  } |S )Nr8   F, z: z
([{,.:}]) z	([{,.:}])r(  )rH  r@  r  rw   ro   tuplerV   r  )r   r  last_was_keywordr  item_strings        r@   r  zContainer.__repr__	  s     KKD}}$$!$i% //%*=>%00{1CDcMF+%#!  " rB   c              #   6   K   | j                   D ]  }|  y wr,  rG  )r   elements     r@   __iter__zContainer.__iter__	  s     {{GM #s   c                      | j                   |   S r,  rG  )r   r   s     r@   __getitem__zContainer.__getitem__	  s    {{3rB   c           
      >   d}t        | j                        D ]  \  }}t        | j                  |dz         }t        |t              rmt        | t
        t        f      }|j                  ||| j                  |      ||xr t        |      dk(         |r|j                  r|j                  |       d}n3|j                  |t        |      |       t        |t
        t        f       }|rt|dk(  rot        |      | j                  k(  rW|rt        |      | j                  k7  r=t        | j                        dk7  st        |t              s|j                  |       d}Ft        | j                  |dz         }	t        |      dvsn|srt        |t               rt        |      d	k7  s|	st        |	t              rt        |      d
k(  s|j#                         r|j%                  | j                  |dz         dz         r|j                  |        y )NFr   r  )r2  r3  r   r   r   )r  r   r  rb   not)r   rH  r  r  r  r  r  r  _get_extentrw   r@  r  r  r|   open_bracketr   rE  r  r  )
r   r/  r0  r  last_was_containerrd  r  	next_itemr2  next_next_items
             r@   r  zContainer.reflow	  s   "&t{{3MUD eai8I$%t&7%FG (N,< ,,U34K)2 *?),Y3)>	  A
 &$--"112BC%*"""4-=)>#;=)3D;L;G;I *J &J" )UaZ D	T...#i.D4F4F"FT[[!Q&jD.I--.>?+0(!)$++uqy!AI%55jI&F	Nc)"J~t,L(+I%(?&113&;;((3a79 #112BCU 4rB   c                 ,   d}t        | j                  |dz
        }|xr t        |      dk(  }|t        | j                        k  rt        | j                  |      }|dz  }t	        |t
        t        f      r	 |S t	        |t              r(|rN|j                  rB|r|dz  }n||j                  z  }|}t        |      dvr|j                  s|j                  s	 |S t        |      dk(  rd}||j                  z  }|}|t        | j                        k  r|S )zbThe extent of the full element.

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

        r   r   r  )r  r  rb   rT  T)r  rH  rw   r|   r  r  r  rE  r  r  r  )r   rd  r1  r5  seen_dotr  s         r@   rU  zContainer._get_extent	  s    T[[%!)4	6Y3!6c$++&&DKK/DQJE$!2L AB* ' $	*!2!2!$))+ $Id)#99ll4>>  4yCdiiFI1 c$++&&4 rB   c                      yNFrc  r  s    r@   r  zContainer.is_string	      rB   c                 4    t        | j                               S r,  )r|   r  r  s    r@   r  zContainer.size	  s    4==?##rB   c                      yr]  rc  r  s    r@   r  zContainer.is_keyword	  r^  rB   c                      yr]  rc  r  s    r@   r  zContainer.is_name 
  r^  rB   c                      yr]  rc  r  s    r@   r@  zContainer.is_comma
  r^  rB   c                      yr]  rc  r  s    r@   r  zContainer.is_colon
  r^  rB   c                      y r,  rc  r  s    r@   rV  zContainer.open_bracket
      rB   c                      y r,  rc  r  s    r@   r   zContainer.close_bracket
  re  rB   Nr$  )r  r  r  r  r   r  rP  rR  r  rU  r  r  r  r  r  r@  r  rV  r   rc  rB   r@   rE  rE  {	  s    -.  ).-D^#J   $ $            rB   rE  c                   0    e Zd ZdZed        Zed        Zy)Tuplez'A high-level representation of a tuple.c                      y)Nr   rc  r  s    r@   rV  zTuple.open_bracket
      rB   c                      y)Nru  rc  r  s    r@   r   zTuple.close_bracket
  rj  rB   Nr  r  r  r  r  rV  r   rc  rB   r@   rh  rh  
  s+    1   rB   rh  c                   0    e Zd ZdZed        Zed        Zy)Listz&A high-level representation of a list.c                      y)Nr   rc  r  s    r@   rV  zList.open_bracket&
  rj  rB   c                      y)N]rc  r  s    r@   r   zList.close_bracket*
  rj  rB   Nrl  rc  rB   r@   rn  rn  "
  s+    0   rB   rn  c                   0    e Zd ZdZed        Zed        Zy)	DictOrSetz3A high-level representation of a dictionary or set.c                      y)Nr   rc  r  s    r@   rV  zDictOrSet.open_bracket3
  rj  rB   c                      y)N}rc  r  s    r@   r   zDictOrSet.close_bracket7
  rj  rB   Nrl  rc  rB   r@   rs  rs  /
  s+    =   rB   rs  c                        e Zd ZdZed        Zy)r  z4A high-level representation of a list comprehension.c                 p    d}| j                   D ]$  }t        |t              r |S ||j                  z  }& |S rg  )rH  r  r  r  )r   lengthr  s      r@   r  zListComprehension.size@
  s>    KKD$- diiF   rB   N)r  r  r  r  r  r  rc  rB   r@   r  r  <
  s    > rB   r  c                       e Zd ZdZy)r  z0A high-level representation of an if-expression.N)r  r  r  r  rc  rB   r@   r  r  J
  s    :rB   r  c                 *   t        t        | |          g}|dz  }t        |       }||k  rht        | |    }|j                  dv r|dk(  rt	        |      |dz
  fS |dk(  rt        |      |dz
  fS |j                  t        |             |j                  dk(  rt        |      |fS |j                  dk(  rt        |      |fS |j                  dk(  rt        |      |fS |j                  dv r!t        | |      \  }}|j                  |       n||j                  dk(  r"t        | |d      \  }}|j                  |       nK|j                  dk(  r"t        | |d      \  }}|j                  |       n|j                  t        |             |dz  }||k  rhy	)
z9Parse a high-level container, such as a list, tuple, etc.r   z,)]}forrm   ru  rq  rv  rn   r  )r  r  r|   rU  r  r  r{   rh  rn  rs  _parse_container)r   rd  	for_or_ifrh  
num_tokenstokr  s          r@   r}  r}  O
  s    %'()E	QJEVJ
*
VE]#v%
 E!)%0%!)<<d"$U+UQY77 LLc# 3&ee,,!!S(UU++!!S(!%(%00&!1&%!@YLL#&!1&%!GYLL#%!1&%!FYLL# LLc#
] *
` rB   c                 p   d}g }t        |       }||k  rt        | |    }|j                  t        j                  k7  sJ |j                  t
        j                  k(  r	 |S |j                  dv r$t        | |      \  }}|sy|j                  |       n|j                  t        |             |dz  }||k  r|S )zoParse the tokens.

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

    r   rn   Nr   )r|   r  r   r  r]  rF   rr   rU  r}  r{   r  )r   rd  parsed_tokensr  r  r  s         r@   _parse_tokensr  
  s     EMVJ
*
VE]#~~--->>X---  u$!1&%!@Y  +  c+
! *
$ rB   c                 X   t        | d         dk(  r|dt        z  z   }n|dt        z  z   }| }t        |      }|j                  t	        |j                  d                   |sTt        | d      }t        | d      }|r:|r8t        |      d   dk(  r't	        |      t	        |      z   dz   t	        |      k(  ry| D ]_  }	|j                  t        |	      d	
       |}
|r(t        |	t              rd}d|j                         dz   z  }|	j                  |||       |
}a |j                         S )z'Reflow the lines so that it looks nice.r   defz  r(  r   r   r   NT)r  F)rw   rp   r  r  r|   r+  r  r  r  rE  r	  r  r  )r  rk  r  start_on_prefix_liner0  r  linesfirst_tokensecond_tokenr  save_continued_indents              r@   _reflow_linesr  
  s<    =%&3F)FF&/B)BB#77_-E	S++F345 }a0q1 La C's;//!3s;K7LL!!#d)4!8 0JtY$?#( "e&8&8&:Q&>?E+-EF0  ::<rB   c              #     K   ||z    t        |       }|rqt        |||d      }|r&t        t        |j	                                     r| t        |||d      }|r(t        t        |j	                                     r| yyyyw)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)r  FN)r  r  rM  r  r+  )r   r   rk  r  r  rH  s         r@   r  r  
  s      
!&)M m[/379\"5elln"EFKm[/38:\"5elln"EFK G5 s   BB	c                    g }t        t        |             D ]  \  }}|\  }	}
}}|	t        j                  k7  sJ |
|v rddddj	                  |
      }|r3t        | |dz   ddg      d   |k(  st        | |dz   ddg      d   |k(  rp|dkD  r|
d	k(  r| |dz
     d   d
v r|t        |      dz
  k  s|j                  |       t        | |dz
        }|	t        j                  k(  s|s|d   t        j                  k(  s|j                  |        d}d}t        ||      D ]6  }|r*|d|z   |z   z  }dD ]  }|j                  |      s||z  } /|}|rJ |}8 |J t        |dkD  rt        |      n|      r||z   S y)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.

    ru  rq  rv  r   r   r   r   Nr  r   r   z,(%[r   r   rn   )r   r  r  r]  rv   r  r|   r{   rF   ry   split_at_offsetsro   rM  r  )r   r   rk  r   r  r   offsetsrd  _tr   rU  r  r  unwanted_next_tokenprevious_tokencurrent_indentrH  r   symbols                      r@   r  r  
  s    G v!67 				U\\))),, # #l+   #V"QY&*D\33469LM V"QY&*D\33469LM  	lc1uqy!!$. CK!O+z* &feai8Nhoo->!#4#G|,Y 8\ NE 1TN*T11E==("k1N  
 E%%%(N 2 !A~ (.+02U""rB   c              #      K   d}d}d}| D ]G  }|d   }|d   }|d   \  }}|d   \  }	}
||z  }||k(  r||z  }|}|t        |      z  }||||f |	}|
}I yw)zYield tokens and offsets.r   r   r   r   NrL  )r   r  previous_end_rowprevious_end_columnr  r   rU  rV  rW  rX  rY  r  s               r@   r  r  @  s     JqT
t$%aD!L !!* 	l"
y(--J " 	c,''
 	
 #(/ s   AAc                     | j                  d      r$| j                         j                  d      r| dz   S | j                  d      rd| z   S | j                  d      r| dz   S | j                  d      r| dz   S | j                  d	      r| dz   S | S )
zrNormalize multiline-related code that will cause syntax error.

    This is for purposes of checking syntax.

    rU   rb   z passzreturn z	def _(): @zdef _(): passzclass )zif zelif for zwhile )rV   r   ro   r  s    r@   r  r  _  s     v4;;=#9#9##>g~		#T!!		o%%		"g~	;	<g~KrB   c                     | d| j                  d      }| |d j                  d      }|j                  d      r| S ||z   |z   S )z3Replace whitespace at offset and return fixed line.NrY  r/  )r   r+  rV   )r   rA  rD  r  r  s        r@   rF  rF  s  sS     =,DM  -E+%%rB   c                      G fddt         j                        t        j                  d|d| }|j                          |j                  j                         S )z,Execute pycodestyle via python method calls.c                   8     e Zd ZdZ fdZ fdZd Z xZS )"_execute_pep8.<locals>.QuietReportz'Version of checker that does not print.c                 2    t         |   |       g | _        y r,  )superr    _QuietReport__full_error_results)r   r   QuietReport	__class__s     r@   r   z+_execute_pep8.<locals>.QuietReport.__init__  s    +t-g6(*D%rB   c                 v    t         |   ||||      }|r$| j                  j                  |||dz   |d       yy)zCollect errors.r   )r   r   r   r   N)r  r   r  r{   )r   line_numberrA  r   checkr_  r  r  s         r@   r   z(_execute_pep8.<locals>.QuietReport.error  sR    d1+2826279D ))00(%z!#$ rB   c                     | 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'.

            )r  r  s    r@   full_error_resultsz5_execute_pep8.<locals>.QuietReport.full_error_results  s     ,,,rB   )r  r  r  r  r   r   r  __classcell__)r  r  s   @r@   r  r    s    5	+	$	-rB   r  )r  reporterr8   )r3   
BaseReportChecker	check_allreportr  )r%  r   checkerr  s      @r@   r!  r!  ~  sW    -k,, -< !! 2F[ 2$02G>>,,..rB   c                     |r.| j                  d      j                  t        t        z         dz   S | j                  d      S )Nz 	r   )r+  r   r  r  )r   with_rstrips     r@   _remove_leading_and_normalizer    s7    {{7#**273d::;;wrB   c                   (    e Zd ZdZddZefdZd Zy)rj  zReindents badly-indented code to uniformly use four-space indentation.

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

    c                 P   t        j                  |      }|j                         }t        |      | _        g | _        t        |d      D ]  \  }}|| j                  v r| j
                  j                  |       0|t        |      k7  }|r2| j
                  j                  t        |      t        ||      z          r| j
                  j                  t        |      j                         t        ||      z           | j
                  j                  dd        d| _        || _        y )Nr   r   r   )r=   r   rR   multiline_string_linesstring_content_line_numbersr  r   r{   r|   r2  r  
expandtabsr  rd  
input_text)r   r  rm  r   r`  r  r   r  s           r@   r   zReindenter.__init__  s    kk*%}}+A*+M(
 
!*<q!AKd>>>

!!$' *S->>JJ%%(.5dKHI
 JJ%%(.99;5dKHI "B$ 	

!T"
$rB   c                    |dk  r| j                   S 	 t        t        j                  | j                              }| j                  }|j                  t        |      df       i }g }|d   d   }|j                  |d|        t        t        |      dz
        D ]  }||   \  }}||dz      d   }	t        ||         }
||z  }|dk  r|
r|j                  |
d      }|dk  rEt        |dz   t        |      dz
        D ]'  }||   \  }}|dk\  s|
t        ||         k(  r||z  } n |dk  rGt        |dz
  dd      D ]4  }||   \  }}|dk\  s|
t        ||dz
           z   t        ||         z
  } n |dk  r|
}nd}|dk\  sJ |||
<   ||
z
  }|dk(  s|
dk(  r|j                  |||	        t        |||	 |      D ]  \  }}|| j                  v r|j                  |       &|dkD  r/|dk(  r|j                  |       B|j                  d|z  |z          Zt!        t        |      |       }|j                  ||d          dj#                  |      S # t
        t        j                  f$ r | j                   cY S w xY w)	zcFix indentation and return modified line numbers.

        Line numbers are indexed at 1.

        r   r   ra   r  r   r(  Nr8   )r  _reindent_statsrF   rL  getlinerL   r   r  r{   r|   extendr~   _leading_space_countrv   r   r  rZ  r   )r   rl  statsr  	have2wantafterr   thisstmt	thislevelnextstmthavewantjjlinejlevelr  r  r   removes                      r@   rk  zReindenter.run  s    ???"	##H$<$<T\\$JKE 

c%j!_%	 !HQKU1QZ s5zA~&A"'(HiQU|AH'h8D{*Dax %==r2Dax!&q1uc%j1n!=A,1!HME6%{#'+?e+M#M+1K+?D % "> ax "'q1ub"!5A,1!HME6%{(,/C(-eai(80: ):(<U5\(J)K !& "6 ax#D199"IdO$;DqyDAIU8H56)25(3K9A*C%K"d&F&FFT*4<!LL.!LLtd):;!$%9$%?$!GT&']3*CU 'n wwu~K X001 	#??"	#s   (I %J ?J c                     | j                   t        | j                        k\  rd}|S | j                  | j                      }| xj                   dz  c_         |S )zLine-getter for tokenize.r8   r   )rd  r|   r  )r   r   s     r@   r  zReindenter.getline%  sH    ::TZZ(D  ::djj)DJJ!OJrB   Nr$  )r  r  r  r  r   rp   rk  r  rc  rB   r@   rj  rj    s    %@ 2 PdrB   rj  c                    d}d}g }| D ]  }|d   }|d   d   }|d   }|t         j                  k(  rd}+|t         j                  k(  rd}|dz  }F|t         j                  k(  rd}|dz  }a|t         j                  k(  r|sw|j                  |df       |t         j                  k(  r|sd}|s|j                  ||f        |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   ra   )rF   rr   r]  r\  rx   r{   rq   )r   	find_stmtlevelr  r  r   r  r   s           r@   r  r  /  s     IEEqT
!Qt))) I8??*IQJE8??*IQJE8+++eR[) 8;;& IeU^,E H LrB   c                 n    d}|t        |       k  r$| |   dk(  r|dz  }|t        |       k  r	| |   dk(  r|S )z(Return number of leading spaces in line.r   r(  r   rL  )r   r   s     r@   r  r  b  sA    	A
c$i-DGsN	Q c$i-DGsNHrB   c                     ddl m} |D cg c]  }d|z   	 }} |||      }ddlm} 	 t	        |j                  | |            S c c}w # |j                  $ r | cY S w xY w)zQUse lib2to3 to refactor the source.

    Return the refactored source code.

    r   )RefactoringToolzlib2to3.fixes.fix_)rx  explicitrE   )name)lib2to3.refactorr  rH   rF   rw   refactor_stringr   )source_textrx  r?   r  r  fixerstoolrO   s           r@   rz  rz  j  ss     16ABkd"T)kFBv?D:4''('CDD C && s   AA AAc                 V    	 t        | ddd      S # t        t        t        f$ r Y yw xY w)zReturn True if syntax is okay.z<string>execT)dont_inheritF)compilerL   	TypeErrorr  rO  s    r@   rM  rM  |  s1    tZdCCJ/ s    ((c                 H  	 t        t        j                  | |            }|sg S |d   j                         }ddi	t	        t        j                  d|      d      D ]!  \  }}|j                         }||kD  r n|	|<   # 	fd}|D cg c]  } |||      dz    c}S c c}w )zA wrapper around 're.finditer' to find line numbers.

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

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

        r   r   )rfindr   )rX   r   newline_offsetnewline_offsetss      r@   get_line_numz,find_with_line_numbers.<locals>.get_line_num  s(     "a?~..rB   )r   r  finditerr   r   )
r  r   matchesr   line_numr  rA  r  rX   r  s
            @r@   find_with_line_numbersr    s    
 2;;w12G	
"+


C 	AO !UH!=qA!C<"*	 B/ <CC7%L)A-7CCCs   	Bc                    t        t        |       }t        t        |       }t        t	        j
                  d|             dz   }i }|D ]  }d||<   	 |D ]  }d||<   	 g }d}d}t        |j                               D ]1  \  }	}
|
du r	|du r|	}d}|
du s|du s|j                  ||	f       d}3 |du r|j                  ||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)	r  ENABLE_REGEXDISABLE_REGEXr|   r  findallr   rh  r{   )r   enable_line_numsdisable_line_numstotal_linesenable_commandsnumdisabled_rangescurrently_enableddisabled_startr   commanded_enableds              r@   get_disabled_rangesr    s     .lFC.}fEbjjv./!3KO#   $ ! ON#)/*?*?*A#B%*;t*C!N %$&+<+E""ND#9: $ $C E!<=rB   c                 F    | d   }|D ]  }|d   |cxk  r
|d   k  s y  y)z;Filter out reports based on tuple of disabled ranges.

    r   r   r   FTrc  )r  r  r   disabled_ranges       r@   filter_disabled_resultsr    s;     &>D)!9q(99 : * rB   c              #   |  K   t        | d      }t        | d      }t        |       }t        |       }|r|D cg c]  }t        ||      s| }}t	        d |D              }|D ]  }	|	d   j                         }
|	d   |v r|
j                  d      r/|	d   |v r|
dv r;|s|	d   d	z   |v r|
j                  d
      rY|dk  r|
j                  d      rp|d	k  r|
j                  d      r|dk  r|
j                  d      r|	d   |v r|
j                  d      r|r|
j                  d      r|	  yc c}w w)z|Filter out spurious reports from pycodestyle.

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

    F)include_docstringsTc              3   H   K   | ]  }|d    j                         dk(    yw)r   e901N)r   )r  r  s     r@   r   z!filter_results.<locals>.<genexpr>  s#     Hf6$<%%'61s    "r   r   )e1r  w191)r  r   )w29w39r   )e711e72w6)e712e713e714r   e704)e261r  r  )r  e7N)r  commented_out_code_linesr  r  r6  r   rV   )r   r  r   !non_docstring_string_line_numbersall_string_line_numberscommented_out_code_line_numbersr  r  has_e901r&  issue_ids              r@   r#  r#    s     )?5)*%44) '?v&F# *&1O!(
!(v,C-F 	 
 HHHHT7==?V999""#9:V9//8# qy1}1HH "">2?""#89?""#;<?""F,V977""#;<
 ""<0U 
s   7D<D7D7C-D<c           	      F   t               }d}	 t        |       D ]d  }|d   }|d   d   }|d   d   }|t        j                  k(  r8||k7  r3|s|t        j                  k7  r|t        t        d|z   d|z               z  }|}f 	 |S # t        t        j                  f$ r Y |S w xY w)zReturn line numbers that are within multiline strings.

    The line numbers are indexed at 1.

    Docstrings are ignored.

    r8   r   r   r   r   )r   rL  rF   ry   r]  r~   rL   r   )r   r  line_numbersprevious_token_typer  r   rV  rX  s           r@   r  r    s     5L (A1J!QId1gGX__,g1E&'8??: !Ca)mQ[(I$JJL", )$  ,,- s   A2B B B c                    g }	 t        |       D ]  }|d   }|d   }|d   d   }|d   }|j                         j                  d      s:|t        j                  k(  sN|j                  d      j                         }t        j                         5  t        j                  dt               d|v r d|vrt        |      r|j                  |       d	d	d	        	 |S # 1 sw Y   xY w# t        t        j                  f$ r Y |S w xY w)
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)rL  r+  rV   rF   rx   r  warningscatch_warningsfilterwarningsSyntaxWarningrM  r{   rL   r   )r   r  r  r   rU  rV  r   stripped_lines           r@   r  r  8  s    L (A1JQ4L!QIQ4D ;;=++C0X--- , 3 3C 8 > > @,,.
 ++H}M},=0$]3$++I6 /. )6  /. ,,- s1   AC) 3C) A C
C) C&	"C) )DDc                    t        |       |kD  sJ | j                         } t        |       dz   }t        |t        |      dz         }d}t        |       t        | j                  | d               z
  |k\  r| d   j	                         s| d| dz   S |rTt        j                  d|       r>t        j                  | j                  d      |||d	d	
      }dj                  |      dz   S | dz   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.

    rj  H   r  ra   Nr   z\s*#+\s*\w+r^  F)initial_indentsubsequent_indentwidthbreak_long_wordsbreak_on_hyphens)r|   r   r2  rZ  r  r  rX   textwrapwrapr+  r   )r   r  r  rk  MIN_CHARACTER_REPEATsplit_liness         r@   r  r  _  s     t9&&&;;=D #4(4/K/k*R/1O D	CDH-..2FFH $_%,,	"((>48mmDKK$73>6A*95:5:< yy%,,$;rB   c                     | D cg c]  }|j                  d      |z    c}}|r1| d   | d   j                  d      k(  r|d   j                  d      |d<   |S c c}w )zcReturn fixed line endings.

    All lines will be modified to use the most common line ending.
    z
ra   )r   )r  r;   r   s      r@   normalize_line_endingsr(    sc    
 7<<edDKK')e<Db	U2Y--f558??6*RK =s   Ac                 J    |j                  |       xs | j                  |      S r,  r-  )rr  rk   s     r@   mutual_startswithr*    s    <<?-all1o-rB   c                    |r_t        |t              rJ |D cg c]  }|j                          c}D ],  }t        | j	                         |j	                               s, y |r`t        |t              rJ |D cg c]  }|j                          c}D ],  }t        | j	                         |j	                               s, y yyc c}w c c}w )NFT)r  rw   r  r*  r   )r_  r  r  r[  ignored_codeselected_codes         r@   r  r    s    fc***0671QWWY7L |/A/A/CD 8 fc***178Aaggi8M }/B/B/DE 9  8 9s   C:C
c                 
   t        ||      }|j                  D cg c]  }|j                          c}|_        |j                  D cg c]  }|j                          c}|_        |j                  }h dt	        |      z  s|j                  j                  d       t        | t              s| j                  |xs
 t                     } t        j                  |       }t        |j                         |      S c c}w c c}w )zgReturn fixed source code.

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

    >   W50r   r   r/  )r   )_get_optionsr  r  r  r   r{   r  rw   decodeget_encodingr=   r   	fix_linesrR   )r   r   r:   apply_configopt
ignore_optr   s          r@   fix_coder7    s     7L1G-4^^<^cciik^<GN-4^^<^cciik^<GN
 J"S_4e$fc"x9<>:
++f
CS]]_g66 =<s   C;D c                    | st        dg|      S t        | t              rt        dg|      }| j                         D ]  \  }}t	        ||      st        dj                  |            t        t        ||            }t        |t        f      s+t        |t        f      rt        dj                  |            t        |||        |S | }|S )zReturn parsed options.r8   r4  zNo such option '{}'z"Option '{}' should not be a string)
parse_argsr  dictrh  r   r  rY   typer   rw   setattr)raw_optionsr4  r   r  valueexpected_types         r@   r0  r0    s    2$\::+t$bT=&,,.KD%7D) !6!=!=d!CDD !$!78MmcW5ecW-$<CCDIK KGT5) / N NrB   c                 J   t        |       }dj                  t        | d            }t               }|j                  r|}nt        |||      }d}t               }t        |      |vr|j                  dk\  r||j                  kD  rna|dz  }|j                  t        |             t        j                  |      }t        ||||      }	|	j                         }t        |      |vrt        j                  |      }
dj                  t        |
j                         |            S )zReturn fixed source code.r8   r   r  r   r   )r   r   )r  r   r(  r   r"  apply_global_fixeshashpep8_passesr  copyr   r  r=   r   rR   )r`  r   r?   original_newline
tmp_sourceprevious_hashesr  passesr   r  r   s              r@   r3  r3    s    $L1/dCDJ eO! ***13;= F U
|
O
3!#1D1D(D!D./YY|,
#9	; wwy |
O
3" ++l
#C77)#--/;KLMMrB   c                 ~   |st        | g|      }t        |       }|}|j                  s|j                  s|rt	        |       }|rt        t        |            }t        |||       }|j                  ryt        j                  |      }|j                         }t        |||       }|r#|j                  |       |j                          |S |j                  dkD  r|j                        }|S |j                  rdj!                  |      j#                         }	|j#                         }
|r|d   j%                  d      d   nd}|j%                  d      d   }|	|
k7  s||k7  r*t'        | d      5 }|j                  |       d d d        |S y |r!|j                  |       |j                          |S # 1 sw Y   |S xY w)	Nr9  rG   r  r   r8   ra   r   w)r:  rS   in_placer  r<   LineEndingWrapperrF  r3  r=   r   rR   r  writeflushjobsencoder   r#  r*  rA   )r?   r   outputr4  original_sourcer  r:   r  r  r  rH  original_source_last_linefixed_source_last_linefps                 r@   fix_filerW    s   hZlC)(3O"L7<<6"8,";v#IJ\7XFL||kk,'mmo_c8<LLLLN  \\A;;x(D			77?+668'')3BOB%%d+B/ 	" ".!3!3D!9"!=u%)??#HcHE& FLL&LLN Fs   0F22F<c               #      K   t        t               j                               D ]A  } t        j                  |       st        |       }|dd dgk7  r.t        |       }|s<|| f C yw)z'Yield multiple (code, function) tuples.Nr   r   )r   globalsvaluesinspect
isfunctionr   extract_code_from_function)function	argumentsr_  s      r@   global_fixesr`  ,  sc     ))+,h''1I!}
*-h7DX&& -s   :A)"A) 	A)c                     t         j                  j                  dk\  rIt        j                  |       r| j
                  } t        t        j                  |       j                        S t        j                  |       d   S )Nr   r   )
r  version_infomajorr[  ismethod__func__r   	signature
parameters
getargspec)r^  s    r@   r   r   9  sc    
" H%((HG%%h/::;;!!(+A..rB   c           	      :   |g }t        fddD              r9t        | j                  t        dj                  j
                               } t               D ]  \  }}t        |j                  j
                        s)j                  r9t        dj                  ||j                               t        j                          || j                        }  t        | j                  j                  j
                  ||j                  	      } | 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).

    c              3   b   K   | ]&  }t        |j                  j                          ( yw)r  N)r  r  r  )r  r_  r   s     r@   r   z%apply_global_fixes.<locals>.<genexpr>Q  s-      )'t d7>>'..II's   ,/)E101E111W191r  )rl  rm  r  r   )r   )r   r  r  r?   r  r  )r6  ro  rl  r  r  r  r`  r  r  rY   r  r  r  r   r  )r   r   r  r?   codesr_  r^  s    `     r@   rB  rB  G  s     }
 )') )++">>">>

 )Nxd7>>'..I4;;E<@JJLJ::' f)0););=F + f!(!3!3$^^$^^'!%oo/F MrB   c                     | j                   j                  d      syt        j                  dd| j                         }|sy	 t	        |dd        |S # t
        $ r Y yw xY w)z Return code handled by function.r   Nz^fix_r8   r   )r  rV   r  r  r)  r  )r^  r_  s     r@   r]  r]  s  sd    ''/66'2x001DDH K  s   A 	AAc                  d    dj                  t        j                        g} dj                  |       S )Nzpycodestyle: {}rJ  )rY   r3   __version__r   )packagess    r@   _get_package_versionrs    s*    !(()@)@ABH99XrB   c            	         t        j                  t        t              d      } | j	                  dddj                  t        t                            | j	                  ddd	d
d       | j	                  dddd       | j	                  dddd       | j	                  ddt        dj                  t                     | j	                  ddd       | j	                  dddd       | j	                  ddt        d d!d"#       | j	                  d$d%d d&t        d'(       | j	                  d)d*d	d
d+       | j	                  d,dd-       | j	                  d.d/d01       | j	                  d2dd3       | j	                  d4d5d6d7j                  t                     | j	                  d8d5d6d9       | j	                  d:d d;t        d<(       | j	                  d=d>d?d@t        dAdBC       | j	                  dDt        t        t         j                  E       | j	                  dFddG       | j	                  dHddI       | j	                  dJdKdLM       | S )NzReturn command-line parser.autopep8)descriptionprogz	--versionversionz%(prog)s {} ({}))actionrx  z-vz	--verboser  r   zCprint verbose messages; multiple -v result in more verbose messages)ry  r  helpz-dz--diff
store_truez#print the diff for the fixed source)ry  rz  z-iz
--in-placezmake changes to files in placez--global-configr?   zapath to a global pep8 config file; if this file does not exist then this is ignored (default: {}))metavarr  rz  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  rz  z-pz--pep8-passesra   z<maximum number of additional pep8 passes (default: infinite))r|  r  r<  rz  z-az--aggressivezLenable non-whitespace changes; multiple -a result in more aggressive changesz--experimentalzenable experimental fixesz	--excludeglobszCexclude file/directory names that match these comma-separated globs)r|  rz  z--list-fixesz3list codes for fixes; used by --ignore and --selectz--ignorerT  r8   z.do not fix these errors/warnings (default: {})z--selectz*fix only these errors/warnings (e.g. E4,W)z--max-line-lengthO   z6set maximum allowed line length (default: %(default)s)z--line-rangez--ranger   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<  nargsrz  z--indent-size)r  r<  rz  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  rz  )argparseArgumentParserdocstring_summaryr  add_argumentrY   rq  rs  DEFAULT_CONFIGr)  DEFAULT_IGNORErp   SUPPRESS)parsers    r@   create_parserr    s   $$1B71K*46F
I 2 9 9')=)?!A  B k' !K  L h|B  D
l<=  ?
): .--3VN-C	  E
 /L  M mLF  G hS#qG  H os "3  4 nWaM  N (8  :
W5  6 |8  9 
Hb--3VN-C  E 
HbI  K
+S"36  7 	6 $3a=  >
 1D x'8'8  :
(H  J
lJ  K
 sE  G MrB   c                     t               }d}t         fdt        D              rd}d|v }d|v } D ]  }|dk(  rB|r|r|j                  h d        |r|j                  h d       6|j                  h d	       J|d
v rA|r|r|j                  dh       e|r|j                  ddh       {|j                  ddh       |dv r|r|j	                  |        |S )zexpand to individual E/W codesFc              3   H   K   | ]  t        fd D                yw)c              3   @   K   | ]  }j                  |        y wr,  r-  )r  r_  conflicting_codes     r@   r   z*_expand_codes.<locals>.<genexpr>.<genexpr>  s$      !D !++D1!r/  N)r6  )r  r  rn  s    @r@   r   z _expand_codes.<locals>.<genexpr>  s0      
 %6 	  !  %6s   "Tr   r   W>   W1W2W3W6W505>   r  r  r  r  r   r  >   r  r  r  r  r   r  )W5r/  r  r   )r   allCONFLICTING_CODESr   r  )rn  ignore_codesretis_conflictis_ignore_w503is_ignore_w504r_  s   `      r@   _expand_codesr    s    
%CK
 
 %6  |+N|+N3;.

;<

CD

CD]".

F8$

FF+,

FF+,**{GGDM! $ JrB   c                    t               }|j                  |       j                  s"j                  s|j	                  t
        d       j                  D cg c]  }t        |       c}_        |rVt        |      }	 t        |      }|r|}|j                  |       j                  D cg c]  }t        |       c}_        dj                  v rt        j                        dkD  r|j	                  t
        d       j                  r|j	                  t
        d       j                  r|j	                  t
        d       j                  r|j	                  t
        d       t        j                        dkD  r.j                  s"j                  s|j	                  t
        d	       j                  r.j                  s"j                  s|j	                  t
        d
       j                  r"j                  r|j	                  t
        d       j                  dk  r|j	                  t
        d       j                  dk  r|j	                  t
        d       j                   rEt#        t%        j                         j&                  rt%        j&                        ng       _        j&                  rRt%        j&                        _        t)        fdt*        D              rVj&                  j-                  t*               n6j                   s*j.                  r
h d_        nt%        t0              _        j2                  rt%        j2                        _        ni _        j4                  dk  rddl}|j9                         _        j4                  dkD  r.j                  s"j                  s|j	                  t
        d       j:                  r]j:                  d   dk  r|j	                  t
        d       j:                  d   j:                  d   kD  r|j	                  t
        d       S c c}w # t        $ r d}Y w xY wc c}w )zParse command-line options.zincorrect number of argumentsNr   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   ^   K   | ]#  t        fd j                  D                % yw)c              3   @   K   | ]  }j                  |        y wr,  r-  )r  ignore_coder  s     r@   r   z'parse_args.<locals>.<genexpr>.<genexpr>O  s$      '2 %//<'2r/  N)r6  r  )r  r  argss    @r@   r   zparse_args.<locals>.<genexpr>N  s9      

 ):$	  '+{{   ):s   )->   Er  r  r  r  z!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decode_filenameread_configread_pyproject_toml	Exceptionr|   r  rL  	recursiver  rl  r  r  _split_comma_separatedr  r  r  r   r   r  excluderP  multiprocessing	cpu_countr"  )r_  r4  r  r  parser_with_pyproject_tomlr  r  s         @r@   r:  r:    sw   _FY'D::doo,.MN48JJ?JD/$'J?DJT6*	.)<T6)J& &/F  +8<

C
od+
C

djjtzz?QKK(4
 99KK(;
 ==KK(?
 >>KK(@
 4::DMMTYY$@	
 ~~t}}		$@	

 }}$:	

 q $6	

 1$2	

 {{#"4;;/48KK#DKK0R

 {{,T[[9 

 ):
 
 KK01[[??7DK0@DK||-dll;yy1} 	#--/	yy1}dmmtyy$/	

 ??1"KK(2 ??1 22KK(  Kw @  	.)-&	.
 Ds   QQ 1QQQc              #     K   |j                  |      D ]  \  }}|j                  d      j                  dd      }|j                  |      s9||   }|t        u rD|j                         dk(  r| j                  rt        d| d|        w|j                  ||      }n-|t        u r|j                  ||      }n|j                  ||      }|||f  y w)Nr   r  autozignore config: r  )rh  r+  rJ  rv   r)  r  r  r  getintbool
getboolean)	r  configsectionoption_listkvnorm_optopt_typer?  s	            r@   _get_normalize_optionsr  ~  s     ,,w'A88C=((c2x(x(s?wwyF"<<OA3as34MM'1-E%%gq1EJJw*E5  ! (s   CCc           	      Z   t               }	 | j                  rMt        j                  j	                  | j
                        r$t        dj                  | j
                               |j                  | j
                         | j                  sN| j                  xrF t        j                  j                  t        j                  j                  | j                              x}}|r|j                  t        D cg c]"  }t        j                  j                  ||      $ c}      r| j                  rt        D ]z  }t        j                  j                  ||      }t        j                  j	                  |      sCt        dj                  t        j                  j                  ||                   | n%t        j                  j                  |      \  }}|ri }|j                   D ci c]0  }|j"                  |j$                  xs t%        |j&                        2 }	}dD ]X  }
|j)                  |
      st+        | ||
|	      D ]3  \  }}}| j                  rt        dj                  |
||             |||<   5 Z  |j,                  di | |S c c}w c c}w # t.        $ r Y |S w xY w)z5Read both user configuration and local configuration.zread config path: {})r4   r3   flake8z+enable config: section={}, key={}, value={}rc  )SafeConfigParserr  ospathexistsglobal_configr  rY   rJ   ignore_local_configr  abspathcommonprefixPROJECT_CONFIGr   r*  _actionsdestr<  r  has_sectionr  set_defaultsr   )r  r  r  parenttailfnconfig_filedefaultsor  r  r  r  r?  s                 r@   r  r    sD   F*<<BGGNN4+=+=>(//0B0BCDD&&''' JJ 2277??$$TZZ0,2 2FT;;*8 :*8B !#VR 8*8 : ;||"0B*,'',,vr*BK#%77>>+#> (! 6 = =$&GGLL$<!"	 #1 !#v!6    &1 /1 vvqvv8aii8 / 	 1 9G%%g.&<fg{'"!U <<GNNE+ ,%*"' 9 	'h'
 MG : 1  M	s8   C,J 8'JB;J J +5J A1J 
J 	J*)J*c                    t         j                  dk\  rddl}nddl}d}t        j
                  j                  | j                        r0t        | j                  d      5 }|j                  |      }ddd       | j                  s| j                  xrF t        j
                  j                  t        j
                  j                  | j                              x}}|rt        j
                  j                  |d      }t        j
                  j                  |      r(t        |d      5 }|j                  |      }	 ddd       n%t        j
                  j                  |      \  }}|r|sy|j!                  di       j!                  d      y|j!                  d      j!                  d      }i }|j"                  D 	ci c]0  }	|	j$                  |	j&                  xs t'        |	j(                        2 }
}	d}|j+                         D ]  \  }}|j-                  d	      j/                  d	d
      }|
j!                  |      s9t'        |      t0        t2        fv r||v rdj                  |      }n|}| j4                  rt7        dj9                  ||             |||<    |r |j:                  di | |S # 1 sw Y   DxY w# 1 sw Y   xY wc c}	w )z+Read pyproject.toml and load configuration.)r      r   NrD   zpyproject.tomlr  ru  )r  r  r   r  r   z.enable pyproject.toml config: key={}, value={}rc  )r  rb  tomllibtomlir  r  r  r  r>   loadr  r  r  r  r   r*  rv   r  r  r<  r  rh  r+  rJ  r   rK  r  r  rY   r  )r  r  r  r  rV  r  r  pyproject_tomlr  r  r  TUPLED_OPTIONSr  r  r  r?  s                   r@   r  r    sS   
7"F	ww~~d(()$$$d+r\\"%F , ##

 .rwwGG  ,(. 	.WW\\&2BCNww~~n-.$/2$\\"-F 0/  WW]]62NVT  zz&"!!*-5ZZ##J/FH"OO-+q 661664T!))_4+  - *N,,.A88C=((c2x(7tUm#N(BHHQKEE<< %%+VAu%57" ! 'h'MY ,+ 0/-s   J2'J?=5K2J<?K	c                     | j                  d      D ch c]#  }|j                         s|j                         % c}S c c}w )zReturn a set of strings.r   )r*  r  )r  r   s     r@   r  r    s3    %+\\#%6G%6T$**,DJJL%6GGGs   ??c                 l    t        | t              r| S | j                  t        j                               S )zReturn Unicode filename.)r  rw   r1  r  getfilesystemencodingr  s    r@   r  r    s)    (C ??344677rB   c               #     K   dt        t        j                        f t        ddd      } t	        |       D ]p  }t        j                  d|      }|s|j                  d      j                         t        j                  ddt        t        | |      j                              f r t        t                     D ]S  \  }}|j                         d	t        |      z
  dz  z   t        j                  ddt        |j                              f U t        t              D ]T  }|j                         d	t        |      z
  dz  z   t        j                  ddt        t        j                              f V yw)
zYield pep8 error codes that autopep8 fixes.

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

    rk  Nr8   )r?   r   r   zfix_([ew][0-9][0-9][0-9])r   z\s+r(  r   )r  ro  r  r   dirr  rX   r  r  r  r   r   r`  r|   r  r  )instance	attributer_  r^  s       r@   supported_fixesr    sB     $X%5%56
77dR@H]	xx3Y?

1##%vs(9)E)M)MNP  # #<>2xzz|q3t9}33vvfc#4X5E5E#FGI 	I 3 |$zz|q3t9}33vvfc#4X5E5E#FGI 	I %s   AE<D'E<c                 2    | r| j                  d      d   S dS )zReturn summary of docstring.r   r   r8   )r*  )	docstrings    r@   r  r  '  s    '09??4 #8b8rB   c                 .   | j                         syd}| j                         j                  d      }d|d   j                         j	                  d      sm|d   j                         d   dvrUdD ]P  \  }}|d   j                  |      }|d   j                  |      }	|dk\  s4|	dk  s	|	|dz   k7  sBt        d|z         R t        fd|D              }
|d	t        d|
|z
        z  z  }|t        |      z  }|d
t        d |D              z  z  }ddddj                  |d   d         }t        |      dkD  r)|r'|d   j                         j	                  |      r|dz  }t        |      D ]  \  }}|j                         }|j	                  d      r)dD ]#  }|j	                  |      r|dz  }||k(  s|dz  }% |j                  d      r	d|v r|dz  }|j                  d      rt        |      t        |      k  r|dz  }|j                  d      r'|dd j                         j                  d      r|dz  }|j                  d      r-t        |      dkD  r|d   j                         s|d   dv r|dz  }|j                  d      r|dz  }t        |      r|dz  }t        j                  d|j                  d            r|dz  }t        j                  d|j                  d            r|dz  }|j                  d       r|dz  }|j	                  d!      r|d"z  }|j                  d#      rt        |      }|dz  }|t        |      k  r_|t        ||         z  }||   j                         j	                  d      r|}n)||   j                  d#      sn|dz  }|t        |      k  r_||k  r|d$z  }n	||rdndz  }d|v r|j                  d%      r|d$z  }|j                  d%      r|dz  }|d$t!        |      z  z  } t        d|      S )&zDReturn rank of candidate.

    This is for sorting candidates.

    r   r   r/  ra   rn   z()z[]z{}r   c              3   V   K   | ]   }t        |j                               z    " y wr,  )r|   r  )r  rB  rA  s     r@   r   z'line_shortening_rank.<locals>.<genexpr>G  s!     A5a&3qwwy>15s   &)r   r   c              3   2   K   | ]  }t        |        y wr,  rL  rM  s     r@   r   z'line_shortening_rank.<locals>.<genexpr>N  s     "?3t9rN  ru  rq  rv  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   rq  
   rb   )r  r   r*  r+  rV   r   rv  r|   standard_deviationrv   r   ro   r  has_arithmetic_operatorr  rX   count_unbalanced_brackets)r  r   r  r   rankr  openingclosingopening_locclosing_loccurrent_longestbad_staring_symbollinenocurrent_line	bad_start	total_lenrA  s                   @r@   rA  rA  ,  sY    ??D$$T*EF!HOO((-a"U*"4Wg  (--0K(--0Ka?k[1_&D [9F #5 A5AAOAA8999DCJD 	A""?"????D  #eAhrl# 
 5zA~!HOO(();<BJD )% 0#))+""3'2I&&y1 y( 3 !!";<\!DLD  !56< C$44 %%c*Sb!((*33C8 %%c*L!A%b!))+|B/?3/F $$S)&|4 88*L,?,?,FGDLD88%|':':5'ABDLD  !56BJD ""6*BJD  & L)IaKF3u:%Sv//	='')44S9 /IV}--d3! 3u:% ?*
|2 ,<#8#8#=BJD   %CKD.|<<< !1B q$<rB   c                     t        |       } | syt        |       t        |       z  t        fd| D              t        |       z  dz  S )zReturn standard deviation.r   c              3   .   K   | ]  }|z
  d z    yw)r   Nrc  )r  r  means     r@   r   z%standard_deviation.<locals>.<genexpr>  s     1ATas   g      ?)r   r$  r|   )numbersr  s    @r@   r  r    sK    7mGw<#g,&D111L   rB   c                 :    t         j                  D ]  }|| v s y y)z6Return True if line contains any arithmetic operators.TF)r3   ARITHMETIC_OP)r   r%   s     r@   r  r    s"    --t . rB   c                 |    d}dD ]4  \  }}|t        | j                  |      | j                  |      z
        z  }6 |S )z/Return number of unmatched open/close brackets.r   r  )r?  r  )r   r  r  r  s       r@   r  r    sC    E.TZZ(4::g+>>?? / LrB   c                     g }d}d}t        |      D ]9  }|t        |       k  r'||k7  r"|j                  | || j                                |}; |j                  | |d        |S )z9Split line at offsets.

    Return list of strings.

    r   N)r   r|   r{   r  )r   r  r  previous_offsetcurrent_offsets        r@   r  r    so     FON /CI%/^*KMM$~>DDFG( *
 MM$~'(MrB   c                   "    e Zd ZdZd Zd Zd Zy)rM  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                     || _         y r,  )_LineEndingWrapper__output)r   rR  s     r@   r   zLineEndingWrapper.__init__  s	    rB   c                 z    | j                   j                  |j                  dd      j                  dd             y )Nr   r   r
   )r  rN  rJ  )r   ss     r@   rN  zLineEndingWrapper.write  s,    AIIfd3;;D$GHrB   c                 8    | j                   j                          y r,  )r  rO  r  s    r@   rO  zLineEndingWrapper.flush  s    rB   N)r  r  r  r  r   rN  rO  rc  rB   r@   rM  rM    s    IrB   rM  c                 *   t         j                  j                  |       }|j                  d      ry|D ]2  }t	        j                  ||      r yt	        j                  | |      s2 y t         j                  j                  |       st        |       syy)z4Return True if file is okay for modifying/recursing.r  FT)r  r  basenamerV   fnmatchisdiris_python_file)r?   r  	base_namer  s       r@   
match_filer    sr      *IC ??9g.??8W-	  77==">(+CrB   c              #   T  K   | r| j                  d      }|rt        j                  j                  |      rt        j                  |      D ]  \  }}}| |D cg c]L  }t        t        j                  j                  ||      |      r t        j                  j                  ||      N c}z  } |D cg c].  }t        t        j                  j                  ||      |      r|0 c}|dd  n)d}	|D ]  }
t        j                  ||
      sd}	 n |	s| | ryyc c}w c c}w w)zYield filenames.r   NFT)r}   r  r  r  walkr  r   r
  )	filenamesr  r  r  rootdirectorieschildrenfr   is_exclude_matchr  s              r@   
find_filesr    s    
}}Qt,/1wwt}+k8X 6X!+BGGLLq,A,3"5 !ggll43X 6 6	 .9 ":[%/T10E07&9 #$[ ":A	 0=  %"??41'+$ # $
# 6":s+   AD(AD*	D(33D#&&D(D(D(c                     | d   j                   r-t        dj                  | d         t        j                         	 t        |  S # t        $ r+}t        t        |      t        j                         |d}~ww xY w)z>Helper function for optionally running fix_file() in parallel.r   z	[file:{}]r   r   N)r  r  rY   r  r  rW  IOErrorrw   )rg  r   s     r@   	_fix_filer  ,  sc    !}k  A/cjjA$$ c%jszz*s   A 	A:&A55A:c                    g }t        | |j                  |j                        } |j                  dkD  rddl}|j                  |j                        }g }| D ],  }|j                  t        ||ff      }|j                  |       . |j                          |j                          |j                  r`|D ][  }	t        j                  j                  |	j                         j!                                t        j                  j#                          ] |j%                  |D 
cg c]  }
|
|
j                          c}
       |S | D ]  }t        |||f      }||j                  r|dk7  s&|j                  |       8|j&                  r|j                  |       Vt)        |      }dj                  |      j+                         |j+                         k7  s|j                  |        |S c c}
w )z?Fix list of files.

    Optionally fix files recursively.

    r   r   Nr8   )r  r  r  rP  r  Poolapply_asyncr  r{   closer   r  r  stdoutrN  rv   r1  rO  r  rL  rS   r#  )r  r   rR  r  r  poolretsr  r  r&  rB  rS  s               r@   fix_multiple_filesr"  7  s    G9g&7&7II||a##GLL1D""9g.@ACKK  	

		<<

  !12

  "  	?A?@ N DT7F34C{||"9NN3'!!s#"5d";77?+668CNN<LLNN3'  N @s   !G-)G-c                 4   | j                  d      ry	 t        | t              5 }|j                  t              }|s
	 ddd       y|j	                         d   }ddd       t        j                        syy# 1 sw Y    xY w# t
        t        f$ r Y yw xY w)z'Return True if filename is Python file.z.pyTr6   NFr   )	ro   rA   MAX_PYTHON_FILE_DETECTION_BYTESrJ   r#  r  
IndexErrorPYTHON_SHEBANG_REGEXrX   )r?   r  r   
first_lines       r@   r  r  ^  s    	!@BEF669:DB B *1-JB  %%j1B B Z  s3   B A9B A9B 9B>B BBc                 X    d| v xs% d| v xs | j                         j                  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.

    r)  z'''rq  )r   ro   r  s    r@   r  r  t  s5     	 	%	%t$rB   c                 p     t        j                  |      t        | d      r| j                        S |       S )z&Return output with specified encoding.buffer)codecs	getwriterr   r*  )rR  r:   s     r@   rF  rF    s<    %6H%)0)B '-mm 3 3+13 3rB   c                  V    t        j                         xs t        j                         S )zReturn preferred encoding.)localegetpreferredencodingr  getdefaultencodingrc  rB   r@   r2  r2    s    &&(DC,B,B,DDrB   c                     | t         j                  } 	 t        j                  t        j                  t        j                         	 t        | dd |      }|j                  r=t        t                     D ]!  \  }}t        dj                  ||             # t        S |j                  dgk(  r|j                  rJ t         j                  j                   xs
 t#               }t         j                  j%                         }t'        |||      }t)        t         j*                  |      j-                  |       t/        |      t/        |      k7  r|j0                  rt2        S |j                  s|j4                  r$t7        t9        |j                              |_        n(t;        |j                        dk(  sJ |j<                  rJ t?        |j                  |t         j*                        }|j4                  r&tA        |D 	cg c]  }	t;        |	      dk7   c}	      }	ntA        |D 	cg c]  }	|	du c}	      }	|j0                  r	|	rt2        S yyyy# t
        $ r Y w xY wc c}	w c c}	w # tB        $ r	 tD        cY S tF        $ r	 tD        cY S w xY w)	zCommand-line entry.Nr   r9  z{code} - {description})r_  rv  r   rG   r   )$r  argvsignalSIGPIPESIG_DFLrO  r:  r  r   r  r  rY   EXIT_CODE_OKr  rL  stdinr:   r2  rJ   r7  rF  r  rN  rC  	exit_codeEXIT_CODE_EXISTS_DIFFr  r   r   r|   r  r"  r6  r  EXIT_CODE_ERRORKeyboardInterrupt)
r2  r4  r  r_  rv  r:   
read_stdinfixed_stdinr  r  s
             r@   mainr>    s   |xxfnnfnn5
)$qr(>??%+O,=%>!k.55; 6 8 9 &?  ::#}}$$yy));\^H)J":thGK 

X6<<[IJ4#44>>00}}		!#djj/2
4::!+++>>))(T3::FGyyG<GS3s8q=G<= g>gs3d?g>?~~#,, #&~ " 51  H = ?   sV   2I AI+  CI+ #BI+ I!I+ 'I&2I+ 	II!
I+ +J<JJc                       e Zd ZdZd Zd Zy)CachedTokenizerzzA one-element cache around tokenize.generate_tokens().

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

    c                      d | _         d | _        y r,  )	last_textlast_tokensr  s    r@   r   zCachedTokenizer.__init__  s    rB   c                     || j                   k7  rIt        j                  |      }t        t	        j
                  |j                              | _        || _         | j                  S )z*A stand-in for tokenize.generate_tokens().)rB  r=   r   r   rF   rL  rI   rC  )r   r   	string_ios      r@   rL  zCachedTokenizer.generate_tokens  sR    4>>!D)I#((););< D "DNrB   N)r  r  r  r  r   rL  rc  rB   r@   r@  r@    s      rB   r@  __main__)r&  Nra   )ra   )r  r~  FFFr,  r$  )Nr8   )r8   F)TNNr8   globalF)FFr8   )Fr8   )Tr  )NNF)rG  r8   N)NT)r  r  r   r   r   r   r  r+  r  rE  r  r
  r[  r=   r3  r8  r.  r  r  r3  r  r#  r  rF   r  r  configparserr   r  r   r3   r	   rq  r  r  r  r  r&  r  r  r  r  r  r1  r  r  r6  r:  r9  r  	frozensetr  r  rp   r  r  platformr  
expanduserr  r   getenvr  r  r$  rA   r<   rS   r_   register_checkr   _checksr%  r   r  r  r  rG  r  rQ  r   r   r  ro  r  rq  r  r  r  r  r   r2  r  r   rD  r  r  
namedtupler  r  r  rE  rh  rn  rs  r  r  r}  r  r  r  r  r  r  rF  r!  r  rj  r  r  rz  rM  r  r  r  r#  r  r  r  r(  r*  r  r7  r0  r3  rW  r`  r   rB  r]  rs  r  r  r:  r  r  r  r  r  r  r  rA  r  r  r  r  rM  r  r  r  r"  r  r  rF  r2  r>  r@  _cached_tokenizerrL  r  r  rc  rB   r@   <module>rQ     s  d	 '  % '        	    	 	  
     
 @  9  
	 "rzz"=> rzzBC#$JK !+,F!G BJJ}- !rzz">? "

#=> rzz34

56   $sese"#"#AB-.%   %  %  LJ$ <<7WW''(9:NWW\\)")),="> #B"$''"4"4["A"/1N 
ww~~n%
||w++J7bgg&8&8&EvN= #' &=*   / 0|:~ '(I(IJ   0 1A6f A6H Rj  =?FK-`878(4.'&HB	 BD%*'$G(
(  H FH#N 35B*J* 	w )A 	BZ v Z z
N,6 N,bW Wt
I 

9 

	 
	 ;9 ;
8vB'T8L^)>(&#/L A AH0f$DBDCL@$N F."720'NT,^
'/ BD!)X"
FR#LCL!(0f6rH
8I89 ',Qh ( &&,$N,3E
5p f  . $% #33 zCHHTV rB   