B
    )°zfä-  ã               @   sº   d 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	y
e	j
Z
W n& ek
r|   ddlm	Z ej
Z
Y nX dZG dd„ deƒZejejejfd	d
„Zedkr¶eƒ  dS )z"Removes commented-out Python code.é    )Úprint_function)Úunicode_literalsN)Útokenizez2.3.0c               @   s  e Zd ZdZe d¡Ze d¡Ze d¡Ze d¡Z	e d¡Z
e d¡Ze d¡Ze d	¡Ze d
¡ZddddddddddddddgZdddddgZedg ZdZd Zejd! e¡ejd"Zd6d$d%„Zd7d&d'„Zd8d(d)„Zd9d*d+„Zd,d-„ Zd:d/d0„Zd1d2„ Zd;d3d4„Zd5S )<Ú
EradicatorzEradicate comments.z^[()\[\]{}\s]+$z$.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)z,def .+\)[\s]+->[\s]+[a-zA-Z_][a-zA-Z0-9_]*:$z"for [a-zA-Z_][a-zA-Z0-9_]* in .+:$z#[0-9]z^\s*\w+\s*=.*[(\[{]$z^\s*[\'"]\w+[\'"]\s*:.+[,{]\s*$z^(print|return)\b\s*z#with .+ as [a-zA-Z_][a-zA-Z0-9_]*:$ú(ú)ú[ú]Ú{Ú}ú:ú=ú%ÚprintÚreturnÚbreakÚcontinueÚimportz	elif\s+.*ÚelseÚtryÚfinallyzexcept\s+.*zif\s+.*z 	
#)ZpylintZpyrightZnoqaZnosecztype:\s*ignorezmypy:zfmt:\s*(on|off)zyapf:\s*(enable|disable)zEisort:\s*(on|off|skip|skip_file|split|dont-add-imports(:\s*\[.*?\])?)ÚTODOÚFIXMEÚXXXú|)ÚflagsTc          
   C   s  |  ¡ }| d¡sdS |  | j¡ ¡ }| j |¡r6dS | j |¡rFdS | j |¡rVdS x| j	D ]}||kr^P q^W dS | j
||dr†dS x0|r’| jn| jD ]}t d| d |¡r˜dS q˜W | j d|¡}| j |¡rÖdS yt|dd	ƒ W n tttfk
r   dS X dS d
S )z"Return True comment contains code.ú#F)Ú
aggressiveTz^\s*z\s*:\s*$Ú z<string>ÚexecN)ÚlstripÚ
startswithÚWHITESPACE_HASHÚstripÚHASH_NUMBERÚsearchÚWHITELIST_REGEXÚCODING_COMMENT_REGEXÚmatchÚCODE_INDICATORSÚmultiline_caseÚCODE_KEYWORDS_AGGRÚCODE_KEYWORDSÚreÚPRINT_RETURN_REGEXÚsubÚPARTIAL_DICTIONARY_REGEXÚcompileÚSyntaxErrorÚ	TypeErrorÚUnicodeDecodeError)ÚselfÚliner   Úsymbol© r8   úL/home/ankuromar296_gmail_com/.local/lib/python3.7/site-packages/eradicate.pyÚcomment_contains_codeM   s6    
z Eradicator.comment_contains_codec             C   sš   |rhx2dD ]*}|  |d ¡r dS | ¡ |d kr
dS q
W | j |¡rHdS | j |¡rXdS | j |¡rhdS |  d¡rvdS | j |¡r†dS | j |¡r–dS dS )z<Return True if line is probably part of some multiline code.z)]}r   Tú,ú\F)	Úendswithr#   ÚDEF_STATEMENT_REGEXr%   ÚWITH_STATEMENT_REGEXÚFOR_STATEMENT_REGEXÚMULTILINE_ASSIGNMENT_REGEXr(   ÚBRACKET_REGEX)r5   r6   r   Úendingr8   r8   r9   r*   {   s&    

zEradicator.multiline_casec          	   c   sŠ   t  |¡}y`xZt |j¡D ]J}|d }|d d }|d }|tjkr| ¡  d¡r|  ||¡r|V  qW W n tj	t
fk
r„   Y nX dS )z)Yield line numbers of commented-out code.r   é   é   r   N)ÚioÚStringIOr   Úgenerate_tokensÚreadlineÚCOMMENTr    r!   r:   Ú
TokenErrorÚIndentationError)r5   Úsourcer   ÚsioÚtokenÚ
token_typeZ	start_rowr6   r8   r8   r9   Úcommented_out_code_line_numbersž   s    

z*Eradicator.commented_out_code_line_numbersc             c   s`   t |  ||¡ƒ}t |¡}d}x<t| ¡ ddD ](\}}||ksN| ¡  d¡rT|V  |}q0W dS )z+Yield code with commented out code removed.r   é   )Ústartr<   N)ÚlistrQ   rF   rG   Ú	enumerateÚ	readlinesÚrstripr=   )r5   rM   r   Zmarked_linesrN   Zprevious_lineÚline_numberr6   r8   r8   r9   Úfilter_commented_out_code¯   s    
z$Eradicator.filter_commented_out_codec       
   	   C   s¼   |   |¡}| j||d}| ¡ }W dQ R X d |  ||j¡¡}||kr¸|jrv| j|d|d}| |¡ W dQ R X n>tj	| 
