B
    rw\                 @   s  d Z ddlmZ ddlZddlZddlmZ ddlZddlm	Z	m
Z
mZmZmZmZmZ ddlZddlZddlZddlZddlZddlmZmZ ddlmZ ddlmZmZmZmZ dd	lmZ dd
l m!Z!m"Z"m#Z# dddddddZ$dd e$% D Z&dddddddZ'dZ(dZ)dZ*dZ+e,dZ-dZ.G dd dZ/e0ddd d!d"d#d$d%d&d'd(d)d*gZ1G d+d, d,e1Z2d-d. Z3d/d0 Z4d1d2 Z5dhd4d5Z6d6d7 Z7d8d9 Z8G d:d; d;Z9G d<d= d=Z:G d>d? d?Z;G d@dA dAZ<G dBdC dCZ=dDdE Z>dFdG Z?dHdI Z@G dJdK dKZAdLZBdMdN ZCdidOdPZDdjdQdRZEdkdTdUZFdVdW ZGdldZd[ZHd\d] ZId^d_ ZJd`da ZKdmdbdcZLddde ZMeLZNdndfdgZOdS )ozVsome various utilities and helper classes, most of them used in the
main pylint class
    )print_functionN)cleandoc)dirnamebasenamesplitextexistsisdirjoinnormpath)nodesModule)modutils)IRawCheckerITokenChecker	UNDEFINED
implements)Section)InvalidMessageErrorUnknownMessageErrorEmptyReportErrorinfoZ
conventionZrefactorwarningerrorfatal)ICRWEFc             C   s   i | ]\}}||qS  r    ).0kvr    r    +lib/python3.7/site-packages/pylint/utils.py
<dictcomp>H   s    r%                  ZEWRCIFz#\s*#.*\bpylint:\s*([^;#]+)[;#]{0,1}ZFRc               @   s   e Zd ZdZdZdS )WarningScopezline-based-msgznode-based-msgN)__name__
__module____qualname__LINENODEr    r    r    r$   r+   Z   s   r+   _MsgBasemsg_idsymbolmsgr   category
confidenceabspathpathmoduleobjlinecolumnc               @   s    e Zd ZdZdd Zdd ZdS )Messagez<This class represent a message to be issued by the reportersc          	   C   s(   t j| ||||d t|d  |f| S )Nr   )r1   __new__	MSG_TYPES)clsr2   r3   locationr4   r6   r    r    r$   r>   u   s    
zMessage.__new__c             C   s   |j f tt| j| S )zFormat the message according to the given template.

        The template format is the one of the format method :
        cf. http://docs.python.org/2/library/string.html#formatstrings
        )formatdictzip_fields)selftemplater    r    r$   rB      s    zMessage.formatN)r,   r-   r.   __doc__r>   rB   r    r    r    r$   r=   r   s   r=   c             C   s~   |   }dg  }}xT|rft|tr*|j}n|t|dd y|j  }W q tk
rb   d}Y qX qW |  |d	|fS )z5return the module name and the frame id in the module namez<lambda>N.)
frame
isinstancer   rJ   appendgetattrparentAttributeErrorreverser	   )noderL   r9   r:   r    r    r$   get_module_and_frameid   s    

rT   c             C   s   |   } | tkr| S t| S )N)upperr?   MSG_TYPES_LONGget)cidr    r    r$   category_id   s    rY   c             O   sF   y| j |pt f||S  tk
r@   | j t f||S X dS )zAreturn decoded line from encoding or decode with default encodingN)decodesysgetdefaultencodingLookupError)r;   encodingargskwargsr    r    r$   safe_decode   s    ra   strictc             C   sD   yt |pt }W n" tk
r8   t t }Y nX || |S )N)codecs	getreaderr[   r\   r]   )streamr^   errorsZ
reader_clsr    r    r$   decoding_stream   s
    rg   c          	   C   s(   |   }|j}tt|S Q R X d S )N)re   readlinelisttokenize)r9   re   rh   r    r    r$   tokenize_module   s    
rk   c             C   s   t | ttfrtj}ntj}i }t|dkr:|\}}}}n4t|dkrR|\}}}n|\}}td| t	 d }|
d| t| ||||f|S )N   r)   zG[pylint 0.26] description of message %s doesn't include a symbolic namescope)r   r   r   r+   r/   r0   lenwarningswarnDeprecationWarning
setdefaultMessageDefinition)checkermsgid	msg_tupleZdefault_scopeoptionsr4   r3   descrr    r    r$   build_message_def   s     ry   c               @   s0   e Zd ZdddZdd Zdd Zdd	d
ZdS )rs   Nc
       
      C   sv   || _ t|dkrtd| |d tkr>td|d |f || _|| _|| _|| _|| _|| _	|| _
|	png | _d S )N   zInvalid message id %rr   zBad message type %s in %r)rt   rn   r   r?   ru   r4   rx   r3   rm   
minversion
maxversion	old_names)
rF   rt   ru   r4   rx   r3   rm   r{   r|   r}   r    r    r$   __init__   s    zMessageDefinition.__init__c             C   s   d | jS )NzMessageDefinition:{})rB   __dict__)rF   r    r    r$   __repr__   s    zMessageDefinition.__repr__c             C   s8   | j dk	r| j tjkrdS | jdk	r4| jtjkr4dS dS )zCreturn True if message may be emitted using the current interpreterNFT)r{   r[   version_infor|   )rF   r    r    r$   may_be_emitted   s
    z MessageDefinition.may_be_emittedFc             C   s  | j }|r|d| jj 7 }| j}| jr8d| j| jf }n| j}| jsJ| jrg }| jrt|dd	dd | jD   | jr|dd	dd | jD   d		|}|r|d
