o
    b8                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddd	d
ddddZ
h dZdddZG dd dejZdd Zdd Zdd ZdS )z"checker for use of Python logging
    N)checkers)
interfaces)utils)check_messages)z>Specify string format arguments as logging function parameterslogging-not-lazya  Used when a logging statement has a call form of "logging.<logging method>(format_string % (format_args...))". Such calls should leave string interpolation to the logging method itself and be written "logging.<logging method>(format_string, format_args...)" so that the program may avoid incurring the cost of the interpolation in those cases in which no message will be logged. For more, see http://www.python.org/dev/peps/pep-0282/.)LUse % formatting in logging functions and pass the % parameters as argumentslogging-format-interpolationzUsed when a logging statement has a call form of "logging.<logging method>(format_string.format(format_args...))". Such calls should use % formatting instead, but leave interpolation to the logging function by passing the parameters as arguments.)r   logging-fstring-interpolationzUsed when a logging statement has a call form of "logging.method(f"..."))". Such calls should use % formatting instead, but leave interpolation to the logging function by passing the parameters as arguments.)z;Unsupported logging format character %r (%#02x) at index %dlogging-unsupported-formatzWUsed when an unsupported format character is used in a logging statement format string.)z<Logging format string ends in middle of conversion specifierlogging-format-truncatedz`Used when a logging statement format string terminates before the end of a conversion specifier.)z,Too many arguments for logging format stringlogging-too-many-argsz>Used when a logging format string is given too many arguments.)z.Not enough arguments for logging format stringlogging-too-few-argsz=Used when a logging format string is given too few arguments.)ZW1201ZW1202ZW1203ZE1200ZE1201ZE1205ZE1206>   infowarndebugerrorfatalwarningcritical	exception c                 C   s@   t | tjot | jtjo|r| jj|v ndo|r| j|v S dS )a  Determines if a BoundMethod node represents a method call.

    Args:
      func (astroid.BoundMethod): The BoundMethod AST node to check.
      types (Optional[String]): Optional sequence of caller type names to restrict check.
      methods (Optional[String]): Optional sequence of method names to restrict check.

    Returns:
      bool: true if the node represents a method call for the given type and
      method names, False otherwise.
    T)
isinstanceastroidBoundMethodboundInstancename)functypesmethodsr   r   V/var/www/secure340b-portal/env/lib/python3.10/site-packages/pylint/checkers/logging.pyis_method_calle   s   r!   c                   @   s   e Zd ZdZejZdZeZ	ddddddfd	d
ddd
dgddffZ
dd Zdd Zdd Zee dd Zdd Zedd Zdd Zdd Zd S )!LoggingCheckerz!Checks use of the logging module.loggingzlogging-modules)r#   csvz<comma separated list>zcLogging modules to check that the string format arguments are in logging function parameter format.)defaulttypemetavarhelpzlogging-format-styleoldchoicez<old (%) or new ({)>newzuFormat style used to check logging format string. `old` means using % formatting, while `new` is for `{}` formatting.)r%   r&   r'   choicesr(   c                 C   sb   t  | _| jj}| jj| _t || _i | _|D ]}|dd}t	|dkr.|d | j|d < qdS )z?Clears any state left in this checker from last module checked..   r   N)
set_logging_namesconfigZlogging_modulesZlogging_format_style_format_style_logging_modules_from_importsrsplitlen)selfnodeZlogging_modsZlogging_modpartsr   r   r    visit_module   s   

zLoggingChecker.visit_modulec                 C   sP   z| j |j }|jD ]\}}||kr| j|p| q
W dS  ty'   Y dS w )z;Checks to see if a module uses a non-Python logging module.N)r4   modnamenamesr0   addKeyError)r7   r8   Zlogging_namemoduleas_namer   r   r    visit_importfrom   s   zLoggingChecker.visit_importfromc                 C   s.   |j D ]\}}|| jv r| j|p| qdS )z<Checks to see if this module uses Python's built-in logging.N)r<   r3   r0   r=   )r7   r8   r?   r@   r   r   r    visit_import   s
   
zLoggingChecker.visit_importc                    sL    fdd} fdd}| r j j}n	| \}}|sdS  | dS )z Checks calls to logging methods.c                      s.   t  jtjot  jjtjo jjjjv S N)r   r   r   	AttributeexprNamer   r0   r   r8   r7   r   r    is_logging_name   s
   z2LoggingChecker.visit_call.<locals>.is_logging_namec                     s   z5 j  D ],} t| tjr2| jj}t|tjr2| dks)t	dd |
 D r2d| jjf  W S qW dS  tjjyA   Y dS w )Nlogging.Loggerc                 s   s    | ]	}|  d kV  qdS )rI   N)qname).0Zancestorr   r   r    	<genexpr>   s
    

zELoggingChecker.visit_call.<locals>.is_logger_class.<locals>.<genexpr>T)FN)r   Zinferr   r   r   Z_proxiedparentClassDefrJ   anyZ	ancestorsr   
exceptionsZInferenceError)inferredrM   r8   r   r    is_logger_class   s&   
z2LoggingChecker.visit_call.<locals>.is_logger_classN)r   attrname_check_log_method)r7   r8   rH   rS   r   resultr   rG   r    
visit_call   s   

