3
tûœ]Ý¼  ã               @   s2  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lmZmZ ddlmZmZmZ d	Zd
dddddddgZddd
ddddddddddgZdddddddd d!d"d#d$d%d&d'd(d)d*d+gZd,d-d.gZd/d0d1gZd2ZeejejejgƒZejejfZdZd3Z d4Z!d5Z"d6Z#d7Z$e#e"e$gZ%e#e"gZ&d’d“d”d•d–d—d˜d™dšdSdTdUdVej'ifd›dZd[d\d]dœd�džgifdŸd djœZ(dkdl„ Z)dmdn„ Z*dodp„ Z+dqdr„ Z,dsdt„ Z-dudv„ Z.dwdx„ Z/G dydz„ dzƒZ0d{Z1d|Z2d}Z3d~Z4dZ5d€Z6d�Z7e1d¡e2d¢e3d£e4d¤e5d¥iZ8d…Z9d†d‡„ Z:dˆd‰„ Z;G dŠd‹„ d‹ƒZ<G dŒd�„ d�ƒZ=G dŽd�„ d�eƒZ>d�d‘„ Z?dS )¦zÍPython code format's checker.

By default try to follow Guido's style guide :

https://www.python.org/doc/essays/styleguide/

Some parts of the process_token method is based from The Tab Nanny std module.
é    N)Úreduce)Únodes)ÚBaseTokenChecker)Úcheck_messages)Ú
OPTION_RGXÚWarningScope)ÚIAstroidCheckerÚIRawCheckerÚITokenCheckerÚasyncÚelifÚexceptÚforÚifÚwhileÚdefÚclassÚwithÚassertÚdelÚinÚnotÚraiseÚreturnÚyieldz==ú<ú>z!=z<>z<=z>=z+=z-=z*=z**=z/=z//=z&=z|=z^=z%=z>>=z<<=ú(ú[Ú{ú)ú]Ú}é   é   é   zdict-separatorztrailing-commaz
empty-lineúLine too long (%s/%s)úline-too-longú=Used when a line is longer than a given number of characters.ú Too many lines in module (%s/%s)útoo-many-linesú@Used when a module has too many lines, reducing its readability.úTrailing whitespaceútrailing-whitespaceúHUsed when there is whitespace between the end of a line and the newline.úFinal newline missingúmissing-final-newlineú7Used when the last line in a file is missing a newline.úTrailing newlinesútrailing-newlinesú3Used when there are trailing blank lines in a file.ú)Bad indentation. Found %s %s, expected %súbad-indentationúUUsed when an unexpected number of indentation's tabulations or spaces has been found.úWrong %s indentation%s%s.
%s%súbad-continuationÚTODOú)Found indentation with %ss instead of %ssúmixed-indentationú;Used when there are some mixed tabs and spaces in a module.úUnnecessary semicolonúunnecessary-semicolonúeUsed when a statement is ended by a semi-colon (";"), which isn't necessary (that's python, not C ;).z(More than one statement on a single linezmultiple-statementsz<Used when more than on statement are found on the same line.Zscopeú#Unnecessary parens after %r keywordúsuperfluous-parensúLUsed when a single item in parentheses follows an if, for, or other keyword.z%s space %s %s %s
%szbad-whitespacezWUsed when a wrong number of spaces is used around an operator, bracket or block opener.Z	old_namesÚC0323úno-space-after-operatorÚC0324úno-space-after-commaÚC0322úno-space-before-operatorúMixed line endings LF and CRLFúmixed-line-endingsú@Used when there are mixed (LF and CRLF) newline signs in a file.úEUnexpected line ending format. There is '%s' while it should be '%s'.úunexpected-line-ending-formatú3Used when there is different newline than expected.)ZC0301ZC0302ZC0303ZC0304ZC0305ZW0311ZC0330ZW0312ZW0301ZC0321ZC0325ZC0326ZC0327ZC0328c             C   sT   | d d | d d  }| d d }| d }|d dkr@|d7 }|d|  d|  S )	Né   r$   r%   é   Ú
ú ú^éÿÿÿÿ© )ÚtokenÚlengthÚoffsetZreferenced_linerV   rV   ú8/tmp/pip-build-fn42xldr/pylint/pylint/checkers/format.pyÚ_underline_tokenÐ   s    r[   c             C   sZ   | |krdS |d | d k r&||  } }| d d |d d krBd S |d d | d d  S )Nr   rP   r%   r$   rV   )Ztoken1Ztoken2rV   rV   rZ   Ú_column_distanceÚ   s    
r\   c             C   sH   |dkr| j |d ƒ|kpF|dkoF| j |d ƒ|koF| j|d ƒtjkS )Nr   r$   r%   )rW   ÚtypeÚtokenizeÚCOMMENT)ÚtokensÚline_endrW   rV   rV   rZ   Ú_last_token_on_line_isä   s
    rb   c             C   s<   | j |d ƒtjkp:| j |d ƒtjko:| j |d ƒtjkS )Nr$   r%   )r]   r^   ÚNLr_   )r`   ÚpositionrV   rV   rZ   Ú_token_followed_by_eolî   s    re   c             C   s*   d}x | D ]}|dkr ||7 }q
P q
W |S )z.Return the indention string of the given line.Ú z 	rV   )ÚlineÚresultÚcharrV   rV   rZ   Ú_get_indent_stringö   s    

rj   c             C   s<   d}x2| D ]*}|dkr |d7 }q
|dkr2|t 7 }q
P q
W |S )z?Return the length of the indentation on the given token's line.r   rS   r$   ú	)Ú_TAB_LENGTH)rg   rh   ri   rV   rV   rZ   Ú_get_indent_length  s    


rm   c       
      C   sÒ   | sdS dd„ | D ƒ} t |ƒ}d}dd„ | D ƒ}t|ƒdkr~|d d }t|| ƒ}||kr`dnd}t|||dkrvd	ndf }|j|d
