forked from lyda/misspell-check
-
Notifications
You must be signed in to change notification settings - Fork 2
/
misspellings
executable file
·226 lines (177 loc) · 7.41 KB
/
misspellings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
"""Checks files against a list of commonly misspelled words.
Wikipedia contains a large number of proper nouns and technical
terms. Traditional spell-checking in this case is problematic.
Instead it has a page that people can use to list commonly misspelled
words and to then use that to find misspellings.
A similar approach can be taken to spell-checking (or misspell-checking)
the files in a body of source code.
This tool uses the English wordlist from Wikipedia in order to scan
source code and identify misspelled words.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import codecs
import io
import argparse
import os
import re
import signal
import sys
import misspellings_lib as misspellings
EXCLUDED_RE = re.compile('\.(py[co]|s?o|a)$')
EXCLUDED_DIRS_RE = re.compile(
r'^(.*\..*egg|.*\..*egg-info|\.bzr|\.git|\.hg|\.svn|\.tox|CVS)$')
def get_a_line(filename, lineno):
"""Read a specific line from a file."""
# Perhaps caching this would be nice, but assuming not an insane
# number of misspellings.
return io.open(filename, 'r').readlines()[lineno - 1].rstrip()
class Suggestions(object):
"""Class to query user on which correction should be used."""
def __init__(self):
self.last_suggestions = {}
def get_suggestion(self, filename, lineno, word, suggestions):
"""Show line from file, a misspelled word and request replacement."""
if word not in self.last_suggestions:
self.last_suggestions[word] = suggestions[0]
line = get_a_line(filename, lineno)
sys.stdout.write('> %s\nReplace "%s" with one of %s\nChoose [%s]:' %
(line, word, ','.join(suggestions),
self.last_suggestions[word]))
suggestion = sys.stdin.readline().strip()
if not suggestion:
suggestion = self.last_suggestions[word]
else:
self.last_suggestions[word] = suggestion
return suggestion
def parse_file_list(filename):
"""Show a line from a file in context."""
f = sys.stdin
try:
if filename != '-':
f = io.open(filename, 'r')
return [line.strip() for line in f]
except IOError:
raise
finally:
if f != sys.stdin:
f.close()
def esc_sed(raw_text):
"""Escape chars for a sed command on a shell command line."""
return raw_text.replace('"', '\\"').replace('/', '\\/')
def esc_file(raw_text):
"""Escape chars for a file name on a shell command line."""
return raw_text.replace('\'', '\'"\'"\'')
# Output routines.
def output_normal(ms, filenames, output):
"""Print a list of misspelled words and their corrections.
Return True if misspellings are found.
"""
found = False
for filename in filenames:
errors, results = ms.check(filename)
for res in results:
output.write(
'%s:%d: %s -> %s\n' %
(res[0], res[1], res[2], ','.join(
['"%s"' % w for w in ms.suggestions(res[2])])))
found = True
for err in errors:
sys.stderr.write('ERROR: %s\n' % err)
output.flush()
return found
def output_sed_script(ms, parser, args, filenames):
"""Output a series of portable sed commands to change the file."""
if os.path.exists(args.script_output):
# Emit an error is the file already exists in case the user
# forgets to give the file - but does give source files.
parser.error('The sed script file "%s" must not exist.' %
args.script_output)
sg = Suggestions()
with io.open(args.script_output, 'w') as sed_script:
for filename in filenames:
errors, results = ms.check(filename)
for res in results:
suggestions = ms.suggestions(res[2])
if len(suggestions) == 1:
suggestion = suggestions[0]
else:
suggestion = sg.get_suggestion(
res[0], res[1], res[2], suggestions)
if suggestion != res[2]:
sed_script.write(
'cp "%(f)s" "%(f)s,"\n'
'sed "%(lc)ds/%(wrd)s/%(rep)s/" "%(f)s" > "%(f)s,"\n'
'mv "%(f)s," "%(f)s"\n'
% {'f': esc_file(res[0]), 'lc': res[1],
'wrd': esc_sed(res[2]), 'rep': esc_sed(suggestion)})
for err in errors:
sys.stderr.write('ERROR: %s\n' % err)
def expand_directories(path_list):
"""Return list with directories replaced their contained files."""
for path in path_list:
if os.path.isdir(path):
for root, dirnames, filenames in os.walk(path):
for name in filenames:
if not EXCLUDED_RE.search(name):
yield os.path.join(root, name)
dirnames[:] = [d for d in dirnames
if not EXCLUDED_DIRS_RE.match(d)]
else:
yield path
def _main():
"""Internal main entry point."""
parser = argparse.ArgumentParser(
description=__doc__.split('\n')[0] if __doc__ else None,
prog='misspellings')
parser.add_argument('-f', dest='file_list', metavar='file',
help='file containing list of files to check')
parser.add_argument('-m', dest='ms_file', metavar='file',
help='file containing list of misspelled words and'
'corrections')
parser.add_argument('-d', dest='dump_ms', action='store_true',
help='dump the list of misspelled words')
parser.add_argument('-s', dest='script_output', metavar='file',
help='create a shell script to interactively correct '
'the files - script saved to the given file')
parser.add_argument('--version', action='version',
version='%(prog)s ' + misspellings.__version__)
parser.add_argument('files', nargs='*',
help='files to check')
args = parser.parse_args()
if args.file_list:
try:
args.files += parse_file_list(args.file_list)
except IOError as exception:
parser.error(exception)
output = codecs.getwriter('utf-8')(sys.stdout.buffer
if hasattr(sys.stdout, 'buffer')
else sys.stdout)
ms = misspellings.Misspellings(misspelling_file=args.ms_file)
args.files = expand_directories(args.files)
if args.dump_ms:
for word, correction in ms.dump_misspelling_list():
output.write('%s %s\n' % (word, correction))
if args.script_output:
output_sed_script(ms, parser, args, filenames=args.files)
else:
return 2 if output_normal(ms,
filenames=args.files,
output=output) else 0
def main():
"""Main entry point."""
try:
# Exit on broken pipe.
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
except AttributeError: # pragma: no cover
# SIGPIPE is not available on Windows.
pass
try:
return _main()
except KeyboardInterrupt: # pragma: no cover
return 2 # pragma: no cover
if __name__ == '__main__':
sys.exit(main())