zLoggingChecker.visit_callc                    s:  |dkr|j s|jst|jdk rdS d}n|tv r(|j s#|js#|js%dS d}ndS t|j| tjrb|j| }|jdk}|jdkrUt	 fdd	|j
|jfD }|dk}|r` jd
|d dS dS t|j| tjru |j|  dS t|j| tjr || dS t|j| tjtjfr jd|d dS dS )z9Checks calls to logging.log(level, format, *format_args).log   Nr.   r   %+c                 3   s$    | ]}  t|rd V  qdS r.   N)_is_operand_literal_strr   
safe_infer)rK   operandr7   r   r    rL      s    
z3LoggingChecker._check_log_method.<locals>.<genexpr>r   rR   r	   )Zstarargskwargsr6   argsCHECKED_CONVENIENCE_FUNCTIONSr   r   BinOpopsumleftrightadd_messageCall_check_call_funcConst_check_format_stringFormattedValue	JoinedStr)r7   r8   r   Z
format_posbinopemitZtotal_number_of_stringsr   r`   r    rU      s:   



z LoggingChecker._check_log_methodc                 C   s   t | tjo
| jdkS )zL
        Return True if the operand in argument is a literal string
        str)r   r   rl   r   )r_   r   r   r    r]     s   z&LoggingChecker._is_operand_literal_strc                 C   sD   t |j}d}d}t|||rt|js | jd|d dS dS dS )zChecks that function call is not format_string.format().

        Args:
          node (astroid.node_classes.Call):
            Call AST node to be checked.
        )rr   unicode)formatr   rR   N)r   r^   r   r!   is_complex_format_strr   ri   )r7   r8   r   r   r   r   r   r    rk   	  s   zLoggingChecker._check_call_funcc              
   C   sL  t |j|d d }|sdS |j| j}t|tsd}nmz4| jdkr2t|\}}}}|r1W dS n| jdkrPt|\}}	}
t	t
dd |D }||	 |
 }W n8 tjyx } z||j }| jd||t||jfd	 W Y d}~dS d}~w tjy   | jd
|d Y dS w ||kr| jd|d dS ||k r| jd|d dS dS )zChecks that format string tokens match the supplied arguments.

        Args:
          node (astroid.node_classes.NodeNG): AST node to be checked.
          format_arg (int): Index of the format string in the node arguments.
        r.   Nr   r)   r+   c                 s   s"    | ]\}}t |ts|V  qd S rC   )r   int)rK   klr   r   r    rL   9  s     z6LoggingChecker._check_format_string.<locals>.<genexpr>r
   )r8   rb   r   rR   r   r   )_count_supplied_tokensrb   valuer   rr   r2   r   Zparse_format_stringZparse_format_method_stringr6   r/   ZUnsupportedFormatCharacterindexri   ordZIncompleteFormatString)r7   r8   Z
format_argnum_argsformat_stringZrequired_num_argsZkeyword_args_Zkeyword_argumentsZimplicit_pos_argsZexplicit_pos_argsZkeyword_args_cntexcharr   r   r    rm     sT   





z#LoggingChecker._check_format_stringN)__name__
__module____qualname____doc__r   ZIAstroidCheckerZ__implements__r   MSGSZmsgsoptionsr:   rA   rB   r   rW   rU   staticmethodr]   rk   rm   r   r   r   r    r"   y   s>    

#&
r"   c                 C   sp   t | }|du st|jtsdS ztt |j}W n
 t	y'   Y dS w |D ]\}}}}|r5 dS q*dS )zChecks if node represents a string with complex formatting specs.

    Args:
        node (astroid.node_classes.NodeNG): AST node to check
    Returns:
        bool: True if inferred string uses complex formatting, False otherwise
    NTF)
r   r^   r   rz   rr   liststring	Formatterparse
ValueError)r8   rQ   parsedr   format_specr   r   r    ru   O  s   
ru   c                 C   s   t dd | D S )a|  Counts the number of tokens in an args list.

    The Python log functions allow for special keyword arguments: func,
    exc_info and extra. To handle these cases correctly, we only count
    arguments that aren't keywords.

    Args:
      args (list): AST nodes that are arguments for a log format string.

    Returns:
      int: Number of AST nodes that aren't keywords.
    c                 s   s     | ]}t |tjsd V  qdS r\   )r   r   Keyword)rK   argr   r   r    rL   r  s    z)_count_supplied_tokens.<locals>.<genexpr>)rf   )rb   r   r   r    ry   e  s   ry   c                 C   s   |  t|  dS )z.Required method to auto-register this checker.N)Zregister_checkerr"   )Zlinterr   r   r    registeru  s   r   )r   r   )r   r   r   Zpylintr   r   Zpylint.checkersr   Zpylint.checkers.utilsr   r   rc   r!   ZBaseCheckerr"   ru   ry   r   r   r   r   r    <module>   s,   

;
 W