| 7 }n|d| 7 }t
d	| dd}|dkr| d }d||d|f S d||f S )z/return the help string for the given message idz( This message belongs to the %s checker.z%s (%s)z< %srK   c             S   s   g | ]}t |qS r    )str)r!   nr    r    r$   
<listcomp>  s    z1MessageDefinition.format_help.<locals>.<listcomp>z>= %sc             S   s   g | ]}t |qS r    )r   )r!   r   r    r    r$   r     s    z or z* It can't be emitted when using Python %s.z4 This message can't be emitted when using Python %s. z  )indentz%sr   z:%s: *%s*
%sz:%s:
%s)rx   rt   rJ   r4   r3   ru   r{   r|   rN   r	   _normalize_textsplit
splitlinesrstrip)rF   
checkerrefZdesctitleru   Zrestrr    r    r$   format_help   s,      

zMessageDefinition.format_help)NNN)F)r,   r-   r.   r~   r   r   r   r    r    r    r$   rs      s   	  
rs   c               @   s   e Zd ZdZg Zdd Zdd Zedd Zedd	 Z	d(ddZ
d)ddZd*ddZd+ddZdd Zdd ZdefddZd,ddZdd Zdddedfd d!Zd"d# Zd-d$d%Zed.d&d'ZdS )/MessagesHandlerMixInz[a mix-in class containing all the messages related methods for the main
    lint class
    c             C   s   i | _ d| _d S )Nr   )_msgs_state
msg_status)rF   r    r    r$   r~     s    zMessagesHandlerMixIn.__init__c             c   s2   x,| j |  D ]}x|jD ]
}|V  qW qW d S )N)	_checkerslowermsgs)rF   rt   Zknown_checkerru   r    r    r$   _checker_messages   s    z&MessagesHandlerMixIn._checker_messagesc             C   s   | j   d S )N))_MessagesHandlerMixIn__by_id_managed_msgsclear)r@   r    r    r$   clear_by_id_managed_msgs%  s    z-MessagesHandlerMixIn.clear_by_id_managed_msgsc             C   s   | j S )N)r   )r@   r    r    r$   get_by_id_managed_msgs)  s    z+MessagesHandlerMixIn.get_by_id_managed_msgsTc             C   s^   yD| j |}x2|D ]*}||jkrtj| j|j|j||f qW W n tk
rX   Y nX dS )zuIf the msgid is a numeric one, then register it to inform the user
        it could furnish instead a symbolic msgid.N)	
msgs_storeget_message_definitionsru   r   r   rN   current_namer3   r   )rF   ru   r;   is_disabledmessage_definitionsmessage_definitionr    r    r$   _register_by_id_managed_msg-  s    

z0MessagesHandlerMixIn._register_by_id_managed_msgpackageNFc             C   s$   | j |d|||d | || dS )z$don't output message of the given idF)enablerm   r;   ignore_unknownN)_set_msg_statusr   )rF   ru   rm   r;   r   r    r    r$   disable@  s    zMessagesHandlerMixIn.disablec             C   s(   | j |d|||d | j||dd dS )z reenable message of the given idT)r   rm   r;   r   F)r   N)r   r   )rF   ru   rm   r;   r   r    r    r$   r   G  s    zMessagesHandlerMixIn.enablec             C   sj  |dkst |dkrLxtD ]}| ||||| qW |rH| jsH| d d S t|}|d k	rx&| jj|D ]}| |||| qlW d S |	 | j
kr| j}x@| j
|	  D ].}	x(|	jD ]}||jkr| |||| qW qW d S |	 dr|r| | n
| | d S y| j|}
W n" tk
rD   |r>d S  Y nX x|
D ]}| |||| qLW d S )N)r   r9   allZpython3Zrp)AssertionErrorr?   r   Z_python3_porting_moder   rY   r   _msgs_by_categoryrW   r   r   r   _alternative_names
startswithenable_reportdisable_reportr   r   _set_one_msg_status)rF   ru   r   rm   r;   r   Z_msgidZcatidr   rt   r   r   r    r    r$   r   N  s@    





z$MessagesHandlerMixIn._set_msg_statusc                s   |dkr@ j ||| |s|jdkr jd||j|jfd nL j}|||j<  fddt| D  j_	 fddt| D  j_
d S )Nr9   zlocally-disabled)r;   r_   c                s   g | ]\}}|r  |qS r    )_message_symbol)r!   midval)rF   r    r$   r     s    z<MessagesHandlerMixIn._set_one_msg_status.<locals>.<listcomp>c                s   g | ]\}}|s  |qS r    )r   )r!   r   r   )rF   r    r$   r     s   )
file_stateset_msg_statusr3   add_messageru   r   sorteditemsconfigr   r   )rF   rm   r4   r;   r   r   r    )rF   r$   r   }  s    

