B
    A!p\©3  ã               @   sj  d Z ddlZddlZddl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 ddlmZmZ dd	lmZ dd
lmZ edk	rðe ¡ Ze ¡ Zdgdd„ eD ƒ Zdd„ eD ƒZd 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)Ú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   ú7lib/python3.7/site-packages/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   )ÚITokenCheckerÚIAstroidChecker)ÚBaseTokenChecker)Úcheck_messagesÚ 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, Znonez3 To make it working install 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 ]}|  ¡ 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 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 |¡ƒ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 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 |¡ƒ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  dd¡\}}|| _ d| _|rp|rp|d  ¡ rp|d  ¡ sŒd| _ d| _|d | dfS |dfS d S )Nú/r   r   r   éÿÿÿÿ)Ú_textÚStopIterationZ_offsetÚsplitÚisalpha)r	   ÚtextÚpre_textÚ	post_textr   r   r   Únextv   s&    
zForwardSlashChunkder.nextc             C   sl   x`d| j kr| j dfS | j  dd¡\}}|r0|s2P |d  ¡ rJ|d  ¡ sPtƒ ‚|d | | _ qW tƒ ‚d S )Nr)   r   r   r*   ú )r+   r-   r.   r,   )r	   r0   r1   r   r   r   Ú_next   s    

zForwardSlashChunkder._nextN)r   r   r   r   r2   r4   r   r   r   r   r(   q   s   r(   c            
   @   sâ   e Zd ZdZeefZdZddddœ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*d+„ ƒZed'ƒd,d-„ ƒZeZd.d/„ Zd0S )1ÚSpellingCheckerz)Check spelling in comments and docstringsZspelling)zEWrong spelling of a word '%s' in a comment:
%s
%s
Did you mean: '%s'?zwrong-spelling-in-commentz5Used when a word in comment is not spelled correctly.)zGWrong spelling of a word '%s' in a docstring:
%s
%s
Did you mean: '%s'?zwrong-spelling-in-docstringz7Used when a word in docstring is not spelled correctly.)z$Invalid characters %r in a docstringzinvalid-characters-in-docstringz;Used when a word in docstring cannot be checked by enchant.)ZC0401ZC0402ZC0403zspelling-dictr   Zchoicez<dict name>z8Spelling 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.)r6   r7   r8   r:   zspelling-private-dict-filez<path to file>zEA path to a file that contains private dictionary; one word per line.zspelling-store-unknown-wordsÚnZynz<y_or_n>z‰Tells whether to store unknown words to indicated private dictionary in --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 d¡D ƒ| _| j ddg¡ | jj	rnt
j | jj	¡| j_	| jj	ršt || jj	¡| _t| jj	dƒ| _nt |¡| _| jjr¶tƒ | _t|tgtttttttgd| _d	| _ d S )
NFc             S   s   g | ]}|  ¡ ‘qS r   )Ústrip)r   Úwr   r   r   r   ó   s    z(SpellingChecker.open.<locals>.<listcomp>ú,Z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ÚopenZDictÚspelling_store_unknown_wordsÚsetÚunknown_wordsr   r(   r   r   r   r   r   r!   r'   Ú	tokenizer)r	   Z	dict_namer   r   r   rO   è   s<    zSpellingChecker.openc             C   s   | j r| j  ¡  d S )N)rF   Úclose)r	   r   r   r   rT     s    zSpellingChecker.closec             C   s®  |}|  ¡  d¡r(|  ¡ dd … }d}nd}xz|  |  ¡ ¡D ]f\}}| ¡ }|| jks>|| jkrfq>| d¡r”t|ƒdkr”|dd … }|dd … }y | j |¡r¤w>| j |¡r²w>W n, tj	j
k
rà   | jd||fd w>Y nX | jjr|| jkr¦| j d	| ¡ | j |¡ q>| j |¡}	|	| jjd …= t d
| |¡}
|
rV|
jd d }n
| |¡}|rn|d7 }d| dt|ƒ  }| j|||||d d |	¡¡fd q>W d S )Nú#r   TF)zu'zu"zr'zr"é   zinvalid-characters-in-docstring)ÚlineÚargsz%s
z(\W|^)(%s)(\W|$)r   r3   ú^z'{}'z' or ')r@   Ú
startswithrS   ÚcasefoldrJ   ÚlenrI   ZcheckrG   ÚerrorsÚErrorZadd_messagerH   rP   rR   rF   ÚwriteÚaddZsuggestZmax_spelling_suggestionsr%   ÚsearchÚregsÚindexÚformatÚjoin)r	   ZmsgidrW   Zline_numZoriginal_lineZstarts_with_commentr
   r    Zlower_cased_wordZsuggestionsÚmÚcolZ	indicatorr   r   r   Ú_check_spelling  sR    

zSpellingChecker._check_spellingc             C   sb   | j s
d S xR|D ]J\}}\}}}}|tjkr|dkr@| d¡r@q| d¡rLq|  d||¡ qW d S )Nr   z#!/z	# pylint:zwrong-spelling-in-comment)rE   ÚtokenizeÚCOMMENTrZ   rh   )r	   ÚtokensÚtok_typeÚtokenZ	start_rowr    r   r   r   Úprocess_tokensa  s    

zSpellingChecker.process_tokenszwrong-spelling-in-docstringc             C   s   | j s
d S |  |¡ d S )N)rE   Ú_check_docstring)r	   Únoder   r   r   Úvisit_modulep  s    zSpellingChecker.visit_modulec             C   s   | j s
d S |  |¡ d S )N)rE   ro   )r	   rp   r   r   r   Úvisit_classdefv  s    zSpellingChecker.visit_classdefc             C   s   | j s
d S |  |¡ d S )N)rE   ro   )r	   rp   r   r   r   Úvisit_functiondef|  s    z!SpellingChecker.visit_functiondefc             C   sH   |j }|sdS |jd }x*t| ¡ ƒD ]\}}|  d||| ¡ q&W dS )z&check the node has any spelling errorsNr   zwrong-spelling-in-docstring)ÚdocÚlinenoÚ	enumerateÚ
splitlinesrh   )r	   rp   Z	docstringZ
start_lineÚidxrW   r   r   r   ro   „  s    
z SpellingChecker._check_docstringN)r   r   r   r   r   r   Z__implements__r   ZmsgsÚdict_choicesÚdictsÚinstrZoptionsrO   rT   rh   rn   r   rq   rr   rs   Zvisit_asyncfunctiondefro   r   r   r   r   r5   š   sT   1Dr5   c             C   s   |   t| ƒ¡ dS )z.required method to auto register this checker N)Zregister_checkerr5   )Zlinterr   r   r   Úregister‘  s    r|   )"r   rL   ri   r%   rG   Zenchant.tokenizer   r   r   r   r   r   ÚImportErrorZpylint.interfacesr   r   Zpylint.checkersr   Zpylint.checkers.utilsr   ZBrokerÚbrZ
list_dictsrz   ry   re   r{   r   r   r!   r'   r(   r5   r|   r   r   r   r   Ú<module>   s>   $

) x