
'![c           @   sy  d  Z  d d l m Z d d l Z d d l m Z m 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 d d l m Z i
 d
 d d f d 6d d d f d 6d d d f d 6d d d f d 6d d d f d 6d d d  f d! 6d" d# d$ f d% 6d& d' d( f d) 6d* d+ d, f d- 6d. d/ d0 f d1 6Z e j d2  Z d3   Z d4 e f d5     YZ d6   Z d S(7   s   check for signs of poor designi(   t   defaultdictN(   t   Ift   BoolOp(   t
   decorators(   t   IAstroidChecker(   t   BaseChecker(   t   utils(   t   check_messagess   Too many ancestors (%s/%s)s   too-many-ancestorss|   Used when class has too many parent classes, try to reduce               this to get a simpler (and so easier to use) class.t   R0901s$   Too many instance attributes (%s/%s)s   too-many-instance-attributess   Used when class has too many instance attributes, try to reduce               this to get a simpler (and so easier to use) class.t   R0902s   Too few public methods (%s/%s)s   too-few-public-methodssZ   Used when class has too few public methods, so be sure it's               really worth it.t   R0903s   Too many public methods (%s/%s)s   too-many-public-methodss|   Used when class has too many public methods, try to reduce               this to get a simpler (and so easier to use) class.t   R0904s"   Too many return statements (%s/%s)s   too-many-return-statementsse   Used when a function or method has too many return statement,               making it hard to follow.t   R0911s   Too many branches (%s/%s)s   too-many-branchess]   Used when a function or method has too many branches,               making it hard to follow.t   R0912s   Too many arguments (%s/%s)s   too-many-argumentss8   Used when a function or method takes too many arguments.t   R0913s    Too many local variables (%s/%s)s   too-many-localss<   Used when a function or method has too many local variables.t   R0914s   Too many statements (%s/%s)s   too-many-statementss~   Used when a function or method has too many statements. You               should then split it in smaller functions / methods.t   R0915s4   Too many boolean expressions in if statement (%s/%s)s   too-many-boolean-expressionss>   Used when a if statement contains too many boolean expressionst   R0916s   ^_{2}[a-z]+_{2}$c         C   sM   d } x@ |  j    D]2 } t | t  r; | t |  7} q | d 7} q W| S(   s   Counts the number of boolean expressions in BoolOp `bool_op` (recursive)

    example: a and (b or c or (d and e)) ==> 5 boolean expressions
    i    i   (   t   get_childrent
   isinstanceR   t   _count_boolean_expressions(   t   bool_opt   nb_bool_exprt	   bool_expr(    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyR   I   s    t   MisdesignCheckerc           B   s  e  Z d  Z e f Z d Z e Z d Z d i d d 6d d 6d d	 6d
 d 6f d i d d 6d d 6d d	 6d d 6f d i d d 6d d 6d d	 6d d 6f d i d d 6d d 6d d	 6d d 6f d i d d 6d d 6d d	 6d d 6f d i d d 6d d 6d d	 6d d 6f d i d d 6d d 6d d	 6d d 6f d i d d 6d d 6d d	 6d  d 6f d! i d" d 6d d 6d d	 6d# d 6f d$ i d d 6d d 6d d	 6d% d 6f f
 Z	 dA d&  Z d'   Z e j d(    Z e d) d* d+ d,  d-    Z e d+ d,  d.    Z e d/ d0 d1 d2 d3 d4  d5    Z e Z e d/ d0 d1 d2 d3  d6    Z e Z d7   Z d8   Z d9   Z d:   Z e d;  d<    Z d=   Z d>   Z e Z d? d@  Z RS(B   s   checks for sign of poor/misdesign:
    * number of methods, attributes, local variables...
    * size, complexity of functions, methods
    t   designis   max-argsi   t   defaultt   intt   types   <int>t   metavars1   Maximum number of arguments for function / methodt   helps
   max-localsi   s3   Maximum number of locals for function / method bodys   max-returnsi   s;   Maximum number of return / yield for function / method bodys   max-branchesi   s3   Maximum number of branch for function / method bodys   max-statementsi2   s6   Maximum number of statements in function / method bodys   max-parentsi   s   <num>s2   Maximum number of parents for a class (see R0901).s   max-attributess5   Maximum number of attributes for a class (see R0902).s   min-public-methodsi   s9   Minimum number of public methods for a class (see R0903).s   max-public-methodsi   s9   Maximum number of public methods for a class (see R0904).s   max-bool-exprs7   Maximum number of boolean expressions in a if statementc         C   s8   t  j |  |  d  |  _ d  |  _ d  |  _ d |  _ d  S(   Ni    (   R   t   __init__t   Nonet   statst   _returnst	   _branchest   _stmts(   t   selft   linter(    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyR      s
    			c         C   s.   |  j  j   |  _ g  |  _ t t  |  _ d S(   s   initialize visit variablesN(   R&   t	   add_statsR!   R"   R    R   R#   (   R%   (    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyt   open   s    	c         C   s   t  j |  d d d  S(   Ns   ignored-argument-namesR   (   R   t   get_global_optionR    (   R%   (    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyt   _ignored_argument_names   s    s   too-many-ancestorss   too-many-instance-attributess   too-few-public-methodss   too-many-public-methodsc         C   s   t  t | j     } | |  j j k rR |  j d d | d | |  j j f n  t  | j  |  j j k r |  j d d | d t  | j  |  j j f n  d S(   sN   check size of inheritance hierarchy and number of instance attributes
        s   too-many-ancestorst   nodet   argss   too-many-instance-attributesN(   t   lent   listt	   ancestorst   configt   max_parentst   add_messaget   instance_attrst   max_attributes(   R%   R+   t
   nb_parents(    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyt   visit_classdef   s    c         C   s  t  d   | j   D  } | |  j j k rV |  j d d | d | |  j j f n  | j d k st t j |  rx d St  d   | j   D  } xB | j   D]4 } t	 j
 | j  r | j d k r | d	 7} q q W| |  j j k  r|  j d
 d | d | |  j j f n  d S(   s   check number of public methodsc         s   s'   |  ] } | j  j d   s d Vq d S(   t   _i   N(   t   namet
   startswith(   t   .0t   method(    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pys	   <genexpr>   s    s   too-many-public-methodsR+   R,   t   classNc         s   s'   |  ] } | j  j d   s d Vq d S(   R7   i   N(   R8   R9   (   R:   R;   (    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pys	   <genexpr>   s    R   i   s   too-few-public-methods(   t   sumt	   mymethodsR0   t   max_public_methodsR2   R   t   checker_utilst   is_enum_classt   methodst   SPECIAL_OBJt   searchR8   t   min_public_methods(   R%   R+   t
   my_methodst   all_methodsR;   (    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyt   leave_classdef   s    
!s   too-many-return-statementss   too-many-branchess   too-many-argumentss   too-many-localss   too-many-statementss   keyword-arg-before-varargc            s  |  j  j d  | j j } |  j   | d k	 r d }   r\ t   f d   | D  } n  t |  | } | |  j j k r |  j	 d d | d t |  |  j j f q n d } t | j
  | } | |  j j k r|  j	 d d | d | |  j j f n  d |  _ d S(	   sd   check function name, docstring, arguments, redefinition,
        variable names, max locals
        i    c         3   s'   |  ] }   j  | j  r d  Vq d S(   i   N(   t   matchR8   (   R:   t   arg(   t   ignored_argument_names(    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pys	   <genexpr>   s    s   too-many-argumentsR+   R,   s   too-many-localsi   N(   R"   t   appendR,   R*   R    R=   R-   R0   t   max_argsR2   t   localst
   max_localsR$   (   R%   R+   R,   t   ignored_args_numt   argnumt   locnum(    (   RK   s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyt   visit_functiondef   s"    	c         C   s   |  j  j   } | |  j j k rI |  j d d | d | |  j j f n  |  j | } | |  j j k r |  j d d | d | |  j j f n  |  j |  j j k r |  j d d | d |  j |  j j f n  d S(   sk   most of the work is done here on close:
        checks for max returns, branch, return in __init__
        s   too-many-return-statementsR+   R,   s   too-many-branchess   too-many-statementsN(	   R"   t   popR0   t   max_returnsR2   R#   t   max_branchesR$   t   max_statements(   R%   R+   t   returnst   branches(    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyt   leave_functiondef  s    c         C   s$   |  j  s d S|  j  d c d 7<d S(   s   count number of returnsNii   (   R"   (   R%   R7   (    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyt   visit_return  s    	c         C   s   | j  r |  j d 7_ n  d S(   sW   default visit method -> increments the statements counter if
        necessary
        i   N(   t   is_statementR$   (   R%   R+   (    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyt   visit_default!  s    	c         C   sH   t  | j  } | j r% | d 7} n  |  j | |  |  j | 7_ d S(   s   increments the branches counteri   N(   R-   t   handlerst   orelset   _inc_branchR$   (   R%   R+   RY   (    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyt   visit_tryexcept(  s
    	c         C   s#   |  j  | d  |  j d 7_ d S(   s   increments the branches counteri   N(   R`   R$   (   R%   R+   (    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyt   visit_tryfinally0  s    s   too-many-boolean-expressionsc         C   sx   |  j  |  d } | j rU t | j  d k sH t | j d t  rU | d 7} n  |  j | |  |  j | 7_ d S(   s>   increments the branches counter and checks boolean expressionsi   i    N(   t   _check_boolean_expressionsR_   R-   R   R   R`   R$   (   R%   R+   RY   (    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyt   visit_if5  s    c         C   sf   | j  } t | t  s d St |  } | |  j j k rb |  j d d | d | |  j j f n  d S(   sw   Go through "if" node `node` and counts its boolean expressions

        if the "if" node test is a BoolOp node
        Ns   too-many-boolean-expressionsR+   R,   (   t   testR   R   R   R0   t   max_bool_exprR2   (   R%   R+   t	   conditionR   (    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyRc   A  s    	c         C   s0   d } | j  r | d 7} n  |  j | |  d S(   s   increments the branches counteri   N(   R_   R`   (   R%   R+   RY   (    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyt   visit_whileN  s    	i   c         C   s   |  j  | j   c | 7<d S(   s   increments the branches counterN(   R#   t   scope(   R%   R+   t   branchesnum(    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyR`   W  s    N(    t   __name__t
   __module__t   __doc__R   t   __implements__R8   t   MSGSt   msgst   priorityt   optionsR    R   R(   R   t   cachedpropertyR*   R   R6   RH   RS   t   visit_asyncfunctiondefRZ   t   leave_asyncfunctiondefR[   R]   Ra   Rb   Rd   Rc   Rh   t	   visit_forR`   (    (    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyR   W   s   	













		%								c         C   s   |  j  t |    d S(   s.   required method to auto register this checker N(   t   register_checkerR   (   R&   (    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyt   register\  s    (   Rm   t   collectionsR    t   ret   astroidR   R   R   t   pylint.interfacesR   t   pylint.checkersR   R   R@   t   pylint.checkers.utilsR   t   pylintRo   t   compileRC   R   R   Rx   (    (    (    s>   lib/python2.7/site-packages/pylint/checkers/design_analysis.pyt   <module>   sZ   








	 