z(MessagesHandlerMixIn._set_one_msg_statusc             C   s0   ydd | j |D S  tk
r*   |S X dS )zGet the message symbol of the given message id

        Return the original message id if the message does not
        exist.
        c             S   s   g | ]
}|j qS r    )r3   )r!   mdr    r    r$   r     s    z8MessagesHandlerMixIn._message_symbol.<locals>.<listcomp>N)r   r   r   )rF   ru   r    r    r$   r     s    z$MessagesHandlerMixIn._message_symbolc          	   C   sP   | j jr|j| j jkrtS y|| jj| kr0tS W n ttfk
rJ   t	S X dS )z:Returns the scope at which a message was enabled/disabled.N)
r   r6   rJ   MSG_STATE_CONFIDENCEr   _module_msgs_stateMSG_STATE_SCOPE_MODULEKeyError	TypeErrorMSG_STATE_SCOPE_CONFIG)rF   ru   r;   r6   r    r    r$   get_message_state_scope  s    z,MessagesHandlerMixIn.get_message_state_scopec             C   sz   | j jr|r|j| j jkrdS y| j|}dd |D }W n tk
rV   |g}Y nX x|D ]}| ||r^dS q^W dS )zreturn true if the message associated to the given message id is
        enabled

        msgid may be either a numeric or symbolic message id.
        Fc             S   s   g | ]
}|j qS r    )ru   )r!   r   r    r    r$   r     s    z;MessagesHandlerMixIn.is_message_enabled.<locals>.<listcomp>T)r   r6   rJ   r   r   r   is_one_message_enabled)rF   	msg_descrr;   r6   r   msgidsru   r    r    r$   is_message_enabled  s    
z'MessagesHandlerMixIn.is_message_enabledc       	         s    d kr| j |dS y| jj|   S  tk
r   | j }|r |krd}| jj|i }t fdd| D }t	|d\}}|d k	r|}| j ||S | j |dS X d S )NTc                s    g | ]\}}| kr||fqS r    r    )r!   Zmessage_liner   )r;   r    r$   r     s   z?MessagesHandlerMixIn.is_one_message_enabled.<locals>.<listcomp>)NN)
r   rW   r   r   r   get_effective_max_line_number_raw_module_msgs_statereversedr   next)	rF   ru   r;   Zmax_line_numberZfallbacklinesZclosest_lines	last_lineZ
is_enabledr    )r;   r$   r     s"    

z+MessagesHandlerMixIn.is_one_message_enabledc       	   	   C   s2   | j |}x |D ]}| |||||| qW dS )a,  Adds a message given by ID or name.

        If provided, the message string is expanded using args.

        AST checkers must provide the node argument (but may optionally
        provide line if the line number is different), raw and token checkers
        must provide the line argument.
        N)r   r   add_one_message)	rF   r   r;   rS   r_   r6   
col_offsetr   r   r    r    r$   r     s    
z MessagesHandlerMixIn.add_messagec             C   s  |j }|jp|}|d tkrx|jtjkrX|d kr<td| |d k	rxtd|||f n |jtjkrx|d krxtd| |d kr|d k	r|j}|d krt	|dr|j
}| |||s| j| |||||||| d S t|d  }	|  jt|d  O  _| j|	  d7  < | jd | j |	  d7  < y| jd |  d7  < W n$ tk
rf   d| jd |< Y nX |j}
|r||
|; }
|d kr| jd	 }}| j}nt|\}}| j}|| jjd	d}| jt|||||||pd|pdf|
| d S )
Nr   z&Message %s must provide line, got Nonez7Message %s must only provide line, got line=%s, node=%sz&Message %s must provide Node, got Noner   r*   Z	by_moduleZby_msgrI   )ru   r3   _SCOPE_EXEMPTrm   r+   r/   r   r0   
fromlinenohasattrr   r   r   handle_ignored_messager   r?   r   MSG_TYPES_STATUSstatsr   r   r4   Zcurrent_filerT   rootfilereplaceZreporterZpath_strip_prefixZhandle_messager=   )rF   r   r;   rS   r_   r6   r   ru   r3   Zmsg_catr4   r9   r:   r7   r8   r    r    r$   r     sh    




z$MessagesHandlerMixIn.add_one_messagec       	   	   C   s  |s
t j}td|d td|d td|d td|d td|d i }x|  D ]}|jdkr|jrx| D ]Z\}}|dkrd}nd	|  }t||d td
t| |d t	|d| td|d qvW qV|j}yF|| d  |
 7  < || d |j || d  |j7  < W qV tk
r\   t|
 t|jt|jd||< Y qVX qVW td|d td|d td|d td|d td|d td|d td|d td|d td|d td|d td|d x*t| D ]\}}| j|||d qW dS )z*output a full documentation in ReST formatz"Pylint global options and switches)r   z"----------------------------------rI   z,Pylint provides global options and switches.ZmasterNzGeneral optionsz
%s options~rw   r   reports)rw   r   r   z%Pylint checkers' options and switchesz%-------------------------------------z2Pylint checkers can provide three set of features:z'* options that control their execution,z* messages that they can raise,z!* reports that they can generate.z3Below is a list of all checkers and their features.)re   )r[   stdoutprintZget_checkersrJ   rw   Zoptions_by_section
capitalizern   _rest_format_sectionZoptions_and_valuesupdater   r   r   ri   rC   r   r   _print_checker_doc)	rF   re   Z
by_checkerrt   sectionrw   r   rJ   r   r    r    r$   print_full_documentation?  sR    