fƒ |jƒ  dg|d d d  }x|D ]\}}	|	||< q°W dj|ƒ|fS )zCReturn a line with |s for each of the positions in the given lists.rf   c             S   s   g | ]}t |ƒ‘qS rV   )rm   )Ú.0ÚindentrV   rV   rZ   ú
<listcomp>  s    z)_get_indent_hint_line.<locals>.<listcomp>c             S   s   g | ]}|d f‘qS )ú|rV   )rn   ÚposrV   rV   rZ   rp     s    r$   r   ÚaddÚremoveÚsrT   rS   )rf   rf   rU   )rm   ÚlenÚabsÚ_CONTINUATION_HINT_MESSAGEÚappendÚsortÚjoin)
Zbar_positionsZbad_positionÚdelta_messageÚmarkersZexpected_positionÚdeltaÚ	directionrg   rd   ÚmarkerrV   rV   rZ   Ú_get_indent_hint_line  s(    r�   c               @   s   e Zd Zd	Zdd„ ZdS )
Ú_ContinuedIndentÚvalid_outdent_stringsÚvalid_continuation_stringsÚcontext_typerW   rd   c             C   s"   || _ || _|| _|| _|| _d S )N)rƒ   r„   r…   rd   rW   )Úselfr…   rW   rd   rƒ   r„   rV   rV   rZ   Ú__init__2  s
    z_ContinuedIndent.__init__N)rƒ   r„   r…   rW   rd   )Ú__name__Ú
__module__Ú__qualname__Ú	__slots__r‡   rV   rV   rV   rZ   r‚   )  s       r‚   z
dict-valueÚhangingzhanging-blockÚ	continuedzcontinued-blockÚsingleZmultiú in dict valuerf   ú before blockz (%s %d space%s)c              G   s   dd„ | D ƒS )z/Valid indentation strings for a continued line.c             S   s   i | ]
}d |“qS )NrV   )rn   ÚarV   rV   rZ   ú
<dictcomp>]  s    z!_Indentations.<locals>.<dictcomp>rV   )ÚargsrV   rV   rZ   Ú_Indentations[  s    r”   c             C   s   | t |tiS )a‹  Valid alternative indentation strings for continued lines before blocks.

    :param int single: Valid indentation string for statements on a single logical line.
    :param int with_body: Valid indentation string for statements on several lines.

    :returns: A dictionary mapping indent offsets to a string representing
        whether the indent if for a line or block.
    :rtype: dict
    )ÚSINGLE_LINEÚ	WITH_BODY)rŽ   Z	with_bodyrV   rV   rZ   Ú_BeforeBlockIndentations`  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 )ÚTokenWrapperz3A wrapper for readable access to token information.c             C   s
   || _ d S )N)Ú_tokens)r†   r`   rV   rV   rZ   r‡   p  s    zTokenWrapper.__init__c             C   s   | j | d S )Nr$   )r™   )r†   ÚidxrV   rV   rZ   rW   s  s    zTokenWrapper.tokenc             C   s   | j | d S )Nr   )r™   )r†   rš   rV   rV   rZ   r]   v  s    zTokenWrapper.typec             C   s   | j | d d S )Nr%   r   )r™   )r†   rš   rV   rV   rZ   Ú
start_liney  s    zTokenWrapper.start_linec             C   s   | j | d d S )Nr%   r$   )r™   )r†   rš   rV   rV   rZ   Ú	start_col|  s    zTokenWrapper.start_colc             C   s   | j | d S )NrQ   )r™   )r†   rš   rV   rV   rZ   rg     s    zTokenWrapper.linec             C   s   t | j|ƒƒS )zPGet the string of TABs and Spaces used for indentation of the line of this token)rj   rg   )r†   rš   rV   rV   rZ   Úline_indent‚  s    zTokenWrapper.line_indentc             C   s$   | j |ƒ}|d| j|ƒt|ƒ   S )a  Get an indentation string for hanging indentation, consisting of the line-indent plus
        a number of spaces to fill up to the column of this token.

        e.g. the token indent for foo
        in "<TAB><TAB>print(foo)"
        is "<TAB><TAB>      "
        rS   )r�   rœ   rv   )r†   rš   r�   rV   rV   rZ   Útoken_indent†  s    
