Your IP : 216.73.216.85


Current Path : /opt/alt/python37/lib/python3.7/site-packages/pylint/checkers/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/pylint/checkers/__pycache__/similar.cpython-37.pyc

B

A!p\�:�@s�dZddlmZddlZddlmZddlmZddlZddl	m
Z
ddlmZddl
mZmZdd	lmZGd
d�d�Zdd
�ZGdd�d�ZddiZdd�ZGdd�dee�Zdd�Zddd�Zddd�Zedkr�e�dS)zGa similarities / code duplication command line tool and pylint checker
�)�print_functionN)�defaultdict)�groupby)�decoding_stream)�IRawChecker)�BaseChecker�table_lines_from_stats)�Tablec@sLeZdZdZddd�Zddd�Zd	d
�Zdd�Zd
d�Zdd�Z	dd�Z
dS)�Similarz,finds copy-pasted lines of code in a project�FcCs"||_||_||_||_g|_dS)N)�	min_lines�ignore_comments�ignore_docstrings�ignore_imports�linesets)�selfrr
rr�r�H/opt/alt/python37/lib/python3.7/site-packages/pylint/checkers/similar.py�__init__$s
zSimilar.__init__NcCsZ|dkr|j}nt||�j}y$|j�t||�|j|j|j��Wntk
rTYnXdS)z(append a file to search for similaritiesN)	�	readlinesrr�append�LineSetr
rr�UnicodeDecodeError)rZstreamid�stream�encodingrrrr�
append_stream1szSimilar.append_streamcCs|�|���dS)z<start looking for similarities and display results on stdoutN)�
_display_sims�
_compute_sims)rrrr�runDszSimilar.runcCs�tt�}x||��D]p\}}}}}||}xX|D]:}||f|ksJ||f|kr.|�||f�|�||f�Pq.W|�||f||fh�qWg}	x0|��D]$\}}
x|
D]}|	�||f�q�Wq�W|	��|	��|	S)z&compute similarities in appended files)r�list�
_iter_sims�addr�items�sort�reverse)rZ
no_duplicates�num�lineset1Zidx1�lineset2Zidx2Z	duplicate�couples�simsZ	ensemblesrrrrHs 

zSimilar._compute_simsc	Cs�d}x�|D]�\}}t�t|dt|�d�t|�}x"|D]\}}td|j|f�q8Wx(|j|||�D]}td|���qjW||t|�d7}q
Wtdd�|jD��}td	|||d
|f�dS)z'display computed similarities on stdoutrzsimilar lines in�filesz==%s:%sz  �cSsg|]}t|��qSr)�len)�.0�linesetrrr�
<listcomp>isz)Similar._display_sims.<locals>.<listcomp>z)TOTAL lines=%s duplicates=%s percent=%.2fgY@N)�printr,�sorted�name�_real_lines�rstrip�sumr)	rr)Znb_lignes_dupliqueesr%r(r.�idx�lineZnb_total_lignesrrrr\s zSimilar._display_simsccs�|j}|j}|j}d}|j}x�|t|�kr�d}d}	x�|||�D]�}
d}x�tt||�||
���D]L\}	\\}}
\}}|
|kr�||kr�|	||||
fVt||	�}P|
r`|d7}q`W|	d7}	||kr�|	||||
fVt||	�}q@W||7}qWdS)z+find similarities in the two given linesetsrr+N)�enumerate_stripped�findrr,�	enumerate�zip�max)rr&r'Zlines1Zlines2r9Zindex1r�skipr%Zindex2Z	non_blank�_Zline1Zline2rrr�_find_commonss0(
zSimilar._find_commonccs\xVt|jdd��D]@\}}x6|j|dd�D] }x|�||�D]
}|VqBWq0WqWdS)zWiterate on similarities among all files, by making a cartesian
        product
        N���r+)r:rr?)rr6r.r'�simrrrr �szSimilar._iter_sims)rFFF)N)�__name__�
