Your IP : 216.73.216.196


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

B

�s�X�)�@sdZddlmZddlZddlZddlZddlmZyddlZddlm	Z	Wn$e
k
rpddlmZm	Z	YnXdZGdd	�d	e
�ZGd
d�de
�ZGdd
�d
e
�ZGdd�de�ZGdd�de
�Zddd�Zddd�Zdd�Zd dd�Zedk�reejdd��dS)!z� Meager code path measurement tool.
    Ned Batchelder
    http://nedbatchelder.com/blog/200803/python_code_complexity_microtool.html
    MIT License.
�)�with_statementN)�defaultdict)�iter_child_nodes)�astrz0.6.1c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�
ASTVisitorz'Performs a depth-first walk of the AST.cCsd|_i|_dS)N)�node�_cache)�self�r
�7/opt/alt/python37/lib/python3.7/site-packages/mccabe.py�__init__szASTVisitor.__init__cGs&x t|�D]}|j|f|��q
WdS)N)r�dispatch)r	r�args�childr
r
r�defaultszASTVisitor.defaultcGsR||_|j}|j�|�}|dkrD|j}t|jd||j�}||j|<||f|��S)N�visit)r�	__class__r�get�__name__�getattr�visitorr)r	rr�klass�methZ	classNamer
r
rr
!s
zASTVisitor.dispatchcGs"||_|j|_|j|f|��dS)z&Do preorder walk of tree using visitorN)rr
r)r	�treerrr
r
r�preorder+szASTVisitor.preorderN)r�
__module__�__qualname__�__doc__rrr
rr
r
r
rrs

rc@s&eZdZd	dd�Zdd�Zdd�ZdS)
�PathNode�circlecCs||_||_dS)N)�name�look)r	r r!r
r
rr3szPathNode.__init__cCstd|j|j|��f�dS)Nznode [shape=%s,label="%s"] %d;)�printr!r �dot_id)r	r
r
r�to_dot7szPathNode.to_dotcCst|�S)N)�id)r	r
r
rr#;szPathNode.dot_idN)r)rrrrr$r#r
r
r
rr2s
rc@s.eZdZddd�Zdd�Zdd�Zdd	�Zd
S)�	PathGraphrcCs&||_||_||_||_tt�|_dS)N)r �entity�lineno�columnr�list�nodes)r	r r'r(r)r
r
rr@s
zPathGraph.__init__cCs|j|�|�g|j|<dS)N)r+�append)r	Zn1Zn2r
r
r�connectGszPathGraph.connectcCsjtd�x|jD]}|��qWx<|j��D].\}}x$|D]}td|��|��f�q:Wq,Wtd�dS)Nz
subgraph {z	%s -- %s;�})r"r+r$�itemsr#)r	rZnexts�nextr
r
rr$Ls
 zPathGraph.to_dotcCs.tdd�|j��D��}t|j�}||dS)zG Return the McCabe complexity for the graph.
            V-E+2
        cSsg|]}t|��qSr
)�len)�.0�nr
r
r�
<listcomp>Ysz(PathGraph.complexity.<locals>.<listcomp>�)�sumr+�valuesr1)r	Z	num_edgesZ	num_nodesr
r
r�
complexityUs
zPathGraph.complexityN)r)rrrrr-r$r8r
r
r
rr&?s
	r&cs�eZdZdZ�fdd�Zdd�Zdd�Zdd	�ZeZd
d�Z	dd
�Z
dd�Z�fdd�Zdd�Z
e
ZZZdd�Zddd�Zdd�Zdd�ZeZdd�ZeZ�ZS) �PathGraphingAstVisitorz\ A visitor for a parsed Abstract Syntax Tree which finds executable
        statements.
    cs&tt|���d|_i|_|��dS)N�)�superr9r�	classname�graphs�reset)r	)rr
rrcszPathGraphingAstVisitor.__init__cCsd|_d|_dS)N)�graph�tail)r	r
r
rr>iszPathGraphingAstVisitor.resetcCsx|D]}|�|�qWdS)N)r
)r	Z	node_listrr
r
r�
dispatch_listms
z$PathGraphingAstVisitor.dispatch_listcCs�|jrd|j|jf}n|j}d|j|j|f}|jdk	r�|�|�}||_|�|j�t	ddd�}|j�
|j|�|j�
||�||_nNt|||j|j�|_t	|�}||_|�|j�|j|jd|j|jf<|�
�dS)Nz%s%sz	%d:%d: %rr:�point)r!)r<r r(�
col_offsetr?�appendPathNoder@rA�bodyrr-r&r=r>)r	rr'r �pathnode�bottomr
r
r�visitFunctionDefqs$

z'PathGraphingAstVisitor.visitFunctionDefcCs0|j}|j|jd7_|�|j�||_dS)N�.)r<r rArE)r	rZ
old_classnamer
r
r�
visitClassDef�sz$PathGraphingAstVisitor.visitClassDefcCs,|js
dSt|�}|j�|j|�||_|S)N)r@rr?r-)r	r rFr
r
rrD�sz%PathGraphingAstVisitor.appendPathNodecCs,|jdkrd}n|j}d|}|�|�dS)NrzStmt %d)r(rD)r	rr(r r
r
r�visitSimpleStatement�s

z+PathGraphingAstVisitor.visitSimpleStatementcs2t|tj�r|�|�ntt|�j|f|��dS)N)�
isinstancerZstmtrKr;r9r)r	rr)rr
rr�szPathGraphingAstVisitor.defaultcCsd|j}|�||�dS)NzLoop %d)r(�	_subgraph)r	rr r
r
r�	visitLoop�s
z PathGraphingAstVisitor.visitLoopcCsd|j}|�||�dS)NzIf %d)r(rM)r	rr r
r
r�visitIf�s
zPathGraphingAstVisitor.visitIfr
cCsp|jdkrTt|||j|j�|_t|�}|�|||�|j|jd|j|f<|��n|�	|�}|�|||�dS)z?create the subgraphs representing any `if` and `for` statementsNz%s%s)
r?r&r(rCr�_subgraph_parser=r<r>rD)r	rr �extra_blocksrFr
r
rrM�s