zTokenWrapper.token_indentN)rˆ   r‰   rŠ   Ú__doc__r‡   rW   r]   r›   rœ   rg   r�   rž   rV   rV   rV   rZ   r˜   m  s   r˜   c               @   s�   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZedd	„ ƒZe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d„ Zdd„ ZdS )ÚContinuedLineStatez8Tracker for continued indentation inside a logical line.c             C   s,   d| _ g | _d| _g | _|| _t|ƒ| _d S )Nr$   FrU   )Ú_line_startÚ_cont_stackÚ_is_block_openerÚretained_warningsÚ_configr˜   r™   )r†   r`   ÚconfigrV   rV   rZ   r‡   •  s    zContinuedLineState.__init__c             C   s
   t | jƒS )N)Úboolr¢   )r†   rV   rV   rZ   Úhas_content�  s    zContinuedLineState.has_contentc             C   s   | j jjddƒS )Nz\trk   )r¥   Úindent_stringÚreplace)r†   rV   rV   rZ   Ú_block_indent_string¡  s    z'ContinuedLineState._block_indent_stringc             C   s   | j d | jj S )Nr   )r«   r¥   Úindent_after_paren)r†   rV   rV   rZ   Ú_continuation_string¥  s    z'ContinuedLineState._continuation_stringc             C   s   | j jS )N)r¥   r¬   )r†   rV   rV   rZ   Ú_continuation_size©  s    z%ContinuedLineState._continuation_sizec             C   sF   | j dkrdS |}| jj|ƒtkr*|d7 }| jj|ƒtk| _|| _ dS )z7Record the first non-junk token at the start of a line.r$   NrU   )r¡   r™   rW   Ú_ASYNC_TOKENÚ_CONTINUATION_BLOCK_OPENERSr£   )r†   rr   Zcheck_token_positionrV   rV   rZ   Úhandle_line_start­  s    
z$ContinuedLineState.handle_line_startc             C   s   d| _ d| _dS )z2Prepares the tracker for a new physical line (NL).r$   FNrU   )r¡   r£   )r†   rV   rV   rZ   Únext_physical_lineº  s    z%ContinuedLineState.next_physical_linec             C   s   | j ƒ  g | _g | _dS )zPrepares the tracker for a new logical line (NEWLINE).

        A new logical line only starts with block indentation.
        N)r²   r¤   r¢   )r†   rV   rV   rZ   Únext_logical_line¿  s    z$ContinuedLineState.next_logical_linec             C   s   | j j|||fƒ d S )N)r¤   ry   )r†   Ztoken_positionÚstateÚvalid_indentationsrV   rV   rZ   Úadd_block_warningÈ  s    z$ContinuedLineState.add_block_warningc             C   s\   d}| j j|ƒdkr(| jd jdkr(d	}| j| }| j j|ƒtkrJ|j}n|j}||jƒ fS )
z>Returns the valid offsets for the token at the given position.r$   r"   r   ú:r%   rU   )r"   r   rU   éþÿÿÿ)r™   rW   r¢   Ú_CLOSING_BRACKETSrƒ   r„   Úcopy)r†   rš   Z	stack_topro   rµ   rV   rV   rZ   Úget_valid_indentationsË  s    
z)ContinuedLineState.get_valid_indentationsc          
   C   sÈ   | j j|ƒ}| jrP| j| jkrPtt||t|| j |ƒt|| j || jd  ƒƒS |dkr¢| j	d j
}| j	d jjƒ }t|jƒ ƒ}d||d | j < tt||||ƒS tt||t||| j ƒt|| j ƒƒS )ab  Extracts indentation information for a hanging indent

        Case of hanging indent after a bracket (including parenthesis)

        :param str bracket: bracket in question
        :param int position: Position of bracket in self._tokens

        :returns: the state and valid positions for hanging indentation
        :rtype: _ContinuedIndent
        r%   r·   r$   Tr   rU   rU   )r™   r�   r£   r­   r«   r‚   ÚHANGING_BLOCKr”   r—   r¢   rƒ   r„   rº   ÚlistÚkeysÚHANGING_DICT_VALUEÚHANGING)r†   Úbracketrd   ÚindentationZparen_alignZ
next_alignZnext_align_keysrV   rV   rZ   Ú_hanging_indent_after_bracketÜ  s0    z0ContinuedLineState._hanging_indent_after_bracketc          	   C   sv   | j j|ƒ}| j j|ƒ}| j j|d ƒ}| jr\||| j kr\tt||t|ƒt||| j	 ƒƒS tt
||t||ƒt|ƒƒS )z8Extracts indentation information for a continued indent.r$   )r™   r�   rž   r£   r«   r‚   ÚCONTINUED_BLOCKr”   r—   r­   Ú	CONTINUED)r†   rÁ   rd   rÂ   rž   Znext_token_indentrV   rV   rZ   Ú_continuation_inside_bracket  s$    z/ContinuedLineState._continuation_inside_bracketc             C   s   | j jƒ  d S )N)r¢   Úpop)r†   rV   rV   rZ   Ú	pop_token)  s    zContinuedLineState.pop_tokenc             C   s:   t | j|ƒr"| jj| j||ƒƒ n| jj| j||ƒƒ dS )a§  Pushes a new token for continued indentation on the stack.

        Tokens that can modify continued indentation offsets are:
          * opening brackets
          * 'lambda'
          * : inside dictionaries

        push_token relies on the caller to filter out those
        interesting tokens.

        :param int token: The concrete token
        :param int position: The position of the token in the stream.
        N)re   r™   r¢   ry   rÃ   rÆ   )r†   rW   rd   rV   rV   rZ   Ú
push_token,  s    zContinuedLineState.push_tokenN)rˆ   r‰   rŠ   rŸ   r‡   Úpropertyr¨   r«   r­   r®   r±   r²   r³   r¶   r»   rÃ   rÆ   rÈ   rÉ   rV   rV   rV   rZ   r    ’  s   	3r    c               @   s¾  e Zd ZdZeeefZdZe	Z
ddddddœfd	d
ddddœfddddddœfddddddœfddjeƒdjeƒdede d e d e d dœfddddd dœfd!d"d#d$d%dœfd&ddd'd(dœfd)d*d+d,d,d-d.gd/d0œff	Zdgd2d3„Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Zd<d=„ Zd>d?„ Zd@dA„ ZdBdC„ ZdDdE„ ZdFdG„ ZdHdI„ ZdJdK„ ZdLdM„ ZdNdO„ Z dPdQ„ Z!dRdS„ Z"dTdU„ Z#dVdW„ Z$dXdY„ Z%dZd[„ Z&d\d]„ Z'e(d^ƒd_d`„ ƒZ)dadb„ Z*dcdd„ Z+dedf„ Z,d1S )hÚFormatCheckerz]checks for :
    * unauthorized constructions
    * strict indentation
    * line length
    Úformatzmax-line-lengthéd   Úintz<int>z.Maximum number of characters on a single line.)Údefaultr]   ÚmetavarÚhelpzignore-long-linesÚregexpz<regexp>z^\s*(# )?<?https?://\S+>?$z>Regexp for a line that is allowed to be longer than the limit.)r]   rÐ   rÏ   rÑ   zsingle-line-if-stmtFZynz<y_or_n>zOAllow the body of an if to be on the same line as the test if there is no else.zsingle-line-class-stmtzfAllow the body of a class to be on the same line as the declaration if body contains single statement.zno-space-checkú,Zmultiple_choicezHList of optional constructs for which whitespace checking is disabled. `zB` is used to allow tabulation in dicts, etc.: {1  : 1,\n222: 2}. `z<` allows a space between comma and closing bracket: (a, ). `z` allows space-only lines.)rÏ   rÐ   r]   ÚchoicesrÑ   zmax-module-linesiè  z$Maximum number of lines in a module.zindent-stringz    Znon_empty_stringz<string>zSString used as indentation unit. This is usually "    " (4 spaces) or "\t" (1 tab).zindent-after-parenrQ   zGNumber of spaces of indent required inside a hanging or continued line.zexpected-line-ending-formatÚchoicez<empty or LF or CRLF>rf   ÚLFÚCRLFzIExpected format of line ending, e.g. empty (any line ending), LF or CRLF.)r]   rÐ   rÏ   rÔ   rÑ   Nc             C   s$   t j| |ƒ d | _d | _d g| _d S )N)r   r‡   Ú_linesÚ_visited_linesÚ_bracket_stack)r†   ÚlinterrV   rV   rZ   r‡   »  s    zFormatChecker.__init__c             C   s   | j jƒ  | jjƒ  d S )N)rÚ   rÇ   Ú_current_linerÈ   )r†   rV   rV   rZ   Ú
_pop_tokenÁ  s    
zFormatChecker._pop_tokenc             C   s   | j j|ƒ | jj||ƒ d S )N)rÚ   ry   rÜ   rÉ   )r†   rW   rš   rV   rV   rZ   Ú_push_tokenÅ  s    zFormatChecker._push_tokenc             C   sf   t ||dƒr | jd|j|ƒd� |j|ƒ}|j|ƒ}|j|ƒtkrV|jdƒd | j|< | j||ƒ dS )z8a new line has been encountered, process it if necessaryú;zunnecessary-semicolon)rg   rR   r   N)	rb   Úadd_messager›   rg   r]   Ú_JUNK_TOKENSÚsplitrØ   Úcheck_lines)r†   r`   ra   Z
line_startÚline_numrg   rV   rV   rZ   Únew_lineÉ  s    

zFormatChecker.new_linec             C   s   t ƒ | _d S )N)ÚsetÚ_keywords_with_parens)r†   Ú_modulerV   rV   rZ   Úprocess_moduleÔ  s    zFormatChecker.process_modulec       	      C   sÐ  | j dƒr"|| d dkr"| jƒ  ||d  d dkr:dS d}d}t|| d ƒ}|| d d }�xft|t|ƒd ƒD �]N}|| }|d tjkr˜dS |d dkr®|d7 }qx|d d	k�rx|d8 }|rÊqx||d  d dk�s||d  d tjtjtj	fk�rt||d k�rdS |dk�r6|�st| j
d||d� n>|dk�rR| j
d||d� n"|| jk�rt|�st| j
d||d� dS |dkrx|d dk�r’dS |d dk�r¦d}qx|d dk�r¸dS |d dkrxdS qxW dS )a†  Check that there are not unnecessary parens after a keyword.

        Parens are unnecessary if there is exactly one balanced outer pair on a
        line, and it is followed by a colon, and contains no commas (i.e. is not a
        tuple).

        Args:
        tokens: list of Tokens; the entire list of Tokens.
        start: int; the position of the keyword in the token list.
        r·   r$   r   r   NFr   r%   r    r!   r"   r   r   zsuperfluous-parens)rg   r“   r   r   rÓ   ÚandÚorT)r·   r    r!   r"   r   )r   r   )rê   rë   )Ú_inside_bracketsrÝ   ÚstrÚrangerv   r^   rc   ÚNEWLINEÚ	ENDMARKERr_   rà   rç   )	r†   r`   ÚstartZfound_and_orÚdepthZkeyword_tokenrä   ÚirW   rV   rV   rZ   Ú_check_keyword_parentheses×  sX    
 

