3
tûœ]ë  ã               @   s‚   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
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. é    )Ú	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Ú é   )ÚnameÚentityÚlineno)Úsuperr   Ú__init__Úroot)ÚselfÚnode)Ú	__class__© ú:/tmp/pip-build-fn42xldr/pylint/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f fdd„Zdd„ Z‡  ZS )r   c                s   t t| ƒjƒ  d| _d S )Nr   )r   r   r   Ú_bottom_counter)r   )r   r   r   r      s    zPathGraphingAstVisitor.__init__c             G   s&   x |j ƒ 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j|ƒ}|d krD|j}t| jd| | jƒ}|| j|< ||f|žŽ S )NÚvisit)r   r   Ú_cacheÚgetr   ÚgetattrÚvisitorr   )r   r   r   ÚklassÚmethÚ
class_namer   r   r   r   !   s    
zPathGraphingAstVisitor.dispatchc             C   s¤   | j d k	rd| j|ƒ}|| _| j|jƒ d| j }|  jd7  _| j j| j|ƒ | j j||ƒ || _n<t|ƒ| _ || _| j|jƒ | j | jd| j	|j
f < | jƒ  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   | j |ƒ d S )N)r'   )r   r   r   r   r   ÚvisitSimpleStatement?   s    z+PathGraphingAstVisitor.visitSimpleStatementc             C   s   | j |ƒ | j|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| j |ƒ || _ |S )N)r(   r&   r+   )r   r   r   r   r   r'   b   s
    z#PathGraphingAstVisitor._append_nodec             C   s^   | j dkrBt|ƒ| _ | j|||ƒ | j | jd| j|f < | jƒ  n| j|ƒ | j|||ƒ 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   Ú	_subgraphi   s    



z PathGraphingAstVisitor._subgraphc             C   sÀ   g }|| _ | j|jƒ |j| j ƒ x*|D ]"}|| _ | j|jƒ |j| j ƒ q(W |jrt|| _ | j|jƒ |j| j ƒ n
|j|ƒ |r¼d| j }|  jd7  _x|D ]}| j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/   r5   Z
loose_endsÚextrar0   Úendr   r   r   r4   u   s&    



z&PathGraphingAstVisitor._subgraph_parse) r   r   r   r   r   r   r1   ZvisitAsyncFunctionDefr2   ZvisitAssertZvisitAssignZvisitAugAssignZvisitDeleteZ
visitPrintZ
visitRaiseZ
visitYieldZvisitImportZ	visitCallZvisitSubscriptZ	visitPassZvisitContinueZ
visitBreakZvisitGlobalZvisitReturnZ	visitExprZ
visitAwaitr3   ZvisitAsyncWithr'   r6   r4   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ú*%s is too complex. The McCabe rating is %dútoo-complexúSUsed 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Úhelpc             C   s’   t ƒ }x|jD ]}|j||ƒ qW xj|jjƒ D ]\}|jƒ }|j}t|dƒrVd|j }nd|j	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   Ú
confidencer   N)r   r*   Zpreorderr,   ÚvaluesÚ
complexityr   Úhasattrr
   r   r   ÚlowerÚconfigZmax_complexityZadd_messager   )r   r   r"   r   r&   rE   Z	node_namer   r   r   Úvisit_module©   s    
z McCabeMethodChecker.visit_moduleN)r;   r<   r=   )r   r   r   Ú__doc__r   Z__implements__r
   ZmsgsÚoptionsr   rI   r   r   r   r   r:   �   s     r:   c             C   s   | j t| ƒƒ dS )z�Required method to auto register this checker.

    :param linter: Main interface object for Pylint plugins
    :type linter: Pylint object
    N)Zregister_checkerr:   )Zlinterr   r   r   Úregister¾   s    rL   N)rJ   Zmccaber   ZMccabe_PathGraphr   ZMccabe_PathGraphingAstVisitorZpylintr   Zpylint.checkers.utilsr   Zpylint.interfacesr   r   ZBaseCheckerr:   rL   r   r   r   r   Ú<module>   s   u1