Skip to content

Commit c64a4f5

Browse files
committed
Implement quiet levels
Add -q (--quiet-level) argument so user can disable messages. Values are a bit mask, and any combination of them is possible.
1 parent 5e28817 commit c64a4f5

2 files changed

Lines changed: 48 additions & 10 deletions

File tree

TODO

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

4-
- Add verbosity levels, so warnings and other messages can be shown or hidden
5-
64
BUGS
75
====

codespell.py

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
misspellings = {}
3232
exclude_lines = set()
3333
options = None
34+
quiet_level = 0
3435
encodings = [ 'utf-8', 'iso-8859-1' ]
3536

3637
#OPTIONS:
@@ -40,6 +41,15 @@
4041
# If set to '-', it will be read from stdin
4142
# file1 .. fileN Files to check spelling
4243

44+
class QuietLevels:
45+
NONE = 0
46+
ENCODING = 1
47+
BINARY_FILE = 2
48+
DISABLED_FIXES = 4
49+
NON_AUTOMATIC_FIXES = 8
50+
FIXES = 16
51+
52+
4353
class Mispell:
4454
def __init__(self, data, fix, reason):
4555
self.data = data
@@ -107,6 +117,18 @@ def parse_options(args):
107117
'choose one fix when more than one is ' \
108118
'available; 3 applies both 1 and 2')
109119

120+
parser.add_option('-q', '--quiet-level',
121+
action='store', type='int', default=0,
122+
help = 'Bitmask that allows codespell to run quietly.'\
123+
'0: the default, in which all messages are '\
124+
'printed. 1: disable warnings about wrong '\
125+
'encoding. 2: disable warnings about binary'\
126+
' file. 4: shut down warnings about automatic'\
127+
' fixes that were disabled in dictionary. '\
128+
'8: don\'t print anything for non-automatic '\
129+
'fixes. 16: don\'t print fixed files.')
130+
131+
110132
(o, args) = parser.parse_args()
111133
if (len(args) < 1):
112134
print('ERROR: you need to specify a dictionary!', file=sys.stderr)
@@ -215,14 +237,16 @@ def parse_file(filename, colors, summary):
215237
global misspellings
216238
global options
217239
global encodings
240+
global quiet_level
218241

219242
if filename == '-':
220243
f = sys.stdin
221244
lines = f.readlines()
222245
else:
223246
# ignore binary files
224247
if not istextfile(filename):
225-
print("WARNING: Binary file: %s " % filename, file=sys.stderr)
248+
if not quiet_level & QuietLevels.BINARY_FILE:
249+
print("WARNING: Binary file: %s " % filename, file=sys.stderr)
226250
return
227251

228252
curr = 0
@@ -232,14 +256,18 @@ def parse_file(filename, colors, summary):
232256
lines = f.readlines()
233257
break
234258
except UnicodeDecodeError:
235-
print('WARNING: Decoding file %s' % filename, file=sys.stderr)
236-
print('WARNING: using encoding=%s failed. '
237-
% encodings[curr], file=sys.stderr)
238259

239-
curr += 1
240-
print('WARNING: Trying next encoding: %s' % encodings[curr],
260+
if not quiet_level & QuietLevels.ENCODING:
261+
print('WARNING: Decoding file %s' % filename,
262+
file=sys.stderr)
263+
print('WARNING: using encoding=%s failed. '
264+
% encodings[curr],
265+
file=sys.stderr)
266+
print('WARNING: Trying next encoding: %s' % encodings[curr],
241267
file=sys.stderr)
242268

269+
curr += 1
270+
243271
finally:
244272
f.close()
245273

@@ -292,10 +320,16 @@ def parse_file(filename, colors, summary):
292320
crightword = "%s%s%s" % (colors.FWORD, fixword, colors.DISABLE)
293321

294322
if misspellings[lword].reason:
323+
if quiet_level & QuietLevels.DISABLED_FIXES:
324+
continue
325+
295326
creason = " | %s%s%s" % (colors.FILE,
296327
misspellings[lword].reason,
297328
colors.DISABLE)
298329
else:
330+
if quiet_level & QuietLevels.NON_AUTOMATIC_FIXES:
331+
continue
332+
299333
creason = ''
300334

301335
if filename != '-':
@@ -318,15 +352,18 @@ def parse_file(filename, colors, summary):
318352
for line in lines:
319353
print(line, end='')
320354
else:
321-
print("%sFIXED:%s %s" % (colors.FWORD, colors.DISABLE, filename),
322-
file=sys.stderr)
355+
if not quiet_level & QuietLevels.FIXES:
356+
print("%sFIXED:%s %s" % (colors.FWORD, colors.DISABLE, filename),
357+
file=sys.stderr)
323358
f = open(filename, 'w')
324359
f.writelines(lines)
325360
f.close()
326361

327362

328363
def main(*args):
329364
global options
365+
global quiet_level
366+
330367
(options, args) = parse_options(args)
331368

332369
build_dict(args[0])
@@ -342,6 +379,9 @@ def main(*args):
342379
if options.exclude_file:
343380
build_exclude_hashes(options.exclude_file)
344381

382+
if options.quiet_level:
383+
quiet_level = options.quiet_level
384+
345385
for filename in args[1:]:
346386
# ignore hidden files
347387
if ishidden(filename):

0 commit comments

Comments
 (0)