z(FormatChecker._check_keyword_parenthesesc             C   s°   | j || d |ƒ || d dkr<||d  d dkr<d S |dkrš||d  d tjkrrtj||d  d ƒ s†||d  d tkrš| j||ttfƒ n| j||ttfƒ d S )Nr$   r   r·   r   )	rÞ   r^   ÚNAMEÚkeywordÚ	iskeywordr¹   Ú_check_spaceÚ	_MUST_NOTÚ_IGNORE)r†   r`   ró   rV   rV   rZ   Ú_opening_bracket#  s    $zFormatChecker._opening_bracketc             C   s�   | j dƒr| jƒ  | jƒ  ||d  d dkrB|| d dkrBd S t}|| d tkrz||d  d dkrzt| jjkrzt}| j|||tfƒ d S )Nr·   r$   r!   rÓ   )	rì   rÝ   rù   r¹   Ú_TRAILING_COMMAr¦   Úno_space_checkrú   rø   )r†   r`   ró   Zpolicy_beforerV   rV   rZ   Ú_closing_bracket2  s    
$$zFormatChecker._closing_bracketc             C   sÀ   | j dƒsdS d}x¨||d dd… D ]’}|d dkr:dS |d dkrJdS |d dkr`|d7 }q&|d d	krv|d8 }q&|d d
krŒ|s¸dS q&|d dkrœq&q&|d tjtjtjfkr&dS q&W dS )z,Extended check of PEP-484 type hint presencer   Fr   r$   Nr·   Tr!   r   rÓ   Ú.ú...rU   )rÿ   r   )rì   r^   rõ   ÚSTRINGrc   )r†   r`   ró   Zbracket_levelrW   rV   rV   rZ   Ú_has_valid_type_annotation@  s(    


z(FormatChecker._has_valid_type_annotationc             C   s^   | j ||ƒr | j||ttfƒ n:| jdƒs4| jdƒrH| j||ttfƒ n| j||ttfƒ dS )z*Check the spacing of a single equals sign.r   ÚlambdaN)r  rø   Ú_MUSTrì   rù   )r†   r`   ró   rV   rV   rZ   Ú_check_equals_spacingZ  s
    z#FormatChecker._check_equals_spacingc             C   s   | j d|ƒ d S )Nr  )rÞ   )r†   r`   ró   rV   rV   rZ   Ú_open_lambdac  s    zFormatChecker._open_lambdac             C   sr   | j dƒrd S | j dƒr.t| jjkr.ttf}nttf}| j|||ƒ | j dƒrX| jƒ  n| j dƒrn| j	d|ƒ d S )Nr   r   r  r·   )