z-MessagesHandlerMixIn.print_full_documentationc             C   sH  |s
t j}|d}|d}|d}|d}|d}d| dd  }|rdtd	| |d
 t||d
 tdt| |d
 td|d
 |rtd| |d
 td|  |d
 td|d
 |rd|}	t|	|d
 tdt|	 |d
 tt||d
 td|d
 |rTd|}	t|	|d
 tdt|	 |d
 t	|d| td|d
 |rd|}	t|	|d
 tdt|	 |d
 xBt
| dd dD ]*\}
}t| |
|}t|jdd|d
 qW td|d
 |r8d|}	t|	|d
 tdt|	 |d
 x&|D ]}td|dd  |d
 q
W td|d
 td|d
 dS )ziHelper method for print_full_documentation.

        Also used by doc/exts/pylint_extensions.py.
        docr9   r   rw   r   z
%s checker_r   z.. _%s:
)r   r   rI   z#This checker is provided by ``%s``.z'Verbatim name of the checker is ``%s``.z{} Documentation^z
{} OptionsNz{} Messagesc             S   s   t | d d | d fS )Nr   r*   )
_MSG_ORDERindex)Zkvr    r    r$   <lambda>  s    z9MessagesHandlerMixIn._print_checker_doc.<locals>.<lambda>)keyF)r   z
{} Reportsz:%s: %sr)   )r[   r   rW   r   r   r   rn   rB   r   r   r   r   ry   r   )Zchecker_namer   re   r   r9   r   rw   r   Zchecker_titler   ru   r4   Zreportr    r    r$   r   s  s\    









z'MessagesHandlerMixIn._print_checker_doc)T)r   NF)r   NF)r   NF)NN)N)N)r,   r-   r.   rH   r   r~   r   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   staticmethodr   r    r    r    r$   r     s0   



