Skip to content

Commit 4ccd5cb

Browse files
committed
Add option to skip paths matching glob
1 parent 6183f80 commit 4ccd5cb

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

TODO

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
- Add option to disable changes to source code, allowing them only on comments
22
and text files
33

4-
- Add option to exclude paths. codespell already ignores .git and .svn
5-
subdirectories. It'd be good to give the user to ignore certain paths. E.g:
6-
.eps files -- they are not binary files but codespell shouldn't check them
7-
whatsoever.
8-
94
- Add option to ignore big files. The biggest issue is if you try to run
105
codespell with a file in the source code tree like cscope.out. I don't know
116
if the best approach is to filter by name or by size

codespell.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import re
2323
from optparse import OptionParser
2424
import os
25+
import fnmatch
2526

2627
USAGE = """
2728
\t%prog [OPTIONS] dict_filename [file1 file2 ... fileN]
@@ -50,6 +51,22 @@ class QuietLevels:
5051
NON_AUTOMATIC_FIXES = 8
5152
FIXES = 16
5253

54+
class GlobMatch:
55+
def __init__(self, pattern):
56+
if pattern:
57+
self.pattern_list = pattern.split(',')
58+
else:
59+
self.pattern_list = None
60+
61+
def match(self, filename):
62+
if self.pattern_list is None:
63+
return False
64+
65+
for p in self.pattern_list:
66+
if fnmatch.fnmatch(filename, p):
67+
return True
68+
69+
return False
5370

5471
class Misspell:
5572
def __init__(self, data, fix, reason):
@@ -188,6 +205,14 @@ def parse_options(args):
188205
action = 'store_true', default = False,
189206
help = 'print summary of fixes')
190207

208+
parser.add_option('-S', '--skip',
209+
help = 'Comma-separated list of files to skip. It '\
210+
'accepts globs as well. E.g.: if you want '\
211+
'codespell to skip .eps and .txt files, '\
212+
'you\'d give "*.eps,*.txt" to this option. '\
213+
'It is expecially useful if you are using in '\
214+
'conjunction with -r option.')
215+
191216
parser.add_option('-x', '--exclude-file',
192217
help = 'FILE with lines that should not be changed',
193218
metavar='FILE')
@@ -463,6 +488,8 @@ def main(*args):
463488

464489
fileopener = FileOpener(options.hard_encoding_detection)
465490

491+
glob_match = GlobMatch(options.skip)
492+
466493
for filename in args[1:]:
467494
# ignore hidden files
468495
if ishidden(filename):
@@ -483,7 +510,8 @@ def main(*args):
483510
for file in files:
484511
if os.path.islink(file):
485512
continue
486-
513+
if glob_match.match(file):
514+
continue
487515
parse_file(os.path.join(root, file), colors, summary)
488516

489517
continue

0 commit comments

Comments
 (0)