rì   Ú_DICT_SEPARATORr¦   rý   rú   rù   r  rø   rÝ   rÞ   )r†   r`   ró   ÚpolicyrV   rV   rZ   Ú_handle_colonf  s    




zFormatChecker._handle_colonc             C   sP   ||d  d t kr(| j||ttfƒ n| j||ttfƒ | jdƒrL| jƒ  d S )Nr$   r·   )r¹   rø   rù   rú   r  rì   rÝ   )r†   r`   ró   rV   rV   rZ   Ú_handle_commau  s
    
zFormatChecker._handle_commac             C   s   | j ||ttfƒ dS )z@Check that a binary operator is surrounded by exactly one space.N)rø   r  )r†   r`   ró   rV   rV   rZ   Ú_check_surrounded_by_space  s    z(FormatChecker._check_surrounded_by_spacec          
      sp  dd„ }‡ fdd„}ddg}|| }||d  |f|||d  fg}xnt t||ƒƒD ]\\}	\}
}||	 d tksT|
tkrzqTt|Ž }|d krŒqT|
tkrœ|dkpª|
tkoª|dk||	< qTW g }t|ƒ ræ|d |d kræ|j|d dfƒ n.x,t||dƒD ]\}}
}|sô|j|
|fƒ qôW xV|D ]N\}
}||ƒ}||
ƒ\}}ˆ j	d|d d ||||t
|ƒf|d d d� �qW d S )Nc             S   s   | t krdS dS )NúExactly oneÚrequiredÚNoÚallowed)r  r  )r  r  )r  )r  rV   rV   rZ   Ú_policy_string„  s    z2FormatChecker._check_space.<locals>._policy_stringc                sR   | d dkrdS | d dkr dS | d dkr0dS | d dkr@dS ˆ j dƒrNdS dS )Nr$   rÓ   Úcommar·   z()[]{}rÁ   r   r   ú<=ú>=ú!=ú==Z
comparisonr   zkeyword argument assignmentZ
assignment)r   r   r  r  r  r  )rì   )rW   )r†   rV   rZ   Ú_name_construct‰  s    
z3FormatChecker._check_space.<locals>._name_constructTr$   r   ZaroundÚbeforeÚafterzbad-whitespacer%   )rg   r“   Ú
col_offset)r  r  )Ú	enumerateÚzipÚ_EOLrú   r\   r  rù   Úanyry   rà   r[   )r†   r`   ró   Zpoliciesr  r  Z
good_spacerW   ÚpairsZ	other_idxr  Z
token_pairZdistanceÚwarningsÚokrd   Z	constructÚcountr´   rV   )r†   rZ   rø   ƒ  s6     
zFormatChecker._check_spacec             C   s   | j d |kS )Nr$   rU   )rÚ   )r†   ÚleftrV   rV   rZ   rì   ¶  s    zFormatChecker._inside_bracketsc          	   C   s|   t | jft| jft| jfdg| jft| jfdg| j	fdg| j
fdg| jfg}i }x&|D ]\}}x|D ]}|||< qdW qVW |S )Nú=rÓ   r·   r  )Ú_KEYWORD_TOKENSrô   Ú_OPENING_BRACKETSrû   r¹   rþ   r  Ú_SPACED_OPERATORSr  r
  r	  r  )r†   ÚrawÚdispatchr`   ÚhandlerrW   rV   rV   rZ   Ú_prepare_token_dispatcher¹  s    



z'FormatChecker._prepare_token_dispatcherc             C   s¢  dg| _ dg}d}d}i | _i | _| jƒ }d| _d}t|| jƒ| _�xÐt|ƒD �]Â\}\}}	}
}}|
d |kr²|
d }|t	j
krœ| jt|ƒ|d |d ƒ n| jt|ƒ|d |ƒ |t	jkrèd}| jt|ƒ|ƒ | jjƒ  | j|	|ƒ nÖ|t	j
k�r"d}| j|	|d d |ƒ |j|d d ƒ nœ|t	jk�rHd}t|ƒdk�r¾|d= nv|t	jk�r„|jdƒ�sd|}| jt|ƒ|d ƒ | jjƒ  n:|t	jt	jfk�r¾| jj|ƒ |�r¾d}| j||d |ƒ |t	jk�rä|	jdƒ�rä| jd|d	� y||	 }W n tk
�r   Y qNX |||ƒ qNW |d8 }|| jj k�r|| j!j"j#d
ƒd }|j$d
f}t%t&dt'| j!j(j)|ƒƒdƒ}| jd
|| jj f|d� ||k�rž|dk�rž| jd|d	� dS )a=  process tokens and search for :

         _ non strict indentation (i.e. not always using the <indent> parameter as
           indent unit)
         _ too long lines (i.e. longer than <max_chars>)
         _ optionally bad construct (if given, bad_construct must be a compiled
           regular expression).
        Nr   Fr$   Tz
Úlzlowercase-l-suffix)rg   ztoo-many-lines)r“   rg   ztrailing-newlinesrU   rU   rU   rU   )*rÚ   rØ   rÙ   r*  Ú_last_line_endingr    r¦   rÜ   r  r^   ÚINDENTrå   r˜   rï   Ú_process_retained_warningsr³   Ú_check_line_endingÚcheck_indent_levelry   ÚDEDENTrv   rc   ÚstripÚ_check_continued_indentationr²   r_   ÚENCODINGr±   ÚNUMBERÚendswithrà   ÚKeyErrorZmax_module_linesrÛ   Z
msgs_storeZget_message_definitionsÚmsgidÚnextÚfilterÚmapZ_pragma_linenoÚget)r†   r`   ÚindentsZcheck_equalrä   Ztoken_handlersZlast_blank_line_numrš   Útok_typerW   rñ   Ú_rg   r)  Zmessage_definitionÚnamesrV   rV   rZ   Úprocess_tokensË  sv    	 