.
"L
4r   c               @   sJ   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )	FileStatez;Hold internal state specific to the currently analyzed fileNc             C   s.   || _ i | _i | _tt| _i | _d | _d S )N)		base_namer   r   collectionsdefaultdictset_ignored_msgs_suppression_mapping_effective_max_line_number)rF   modnamer    r    r$   r~     s    zFileState.__init__c             C   sV   x$| j  D ]\}}| | j|< qW | j  }i | _ i | _|j| _| ||| dS )z9Walk the AST to collect block level options line numbers.N)r   r   copyr   r   tolinenor   _collect_block_lines)rF   r   Zmodule_noder4   r   Z
orig_stater    r    r$   collect_block_lines  s    
zFileState.collect_block_linesc             C   s  x|  D ]}| ||| q
W |j}|j}t|tjtjtjfrV|j	rV|j	d j}n|}x&|
 D ]\}}	xt|	
 D ]\}
}|
}||
ks||
k rq||}x<|D ]4}|jtjkr|
|krd}||
\}}q|
}|}qW xt||d D ]|}|| j|dkrq||	kr&|	| }|}|s:|| j||f< y|| j| |< W q tk
rp   ||i| j|< Y qX qW |	|
= qW qfW dS )z`Recursively walk (depth first) AST to collect block level options
        line numbers.
        r   Tr*   r    N)get_childrenr   r   r   rM   r   r   ZClassDefZFunctionDefZbodyr   ri   r   rm   r+   r0   Zblock_rangeranger   rW   r   r   )rF   r   rS   Z	msg_statechildfirstZlastZfirstchildlinenoru   r   linenostateZoriginal_linenor   r   Zfirst_Zlast_r;   r    r    r$   r     sD    


zFileState._collect_block_linesc             C   sJ   |dkst y|| j|j |< W n$ tk
rD   ||i| j|j< Y nX dS )z@Set status (enabled/disable) for a given message at a given liner   N)r   r   ru   r   )rF   r4   r;   Zstatusr    r    r$   r     s
    zFileState.set_msg_statusc             C   sH   |t krDy&| j||f }| j||f | W n tk
rB   Y nX dS )a  Report an ignored message.

        state_scope is either MSG_STATE_SCOPE_MODULE or MSG_STATE_SCOPE_CONFIG,
        depending on whether the message was disabled locally in the module,
        or globally. The other arguments are the same as for add_message.
        N)r   r   r   addr   )rF   Zstate_scoperu   r;   rS   r_   r6   Z	orig_liner    r    r$   r   	  s    	z FileState.handle_ignored_messagec             c   s   xR| j  D ]D\}}x:| D ].\}}|s||f| jkrd|||ffV  qW qW xBt| j D ]0\\}}}x"|D ]}d||||ffV  qvW qdW d S )Nzuseless-suppressionzsuppressed-message)r   r   r   get_msg_display_stringri   )rF   r   r   r   r;   r   Zfrom_r    r    r$   "iter_spurious_suppression_messages  s    
z,FileState.iter_spurious_suppression_messagesc             C   s   | j S )N)r   )rF   r    r    r$   r   (  s    z'FileState.get_effective_max_line_number)N)r,   r-   r.   rH   r~   r   r   r   r   r   r   r    r    r    r$   r     s   

=r   c               @   s   e Zd ZdZdd Zedd Zdd Zedd	 Z	d
d Z
dd Zedd Zdd Zdd Zdd Zdd Zedd Zedd ZeedddZdd  Zd!d" Zd#d$ Zd%S )&MessagesStorezwThe messages store knows information about every possible message but has
    no particular state during analysis.
    c             C   s   i | _ i | _tt| _d S )N)_messages_definitionsr   r   r   ri   r   )rF   r    r    r$   r~   1  s    zMessagesStore.__init__c             C   s
   | j  S )z The list of all active messages.)r   values)rF   r    r    r$   messages?  s    zMessagesStore.messagesc             C   s0   |  |d }|j||f | ||| dS )zRegister the old ID and symbol for a warning that was renamed.

        This allows users to keep using the old ID/symbol in suppressions.
        r   N)r   r}   rN   _register_alternative_name)rF   old_id
old_symbolZ
new_symbolr   r    r    r$   add_renamed_messageD  s    z!MessagesStore.add_renamed_messagec             C   s:   g }x0t | j D ]\}}t| ||}|| qW |S )zReturn the list of messages definitions for a checker.

        :param BaseChecker checker:
        :rtype: list
        :return: A list of MessageDefinition.
        )r   r   r   ry   rN   )rt   r   ru   rv   messager    r    r$   get_checker_message_definitionsM  s
    z-MessagesStore.get_checker_message_definitionsc             C   s0   |  |}| | x|D ]}| | qW dS )zSRegister all messages from a checker.

        :param BaseChecker checker:
        N)r  _check_checker_consistencyregister_message)rF   rt   Zchecker_message_definitionsr   r    r    r$   register_messages_from_checker[  s    


z,MessagesStore.register_messages_from_checkerc             C   s   |  |j|j | |j|j | |j|j x |jD ]}| |j|d  q8W || j|j< | ||j|j x |jD ]\}}| ||| qxW | j|jd  	|j dS )zRegister a MessageDefinition with consistency in mind.

        :param MessageDefinition message: The message definition being added.
        r*   r   N)
 _check_id_and_symbol_consistencyru   r3   _check_symbol_check_msgidr}   r   r  r   rN   )rF   r  Zold_namer  r  r    r    r$   r  e  s    zMessagesStore.register_messagec             C   s~   d}g }xp| D ]h}|dk	r\||j dd kr\d}|dj|j |d7 }|dj|d7 }t||j dd }||j  qW dS )	a  Check the msgid consistency in a list of messages definitions.

        msg ids for a checker should be a string of len 4, where the two first
        characters are the checker id and the two last the msg id in this
        checker.

        :param list messages: List of MessageDefinition.
        :raises InvalidMessageError: If the checker id in the messages are not
        always the same
        Nr*   rl   z(Inconsistent checker part in message id z!'{}' (expected 'x{checker_id}xx' )
checker_idz'because we already had {existing_ids}).)existing_ids)ru   rB   r   rN   )r   r  r  r  	error_msgr    r    r$   r  u  s    

z(MessagesStore._check_checker_consistencyc             C   s$   |  || || j|< || j|< dS )zhelper for register_message()N)r
  r   )rF   r4   ru   r3   r    r    r$   r    s    
z(MessagesStore._register_alternative_namec             C   s   | j |}|r"| |||j nd}| j|}|r|j|krH|j}n"x |jD ]\}}||krP|}P qPW ||kr| ||| dS )z)Check that a symbol is not already used. N)r   rW   _raise_duplicate_msg_idru   r   r3   r}   )rF   ru   r3   Zother_messageZalternative_msgidZalternative_messageZ	old_msgidr  r    r    r$   r    s    
zMessagesStore._check_symbolc             C   s2   x,| j  D ]}|j|kr| |||j qW d S )N)r   r   ru   _raise_duplicate_symbolr3   )rF   ru   r3   r  r    r    r$   r    s    
zMessagesStore._check_msgidc             C   s   y| j | }W n tk
r&   d}Y nX y|s8| j | }W n tk
rN   d S X d }d }x*|jD ] \}}||ksx||kr`|}|}q`W ||j|fkr||kr| ||| n| |||j d S )NF)r   r   r}   r3   r  )rF   ru   r3   ZalternativeZold_symbolic_nameZold_symbolic_idZalternate_msgidZalternate_symbolr    r    r$   r
    s(    
z.MessagesStore._check_id_and_symbol_consistencyc             C   sB   ||g}|   dj| d}|dj|d |d d7 }t|dS )a  Raise an error when a symbol is duplicated.

        :param str msgid: The msgid corresponding to the symbols
        :param str symbol: Offending symbol
        :param str other_symbol: Other offending symbol
        :raises InvalidMessageError: when a symbol is duplicated.
        z&Message id '{msgid}' cannot have both )ru   z1'{other_symbol}' and '{symbol}' as symbolic name.r   r*   )other_symbolr3   N)sortrB   r   )ru   r3   r  Zsymbolserror_messager    r    r$   r    s    	z%MessagesStore._raise_duplicate_symbolc             C   sB   ||g}|   dj| d}|dj|d |d d7 }t|dS )a  Raise an error when a msgid is duplicated.

        :param str symbol: The symbol corresponding to the msgids
        :param str msgid: Offending msgid
        :param str other_msgid: Other offending msgid
        :raises InvalidMessageError: when a msgid is duplicated.
        z-Message symbol '{symbol}' cannot be used for )r3   z/'{other_msgid}' and '{msgid}' at the same time.r   r*   )other_msgidru   N)r  rB   r   )r3   ru   r  r   r  r    r    r$   r    s    	z%MessagesStore._raise_duplicate_msg_id)msgid_or_symbolreturnc          	   C   sf   |dd   r| }x4| j| jfD ]$}y
|| gS  tk
rH   Y q&X q&W dj|d}t|dS )aJ  Returns the Message object for this message.

        :param str msgid_or_symbol: msgid_or_symbol may be either a numeric or symbolic id.
        :raises UnknownMessageError: if the message id is not defined.
        :rtype: List of MessageDefinition
        :return: A message definition corresponding to msgid_or_symbol
        r*   Nz1No such message id or symbol '{msgid_or_symbol}'.)r  )isdigitrU   r   r   r   rB   r   )rF   r  sourcer  r    r    r$   r     s    

z%MessagesStore.get_message_definitionsc             C   s6   |  |}t|dkr$t|d jS tdd |D S )zGenerates a user-consumable representation of a message.

        Can be just the message ID or the ID and the symbol.
        r*   r   c             S   s   g | ]
}|j qS r    )r3   )r!   r   r    r    r$   r     s    z8MessagesStore.get_msg_display_string.<locals>.<listcomp>)r   rn   reprr3   )rF   ru   r   r    r    r$   r     s    
z$MessagesStore.get_msg_display_stringc             C   sx   xr|D ]j}y0x*|  |D ]}t|jdd td qW W q tk
rn } zt| td wW dd}~X Y qX qW dS )z7Display help messages for the given message identifiersT)r   rI   N)r   r   r   r   )rF   r   ru   r   exr    r    r$   help_message	  s    
zMessagesStore.help_messagec             C   sJ   t | j dd d}x&|D ]}| s*qt|jdd qW td dS )z8Output full messages list documentation in ReST format. c             S   s   | j S )N)ru   )mr    r    r$   r     s    z-MessagesStore.list_messages.<locals>.<lambda>)r   F)r   rI   N)r   r   r   r   r   r   )rF   r   r  r    r    r$   list_messages  s    
zMessagesStore.list_messagesN)r,   r-   r.   rH   r~   propertyr   r  r   r  r	  r  r  r  r  r  r
  r  r  r   ri   r   r   r  r  r    r    r    r$   r   ,  s$   	

r   c               @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )ReportsHandlerMixInzqa mix-in class containing all the reports and stats manipulation
    related methods for the main lint class
    c             C   s   t t| _i | _d S )N)r   r   ri   _reports_reports_state)rF   r    r    r$   r~   $  s    zReportsHandlerMixIn.__init__c             C   s
   t | jS )z] Return a list of reports, sorted in the order
        in which they must be called.
        )ri   r!  )rF   r    r    r$   report_order(  s    z ReportsHandlerMixIn.report_orderc             C   s"   |  }| j| |||f dS )zregister a report

        reportid is the unique identifier for the report
        r_title the report's title
        r_cb the method to call to make the report
        checker is the checker defining the report
        N)rU   r!  rN   )rF   reportidr_titler_cbrt   r    r    r$   register_report.  s    z#ReportsHandlerMixIn.register_reportc             C   s   |  }d| j|< dS )z"disable the report of the given idTN)rU   r"  )rF   r$  r    r    r$   r   9  s    z!ReportsHandlerMixIn.enable_reportc             C   s   |  }d| j|< dS )z"disable the report of the given idFN)rU   r"  )rF   r$  r    r    r$   r   >  s    z"ReportsHandlerMixIn.disable_reportc             C   s   | j |dS )zXreturn true if the report associated to the given identifier is
        enabled
        T)r"  rW   )rF   r$  r    r    r$   report_is_enabledC  s    z%ReportsHandlerMixIn.report_is_enabledc       	   
   C   s   t dd| jd  }xv|  D ]j}xd| j| D ]V\}}}| |sDq.t |}y|||| W n tk
rr   w.Y nX ||_|| q.W qW |S )zrender registered reportsZReportz%s statements analysed.Z	statement)r   r   r#  r!  r(  r   Z	report_idrN   )	rF   r   Z	old_statsZsectrt   r$  r%  r&  Zreport_sectr    r    r$   make_reportsI  s    
z ReportsHandlerMixIn.make_reportsc             K   sL   xD|  D ]8\}}|d dkr*|dd }|| jks8t|| j|< q
W | jS )zvadd some stats entries to the statistic dictionary
        raise an AssertionError if there is a key conflict
        r   N)r   r   r   )rF   r`   r   valuer    r    r$   	add_statsY  s    zReportsHandlerMixIn.add_statsN)r,   r-   r.   rH   r~   r#  r'  r   r   r(  r)  r,  r    r    r    r$   r     s   r   c             C   s    x|D ]}| | rdS qW dS )aI  Determines if the basename is matched in a regex blacklist

    :param str base_name: The basename of the file
    :param list black_list_re: A collection of regex patterns to match against.
        Successful matches are blacklisted.

    :returns: `True` if the basename is blacklisted, `False` otherwise.
    :rtype: bool
    TF)match)r   black_list_reZfile_patternr    r    r$   _basename_in_blacklist_ree  s    


