Skip to content

Commit 5c29f3e

Browse files
committed
Fixed #446
* Better error handling * provide data for `license_key_to_license_name` if only license_key is provided
1 parent 564c3f2 commit 5c29f3e

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

docs/CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Add support for `package_url` #396
55
* Fixed #443 and #444 issue with multiple licenses/license_files
66
* Fixed #442 no special characters allowed for `license_key`, `license_name` and `license_expression`
7+
* Fixed #446 Better error handling
78

89
2020-08-11
910
Release 5.0.0

src/attributecode/attrib.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from attributecode import ERROR
3131
from attributecode import Error
3232
from attributecode.licenses import COMMON_LICENSES
33-
from attributecode.model import parse_license_expression
33+
from attributecode.model import detect_special_char, parse_license_expression
3434
from attributecode.util import add_unc
3535
from attributecode.attrib_util import multi_sort
3636

@@ -88,9 +88,22 @@ def generate(abouts, template=None, variables=None):
8888
sorted_license_key_and_context = collections.OrderedDict(sorted(license_key_and_context.items()))
8989
license_file_name_and_key[license_text_name] = license_key
9090

91-
# Convert/map the key in license expression to license name
92-
if about.license_expression.value and about.license_name.value:
93-
special_char_in_expression, lic_list = parse_license_expression(about.license_expression.value)
91+
# Convert/map the key to name
92+
if (about.license_expression.value or about.license_key.value) and about.license_name.value:
93+
if about.license_expression.value:
94+
special_char, lic_list = parse_license_expression(about.license_expression.value)
95+
else:
96+
lic_list = about.license_key.value
97+
special_char = []
98+
for lic in lic_list:
99+
special_char_list = detect_special_char(lic)
100+
if special_char_list:
101+
for char in special_char_list:
102+
special_char.append(char)
103+
if special_char:
104+
error = Error(CRITICAL, 'Special character(s) are not allowed in '
105+
'license_expression or license_key: %s' % special_char)
106+
return error, ''
94107
lic_name_list = about.license_name.value
95108
lic_name_expression_list = []
96109

@@ -204,4 +217,4 @@ def generate_and_save(abouts, output_location, template_loc=None, variables=None
204217
with io.open(output_location, 'w', encoding='utf-8') as of:
205218
of.write(rendered)
206219

207-
return errors
220+
return errors, rendered

src/attributecode/cmd.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def attrib(location, output, template, vartext, quiet, verbose):
346346

347347
errors, abouts = collect_inventory(location)
348348

349-
attrib_errors = generate_attribution_doc(
349+
attrib_errors, rendered = generate_attribution_doc(
350350
abouts=abouts,
351351
output_location=output,
352352
template_loc=template,
@@ -357,8 +357,12 @@ def attrib(location, output, template, vartext, quiet, verbose):
357357
errors_count = report_errors(errors, quiet, verbose, log_file_loc=output + '-error.log')
358358

359359
if not quiet:
360-
msg = 'Attribution generated in: {output}'.format(**locals())
361-
click.echo(msg)
360+
if rendered:
361+
msg = 'Attribution generated in: {output}'.format(**locals())
362+
click.echo(msg)
363+
else:
364+
msg = 'Attribution generation failed.'
365+
click.echo(msg)
362366
sys.exit(errors_count)
363367

364368

0 commit comments

Comments
 (0)