zFormatChecker.process_tokensc             C   sx   | j d k	r&|r&|| j kr&| jd|d� || _ | jj}|rttdd„ |dƒ}|dkrTdnd}||krt| jd	||f|d
� d S )Nzmixed-line-endings)rg   c             S   s   | |kr| | S | S )NrV   )ÚxÚyrV   rV   rZ   Ú<lambda>>  s    z2FormatChecker._check_line_ending.<locals>.<lambda>rf   rR   rÖ   r×   zunexpected-line-ending-format)r“   rg   )r,  rà   r¦   Zexpected_line_ending_formatr   )r†   Zline_endingrä   ÚexpectedrV   rV   rZ   r/  /  s    
z FormatChecker._check_line_endingc                sŠ   t ||dƒ }xv| jjD ]j\}}}||j|ƒ ‰ ‡ fdd„|jƒ D ƒ}|rdˆ tkrd| j||||ƒ q| rˆ tkr| j||||ƒ qW d S )Nr·   c                s   i | ]\}}|ˆ kr||“qS rV   rV   )rn   ÚkÚv)Ú
block_typerV   rZ   r’   L  s    z<FormatChecker._process_retained_warnings.<locals>.<dictcomp>)rb   rÜ   r¤   rž   Úitemsr–   Ú_add_continuation_messager•   )r†   r`   Zcurrent_posZsingle_line_block_stmtZ
indent_posr´   ÚindentationsZhintsrV   )rH  rZ   r.  G  s    z(FormatChecker._process_retained_warningsc                sÖ   ‡‡fdd„}| j j s(ˆjˆƒtjkr,d S | j jˆƒ\}}|tjƒsP|tjƒrbd|ˆjˆd ƒ< |j	t
tfkr�ˆjˆƒ|kr�| j jˆ||ƒ nBˆjˆƒ|krÒtˆjˆƒƒ‰ t‡ fdd„|D ƒƒsÒ| j||ˆˆƒ d S )Nc                s    ˆj ˆ ƒ| koˆj ˆ d ƒ| kS )Nr%   )r]   )Ú
token_type)Únext_idxr`   rV   rZ   Úsame_token_around_nlS  s    zHFormatChecker._check_continued_indentation.<locals>.same_token_around_nlTr%   c             3   s   | ]}ˆ d t |ƒ kV  qdS )r%   N)rv   )rn   rÂ   )Úlength_indentationrV   rZ   ú	<genexpr>r  s   z=FormatChecker._check_continued_indentation.<locals>.<genexpr>)rÜ   r¨   r]   r^   rc   r»   r_   r  rž   r…   r¼   rÄ   r¶   rv   r  rJ  )r†   r`   rM  rN  r´   rµ   rV   )rO  rM  r`   rZ   r3  R  s"    