r/  c                s    fdd}t j| |dS )Nc                s   t | |p S )N)r   Zcheck_modpath_has_init)r8   parts)is_namespacer    r$   _is_package_cbv  s    z*_modpath_from_file.<locals>._is_package_cb)Zis_package_cb)r   Zmodpath_from_file_with_callback)filenamer1  r2  r    )r1  r$   _modpath_from_fileu  s    r4  c             C   s"  g }g }x| D ]}t j||kr(qtt j||r<qt|rydt|}W n$ tk
r|   t	t|d }Y nX t
|rt|d}q|}n^|}yt|d}|dkrwW n: ttfk
r } z|d||d wW dd}~X Y nX t|}|p|d}	ytj|	tjd}
W n" tk
rD   d}t
|}Y nX t|
}t|
}|sv|||d	||d
 |dp|dk ot|dk}|s|s|rxjtjt|||dD ]R}||krҐqtt||rqt||}d|}|||d||d
 qW qW ||fS )ztake a list of files/modules/packages and return the list of tuple
    (file, module name) which have to be actually checked
    rK   r   z__init__.pyNr   )r   modr  )r8   FT)r8   rJ   ZisargZbasepathr   z	.__init__r~   )Zlist_all)osr8   r   r/  r   r	   r   Zmodpath_from_fileImportErrorr   r   Zfile_from_modpathr   SyntaxErrorrN   r
   Zfile_info_from_modpathr[   r1  is_directoryendswithZget_module_filesr   r4  )Zfiles_or_modulesZ
