22# -*- coding: utf8 -*-
33
44# ============================================================================
5- # Copyright (c) 2013-2019 nexB Inc. http://www.nexb.com/ - All rights reserved.
5+ # Copyright (c) 2013-2020 nexB Inc. http://www.nexb.com/ - All rights reserved.
66# Licensed under the Apache License, Version 2.0 (the "License");
77# you may not use this file except in compliance with the License.
88# You may obtain a copy of the License at
4646
4747
4848__copyright__ = """
49- Copyright (c) 2013-2019 nexB Inc and others. All rights reserved.
49+ Copyright (c) 2013-2020 nexB Inc and others. All rights reserved.
5050 Licensed under the Apache License, Version 2.0 (the "License");
5151 you may not use this file except in compliance with the License.
5252 You may obtain a copy of the License at
@@ -210,6 +210,11 @@ def inventory(location, output, format, quiet, verbose): # NOQA
210210 metavar = 'OUTPUT' ,
211211 type = click .Path (exists = True , file_okay = False , writable = True , resolve_path = True ))
212212
213+ @click .option ('--android' ,
214+ is_flag = True ,
215+ help = 'Generate MODULE_LICENSE_XXX (XXX will be replaced by license key) and NOTICE '
216+ 'as the same design as from Android.' )
217+
213218# FIXME: the CLI UX should be improved with two separate options for API key and URL
214219@click .option ('--fetch-license' ,
215220 nargs = 2 ,
@@ -233,7 +238,7 @@ def inventory(location, output, format, quiet, verbose): # NOQA
233238
234239@click .help_option ('-h' , '--help' )
235240
236- def gen (location , output , fetch_license , reference , quiet , verbose ):
241+ def gen (location , output , android , fetch_license , reference , quiet , verbose ):
237242 """
238243Generate .ABOUT files in OUTPUT from an inventory of .ABOUT files at LOCATION.
239244
@@ -245,12 +250,14 @@ def gen(location, output, fetch_license, reference, quiet, verbose):
245250 print_version ()
246251 click .echo ('Generating .ABOUT files...' )
247252
253+ #FIXME: This should be checked in the `click`
248254 if not location .endswith (('.csv' , '.json' ,)):
249255 raise click .UsageError ('ERROR: Invalid input file extension: must be one .csv or .json.' )
250256
251257 errors , abouts = generate_about_files (
252258 location = location ,
253259 base_dir = output ,
260+ android = android ,
254261 reference_dir = reference ,
255262 fetch_license = fetch_license ,
256263 )
@@ -401,17 +408,17 @@ def print_config_help(ctx, param, value):
401408
402409
403410@about .command (cls = AboutCommand ,
404- short_help = 'Transform a CSV by applying renamings, filters and checks.' )
411+ short_help = 'Transform a CSV/JSON by applying renamings, filters and checks.' )
405412
406413@click .argument ('location' ,
407414 required = True ,
408- callback = partial (validate_extensions , extensions = ('.csv' ,)),
415+ callback = partial (validate_extensions , extensions = ('.csv' , '.json' , )),
409416 metavar = 'LOCATION' ,
410417 type = click .Path (exists = True , dir_okay = False , readable = True , resolve_path = True ))
411418
412419@click .argument ('output' ,
413420 required = True ,
414- callback = partial (validate_extensions , extensions = ('.csv' ,)),
421+ callback = partial (validate_extensions , extensions = ('.csv' , '.json' , )),
415422 metavar = 'OUTPUT' ,
416423 type = click .Path (exists = False , dir_okay = False , writable = True , resolve_path = True ))
417424
@@ -438,30 +445,39 @@ def print_config_help(ctx, param, value):
438445
439446def transform (location , output , configuration , quiet , verbose ): # NOQA
440447 """
441- Transform the CSV file at LOCATION by applying renamings, filters and checks
442- and write a new CSV to OUTPUT.
448+ Transform the CSV/JSON file at LOCATION by applying renamings, filters and checks
449+ and write a new CSV/JSON to OUTPUT.
443450
444- LOCATION: Path to a CSV file.
451+ LOCATION: Path to a CSV/JSON file.
445452
446- OUTPUT: Path to CSV inventory file to create.
453+ OUTPUT: Path to CSV/JSON inventory file to create.
447454 """
448455 from attributecode .transform import transform_csv_to_csv
456+ from attributecode .transform import transform_json_to_json
449457 from attributecode .transform import Transformer
450458
451- if not quiet :
452- print_version ()
453- click .echo ('Transforming CSV...' )
454459
455460 if not configuration :
456461 transformer = Transformer .default ()
457462 else :
458463 transformer = Transformer .from_file (configuration )
459464
460- errors = transform_csv_to_csv (location , output , transformer )
465+ if location .endswith ('.csv' ) and output .endswith ('.csv' ):
466+ errors = transform_csv_to_csv (location , output , transformer )
467+ elif location .endswith ('.json' ) and output .endswith ('.json' ):
468+ errors = transform_json_to_json (location , output , transformer )
469+ else :
470+ msg = 'Extension for the input and output need to be the same.'
471+ click .echo (msg )
472+ sys .exit ()
473+
474+ if not quiet :
475+ print_version ()
476+ click .echo ('Transforming...' )
461477
462478 errors_count = report_errors (errors , quiet , verbose , log_file_loc = output + '-error.log' )
463479 if not quiet and not errors :
464- msg = 'Transformed CSV written to {output}.' .format (** locals ())
480+ msg = 'Transformed file written to {output}.' .format (** locals ())
465481 click .echo (msg )
466482 sys .exit (errors_count )
467483
0 commit comments