#!/usr/bin/env python3 import glob, argparse, sys, os.path, tokenize try: import enchant.checker except ImportError: print('Please install python3-enchant.') sys.exit(1) def recover_from_file(path): """ Use tokenize to recover strings and comments """ with open(path, 'rb') as checkme: print("File : %s" % path) return {i for i in tokenize.tokenize(checkme.readline) if i[0] in (tokenize.COMMENT, tokenize.STRING)} def check_spelling(checker, tokens, interactive=False): """ Use python3-enchant to check spelling. We remove some false positives (less than two letters, more than 12 letters, etc) and use a custom word file for python specific words to check remaining words. """ for j in tokens: # Remove false positives and duplicates words = {i.strip() for i in j[1].split(' ') if all((2 < len(i) < 12, i.isalpha(), i[1:].islower()))} for word in words: if not checker.check(word): print("Error : %s" % word) # It's faster to type when the word does not exist... if interactive and input()=='': add_to_pwl(word) def add_to_pwl(word): """ Add the word to our custom words. """ with open('python_words.txt', 'a') as pwl: pwl.write('\n%s' % word) interactive = True for i in glob.iglob('Lib/*.py'): d = enchant.DictWithPWL('en_US', 'python_words.txt') check_spelling(d, recover_from_file(i), True)