3
tûœ]ù4  ã               @   sj  d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 y,ddlZddlmZmZmZmZmZmZ W n4 ek
r¤   dZG dd„ dƒZG d	d
„ d
ƒZY nX edk	rðejƒ Zejƒ Zdgdd„ eD ƒ Zdd„ eD ƒZdjeƒZdZndZdgZdZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ dd„ Z!dS ) z8Checker for spelling errors in comments and docstrings.
é    N)ÚBaseTokenChecker)Úcheck_messages)ÚIAstroidCheckerÚITokenChecker)Úget_tokenizerÚChunkerÚFilterÚEmailFilterÚ	URLFilterÚWikiWordFilterc               @   s   e Zd Zdd„ ZdS )r   c             C   s   t ‚d S )N)ÚNotImplementedError)ÚselfÚword© r   ú:/tmp/pip-build-fn42xldr/pylint/pylint/checkers/spelling.pyÚ_skip+   s    zFilter._skipN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   *   s   r   c               @   s   e Zd ZdS )r   N)r   r   r   r   r   r   r   r   .   s   r   Ú c             C   s   g | ]}|d  ‘qS )r   r   )Ú.0Údr   r   r   ú
<listcomp>5   s    r   c             C   s"   g | ]}d |d |d j f ‘qS )z%s (%s)r   é   )Úname)r   r   r   r   r   r   6   s    z, Únonez5 To make it work, install the python-enchant package.c               @   s   e Zd ZdZdd„ ZdS )ÚWordsWithDigigtsFilterzSkips words with digits.
    c             C   s   x|D ]}|j ƒ rdS qW dS )NTF)Úisdigit)r   r   Úcharr   r   r   r   C   s    
zWordsWithDigigtsFilter._skipN)r   r   r   Ú__doc__r   r   r   r   r   r   ?   s   r   c               @   s   e Zd ZdZdd„ ZdS )ÚWordsWithUnderscoreszSSkips words with underscores.

    They are probably function parameter names.
    c             C   s   d|kS )NÚ_r   )r   r   r   r   r   r   P   s    zWordsWithUnderscores._skipN)r   r   r   r   r   r   r   r   r   r    J   s   r    c               @   s"   e Zd ZdZejdƒZdd„ ZdS )ÚCamelCasedWordzÈFilter skipping over camelCasedWords.
    This filter skips any words matching the following regular expression:

           ^([a-z]\w+[A-Z]+\w+)

    That is, any words that are camelCasedWords.
    z^([a-z]+([\d]|[A-Z])(?:\w+)?)c             C   s   t | jj|ƒƒS )N)ÚboolÚ_patternÚmatch)r   r   r   r   r   r   ^   s    zCamelCasedWord._skipN)r   r   r   r   ÚreÚcompiler$   r   r   r   r   r   r"   T   s   
r"   c               @   s"   e Zd ZdZejdƒZdd„ ZdS )ÚSphinxDirectiveszÉFilter skipping over Sphinx Directives.
    This filter skips any words matching the following regular expression:

           ^:([a-z]+):`([^`]+)(`)?

    That is, for example, :class:`BaseQuery`
    z^:([a-z]+):`([^`]+)(`)?c             C   s   t | jj|ƒƒS )N)r#   r$   r%   )r   r   r   r   r   r   m   s    zSphinxDirectives._skipN)r   r   r   r   r&   r'   r$   r   r   r   r   r   r(   b   s   
r(   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚForwardSlashChunkderz[
    This chunker allows splitting words like 'before/after' into 'before' and 'after'
    c             C   s    xš| j stƒ ‚d| j kr2| j }d| _d| _ |dfS | j jddƒ\}}|| _ d| _| sx| sx|d jƒ  sx|d jƒ  r”d| _ d| _|d | dfS |dfS d S )Nú/r   r   r   éÿÿÿÿ)Ú_textÚStopIterationÚ_offsetÚsplitÚisalpha)r   ÚtextÚpre_textÚ	post_textr   r   r   Únextv   s&    
zForwardSlashChunkder.nextc             C   st   xhd| j kr| j dfS | j jddƒ\}}| s4| r6P |d jƒ  sR|d jƒ  rXtƒ ‚|d | | _ qW tƒ ‚d S )Nr*   r   r   ú r+   )r,   r/   r0   r-   )r   r2   r3   r   r   r   Ú_next�   s    

zForwardSlashChunkder._nextN)r   r   r   r   r4   r6   r   r   r   r   r)   q   s   r)   c            
   @   sâ   e Zd ZdZeefZdZd6d7d8dœZdddde	de