black_listr.  resultrf   Z	somethingr   filepathr  Zmodpartsspecr1  r9  Zhas_initZsubfilepathZmodpathZ
submodnamer    r    r$   expand_modules~  sr    





r>  c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
PyLintASTWalkerc             C   s(   d| _ tt| _tt| _|| _d S )Nr   )nbstatementsr   r   ri   visit_eventsleave_eventslinter)rF   rC  r    r    r$   r~     s    zPyLintASTWalker.__init__c             C   s2   t |dsdS x|jD ]}| j|rdS qW dS )Nchecks_msgsTF)r   rD  rC  r   )rF   methodZmsg_descr    r    r$   _is_method_enabled  s    
z"PyLintASTWalker._is_method_enabledc             C   s   t  }t  }| j}| j}xt|D ]}|dd }|dkr<q"|drtt||}| |r|| | || q"|dr"t||}	| |	r"|| |	 || q"W t|dd}
|
rx.t	j
D ]$}|j }||kr|| |
 qW dS )z=walk to the checker's dir and collect visit and leave methods   NdefaultZvisit_Zleave_visit_default)r   rA  rB  dirr   rO   rF  rN   r   r   ZALL_NODE_CLASSESr,   r   )rF   rt   ZvcidsZlcidsZvisitsZleavesmemberrX   Zv_methZl_methrI  r@   r    r    r$   add_checker  s0    