z PathGraphingAstVisitor._subgraphcCs�g}||_|�|j�|�|j�x*|D]"}||_|�|j�|�|j�q(W|jrt||_|�|j�|�|j�n
|�|�|r�tddd�}x|D]}|j�||�q�W||_dS)z@parse the body and any `else` block of `if` and `for` statementsr:rB)r!N)r@rArEr,Zorelserr?r-)r	rrFrQZ
loose_ends�extrarG�ler
r
rrP�s$


z&PathGraphingAstVisitor._subgraph_parsecCs d|j}|j|||jd�dS)NzTryExcept %d)rQ)r(rM�handlers)r	rr r
r
r�visitTryExcept�s
z%PathGraphingAstVisitor.visitTryExceptcCs$d|j}|�|�|�|j�dS)NzWith %d)r(rDrArE)r	rr r
r
r�	visitWith�s

z PathGraphingAstVisitor.visitWith)r
)rrrrrr>rArHZvisitAsyncFunctionDefrJrDrKrrNZ
visitAsyncForZvisitForZ
visitWhilerOrMrPrUZvisitTryrVZvisitAsyncWith�
__classcell__r
r
)rrr9^s&

r9c@sLeZdZdZdZeZdZdZdZ	dd�Z
edd	��Zed
d��Z
dd
�ZdS)�
McCabeCheckerz%McCabe cyclomatic complexity checker.�mccabeZC901zC901 %r is too complex (%d)���cCs
||_dS)N)r)r	r�filenamer
r
rr�szMcCabeChecker.__init__cCsbd}dddddd�}t|dd�}t|t�rP|�d	�|j|f|�|j�d
�n|j|f|�dS)Nz--max-complexityrZ�store�intzMcCabe complexity threshold�True)r�action�type�help�parse_from_config�config_optionsrbzmax-complexity)rrLr*�pop�
add_optionrcr,)�cls�parser�flag�kwargsZconfig_optsr
r
r�add_options�s

zMcCabeChecker.add_optionscCst|j�|_dS)N)r]�max_complexity)rf�optionsr
r
r�
parse_optionsszMcCabeChecker.parse_optionsccsr|jdkrdSt�}|�|j|�xJ|j��D]<}|��|jkr.|j|j|��f}|j	|j
|t|�fVq.WdS)Nr)rkr9rrr=r7r8�_error_tmplr'r(r)r`)r	rr?�textr
r
r�runs
zMcCabeChecker.runN)rrrrr �__version__�version�_codernrkr�classmethodrjrmrpr
r
r
rrX�srX��stdinc
Cs�yt||dtj�}Wn4tk
rHt��d}tj�d||f�dSXg}|t_	x2t||��
�D] \}}}}	|�d|||f�qdWt|�dkr�dSt
d�|��t|�S)N�exec�zUnable to parse %s: %s
rz%s:%d:1: %s�
)�compiler�
PyCF_ONLY_AST�SyntaxError�sys�exc_info�stderr�writerXrkrpr,r1r"�join)
�code�	thresholdr[r�eZcomplxr(�offsetro�checkr
r
r�get_code_complexitysr�c	Cs,t|d��}|��}WdQRXt|||d�S)z"Returns the complexity of a module�rUN)r[)�open�readr�)�module_pathr��modr�r
r
r�get_module_complexity$sr�cCs�dtjkrdkr6nnt|d��
}|��SQRXn�dtjkrLdkr�nn�y*t|d��}t�|j�\}}WdQRXWn6ttt	fk
r�t|dd��
}|��SQRXYnXt|d|d��
}|��SQRXdS)	N)r5�)�rr�)�r�rbzlatin-1)�encoding�r)
r}�version_infor�r��tokenize�detect_encoding�readline�LookupErrorr|�UnicodeError)r[�fr��_r
r
r�_read+sr�cCs|dkrtjdd�}t��}|jdddddd�|jdd	d
dddd
�|�|�\}}t|d�}t||ddtj	�}t
�}|�||�|jr�t
d�x.|j��D] }|jr�|��|jkr�|��q�Wt
d�n2x0|j��D]"}|��|jkr�t
|j|���q�WdS)Nrxz-dz--dot�dotzoutput a graphviz dot file�
store_true)�destrar_z-mz--minr�zminimum complexity for outputr])r�rar`rrrwzgraph {r.)r}�argv�optparse�OptionParserre�
parse_argsr�rzrr{r9rr�r"r=r7r�r8r$r )r�Zoparrlrr�rrr?r
r
r�main<s.



r��__main__rx)rurv)ru)N)r�
__future__rr�r}r��collectionsrrr�ImportErrorZflake8.utilrq�objectrrr&r9rXr�r�r�r�rr�r
r
r
r�<module>s.
-