3434
3535from attributecode import __about_spec_version__
3636from attributecode import __version__
37- from attributecode import DEFAULT_MAPPING
3837from attributecode import severities
3938from attributecode .attrib import check_template
4039from attributecode .attrib import DEFAULT_TEMPLATE_FILE
4342from attributecode .model import collect_inventory
4443from attributecode .model import write_output
4544from attributecode .util import extract_zip
45+ from attributecode .util import filter_errors
4646
4747
4848__copyright__ = """
@@ -107,7 +107,7 @@ def about():
107107
108108def validate_key_values (ctx , param , value ):
109109 """
110- Return the a mapping of {key: [values,...] if valid or raise a UsageError
110+ Return the a dict of {key: [values,...] if valid or raise a UsageError
111111 otherwise.
112112 """
113113 if not value :
@@ -122,20 +122,6 @@ def validate_key_values(ctx, param, value):
122122 return kvals
123123
124124
125- def validate_mapping (mapping , mapping_file ):
126- """
127- Return a mapping_file or None.
128- Raise a UsageError on errors.
129- """
130- if mapping and mapping_file :
131- raise click .UsageError (
132- 'Invalid options combination: '
133- '--mapping and --mapping-file are mutually exclusive.' )
134- if mapping :
135- return DEFAULT_MAPPING
136- return mapping_file or None
137-
138-
139125def validate_extensions (ctx , param , value , extensions = tuple (('.csv' , '.json' ,))):
140126 if not value :
141127 return
@@ -171,19 +157,6 @@ def validate_extensions(ctx, param, value, extensions=tuple(('.csv', '.json',)))
171157 type = click .Choice (['json' , 'csv' ]),
172158 help = 'Set OUTPUT inventory file format.' )
173159
174- @click .option ('--mapping' ,
175- is_flag = True ,
176- help = 'Use the default built-in "mapping.config" file '
177- 'with mapping between input keys and .ABOUT field names.'
178- 'Cannot be combined with the --mapping-file option.' )
179-
180- @click .option ('--mapping-file' ,
181- metavar = 'FILE' ,
182- type = click .Path (exists = True , dir_okay = False , readable = True , resolve_path = True ),
183- help = 'Path to an optional custom mapping FILE '
184- 'with mapping between input keys and .ABOUT field names. '
185- 'Cannot be combined with the --mapping option.' )
186-
187160@click .option ('-q' , '--quiet' ,
188161 is_flag = True ,
189162 help = 'Do not print error or warning messages.' )
@@ -194,8 +167,7 @@ def validate_extensions(ctx, param, value, extensions=tuple(('.csv', '.json',)))
194167
195168@click .help_option ('-h' , '--help' )
196169
197- def inventory (location , output , mapping , mapping_file ,
198- format , quiet , verbose ): # NOQA
170+ def inventory (location , output , format , quiet , verbose ): # NOQA
199171 """
200172Collect the inventory of .ABOUT file data as CSV or JSON.
201173
@@ -212,9 +184,7 @@ def inventory(location, output, mapping, mapping_file,
212184 # accept zipped ABOUT files as input
213185 location = extract_zip (location )
214186
215- mapping_file = validate_mapping (mapping , mapping_file )
216-
217- errors , abouts = collect_inventory (location , mapping_file = mapping_file )
187+ errors , abouts = collect_inventory (location )
218188
219189 # Do not write the output if one of the ABOUT files has duplicated keys
220190 # TODO: why do this check here?? Also if this is the place, we should list what the errors are.
@@ -272,19 +242,6 @@ def inventory(location, output, mapping, mapping_file,
272242 type = click .Path (exists = True , file_okay = False , readable = True , resolve_path = True ),
273243 help = 'Path to a directory with reference license data and text files.' )
274244
275- @click .option ('--mapping' ,
276- is_flag = True ,
277- help = 'Use the default built-in "mapping.config" file '
278- 'with mapping between input keys and .ABOUT field names.'
279- 'Cannot be combined with the --mapping-file option.' )
280-
281- @click .option ('--mapping-file' ,
282- metavar = 'FILE' ,
283- type = click .Path (exists = True , dir_okay = False , readable = True , resolve_path = True ),
284- help = 'Path to an optional custom mapping FILE '
285- 'with mapping between input keys and .ABOUT field names. '
286- 'Cannot be combined with the --mapping option.' )
287-
288245@click .option ('-q' , '--quiet' ,
289246 is_flag = True ,
290247 help = 'Do not print error or warning messages.' )
@@ -295,11 +252,7 @@ def inventory(location, output, mapping, mapping_file,
295252
296253@click .help_option ('-h' , '--help' )
297254
298- def gen (location , output ,
299- fetch_license ,
300- reference ,
301- mapping , mapping_file ,
302- quiet , verbose ):
255+ def gen (location , output , fetch_license , reference , quiet , verbose ):
303256 """
304257Generate .ABOUT files in OUTPUT from an inventory of .ABOUT files at LOCATION.
305258
@@ -311,8 +264,6 @@ def gen(location, output,
311264 print_version ()
312265 click .echo ('Generating .ABOUT files...' )
313266
314- mapping_file = validate_mapping (mapping , mapping_file )
315-
316267 if not location .endswith (('.csv' , '.json' ,)):
317268 raise click .UsageError ('ERROR: Invalid input file extension: must be one .csv or .json.' )
318269
@@ -321,7 +272,6 @@ def gen(location, output,
321272 base_dir = output ,
322273 reference_dir = reference ,
323274 fetch_license = fetch_license ,
324- mapping_file = mapping_file
325275 )
326276
327277 errors_count = report_errors (errors , quiet , verbose , log_file_loc = output + '-error.log' )
@@ -378,25 +328,6 @@ def validate_template(ctx, param, value):
378328 metavar = '<key>=<value>' ,
379329 help = 'Add variable text as key=value for use in a custom attribution template.' )
380330
381- @click .option ('--inventory' ,
382- metavar = 'FILE' ,
383- type = click .Path (exists = True , dir_okay = False , resolve_path = True ),
384- help = 'Path to an optional JSON or CSV inventory FILE listing the '
385- 'subset of .ABOUT files paths to consider when generating the attribution document.' )
386-
387- @click .option ('--mapping' ,
388- is_flag = True ,
389- help = 'Use the default built-in "mapping.config" file '
390- 'with mapping between input keys and .ABOUT field names.'
391- 'Cannot be combined with the --mapping-file option.' )
392-
393- @click .option ('--mapping-file' ,
394- metavar = 'FILE' ,
395- type = click .Path (exists = True , dir_okay = False , readable = True , resolve_path = True ),
396- help = 'Path to an optional custom mapping FILE '
397- 'with mapping between input keys and .ABOUT field names. '
398- 'Cannot be combined with the --mapping option.' )
399-
400331@click .option ('-q' , '--quiet' ,
401332 is_flag = True ,
402333 help = 'Do not print error or warning messages.' )
@@ -407,9 +338,7 @@ def validate_template(ctx, param, value):
407338
408339@click .help_option ('-h' , '--help' )
409340
410- def attrib (location , output , template , vartext ,
411- inventory , mapping , mapping_file ,
412- quiet , verbose ):
341+ def attrib (location , output , template , vartext , quiet , verbose ):
413342 """
414343Generate an attribution document at OUTPUT using .ABOUT files at LOCATION.
415344
@@ -421,20 +350,17 @@ def attrib(location, output, template, vartext,
421350 print_version ()
422351 click .echo ('Generating attribution...' )
423352
424- mapping_file = validate_mapping (mapping , mapping_file )
425-
426353 # accept zipped ABOUT files as input
427354 if location .lower ().endswith ('.zip' ):
428355 location = extract_zip (location )
429356
430- errors , abouts = collect_inventory (location , mapping_file = mapping_file )
357+ errors , abouts = collect_inventory (location )
431358
432359 attrib_errors = generate_attribution_doc (
433360 abouts = abouts ,
434361 output_location = output ,
435362 template_loc = template ,
436363 variables = vartext ,
437- mapping_file = mapping_file ,
438364 )
439365 errors .extend (attrib_errors )
440366
@@ -531,7 +457,7 @@ def print_config_help(ctx, param, value):
531457
532458def transform (location , output , configuration , quiet , verbose ): # NOQA
533459 """
534- Transform the CSV file at LOCATION by applying renamings, filters and checks
460+ Transform the CSV file at LOCATION by applying renamings, filters and checks
535461and write a new CSV to OUTPUT.
536462
537463LOCATION: Path to a CSV file.
@@ -609,23 +535,14 @@ def get_error_messages(errors, quiet=False, verbose=False):
609535 messages .append (msg )
610536 return messages , severe_errors_count
611537
612-
613- def filter_errors (errors , minimum_severity = WARNING ):
614- """
615- Return a list of unique `errors` Error object filtering errors that have a
616- severity below `minimum_severity`.
617- """
618- return unique ([e for e in errors if e .severity >= minimum_severity ])
619-
620-
621538######################################################################
622539# Misc
623540######################################################################
624541
625542def parse_key_values (key_values ):
626543 """
627544 Given a list of "key=value" strings, return:
628- - a mapping {key: [value, value, ...]}
545+ - a dict {key: [value, value, ...]}
629546 - a sorted list of unique error messages for invalid entries where there is
630547 a missing a key or value.
631548 """
0 commit comments