z*FormatChecker._check_continued_indentationc       	      C   sL   t |j \}}t||j|ƒƒ\}}| jd|j|ƒ||||j|ƒ|fd� d S )Nzbad-continuation)rg   r“   )Ú_CONTINUATION_MSG_PARTSr…   r�   rž   rà   r›   rg   )	r†   r´   rK  r`   rd   Zreadable_typeZreadable_positionZ	hint_liner|   rV   rV   rZ   rJ  y  s    z'FormatChecker._add_continuation_messagezmultiple-statementsc             C   sH  |j s
dS |jƒ jsdS |jƒ }|dk	r0|j}n:t|jtjƒr^||jj	kr^|jj
d jd }n|jjƒ j}|j}|s|t|ƒ‚||kr¤| jj|ƒdkr¤| j||ƒ dS || jkr²dS y
|j}W n tk
rÖ   |j}Y nX |sät|ƒ‚g }xZt||d ƒD ]H}d| j|< y|j| j| jƒ ƒ W qø tk
�r>   |jdƒ Y qøX qøW dS )z7check the node line number and check it if not yet doneNr   r$   r%   rf   )Zis_statementÚrootZpure_pythonZprevious_siblingZ
fromlinenoÚ
isinstanceÚparentr   Ú
TryFinallyZ	finalbodyÚbodyÚtolinenoZ	statementÚAssertionErrorrÙ   r<  Ú_check_multi_statement_lineZblockstart_tolinenoÚAttributeErrorrî   ry   rØ   Úrstripr7  )r†   ÚnodeZ	prev_siblÚ	prev_linerg   rW  ÚlinesrV   rV   rZ   Úvisit_defaultŠ  s<    



zFormatChecker.visit_defaultc             C   s˜   t |tjƒrdS t |tjƒr.t |jtjƒr.dS t |jtjƒrR|jj rR| jj	rRdS t |jtj
ƒr|t|jjƒdkr|| jjr|dS | jd|d� d| j|< dS )z/Check for lines containing multiple statements.Nr$   zmultiple-statements)r\  r%   )rS  r   ÚWithZ	TryExceptrT  rU  ÚIfZorelser¦   Zsingle_line_if_stmtÚClassDefrv   rV  Zsingle_line_class_stmtrà   rÙ   )r†   r\  rg   rV   rV   rZ   rY  ´  s    
z)FormatChecker._check_multi_statement_linec          
      s°   ˆj j‰ˆj j‰ ‡ ‡‡fdd„}ddddddddd	d
h
}g }xZ|jdƒD ]L}|d |krd|j|ƒ qH|r€|j|ƒ dj|ƒ}g }|||ƒ}|dkrHP qHW |r¬|dj|ƒ|ƒ dS )zBcheck lines have less than a maximum number of characters
        c                s  | j dƒsˆjd|d� nJ| jdƒ}| r8tˆjjkr8n(| t|ƒd … dkr`ˆjd|t|ƒd� |} tj| ƒ}|rÌd| krÌ|j	d	ƒj
