diff options
author | Michal Nowikowski <godfryd@gmail.com> | 2014-07-29 07:26:53 +0200 |
---|---|---|
committer | Michal Nowikowski <godfryd@gmail.com> | 2014-07-29 07:26:53 +0200 |
commit | 1035bcc1d315d28aee02b9fdaafa39e2d26f6027 (patch) | |
tree | 400ab49c5b17fb8ac25c1645113520a1562e251d | |
parent | 77a89d079ff876903fbfe662b39b7fd662a5007b (diff) | |
parent | cfdf3f3bc090327c01d49f637f50789e25672458 (diff) | |
download | pylint-1035bcc1d315d28aee02b9fdaafa39e2d26f6027.tar.gz |
Fixed merge
-rw-r--r-- | ChangeLog | 13 | ||||
-rw-r--r-- | checkers/spelling.py | 146 | ||||
-rw-r--r-- | debian.sid/control | 4 | ||||
-rw-r--r-- | debian/control | 2 | ||||
-rw-r--r-- | test/test_func.py | 2 |
5 files changed, 158 insertions, 9 deletions
@@ -2,6 +2,10 @@ ChangeLog for Pylint ==================== -- + * Add new checker for finding spelling errors. New messages: + wrong-spelling-in-comment, wrong-spelling-in-docstring. + New options: spelling-dict, spelling-ignore-words. + * Fix a false positive with string formatting checker, when encountering a string which uses only position-based arguments. Closes issue #285. @@ -17,14 +21,14 @@ ChangeLog for Pylint Closes issue #233. * Return new astroid class nodes when the inferencer can detect that - that result of a function invocation on a type (like `type` or + that result of a function invocation on a type (like `type` or `abc.ABCMeta`) is requested. Closes #205. * Emit 'undefined-variable' for undefined names when using the Python 3 `metaclass=` argument. * Checkers respect priority now. Close issue #229. - + * Fix a false positive regarding W0511. Closes issue #149. * Fix unused-import false positive with Python 3 metaclasses (#143). @@ -65,7 +69,7 @@ ChangeLog for Pylint * Don't emit 'unused-variable' when assigning to a nonlocal. Closes issue #275. - + * Do not let ImportError propagate from the import checker, leading to crash in some namespace package related cases. Closes issue #203. @@ -1516,6 +1520,3 @@ ChangeLog for Pylint 2003-05-19 -- 0.1 * initial release - - - diff --git a/checkers/spelling.py b/checkers/spelling.py new file mode 100644 index 0000000..dcb7b59 --- /dev/null +++ b/checkers/spelling.py @@ -0,0 +1,146 @@ +# Copyright 2014 Michal Nowikowski. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +"""Checker for spelling errors in comments and docstrings. +""" + +import sys +import tokenize +import string +import re + +import astroid + +from pylint.interfaces import ITokenChecker, IAstroidChecker, IRawChecker +from pylint.checkers import BaseChecker, BaseTokenChecker +from pylint.checkers import utils +from pylint.checkers.utils import check_messages + +try: + import enchant +except ImportError: + enchant = None + +if enchant is not None: + br = enchant.Broker() + dicts = br.list_dicts() + dict_choices = [''] + [d[0] for d in dicts] + dicts = ["%s (%s)" % (d[0], d[1].name) for d in dicts] + dicts = ", ".join(dicts) + instr = "" +else: + dicts = "none" + dict_choices = [''] + instr = " To make it working install python-enchant package." + +table = string.maketrans("", "") + +class SpellingInCommentsChecker(BaseTokenChecker): + """Check spelling in comments""" + __implements__ = (ITokenChecker, IAstroidChecker) + name = 'spelling' + msgs = { + 'C0401': ('Wrong spelling of a word \'%s\' in a comment:\n%s\n%s\nDid you mean: \'%s\'?', + 'wrong-spelling-in-comment', + 'Used when a word in comment is not spelled correctly.'), + 'C0402': ('Wrong spelling of a word \'%s\' in a docstring:\n%s\n%s\nDid you mean: \'%s\'?', + 'wrong-spelling-in-docstring', + 'Used when a word in docstring is not spelled correctly.'), + } + options = (('spelling-dict', + {'default' : '', 'type' : 'choice', 'metavar' : '<dict name>', + 'choices': dict_choices, + 'help' : 'Spelling dictionary name. Available dictionaries: %s.%s' % (dicts, instr)}), + ('spelling-ignore-words', + {'default' : '', 'type' : 'string', 'metavar' : '<comma separated words>', + 'help' : 'List of comma separated words that should not be checked.'}), + ) + + def open(self): + self.initialized = False + + if enchant is None: + return + + dict_name = self.config.spelling_dict + if not dict_name: + return + + self.ignore_list = self.config.spelling_ignore_words.split(",") + self.spelling_dict = enchant.Dict(dict_name) + + puncts = string.punctuation.replace("'", "") + self.regex = re.compile('[%s]' % re.escape(puncts)) + + self.initialized = True + + def _check_spelling(self, line, line_num): + # replace punctuation signs with space: e.g. and/or -> and or + line2 = self.regex.sub(' ', line.strip().replace("' ", " ").replace(" '", " ")) + words = line2.split() + + # go through words and check them + for word in words: + if not word in self.ignore_list and not self.spelling_dict.check(word): + suggestions = self.spelling_dict.suggest(word)[:4] + + col = line.index(word) + indicator = (" " * col) + ("^" * len(word)) + + self.add_message('wrong-spelling-in-comment', line=line_num, + args=(word, line, indicator, "' or '".join(suggestions))) + + def process_tokens(self, tokens): + if not self.initialized: + return + + # process tokens and look for comments + for (tok_type, token, (start_row, start_col), _, _) in tokens: + if tok_type == tokenize.COMMENT: + self._check_spelling(token, start_row) + + @check_messages('wrong-spelling-in-docstring') + def visit_module(self, node): + if not self.initialized: + return + self._check_docstring(node) + + @check_messages('wrong-spelling-in-docstring') + def visit_class(self, node): + if not self.initialized: + return + self._check_docstring(node) + + @check_messages('wrong-spelling-in-docstring') + def visit_function(self, node): + if not self.initialized: + return + self._check_docstring(node) + + def _check_docstring(self, node): + """check the node has any spelling errors""" + docstring = node.doc + if not docstring: + return + + start_line = node.lineno + 1 + + # go through lines of docstring + for idx, line in enumerate(docstring.splitlines()): + self._check_spelling(line, start_line + idx) + + +def register(linter): + """required method to auto register this checker """ + linter.register_checker(SpellingInCommentsChecker(linter)) diff --git a/debian.sid/control b/debian.sid/control index f062531..3f6ad93 100644 --- a/debian.sid/control +++ b/debian.sid/control @@ -13,7 +13,7 @@ Vcs-Browser: http://hg.logilab.org/pylint Package: pylint Architecture: all Depends: ${python:Depends}, ${misc:Depends}, python-logilab-common (>= 0.53), python-astroid -Recommends: python-tk +Recommends: python-tk, python-enchant XB-Python-Version: ${python:Versions} Description: python code static checker and UML diagram generator Pylint is a Python source code analyzer which looks for programming @@ -38,7 +38,7 @@ Description: python code static checker and UML diagram generator Package: pylint3 Architecture: all Depends: ${python3:Depends}, ${misc:Depends}, python3-logilab-common (>= 0.53), python3-astroid -Recommends: python3-tk +Recommends: python3-tk, python3-enchant XB-Python-Version: ${python3:Versions} Description: python code static checker and UML diagram generator Pylint is a Python source code analyzer which looks for programming diff --git a/debian/control b/debian/control index da0c1d1..660eda1 100644 --- a/debian/control +++ b/debian/control @@ -20,7 +20,7 @@ Depends: ${python:Depends}, ${misc:Depends}, python-logilab-common (>= 0.53.0), python-astroid (>= 1.2) -Suggests: python-tk +Suggests: python-tk, python-enchant XB-Python-Version: ${python:Versions} Description: python code static checker and UML diagram generator Pylint is a Python source code analyzer which looks for programming diff --git a/test/test_func.py b/test/test_func.py index e34c0a2..66e092d 100644 --- a/test/test_func.py +++ b/test/test_func.py @@ -60,6 +60,8 @@ class TestTests(testlib.TestCase): except KeyError: continue not_tested -= self.PORTED + not_tested.remove("C0401") # requires optional lib python-enchant for spell checking + not_tested.remove("C0402") # requires optional lib python-enchant for spell checking self.assertFalse(not_tested) |