From 3acf1b5a474949e8253066d753911dac5c6ce606 Mon Sep 17 00:00:00 2001 From: cpopa Date: Tue, 16 Sep 2014 23:30:34 +0300 Subject: Add some style fixes. Shorten a couple of long lines and keep a consistency regarding the comments. Comments can now be read as plain english, they start with capital letter and the sentence is finished with a dot. --- checkers/spelling.py | 107 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 67 insertions(+), 40 deletions(-) diff --git a/checkers/spelling.py b/checkers/spelling.py index fccf964..b20011b 100644 --- a/checkers/spelling.py +++ b/checkers/spelling.py @@ -56,28 +56,39 @@ class SpellingChecker(BaseTokenChecker): __implements__ = (ITokenChecker, IAstroidChecker) name = 'spelling' msgs = { - 'C0401': ('Wrong spelling of a word \'%s\' in a comment:\n%s\n%s\nDid you mean: \'%s\'?', + '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\'?', + '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' : '', 'choices': dict_choices, - 'help' : 'Spelling dictionary name. Available dictionaries: %s.%s' % (dicts, instr)}), + 'help' : 'Spelling dictionary name. ' + 'Available dictionaries: %s.%s' % (dicts, instr)}), ('spelling-ignore-words', - {'default' : '', 'type' : 'string', 'metavar' : '', - 'help' : 'List of comma separated words that should not be checked.'}), + {'default' : '', + 'type' : 'string', + 'metavar' : '', + 'help' : 'List of comma separated words that ' + 'should not be checked.'}), ('spelling-private-dict-file', - {'default' : '', 'type' : 'string', 'metavar' : '', - 'help' : 'A path to a file that contains private dictionary; one word per line.'}), + {'default' : '', + 'type' : 'string', + 'metavar' : '', + 'help' : 'A path to a file that contains private ' + 'dictionary; one word per line.'}), ('spelling-store-unknown-words', {'default' : 'n', 'type' : 'yn', 'metavar' : '', - 'help' : 'Tells whether to store unknown words to indicated private dictionary' - ' in --spelling-private-dict-file option instead of raising a message.'}), - ) + 'help' : 'Tells whether to store unknown words to ' + 'indicated private dictionary in ' + '--spelling-private-dict-file option instead of ' + 'raising a message.'}), + ) def open(self): self.initialized = False @@ -85,29 +96,30 @@ class SpellingChecker(BaseTokenChecker): 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.ignore_list.extend(["param", # appears in docstring in param description - "pylint", # appears in comments in pylint pragmas - ]) + # "param" appears in docstring in param description and + # "pylint" appears in comments in pylint pragmas. + self.ignore_list.extend(["param", "pylint"]) if self.config.spelling_private_dict_file: - self.spelling_dict = enchant.DictWithPWL(dict_name, self.config.spelling_private_dict_file) - self.private_dict_file = open(self.config.spelling_private_dict_file, "a") + self.spelling_dict = enchant.DictWithPWL( + dict_name, self.config.spelling_private_dict_file) + self.private_dict_file = open( + self.config.spelling_private_dict_file, "a") else: self.spelling_dict = enchant.Dict(dict_name) if self.config.spelling_store_unknown_words: self.unknown_words = set() - # prepare regex for stripping punctuation signs from text - puncts = string.punctuation.replace("'", "").replace("_", "") # ' and _ are treated in a special way + # Prepare regex for stripping punctuation signs from text. + # ' and _ are treated in a special way. + puncts = string.punctuation.replace("'", "").replace("_", "") self.punctuation_regex = re.compile('[%s]' % re.escape(puncts)) - self.initialized = True def close(self): @@ -116,70 +128,84 @@ class SpellingChecker(BaseTokenChecker): def _check_spelling(self, msgid, line, line_num): line2 = line.strip() - line2 = re.sub("'([^a-zA-Z]|$)", " ", line2) # replace ['afadf with afadf (but preserve don't) - line2 = re.sub("([^a-zA-Z]|^)'", " ", line2) # replace afadf'] with afadf (but preserve don't) - line2 = self.punctuation_regex.sub(' ', line2) # replace punctuation signs with space e.g. and/or -> and or + # Replace ['afadf with afadf (but preserve don't) + line2 = re.sub("'([^a-zA-Z]|$)", " ", line2) + # Replace afadf'] with afadf (but preserve don't) + line2 = re.sub("([^a-zA-Z]|^)'", " ", line2) + # Replace punctuation signs with space e.g. and/or -> and or + line2 = self.punctuation_regex.sub(' ', line2) words = [] for word in line2.split(): - # skip words with digits + # Skip words with digits. if len(re.findall("\d", word)) > 0: continue - # skip words with mixed big and small letters - they are probaly class names - if len(re.findall("[A-Z]", word)) > 0 and len(re.findall("[a-z]", word)) > 0 and len(word) > 2: + # Skip words with mixed big and small letters, + # they are probaly class names. + if (len(re.findall("[A-Z]", word)) > 0 and + len(re.findall("[a-z]", word)) > 0 and + len(word) > 2): continue - # skip words with _ - they are probably function parameter names + # Skip words with _ - they are probably function parameter names. if word.count('_') > 0: continue words.append(word) - # go through words and check them + # Go through words and check them. for word in words: - # skip words from ignore list + # Skip words from ignore list. if word in self.ignore_list: continue orig_word = word word = word.lower() - # strip starting u' from unicode literals and r' from raw strings - if (word.startswith("u'") or word.startswith('u"') or - word.startswith("r'") or word.startswith('r"')) and len(word) > 2: + # Strip starting u' from unicode literals and r' from raw strings. + if (word.startswith("u'") or + word.startswith('u"') or + word.startswith("r'") or + word.startswith('r"')) and len(word) > 2: word = word[2:] - # if known word then continue + # If it is a known word, then continue. if self.spelling_dict.check(word): continue - # otherwise either store word to private dict or raise a message + # Store word to private dict or raise a message. if self.config.spelling_store_unknown_words: if word not in self.unknown_words: self.private_dict_file.write("%s\n" % word) self.unknown_words.add(word) else: - suggestions = self.spelling_dict.suggest(word)[:4] # present upto 4 suggestions + # Present up to 4 suggestions. + # TODO: add support for customising this. + suggestions = self.spelling_dict.suggest(word)[:4] m = re.search("(\W|^)(%s)(\W|$)" % word, line.lower()) if m: - col = m.regs[2][0] # start position of second group in regex + # Start position of second group in regex. + col = m.regs[2][0] else: col = line.lower().index(word) indicator = (" " * col) + ("^" * len(word)) self.add_message(msgid, line=line_num, - args=(orig_word, line, indicator, "' or '".join(suggestions))) + args=(orig_word, line, + indicator, + "' or '".join(suggestions))) def process_tokens(self, tokens): if not self.initialized: return - # process tokens and look for comments + # Process tokens and look for comments. for (tok_type, token, (start_row, start_col), _, _) in tokens: if tok_type == tokenize.COMMENT: - self._check_spelling('wrong-spelling-in-comment', token, start_row) + self._check_spelling('wrong-spelling-in-comment', + token, start_row) @check_messages('wrong-spelling-in-docstring') def visit_module(self, node): @@ -207,9 +233,10 @@ class SpellingChecker(BaseTokenChecker): start_line = node.lineno + 1 - # go through lines of docstring + # Go through lines of docstring for idx, line in enumerate(docstring.splitlines()): - self._check_spelling('wrong-spelling-in-docstring', line, start_line + idx) + self._check_spelling('wrong-spelling-in-docstring', + line, start_line + idx) def register(linter): -- cgit v1.2.1