__module__�__qualname__�__doc__rrrrrr?r rrrrr
!s

r
cCs�|rBt�d�|��}dd�|jD�}dd�t|dd�d�D�}d	}g}d
}	x�t|dd�D]�\}
}|��}|r�|	s�|�d
�s�|�d�r�|d
d�}	|dd
�}|	r�|�|	�r�d
}	d}|r�|�	|
|�}|r�d}|r�|�
dd�d��}|�|�qXW|S)z\return lines with leading/trailing whitespace and any ignored code
    features removed
    �css&|]}|jt|tjtjf�fVqdS)N)�lineno�
isinstance�astroidZImportZ
ImportFrom)r-�noderrr�	<genexpr>�sz!stripped_lines.<locals>.<genexpr>cSs$i|]\}}tdd�|D��|�qS)css|]\}}|VqdS)Nr)r-r>Z	is_importrrrrK�sz,stripped_lines.<locals>.<dictcomp>.<genexpr>)�all)r-rGZnode_is_import_grouprrr�
<dictcomp>�sz"stripped_lines.<locals>.<dictcomp>cSs|dS)Nrr)�xrrr�<lambda>��z stripped_lines.<locals>.<lambda>)�keyFNr+)�startz"""z'''��#r)rI�parse�join�bodyrr:�strip�
startswith�endswith�get�splitr)�linesr
rrZtreeZnode_is_import_by_linenoZline_begins_importZcurrent_line_is_importZ
strippedlinesZ	docstringrGr7rrr�stripped_lines�s:

r^c@s\eZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	ddd�Z
dd�Zdd�ZdS)rz7Holds and indexes all the lines of a single source fileFcCs*||_||_t||||�|_|��|_dS)N)r2r3r^�_stripped_lines�	_mk_index�_index)rr2r]r
rrrrrr�s
zLineSet.__init__cCs
d|jS)Nz<Lineset for %s>)r2)rrrr�__str__�szLineSet.__str__cCs
t|j�S)N)r,r3)rrrr�__len__�szLineSet.__len__cCs
|j|S)N)r_)r�indexrrr�__getitem__�szLineSet.__getitem__cCs|j|jkS)N)r2)r�otherrrr�__lt__�szLineSet.__lt__cCst|�S)N)�id)rrrr�__hash__�szLineSet.__hash__rccsB|}|r|j|d�}n|j}x|D]}||fV|d7}q$WdS)zgreturn an iterator on stripped lines, starting from a given index
        if specified, else 0
        Nr+)r_)rZstart_atr6r]r7rrrr8�s

zLineSet.enumerate_strippedcCs|j�|d�S)z7return positions of the given stripped line in this setr)rar[)rZ
stripped_linerrrr9�szLineSet.findcCs6tt�}x(t|j�D]\}}|r||�|�qW|S)zcreate the index for this set)rrr:r_r)rrdZline_nor7rrrr`�s
zLineSet._mk_indexN)FFF)r)
rBrCrDrErrbrcrergrir8r9r`rrrrr�s
	
r�R0801)zSimilar lines in %s files
%szduplicate-codez�Indicates that a set of similar lines has been detected among multiple file. This usually means that the code should be refactored to avoid this duplication.cCs6ddddg}|t||d�7}|�t|dddd��d	S)
z/make a layout with some stats about duplicationrF�nowZprevious�
difference)�nb_duplicated_lines�percent_duplicated_linesrr+)�children�colsZrheadersZcheadersN)rrr	)�sect�statsZ	old_statsr]rrr�report_similaritiessrsc	@s�eZdZdZefZdZeZdddddd�fd	d
ddd
d�fdd
dddd�fdddddd�ffZ	dde
ffZd dd�Zd!dd�Z
dd�Zdd�Zdd�ZdS)"�SimilarCheckerz�checks for similarities and duplicated code. This computation may be
    memory / CPU intensive, so you should disable it if you experiment some
    problems.
    Zsimilaritieszmin-similarity-linesr�intz<int>z%Minimum lines number of a similarity.)�default�type�metavar�helpzignore-commentsTZynz<y or n>z,Ignore comments when computing similarities.zignore-docstringsz.Ignore docstrings when computing similarities.zignore-importsFz+Ignore imports when computing similarities.ZRP0801ZDuplicationNcCs(t�||�tj|dddd�d|_dS)NrT)rr
r)rrr
rr)r�linterrrrrNszSimilarChecker.__init__cCsdt�|||||�|dkr&|jj|_n:|dkr:|jj|_n&|dkrN|jj|_n|dkr`|jj|_dS)z�method called to set an option (registered in the options list)

        overridden to report options setting to Similar
        zmin-similarity-lineszignore-commentszignore-docstringszignore-importsN)r�
set_option�configZmin_similarity_linesrr
rr)r�optname�value�action�optdictrrrr{UszSimilarChecker.set_optioncCsg|_|jjddd�|_dS)z<init the checkers: reset linesets and statistics informationr)rmrnN)rrzZ	add_statsrr)rrrr�opendszSimilarChecker.openc	Cs,|���}|�|jj||j�WdQRXdS)z�process a module

        the module's content is accessible via the stream object

        stream must implement the readlines method
        N)rrrzZcurrent_name�
file_encoding)rrJrrrr�process_moduleks
zSimilarChecker.process_modulec
Cs�tdd�|jD��}d}|j}x�|��D]�\}}g}x$|D]\}}|�d|j|f�q:W|��x(|j|||�D]}	|�|	���qvW|j	dt
|�d�|�fd�||t
|�d7}q(W||d	<|o�|d
||d<dS)
zAcompute and display similarities on closing (i.e. end of parsing)css|]}t|�VqdS)N)r,)r-r.rrrrKwsz'SimilarChecker.close.<locals>.<genexpr>rz==%s:%srj�
)�argsr+rmgY@rnN)r5rrrrrr2r#r3r4Zadd_messager,rV)
r�totalZ
duplicatedrrr%r(�msgr.r6r7rrr�closeuszSimilarChecker.close)N)NN)rBrCrDrErZ__implements__r2�MSGSZmsgs�optionsrsZreportsrr{r�r�r�rrrrrts<


rtcCs|�t|��dS)z.required method to auto register this checker N)Zregister_checkerrt)rzrrr�register�sr�cCs$td�t�td�t�|�dS)z&display command line usage informationz*finds copy pasted blocks in a set of filesz~Usage: symilar [-d|--duplicates min_duplicated_lines] [-i|--ignore-comments] [--ignore-docstrings] [--ignore-imports] file1...N)r0�sys�exit)�statusrrr�usage�s
r�c
Cs|dkrtjdd�}ddlm}d}d}d}d}d}d}||||�\}}	xZ|D]R\}
}|
d	krjt|�}qP|
d
krzt�qP|
dkr�d}qP|
d
kr�d}qP|
dkrPd}qPW|	s�td�t||||�}x,|	D]$}
t|
��}|�|
|�WdQRXq�W|��t�	d�dS)z$standalone command line access pointNr+r)�getoptZhdi)ryzduplicates=zignore-commentszignore-importszignore-docstringsrF)z-dz--duplicates)z-hz--help)z-iz--ignore-commentsT)z--ignore-docstrings)z--ignore-imports)
r��argvr�rur�r
r�rrr�)r�r�Zs_optsZl_optsrr
rr�optsr��opt�valrA�filenamerrrr�Run�s:


r��__main__)r)N)rE�
__future__rr��collectionsr�	itertoolsrrIZpylint.utilsrZpylint.interfacesrZpylint.checkersrrZpylint.reporters.ureports.nodesr	r
r^rr�rsrtr�r�r�rBrrrr�<module>s*z,=	
p

(