Skip to content

Commit 79a0922

Browse files
committed
Implement --show-all options to all the subcommands.
Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 195d5a2 commit 79a0922

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

src/attributecode/cmd.py

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,20 @@ def cli():
110110
type=click.Choice(['json', 'csv']),
111111
help='Set OUTPUT inventory file format.')
112112

113+
@click.option('--show-all', is_flag=True, default=False,
114+
help='Show all errors and warnings. '
115+
'By default, the tool only prints these '
116+
'error levels: CRITICAL, ERROR, and WARNING. '
117+
'Use this option to print all errors and warning '
118+
'for any level.'
119+
)
120+
113121
@click.option('-q', '--quiet', is_flag=True,
114122
help='Do not print error or warning messages.')
115123

116124
@click.help_option('-h', '--help')
117125

118-
def inventory(location, output, quiet, format):
126+
def inventory(location, output, quiet, format, show_all):
119127
"""
120128
Collect a JSON or CSV inventory of components from .ABOUT files.
121129
@@ -143,7 +151,8 @@ def inventory(location, output, quiet, format):
143151
write_errors = model.write_output(abouts, output, format)
144152
for err in write_errors:
145153
errors.append(err)
146-
log_errors(errors, quiet, os.path.dirname(output))
154+
log_errors(errors, quiet, show_all, os.path.dirname(output))
155+
sys.exit(0)
147156

148157

149158
######################################################################
@@ -178,12 +187,20 @@ def inventory(location, output, quiet, format):
178187
@click.option('--mapping', is_flag=True,
179188
help='Use file mapping.config with mapping between input keys and ABOUT field names')
180189

190+
@click.option('--show-all', is_flag=True, default=False,
191+
help='Show all errors and warnings. '
192+
'By default, the tool only prints these '
193+
'error levels: CRITICAL, ERROR, and WARNING. '
194+
'Use this option to print all errors and warning '
195+
'for any level.'
196+
)
197+
181198
@click.option('-q', '--quiet', is_flag=True,
182199
help='Do not print error or warning messages.')
183200

184201
@click.help_option('-h', '--help')
185202

186-
def gen(location, output, mapping, license_notice_text_location, fetch_license, quiet):
203+
def gen(location, output, mapping, license_notice_text_location, fetch_license, quiet, show_all):
187204
"""
188205
Generate .ABOUT files in OUTPUT directory from a JSON or CSV inventory of .ABOUT files at LOCATION.
189206
@@ -214,8 +231,8 @@ def gen(location, output, mapping, license_notice_text_location, fetch_license,
214231
error_count = error_count + 1
215232
click.echo(
216233
'Generated %(about_count)d .ABOUT files with %(error_count)d errors or warnings' % locals())
217-
log_errors(errors, quiet, output)
218-
# FIXME: return error code?
234+
log_errors(errors, quiet, show_all, output)
235+
sys.exit(0)
219236

220237

221238
######################################################################
@@ -241,6 +258,14 @@ def gen(location, output, mapping, license_notice_text_location, fetch_license,
241258
help='Use the file "mapping.config" with mappings between the CSV '
242259
'inventory columns names and .ABOUT field names')
243260

261+
@click.option('--show-all', is_flag=True, default=False,
262+
help='Show all errors and warnings. '
263+
'By default, the tool only prints these '
264+
'error levels: CRITICAL, ERROR, and WARNING. '
265+
'Use this option to print all errors and warning '
266+
'for any level.'
267+
)
268+
244269
@click.option('--template', type=click.Path(exists=True), nargs=1,
245270
help='Path to an optional custom attribution template used for generation.')
246271

@@ -249,7 +274,7 @@ def gen(location, output, mapping, license_notice_text_location, fetch_license,
249274

250275
@click.help_option('-h', '--help')
251276

252-
def attrib(location, output, template, mapping, inventory, quiet):
277+
def attrib(location, output, template, mapping, inventory, quiet, show_all):
253278
"""
254279
Generate an attribution document at OUTPUT using .ABOUT files at LOCATION.
255280
@@ -273,9 +298,9 @@ def attrib(location, output, template, mapping, inventory, quiet):
273298
for no_match_error in no_match_errors:
274299
inv_errors.append(no_match_error)
275300

276-
log_errors(inv_errors, quiet, os.path.dirname(output))
301+
log_errors(inv_errors, quiet, show_all, os.path.dirname(output))
277302
click.echo('Finished.')
278-
# FIXME: return error code?
303+
sys.exit(0)
279304

280305

281306
######################################################################
@@ -289,9 +314,9 @@ def attrib(location, output, template, mapping, inventory, quiet):
289314

290315
@click.option('--show-all', is_flag=True, default=False,
291316
help='Show all errors and warnings. '
292-
'By default, running a check only reports these '
317+
'By default, the tool only prints these '
293318
'error levels: CRITICAL, ERROR, and WARNING. '
294-
'Use this option to report all errors and warning '
319+
'Use this option to print all errors and warning '
295320
'for any level.'
296321
)
297322

@@ -329,10 +354,10 @@ def check(location, show_all):
329354
sys.exit(1)
330355
else:
331356
click.echo('No error found.')
332-
# FIXME: return error code?
357+
sys.exit(0)
333358

334359

335-
def log_errors(errors, quiet, base_dir=False):
360+
def log_errors(errors, quiet, show_all, base_dir=False):
336361
"""
337362
Iterate of sequence of Error objects and print and log errors with
338363
a severity superior or equal to level.
@@ -361,7 +386,10 @@ def log_errors(errors, quiet, base_dir=False):
361386
for severity, message in errors:
362387
sever = severities[severity]
363388
if not quiet:
364-
print(msg_format % locals())
389+
if show_all:
390+
print(msg_format % locals())
391+
elif sever in problematic_errors:
392+
print(msg_format % locals())
365393
if base_dir:
366394
file_logger.log(severity, msg_format % locals())
367395

0 commit comments

Comments
 (0)