B
    A!p\:  ã               @   s†   d Z ddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZ G dd„ deƒZG d	d
„ d
eƒZG dd„ de	jƒZdd„ ZdS )z/Module to add McCabe checker class for pylint. é    )Úabsolute_import)Ú	PathGraphÚPathGraphingAstVisitor)Úcheckers)Úcheck_messages)ÚHIGHÚIAstroidCheckerc                   s   e Zd Z‡ fdd„Z‡  ZS )r   c                s    t t| ƒjdddd || _d S )NÚ é   )ÚnameZentityÚlineno)Úsuperr   Ú__init__Úroot)ÚselfÚnode)Ú	__class__© ú7lib/python3.7/site-packages/pylint/extensions/mccabe.pyr      s    zPathGraph.__init__)Ú__name__Ú
__module__Ú__qualname__r   Ú__classcell__r   r   )r   r   r      s   r   c                   sª   e Zd Z‡ fdd„Zdd„ Zdd„ Zdd„ ZeZd	d
„ Ze Z	 Z
 Z Z Z Z Z Z Z Z Z Z Z Z Z ZZdd„ ZeZdd„ Zddd„Zdd„ Z‡  ZS )r   c                s   t t| ƒ ¡  d| _d S )Nr   )r   r   r   Ú_bottom_counter)r   )r   r   r   r      s    zPathGraphingAstVisitor.__init__c             G   s&   x |  ¡ D ]}| j|f|žŽ  q
W d S )N)Zget_childrenÚdispatch)r   r   ÚargsÚchildr   r   r   Údefault    s    zPathGraphingAstVisitor.defaultc             G   sR   || _ |j}| j |¡}|d krD|j}t| jd| | jƒ}|| j|< ||f|žŽ S )NZvisit)r   r   Ú_cacheÚgetr   ÚgetattrÚvisitorr   )r   r   r   ÚklassÚmethZ	classNamer   r   r   r   $   s    
zPathGraphingAstVisitor.dispatchc             C   s¤   | j d k	rd|  |¡}|| _|  |j¡ d| j }|  jd7  _| j  | j|¡ | j  ||¡ || _n<t|ƒ| _ || _|  |j¡ | j | jd| j	|j
f < |  ¡  d S )Nz%sr
   z%s%s)ÚgraphÚ_append_nodeÚtailÚdispatch_listÚbodyr   Úconnectr   ÚgraphsÚ	classnamer   Úreset)r   r   ÚpathnodeÚbottomr   r   r   ÚvisitFunctionDef.   s    



z'PathGraphingAstVisitor.visitFunctionDefc             C   s   |   |¡ d S )N)r%   )r   r   r   r   r   ÚvisitSimpleStatementB   s    z+PathGraphingAstVisitor.visitSimpleStatementc             C   s   |   |¡ |  |j¡ d S )N)r%   r'   r(   )r   r   r   r   r   Ú	visitWith_   s    
z PathGraphingAstVisitor.visitWithc             C   s$   | j s
d S | j | j |¡ || _ |S )N)r&   r$   r)   )r   r   r   r   r   r%   e   s
    z#PathGraphingAstVisitor._append_noder   c             C   s^   | j dkrBt|ƒ| _ |  |||¡ | j | jd| j|f < |  ¡  n|  |¡ |  |||¡ dS )z?create the subgraphs representing any `if` and `for` statementsNz%s%s)r$   r   Ú_subgraph_parser*   r+   r,   r%   )r   r   r   Úextra_blocksr   r   r   Ú	_subgraphl   s    



z PathGraphingAstVisitor._subgraphc             C   sÀ   g }|| _ |  |j¡ | | j ¡ x*|D ]"}|| _ |  |j¡ | | j ¡ q(W |jrt|| _ |  |j¡ | | j ¡ n
| |¡ |r¼d| j }|  jd7  _x|D ]}| j ||¡ q W || _ dS )z@parse the body and any `else` block of `if` and `for` statementsz%sr
   N)r&   r'   r(   ÚappendZorelser   r$   r)   )r   r   r-   r3   Z
loose_endsZextrar.   Úler   r   r   r2   x   s&    



z&PathGraphingAstVisitor._subgraph_parse)r   ) r   r   r   r   r   r   r/   ZvisitAsyncFunctionDefr0   ZvisitAssertZvisitAssignZvisitAugAssignZvisitDeleteZ
visitPrintZ
visitRaiseZ
visitYieldZvisitImportZ	visitCallZvisitSubscriptZ	visitPassZvisitContinueZ
visitBreakZvisitGlobalZvisitReturnZ	visitExprZ
visitAwaitr1   ZvisitAsyncWithr%   r4   r2   r   r   r   )r   r   r      s   
D
r   c               @   sD   e Zd ZdZeZdZddiZddddd	d
œffZe	dƒdd„ ƒZ
dS )ÚMcCabeMethodCheckerzoChecks McCabe complexity cyclomatic threshold in methods and functions
    to validate a too complex code.
    ZdesignZR1260)z*%s is too complex. The McCabe rating is %dztoo-complexzSUsed when a method or function is too complex based on McCabe Complexity Cyclomaticzmax-complexityé
   Úintz<int>z&McCabe complexity cyclomatic threshold)r   ÚtypeÚmetavarÚhelpztoo-complexc             C   s’   t ƒ }x|jD ]}| ||¡ qW xj|j ¡ D ]\}| ¡ }|j}t|dƒrVd|j }nd|j	j
 ¡  }|| jjkrtq.| jd|t||fd q.W dS )z‡visit an astroid.Module node to check too complex rating and
        add message if is greather than max_complexity stored from optionsr   z'%s'z	This '%s'ztoo-complex)r   Z
confidencer   N)r   r(   Zpreorderr*   ÚvaluesÚ
complexityr   Úhasattrr   r   r   ÚlowerZconfigZmax_complexityZadd_messager   )r   r   r!   r   r$   r>   Z	node_namer   r   r   Úvisit_module®   s    
z McCabeMethodChecker.visit_moduleN)r   r   r   Ú__doc__r   Z__implements__r   ZmsgsZoptionsr   rA   r   r   r   r   r7   ’   s   r7   c             C   s   |   t| ƒ¡ dS )zRequired method to auto register this checker.

    :param linter: Main interface object for Pylint plugins
    :type linter: Pylint object
    N)Zregister_checkerr7   )Zlinterr   r   r   ÚregisterÃ   s    rC   N)rB   Z
__future__r   Zmccaber   ZMccabe_PathGraphr   ZMccabe_PathGraphingAstVisitorZpylintr   Zpylint.checkers.utilsr   Zpylint.interfacesr   r   ZBaseCheckerr7   rC   r   r   r   r   Ú<module>   s   w1