Add a checker for words which I often misspell.

This commit is contained in:
Florian Bruhin 2015-03-31 22:12:38 +02:00
parent 8ebac8d38c
commit 38c63ca2ea
2 changed files with 57 additions and 9 deletions

View File

@ -21,18 +21,27 @@
"""Various small code checkers.""" """Various small code checkers."""
import os import os
import re
import sys import sys
import os.path import os.path
import argparse import argparse
import subprocess import subprocess
import tokenize import tokenize
import traceback import traceback
import collections
sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir)) sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir))
from scripts import utils from scripts import utils
def _py_files(target):
"""Iterate over all python files and yield filenames."""
for (dirpath, _dirnames, filenames) in os.walk(target):
for name in (e for e in filenames if e.endswith('.py')):
yield os.path.join(dirpath, name)
def check_git(): def check_git():
"""Check for uncommitted git files..""" """Check for uncommitted git files.."""
if not os.path.isdir(".git"): if not os.path.isdir(".git"):
@ -55,18 +64,49 @@ def check_git():
return status return status
def check_spelling(target):
"""Check commonly misspelled words."""
# Words which I often misspell
words = {'behaviour', 'quitted', 'likelyhood', 'sucessfully',
'occur[^r .]', 'seperator', 'explicitely', 'resetted',
'auxillary', 'accidentaly', 'ambigious', 'loosly',
'initialis', 'convienence', 'similiar', 'uncommited',
'reproducable'}
# Words which look better when splitted, but might need some fine tuning.
words |= {'keystrings', 'webelements', 'mouseevent', 'keysequence',
'normalmode', 'eventloops', 'sizehint', 'statemachine',
'metaobject', 'logrecord', 'monkeypatch', 'filetype'}
seen = collections.defaultdict(list)
try:
ok = True
for fn in _py_files(target):
with tokenize.open(fn) as f:
if fn == os.path.join('scripts', 'misc_checks.py'):
continue
for line in f:
for w in words:
if re.search(w, line) and fn not in seen[w]:
print("Found '{}' in {}!".format(w, fn))
seen[w].append(fn)
print()
return ok
except Exception:
traceback.print_exc()
return None
def check_vcs_conflict(target): def check_vcs_conflict(target):
"""Check VCS conflict markers.""" """Check VCS conflict markers."""
try: try:
ok = True ok = True
for (dirpath, _dirnames, filenames) in os.walk(target): for fn in _py_files(target):
for name in (e for e in filenames if e.endswith('.py')): with tokenize.open(fn) as f:
fn = os.path.join(dirpath, name) for line in f:
with tokenize.open(fn) as f: if any(line.startswith(c * 7) for c in '<>=|'):
for line in f: print("Found conflict marker in {}".format(fn))
if any(line.startswith(c * 7) for c in '<>=|'): ok = False
print("Found conflict marker in {}".format(fn))
ok = False
print() print()
return ok return ok
except Exception: except Exception:
@ -77,7 +117,7 @@ def check_vcs_conflict(target):
def main(): def main():
"""Main entry point.""" """Main entry point."""
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('checker', choices=('git', 'vcs'), parser.add_argument('checker', choices=('git', 'vcs', 'spelling'),
help="Which checker to run.") help="Which checker to run.")
parser.add_argument('target', help="What to check", nargs='*') parser.add_argument('target', help="What to check", nargs='*')
args = parser.parse_args() args = parser.parse_args()
@ -91,6 +131,13 @@ def main():
if not ok: if not ok:
is_ok = False is_ok = False
return 0 if is_ok else 1 return 0 if is_ok else 1
elif args.checker == 'spelling':
is_ok = True
for target in args.target:
ok = check_spelling(target)
if not ok:
is_ok = False
return 0 if is_ok else 1
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -39,6 +39,7 @@ commands =
commands = commands =
{envpython} scripts/misc_checks.py git {envpython} scripts/misc_checks.py git
{envpython} scripts/misc_checks.py vcs qutebrowser scripts {envpython} scripts/misc_checks.py vcs qutebrowser scripts
{envpython} scripts/misc_checks.py spelling qutebrowser scripts
[testenv:pylint] [testenv:pylint]
skip_install = true skip_install = true