¡ | 
¡ d| d| dd}	| d	 t|	ƒdg ¡¡ d
S dS )z(Run filter_commented_out_code() on file.)ÚencodingNr   Úw)ÚmoderZ   zbefore/zafter/)ZlinetermÚ
T)Údetect_encodingÚopen_with_encodingÚreadÚjoinrY   r   Zin_placeÚwriteÚdifflibZunified_diffÚ
splitlinesrT   )
r5   ÚfilenameÚargsÚstandard_outrZ   Ú
input_filerM   Zfiltered_sourceZoutput_fileÚdiffr8   r8   r9   Úfix_file¼   s$    



zEradicator.fix_fileÚrc             C   s   t j|||ddS )z,Return opened file with a specific encoding.r   )r\   rZ   Únewline)rF   Úopen)r5   re   rZ   r\   r8   r8   r9   r_   Õ   s    
zEradicator.open_with_encodingc          
   C   sh   yHt |dƒ4}t|jƒd }|  ||¡}| ¡  W dQ R X W dQ R X |S  tttfk
rb   dS X dS )zReturn file encoding.Úrbr   Nzlatin-1)rm   r^   rI   r_   r`   r2   ÚLookupErrorr4   )r5   re   rh   rZ   r8   r8   r9   r^   Û   s    zEradicator.detect_encodingc             C   sD   |r(t jd t| jƒ| ¡t jd| _nt jd |¡t jd| _dS )zUpdates the whitelist.r   )r   N)r-   r1   ra   rT   ÚDEFAULT_WHITELISTÚ
IGNORECASEr&   )r5   Znew_whitelistZextend_defaultr8   r8   r9   Úupdate_whitelisté   s    zEradicator.update_whitelistN)T)T)T)T)rk   )T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   r1   rB   r'   r>   r@   r$   rA   r0   r.   r?   r)   r,   r+   r"   rp   ra   rq   r&   r:   r*   rQ   rY   rj   r_   r^   rr   r8   r8   r8   r9   r   +   s2   










.
#


r   c          
      sþ  ddl }|jtdd}|jddddd	 |jd
dddd	 |jddddd	 |jddddd	 |jdddt d |jddd tj¡d	 |jddd tj¡d	 |jdddd | | d d… ¡}tƒ }|j	ræ| 
|j	 d!¡d"¡ n|jr| 
|j d!¡d#¡ tt|jƒƒ}d#}xÒ|ræ| d¡}	|jrŒtj |	¡rŒx¤t d$ |	¡¡D ]8\‰ }
}|‡ fd%d&„|D ƒ7 }d'd&„ |
D ƒ|
dd…< qNW nVy|j|	||d(p¢|}W n: tk
rà } ztd$ |¡|d) d"}W dd}~X Y nX qW |rú|jrúd S dS )*zMain entry point.r   NZ	eradicate)ÚdescriptionÚprogz-iz
--in-placeÚ
store_truez/make changes to files instead of printing diffs)ÚactionÚhelpz-rz--recursivez"drill down directories recursivelyz-az--aggressivez@make more aggressive changes; this may result in false positivesz-ez--errorz"Exit code based on result of checkz	--versionÚversionz	%(prog)s )rz   r|   z--whitelistÚstorez…String of "#" separated comment beginnings to whitelist. Single parts are interpreted as regex. OVERWRITING the default whitelist: {}z--whitelist-extendz›String of "#" separated comment beginnings to whitelist Single parts are interpreted as regex. Overwrites --whitelist. EXTENDING the default whitelist: {} Úfilesú+zfiles to format)Únargsr{   rR   r   TFz{}c                s.   g | ]&}|  d ¡r| d¡stj ˆ |¡‘qS )z.pyÚ.)r=   r!   ÚosÚpathra   )Ú.0Úf)Úrootr8   r9   ú
<listcomp>"  s    
zmain.<locals>.<listcomp>c             S   s   g | ]}|  d ¡s|‘qS )r   )r!   )r„   Údr8   r8   r9   r‡   %  s    )rf   rg   )Úfile)ÚargparseÚArgumentParserrv   Úadd_argumentÚ__version__Úformatr   rp   Ú
parse_argsZwhitelist_extendrr   ÚsplitÚ	whitelistrT   Úsetr~   ÚpopÚ	recursiver‚   rƒ   ÚisdirÚwalkrj   ÚIOErrorr   Úerror)Úargvrg   Zstandard_errorrŠ   Úparserrf   Z
eradicatorÚ	filenamesZchange_or_errorÚnameÚdirectoriesÚchildrenÚ	exceptionr8   )r†   r9   Úmainõ   sP    




r    Ú__main__)rv   Ú
__future__r   r   rc   rF   r‚   Úsysr-   r   r^   ÚAttributeErrorZlib2to3.pgen2Zlib2to3_tokenizer   Úobjectr   r™   ÚstdoutÚstderrr    rs   r8   r8   r8   r9   Ú<module>   s&   
 K=