zPyLintASTWalker.add_checkerc             C   s   |j j }| j|d}| j|d}|jr<|  jd7  _x|pDdD ]}|| qFW x| D ]}| 	| q`W x|pzdD ]}|| q|W dS )zvcall visit events of astroid checkers for the given node, recurse on
        its children, then leave events.
        r    r*   N)
	__class__r,   r   rA  rW   rB  Zis_statementr@  r   walk)rF   astroidrX   rA  rB  cbr   r    r    r$   rN    s    zPyLintASTWalker.walkN)r,   r-   r.   r~   rF  rL  rN  r    r    r    r$   r?    s   r?  )z.pyz.pycz.pyoz.pywz.soz.dllc             C   s   i }xt |D ]}t|\}}||ks|dkr2q|tkrB|dksT|stt||rytt||}W nL tk
r~   wY q t	k
r } zt
d||f tjd W dd}~X Y qX t|dr||  d||< qW dS )zload all module and package in the given directory, looking for a
    'register' function in each one, used to register pylint checkers
    __pycache__r~   zProblem importing module %s: %s)r   Nregisterr*   )r6  listdirr   PY_EXTSr   r	   r   Zload_module_from_file
ValueErrorr7  r   r[   stderrr   rR  )rC  Z	directoryZimportedr3  base	extensionr9   excr    r    r$   register_plugins  s$    &

rZ  c             C   sp   yt | j|ddS  tk
r(   Y nX x@| jjD ]4}x.|jD ]$}|d |kr@t |j|ddS q@W q4W |S )a   Retrieve an option defined by the given *checker* or
    by all known option providers.

    It will look in the list of all options providers
    until the given *option* will be found.
    If the option wasn't found, the *default* value will be returned.
    -r   r   )rO   r   r   rQ   rC  Zoptions_providersrw   )rt   optionrH  Zproviderrw   r    r    r$   get_global_option<  s    r]  c                s.    fdd}|d|d|dd}| r*| |d< |S )Nc                s    rt j |f  d S )N)r[   rV  write)r\  optnamer_   )deprecation_msgr    r$   _warn_deprecatedU  s    z+deprecated_option.<locals>._warn_deprecatedTcallback)helpZhidetypeactionrb  Z
deprecated	shortnamer    )rf  Zopt_typeZhelp_msgr`  ra  r\  r    )r`  r$   deprecated_optionR  s    rg  ,c             C   s   dd |  |D S )a%  return a list of stripped string by splitting the string given as
    argument on `sep` (',' by default). Empty string are discarded.

    >>> _splitstrip('a, b, c   ,  4,,')
    ['a', 'b', 'c', '4']
    >>> _splitstrip('a')
    ['a']
    >>> _splitstrip('a,
b,
c,')
    ['a', 'b', 'c']

    :type string: str or unicode
    :param string: a csv line

    :type sep: str or unicode
    :param sep: field separator, default to the comma (',')

    :rtype: str or unicode
    :return: the unquoted string (or the input string if it wasn't quoted)
    c             S   s   g | ]}|  r|  qS r    )strip)r!   Zwordr    r    r$   r   z  s    z_splitstrip.<locals>.<listcomp>)r   )stringsepr    r    r$   _splitstripf  s    rl  c             C   s<   | s| S | d dkr | dd } | d dkr8| dd } | S )zremove optional quotes (simple or double) from the string

    :type string: str or unicode
    :param string: an optionally quoted string

    :rtype: str or unicode
    :return: the unquoted string (or the input string if it wasn't quoted)
    r   z"'r*   Nr*  r    )rj  r    r    r$   _unquote}  s    	rm  P   rI   c             C   s   d tj| |||dS )z'Wrap the text on the given line length.
)widthZinitial_indentZsubsequent_indent)r	   textwrapZwrap)textline_lenr   r    r    r$   r     s    r   c             C   s   t | ttfr| S t| S )N)rM   ri   tuplerl  )r+  r    r    r$   
_check_csv  s    ru  c             C   s&   dd |   D }ddtj | S )zreturn string as a commentc             S   s   g | ]}|  qS r    )ri  )r!   r;   r    r    r$   r     s    z_comment.<locals>.<listcomp>z# z%s# )r   r6  linesepr	   )rj  r   r    r    r$   _comment  s    rw  c                s   t |ttfr(d fdd|D }nlt |trLddd | D }nHt|dr^|j}n6 ddkrz|rtdnd	}nt |t	r|
 rd
| }|S )z5return the user input's value from a 'compiled' valuerh  c             3   s   | ]}t  |V  qd S )N)_format_option_value)r!   item)optdictr    r$   	<genexpr>  s    z'_format_option_value.<locals>.<genexpr>c             s   s   | ]\}}d ||f V  qdS )z%s:%sNr    )r!   r"   r#   r    r    r$   r{    s    r-  rd  ZynZyesZnoz'%s')rM   ri   rt  r	   rC   r   r   patternrW   r   isspace)rz  r+  r    )rz  r$   rx    s    

rx  c             C   s2   |rt t|| d t d| | d t| | dS )z.format an options section using the INI format)r   z[%s]N)r   rw  _ini_format)re   r   rw   r   r    r    r$   _ini_format_section  s    r  c             C   s   x|D ]\}}}t ||}|d}|rNt|ddd}t| d t|| d n
t| d |dkrrtd| | d qt| }tdt|rd	d
t|  }|	dd t|
dD }|dd }td||f | d qW dS )z#format options using the INI formatrc  O   z# )rs  r   )r   Nz#%s=z^([\w-]+,)+[\w-]+$z
 r   c             s   s   | ]}|d  V  qdS )rh  Nr    )r!   xr    r    r$   r{    s    z_ini_format.<locals>.<genexpr>rh  r*  z%s=%s)rx  rW   r   r   r   ri  rer-  rn   r	   r   )re   rw   r_  rz  r+  help_optZ	separatorr    r    r$   r~    s     



r~  c             C   s   |r t d|dt| f | d |rDt t|ddd| d t | d x~|D ]v\}}}|d}t d| | d |rt|dd	d}t || d |rJtt||}t | d t d
|dd | d qJW dS )z8format an options section using as ReST formatted outputz%s
%s')r   r  rI   )rs  r   rc  z:%s:z  z  Default: ``%s``z`` z```` ``N)r   rn   r   rW   r   rx  r   )re   r   rw   r   r_  rz  r+  r  r    r    r$   r     s    


r   )rb   )N)NNNN)rh  )rn  rI   )N)N)PrH   Z
__future__r   rc   r   inspectr   r6  os.pathr   r   r   r   r   r	   r
   r  r[   rj   ro   rq  rO  r   r   r   Zpylint.interfacesr   r   r   r   Zpylint.reporters.ureports.nodesr   Zpylint.exceptionsr   r   r   r?   r   rV   r   r   r   r   r   compileZ
OPTION_RGXr   r+   
namedtupler1   r=   rT   rY   ra   rg   rk   ry   rs   r   r   r   r   r/  r4  r>  r?  rT  rZ  r]  rg  rl  rm  r   ru  rw  rx  r  r~  Zformat_sectionr   r    r    r    r$   <module>*   s   $

C   } tF	VH



	
