OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 Michal Nowikowski. |
| 2 # |
| 3 # This program is free software; you can redistribute it and/or modify it under |
| 4 # the terms of the GNU General Public License as published by the Free Software |
| 5 # Foundation; either version 2 of the License, or (at your option) any later |
| 6 # version. |
| 7 # |
| 8 # This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 10 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details |
| 11 # |
| 12 # You should have received a copy of the GNU General Public License along with |
| 13 # this program; if not, write to the Free Software Foundation, Inc., |
| 14 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 15 """Checker for spelling errors in comments and docstrings. |
| 16 """ |
| 17 |
| 18 import sys |
| 19 import tokenize |
| 20 import string |
| 21 import re |
| 22 |
| 23 if sys.version_info[0] >= 3: |
| 24 maketrans = str.maketrans |
| 25 else: |
| 26 maketrans = string.maketrans |
| 27 |
| 28 from pylint.interfaces import ITokenChecker, IAstroidChecker |
| 29 from pylint.checkers import BaseTokenChecker |
| 30 from pylint.checkers.utils import check_messages |
| 31 |
| 32 try: |
| 33 import enchant |
| 34 except ImportError: |
| 35 enchant = None |
| 36 |
| 37 if enchant is not None: |
| 38 br = enchant.Broker() |
| 39 dicts = br.list_dicts() |
| 40 dict_choices = [''] + [d[0] for d in dicts] |
| 41 dicts = ["%s (%s)" % (d[0], d[1].name) for d in dicts] |
| 42 dicts = ", ".join(dicts) |
| 43 instr = "" |
| 44 else: |
| 45 dicts = "none" |
| 46 dict_choices = [''] |
| 47 instr = " To make it working install python-enchant package." |
| 48 |
| 49 table = maketrans("", "") |
| 50 |
| 51 class SpellingChecker(BaseTokenChecker): |
| 52 """Check spelling in comments and docstrings""" |
| 53 __implements__ = (ITokenChecker, IAstroidChecker) |
| 54 name = 'spelling' |
| 55 msgs = { |
| 56 'C0401': ('Wrong spelling of a word \'%s\' in a comment:\n%s\n' |
| 57 '%s\nDid you mean: \'%s\'?', |
| 58 'wrong-spelling-in-comment', |
| 59 'Used when a word in comment is not spelled correctly.'), |
| 60 'C0402': ('Wrong spelling of a word \'%s\' in a docstring:\n%s\n' |
| 61 '%s\nDid you mean: \'%s\'?', |
| 62 'wrong-spelling-in-docstring', |
| 63 'Used when a word in docstring is not spelled correctly.'), |
| 64 } |
| 65 options = (('spelling-dict', |
| 66 {'default' : '', 'type' : 'choice', 'metavar' : '<dict name>', |
| 67 'choices': dict_choices, |
| 68 'help' : 'Spelling dictionary name. ' |
| 69 'Available dictionaries: %s.%s' % (dicts, instr)}), |
| 70 ('spelling-ignore-words', |
| 71 {'default' : '', |
| 72 'type' : 'string', |
| 73 'metavar' : '<comma separated words>', |
| 74 'help' : 'List of comma separated words that ' |
| 75 'should not be checked.'}), |
| 76 ('spelling-private-dict-file', |
| 77 {'default' : '', |
| 78 'type' : 'string', |
| 79 'metavar' : '<path to file>', |
| 80 'help' : 'A path to a file that contains private ' |
| 81 'dictionary; one word per line.'}), |
| 82 ('spelling-store-unknown-words', |
| 83 {'default' : 'n', 'type' : 'yn', 'metavar' : '<y_or_n>', |
| 84 'help' : 'Tells whether to store unknown words to ' |
| 85 'indicated private dictionary in ' |
| 86 '--spelling-private-dict-file option instead of ' |
| 87 'raising a message.'}), |
| 88 ) |
| 89 |
| 90 def open(self): |
| 91 self.initialized = False |
| 92 self.private_dict_file = None |
| 93 |
| 94 if enchant is None: |
| 95 return |
| 96 dict_name = self.config.spelling_dict |
| 97 if not dict_name: |
| 98 return |
| 99 |
| 100 self.ignore_list = self.config.spelling_ignore_words.split(",") |
| 101 # "param" appears in docstring in param description and |
| 102 # "pylint" appears in comments in pylint pragmas. |
| 103 self.ignore_list.extend(["param", "pylint"]) |
| 104 |
| 105 if self.config.spelling_private_dict_file: |
| 106 self.spelling_dict = enchant.DictWithPWL( |
| 107 dict_name, self.config.spelling_private_dict_file) |
| 108 self.private_dict_file = open( |
| 109 self.config.spelling_private_dict_file, "a") |
| 110 else: |
| 111 self.spelling_dict = enchant.Dict(dict_name) |
| 112 |
| 113 if self.config.spelling_store_unknown_words: |
| 114 self.unknown_words = set() |
| 115 |
| 116 # Prepare regex for stripping punctuation signs from text. |
| 117 # ' and _ are treated in a special way. |
| 118 puncts = string.punctuation.replace("'", "").replace("_", "") |
| 119 self.punctuation_regex = re.compile('[%s]' % re.escape(puncts)) |
| 120 self.initialized = True |
| 121 |
| 122 def close(self): |
| 123 if self.private_dict_file: |
| 124 self.private_dict_file.close() |
| 125 |
| 126 def _check_spelling(self, msgid, line, line_num): |
| 127 line2 = line.strip() |
| 128 # Replace ['afadf with afadf (but preserve don't) |
| 129 line2 = re.sub("'([^a-zA-Z]|$)", " ", line2) |
| 130 # Replace afadf'] with afadf (but preserve don't) |
| 131 line2 = re.sub("([^a-zA-Z]|^)'", " ", line2) |
| 132 # Replace punctuation signs with space e.g. and/or -> and or |
| 133 line2 = self.punctuation_regex.sub(' ', line2) |
| 134 |
| 135 words = [] |
| 136 for word in line2.split(): |
| 137 # Skip words with digits. |
| 138 if len(re.findall(r"\d", word)) > 0: |
| 139 continue |
| 140 |
| 141 # Skip words with mixed big and small letters, |
| 142 # they are probaly class names. |
| 143 if (len(re.findall("[A-Z]", word)) > 0 and |
| 144 len(re.findall("[a-z]", word)) > 0 and |
| 145 len(word) > 2): |
| 146 continue |
| 147 |
| 148 # Skip words with _ - they are probably function parameter names. |
| 149 if word.count('_') > 0: |
| 150 continue |
| 151 |
| 152 words.append(word) |
| 153 |
| 154 # Go through words and check them. |
| 155 for word in words: |
| 156 # Skip words from ignore list. |
| 157 if word in self.ignore_list: |
| 158 continue |
| 159 |
| 160 orig_word = word |
| 161 word = word.lower() |
| 162 |
| 163 # Strip starting u' from unicode literals and r' from raw strings. |
| 164 if (word.startswith("u'") or |
| 165 word.startswith('u"') or |
| 166 word.startswith("r'") or |
| 167 word.startswith('r"')) and len(word) > 2: |
| 168 word = word[2:] |
| 169 |
| 170 # If it is a known word, then continue. |
| 171 if self.spelling_dict.check(word): |
| 172 continue |
| 173 |
| 174 # Store word to private dict or raise a message. |
| 175 if self.config.spelling_store_unknown_words: |
| 176 if word not in self.unknown_words: |
| 177 self.private_dict_file.write("%s\n" % word) |
| 178 self.unknown_words.add(word) |
| 179 else: |
| 180 # Present up to 4 suggestions. |
| 181 # TODO: add support for customising this. |
| 182 suggestions = self.spelling_dict.suggest(word)[:4] |
| 183 |
| 184 m = re.search(r"(\W|^)(%s)(\W|$)" % word, line.lower()) |
| 185 if m: |
| 186 # Start position of second group in regex. |
| 187 col = m.regs[2][0] |
| 188 else: |
| 189 col = line.lower().index(word) |
| 190 indicator = (" " * col) + ("^" * len(word)) |
| 191 |
| 192 self.add_message(msgid, line=line_num, |
| 193 args=(orig_word, line, |
| 194 indicator, |
| 195 "' or '".join(suggestions))) |
| 196 |
| 197 def process_tokens(self, tokens): |
| 198 if not self.initialized: |
| 199 return |
| 200 |
| 201 # Process tokens and look for comments. |
| 202 for (tok_type, token, (start_row, _), _, _) in tokens: |
| 203 if tok_type == tokenize.COMMENT: |
| 204 self._check_spelling('wrong-spelling-in-comment', |
| 205 token, start_row) |
| 206 |
| 207 @check_messages('wrong-spelling-in-docstring') |
| 208 def visit_module(self, node): |
| 209 if not self.initialized: |
| 210 return |
| 211 self._check_docstring(node) |
| 212 |
| 213 @check_messages('wrong-spelling-in-docstring') |
| 214 def visit_class(self, node): |
| 215 if not self.initialized: |
| 216 return |
| 217 self._check_docstring(node) |
| 218 |
| 219 @check_messages('wrong-spelling-in-docstring') |
| 220 def visit_function(self, node): |
| 221 if not self.initialized: |
| 222 return |
| 223 self._check_docstring(node) |
| 224 |
| 225 def _check_docstring(self, node): |
| 226 """check the node has any spelling errors""" |
| 227 docstring = node.doc |
| 228 if not docstring: |
| 229 return |
| 230 |
| 231 start_line = node.lineno + 1 |
| 232 |
| 233 # Go through lines of docstring |
| 234 for idx, line in enumerate(docstring.splitlines()): |
| 235 self._check_spelling('wrong-spelling-in-docstring', |
| 236 line, start_line + idx) |
| 237 |
| 238 |
| 239 def register(linter): |
| 240 """required method to auto register this checker """ |
| 241 linter.register_checker(SpellingChecker(linter)) |
OLD | NEW |