ef 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œffZd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zedƒd-d.„ ƒZedƒd/d0„ ƒZedƒd1d2„ ƒZeZd3d4„ Zd5S )9ÚSpellingCheckerz)Check spelling in comments and docstringsZspellingúEWrong spelling of a word '%s' in a comment:
%s
%s
Did you mean: '%s'?úwrong-spelling-in-commentú5Used when a word in comment is not spelled correctly.úGWrong spelling of a word '%s' in a docstring:
%s
%s
Did you mean: '%s'?úwrong-spelling-in-docstringú7Used when a word in docstring is not spelled correctly.ú$Invalid characters %r in a docstringúinvalid-characters-in-docstringú;Used when a word in docstring cannot be checked by enchant.)ZC0401ZC0402ZC0403zspelling-dictr   Úchoicez<dict name>z7Spelling dictionary name. Available dictionaries: %s.%s)ÚdefaultÚtypeÚmetavarÚchoicesÚhelpzspelling-ignore-wordsÚstringz<comma separated words>z9List of comma separated words that should not be checked.)rB   rC   rD   rF   zspelling-private-dict-filez<path to file>zIA path to a file that contains the private dictionary; one word per line.zspelling-store-unknown-wordsÚnZynz<y_or_n>zŠTells whether to store unknown words to the private dictionary (see the --spelling-private-dict-file option) instead of raising a message.zmax-spelling-suggestionsé   ÚintÚNz:Limits count of emitted suggestions for spelling mistakes.c          
   C   sà   d| _ d | _td krd S | jj}|s(d S dd„ | jjjdƒD ƒ| _| jjddgƒ | jj	rnt
jj| jj	ƒ| j_	| jj	rštj|| jj	ƒ| _t| jj	dƒ| _ntj|ƒ| _| jjr¶tƒ | _t|tgtttttttgd�| _d	| _ d S )
NFc             S   s   g | ]}|j ƒ ‘qS r   )Ústrip)r   Úwr   r   r   r   ó   s    z(SpellingChecker.open.<locals>.<listcomp>ú,ÚparamZpylintÚa)ZchunkersÚfiltersT)ÚinitializedÚprivate_dict_fileÚenchantÚconfigÚspelling_dictZspelling_ignore_wordsr/   Úignore_listÚextendZspelling_private_dict_fileÚosÚpathÚ
expanduserZDictWithPWLÚopenÚDictÚspelling_store_unknown_wordsÚsetÚunknown_wordsr   r)   r	   r
   r   r   r    r"   r(   Ú	tokenizer)r   Z	dict_namer   r   r   r\   è   s<    zSpellingChecker.openc             C   s   | j r| j jƒ  d S )N)rS   Úclose)r   r   r   r   rb     s    zSpellingChecker.closec             C   s  |}yt jd|ƒjd d }W n ttfk
r:   d}Y nX |jƒ jdƒr`|jƒ dd … }d}nd}�x˜| j|jƒ ƒD �]„\}}||7 }|jƒ }	|| j	ksv|	| j	kr¦qv|jdƒrÔt
|ƒdkrÔ|dd … }|	dd … }	y | jj|	ƒräwv| jj|ƒròwvW n. tjjk
�r"   | jd||fd� wvY nX | jj�rX|	| jk�rü| jjd|	 ƒ | jj|	ƒ qv| jj|ƒ}
|
| jjd …= ||d … }t jd| |ƒ}|�r¤|jd d }n
|j|ƒ}||7 }|�rÄ|d7 }d| dt
|ƒ  }| j|||||djdj|
ƒƒfd� qvW d S )Nz	^[^\S]\s*r   r   ú#TFúu'úu"úr'úr"é   zinvalid-characters-in-docstring)ÚlineÚargsz%s
z(\W|^)(%s)(\W|$)r5   ú^z'{}'z' or ')rd   re   rf   rg   )r&   ÚsearchÚregsÚ
IndexErrorÚAttributeErrorrL   Ú
startswithra   ÚcasefoldrW   ÚlenrV   ÚcheckrT   ÚerrorsÚErrorZadd_messagerU   r^   r`   rS   ÚwriteÚaddZsuggestZmax_spelling_suggestionsÚindexÚformatÚjoin)r   Úmsgidri   Úline_numZoriginal_lineZinitial_spaceZstarts_with_commentr   Zword_start_atZlower_cased_wordZsuggestionsZline_segmentr%   ÚcolZ	indicatorr   r   r   Ú_check_spelling  s`    


zSpellingChecker._check_spellingc             C   sb   | j s
d S xR|D ]J\}}\}}}}|tjkr|dkr@|jdƒr@q|jdƒrLq| jd||ƒ qW d S )Nr   z#!/z	# pylint:zwrong-spelling-in-comment)rR   ÚtokenizeÚCOMMENTrp   r~   )r   ÚtokensÚtok_typeÚtokenZ	start_rowr!   r   r   r   Úprocess_tokensi  s    

zSpellingChecker.process_tokensc             C   s   | j s
d S | j|ƒ d S )N)rR   Ú_check_docstring)r   Únoder   r   r   Úvisit_modulex  s    zSpellingChecker.visit_modulec             C   s   | j s
d S | j|ƒ d S )N)rR   r…   )r   r†   r   r   r   Úvisit_classdef~  s    zSpellingChecker.visit_classdefc             C   s   | j s
d S | j|ƒ d S )N)rR   r…   )r   r†   r   r   r   Úvisit_functiondef„  s    z!SpellingChecker.visit_functiondefc             C   sH   |j }|sdS |jd }x*t|jƒ ƒD ]\}}| jd||| ƒ q&W dS )z&check the node has any spelling errorsNr   zwrong-spelling-in-docstring)ÚdocÚlinenoÚ	enumerateÚ
splitlinesr~   )r   r†   Z	docstringZ
start_lineÚidxri   r   r   r   r…   Œ  s    
z SpellingChecker._check_docstringN)r8   r9   r:   )r;   r<   r=   )r>   r?   r@   )r   r   r   r   r   r   Z__implements__r   ZmsgsÚdict_choicesÚdictsÚinstrÚoptionsr\   rb   r~   r„   r   r‡   rˆ   r‰   Zvisit_asyncfunctiondefr…   r   r   r   r   r7   š   s`         1Lr7   c             C   s   | j t| ƒƒ dS )z.required method to auto register this checker N)Zregister_checkerr7   )Zlinterr   r   r   Úregister™  s    r“   )"r   rY   r&   r   Zpylint.checkersr   Zpylint.checkers.utilsr   Zpylint.interfacesr   r   rT   Zenchant.tokenizer   r   r   r	   r
   r   ÚImportErrorZBrokerÚbrZ
list_dictsr�   r�   rz   r‘   r   r    r"   r(   r)   r7   r“   r   r   r   r   Ú<module>   s@   $

)  