Skip to content

Commit 033df51

Browse files
committed
Add null handling when parsing scancode items
1 parent 776f53c commit 033df51

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

src/fosslight_source/_parsing_scancode_file_item.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,23 @@ def parsing_file_item(scancode_file_list, has_error, need_matched_license=False)
5353
if scancode_file_list:
5454
for file in scancode_file_list:
5555
try:
56-
is_binary = False
5756
is_dir = False
58-
file_path = file["path"]
59-
60-
if "is_binary" in file:
61-
is_binary = file["is_binary"]
57+
file_path = file.get("path", "")
58+
is_binary = file.get("is_binary", False)
6259
if "type" in file:
6360
is_dir = file["type"] == "directory"
6461
if is_dir:
6562
prev_dir_value = is_exclude_dir(file_path)
6663
prev_dir = file_path
6764

6865
if not is_binary and not is_dir:
69-
licenses = file["licenses"]
70-
copyright_list = file["copyrights"]
66+
licenses = file.get("licenses", [])
67+
copyright_list = file.get("copyrights", [])
7168

7269
result_item = ScanItem(file_path)
7370

7471
if has_error and "scan_errors" in file:
75-
error_msg = file["scan_errors"]
72+
error_msg = file.get("scan_errors", [])
7673
if len(error_msg) > 0:
7774
result_item.comment = ",".join(error_msg)
7875
scancode_file_item.append(result_item)
@@ -95,8 +92,8 @@ def parsing_file_item(scancode_file_list, has_error, need_matched_license=False)
9592

9693
for lic_item in licenses:
9794
license_value = ""
98-
key = lic_item["key"]
99-
spdx = lic_item["spdx_license_key"]
95+
key = lic_item.get("key", "")
96+
spdx = lic_item.get("spdx_license_key", "")
10097
# logger.debug("LICENSE_KEY:"+str(key)+",SPDX:"+str(spdx))
10198

10299
if key is not None and key != "":
@@ -111,11 +108,10 @@ def parsing_file_item(scancode_file_list, has_error, need_matched_license=False)
111108
if license_value != "":
112109
if key == "unknown-spdx":
113110
try:
114-
if "matched_text" in lic_item:
115-
matched_txt = lic_item["matched_text"].lower()
116-
matched = regex.search(matched_txt)
117-
if matched:
118-
license_value = str(matched.group())
111+
matched_txt = lic_item.get("matched_text", "").lower()
112+
matched = regex.search(matched_txt)
113+
if matched:
114+
license_value = str(matched.group())
119115
except Exception:
120116
pass
121117

@@ -136,9 +132,8 @@ def parsing_file_item(scancode_file_list, has_error, need_matched_license=False)
136132
lic_info = MatchedLicense(license_value, lic_category, lic_matched_text, file_path)
137133
license_list[lic_matched_key] = lic_info
138134

139-
matched_rule = lic_item["matched_rule"]
140-
if matched_rule["is_license_text"]:
141-
result_item.is_license_text = True
135+
matched_rule = lic_item.get("matched_rule", {})
136+
result_item.is_license_text = matched_rule.get("is_license_text", False)
142137

143138
if len(license_detected) > 0:
144139
result_item.licenses = license_detected

0 commit comments

Comments
 (0)