dƒ\}}}|jƒ d
krÌddd„ |jdƒD ƒkr¸d S | jdd	ƒd jƒ } t| ƒˆkoâˆ j| ƒ � rþˆjd|t| ƒˆfd� |d	 S )NrR   zmissing-final-newline)rg   z	
 ú
ztrailing-whitespace)rg   r  r#  r$   Údisablezline-too-longc             S   s   h | ]}|j ƒ ’qS rV   )r2  )rn   Z_msg_idrV   rV   rZ   ú	<setcomp>è  s   z@FormatChecker.check_lines.<locals>.check_line.<locals>.<setcomp>rÓ   ú#r   )rg   r“   )rR   rc  )r6  rà   r[  Ú_EMPTY_LINEr¦   rý   rv   r   ÚsearchÚgroupÚ	partitionr2  râ   Úrsplit)rg   ró   Zstripped_lineZmobjZfront_of_equalr?  Zback_of_equal)Úignore_long_lineÚ	max_charsr†   rV   rZ   Ú
check_lineÕ  s(    


z-FormatChecker.check_lines.<locals>.check_lineúúúúúõ   Â…u   â€¨u   â€©Tr$   rf   NrU   )r¦   Úmax_line_lengthZignore_long_linesÚ
splitlinesry   r{   )r†   r^  ró   rn  Zunsplit_endsZunsplitrg   rV   )rl  rm  r†   rZ   rã   Ï  s6    



zFormatChecker.check_linesc       
      C   s  | j j}|dkrd}d}t|ƒ}x(|d|… |krH||d… }|d7 }q"W d}xf|r´|d dkr´|d |d krš|d dkr‚d}nd}| jd
||d� |S ||d 7 }|dd… }qPW ||ksÄ|� rþd}	|d dkrØd}	| jd||| t|ƒ |	|| fd� dS )z.return the indent level of the string
        z\trk   r   Nr$   rf   z 	ÚtabÚspacezmixed-indentation)r“   rg   ÚspacesÚtabszbad-indentation)rg   r“   )rw  rx  )rx  rw  )r¦   r©   rv   rà   )
r†   ÚstringrE  rä   ro   ÚlevelZ	unit_sizeZsupplr“   Zi_typerV   rV   rZ   r0    s6    z FormatChecker.check_indent_level)N)-rˆ   r‰   rŠ   rŸ   r
   r   r	   Z__implements__ÚnameÚMSGSZmsgsr{   Ú_DEFAULT_NO_SPACE_CHECK_CHOICESÚ_NO_SPACE_CHECK_CHOICESr  rü   rg  Úoptionsr‡   rÝ   rÞ   rå   ré   rô   rû   rþ   r  r  r  r	  r
  r  rø   rì   r*  rA  r/  r.  r3  rJ  r   r_  rY  rã   r0  rV   rV   rV   rZ   rË   @  sš   
 
L	
3d'*ArË   c             C   s   | j t| ƒƒ dS )z.required method to auto register this checker N)Zregister_checkerrË   )rÛ   rV   rV   rZ   Úregister2  s    r‚  )r&   r'   r(   )r)   r*   r+   )r,   r-   r.   )r/   r0   r1   )r2   r3   r4   )r5   r6   r7   )r8   r9   r:   )r;   r<   r=   )r>   r?   r@   )rA   rB   rC   )rD   rE   )rF   rG   )rH   rI   )rJ   rK   rL   )rM   rN   rO   )rŒ   r�   )rŒ   rf   )rŒ   r�   )r�   rf   )r�   r�   )@rŸ   rö   r^   Ú	functoolsr   Zastroidr   Zpylint.checkersr   Zpylint.checkers.utilsr   Zpylint.constantsr   r   Zpylint.interfacesr   r	   r
   r¯   r°   r$  r&  r%  r¹   rl   Ú	frozensetrï   rc   r_   r  rá   r  rù   rú   r  rü   rg  r€  r  ZNODEr~  r[   r\   rb   re   rj   rm   r�   r‚   r¿   rÀ   r¼   rÅ   rÄ   r•   r–   rQ  rx   r”   r—   r˜   r    rË   r‚  rV   rV   rV   rZ   Ú<module>,   s  


                
      


% /     w