22
33import sys
44import os
5+ import fnmatch
56import glob
67import argparse
78import re
1415from comment_parser import comment_parser
1516
1617
17- # Split a camel case string into individual words.
18+ SUFFIX2MIME = {
19+ '.h' : 'text/x-c++' ,
20+ '.cxx' : 'text/x-c++' ,
21+ '.c' : 'text/x-c++' ,
22+ '.hxx' : 'text/x-c++' ,
23+ '.py' : 'text/x-python' ,
24+ '.ruby' : 'text/x-ruby' ,
25+ '.java' : 'text/x-java-source' ,
26+ '.txt' : 'text/plain' ,
27+ '.rst' : 'text/plain' ,
28+ '.md' : 'text/plain' ,
29+ }
30+
31+
1832def splitCamelCase (word ):
33+ """Split a camel case string into individual words."""
1934
2035 result = []
2136
@@ -34,33 +49,17 @@ def splitCamelCase(word):
3449 return result
3550
3651
37- # Map file suffix to file type.
3852def getMimeType (filepath ):
39-
40- suffix2mime = { '.h' : 'text/x-c++' ,
41- '.cxx' : 'text/x-c++' ,
42- '.c' : 'text/x-c++' ,
43- '.hxx' : 'text/x-c++' ,
44- '.py' : 'text/x-python' ,
45- '.ruby' : 'text/x-ruby' ,
46- '.java' : 'text/x-java-source' ,
47- '.txt' : 'text/plain' ,
48- '.rst' : 'text/plain' ,
49- '.md' : 'text/plain' ,
50- }
53+ """Map `filepath`` extension to file type."""
5154 name , ext = os .path .splitext (filepath )
55+ return SUFFIX2MIME .get (ext , 'text/plain' )
5256
53- if ext in suffix2mime :
54- return suffix2mime [ext ]
55- else :
56- return 'text/plain'
5757
58-
59- #
60- # For a regular text file, we don't need to parse it for comments. We
61- # just pass every line to the spell checked
62- #
6358def load_text_file (filename ):
59+ """
60+ For a regular text file, we don't need to parse it for comments. We
61+ just pass every line to the spell checked.
62+ """
6463
6564 output = []
6665 lc = 0
@@ -73,10 +72,9 @@ def load_text_file(filename):
7372 return output
7473
7574
76- # The main spell checking procedure
77- #
7875def spell_check_file (filename , spell_checker , mime_type = '' ,
7976 output_lvl = 1 , prefixes = []):
77+ """Check spelling in ``filename``."""
8078
8179 if len (mime_type ) == 0 :
8280 mime_type = getMimeType (filename )
@@ -175,9 +173,9 @@ def spell_check_file(filename, spell_checker, mime_type='',
175173 return bad_words
176174
177175
178- # Does the file match any pattern in the exclude_list? Then exclude it.
179- #
180176def exclude_check (name , exclude_list ):
177+ """Return True if ``name`` matches any of the regular expressions listed in
178+ ``exclude_list``."""
181179 if exclude_list is None :
182180 return False
183181 for pattern in exclude_list :
@@ -187,6 +185,17 @@ def exclude_check(name, exclude_list):
187185 return False
188186
189187
188+ def skip_check (name , skip_list ):
189+ """Return True if ``name`` matches any of the glob pattern listed in
190+ ``skip_list``."""
191+ if skip_list is None :
192+ return False
193+ for skip in "," .join (skip_list ).split ("," ):
194+ if fnmatch .fnmatch (name , skip ):
195+ return True
196+ return False
197+
198+
190199def parse_args ():
191200 parser = argparse .ArgumentParser ()
192201
@@ -204,13 +213,20 @@ def parse_args():
204213 parser .add_argument ('--vim' , '-V' , action = 'store_true' , default = False ,
205214 dest = 'vim' , help = 'Output results in vim command format' )
206215
207- parser .add_argument ('--dict' , '-d' , action = 'append' ,
216+ parser .add_argument ('--dict' , '-d' , '--ignore-words' , '-I' , action = 'append' ,
208217 dest = 'dict' ,
209- help = 'Add a dictionary (multiples allowed)' )
218+ help = 'File that contains words that will be ignored (multiples allowed).'
219+ ' File must contain 1 word per line.' )
210220
211221 parser .add_argument ('--exclude' , '-e' , action = 'append' ,
212222 dest = 'exclude' ,
213- help = 'Add exclude regex (multiples allowed)' )
223+ help = 'Specify regex for excluding files (multiples allowed)' )
224+
225+ parser .add_argument ('--skip' , '-S' , action = 'append' ,
226+ help = 'comma-separated list of files to skip. It '
227+ 'accepts globs as well. E.g.: if you want '
228+ 'codespell to skip .eps and .txt files, '
229+ 'you\' d give "*.eps,*.txt" to this option.' )
214230
215231 parser .add_argument ('--prefix' , '-p' , action = 'append' , default = [],
216232 dest = 'prefixes' ,
@@ -228,9 +244,9 @@ def parse_args():
228244 return args
229245
230246
231- # Add the words from a dictionary file into our spell checking dictionary.
232- #
233247def add_dict (enchant_dict , filename ):
248+ """Update ``enchant_dict`` spell checking dictionary with the words listed
249+ in ``filename`` (one word per line)."""
234250 with open (filename ) as f :
235251 lines = f .read ().splitlines ()
236252
@@ -274,15 +290,20 @@ def main():
274290 if len (args .filenames ):
275291 file_list = args .filenames
276292 else :
277- sys . exit ( 0 )
293+ file_list = [ '.' ]
278294
279295 prefixes = ['sitk' , 'itk' , 'vtk' ] + args .prefixes
280296
281297 bad_words = []
282298
299+ if not args .suffix :
300+ suffixes = list (SUFFIX2MIME .keys ())
301+ else :
302+ suffixes = args .suffix
303+
283304 if output_lvl > 1 :
284305 print ("Prefixes:" , prefixes )
285- print ("Suffixes:" , args . suffix )
306+ print ("Suffixes:" , suffixes )
286307
287308 #
288309 # Spell check the files
@@ -297,7 +318,7 @@ def main():
297318
298319 # f is a directory, so search for files inside
299320 dir_entries = []
300- for s in args . suffix :
321+ for s in suffixes :
301322 dir_entries = dir_entries + glob .glob (f + '/**/*' + s , recursive = True )
302323
303324 if output_lvl > 0 :
@@ -306,7 +327,7 @@ def main():
306327 # spell check the files found in f
307328 for x in dir_entries :
308329
309- if exclude_check (x , args .exclude ):
330+ if exclude_check (x , args .exclude ) or skip_check ( x , args . skip ) :
310331 if not args .miss :
311332 print ("\n Excluding" , x )
312333 continue
@@ -321,7 +342,7 @@ def main():
321342 else :
322343
323344 # f is a file
324- if exclude_check (f , args .exclude ):
345+ if exclude_check (f , args .exclude ) or skip_check ( f , args . skip ) :
325346 if not args .miss :
326347 print ("\n Excluding" , x )
327348 continue
0 commit comments