2222
2323import json
2424import pathlib
25+ from collections import defaultdict
2526
2627import jsonschema
2728from hoppr_cyclonedx_models .cyclonedx_1_4 import Component
4041)
4142
4243
43- def get_bom (cyclonedx_document : dict ):
44+ def get_bom (cyclonedx_document ):
4445 """
45- Return CycloneDx BOM object.
46+ Return CycloneDX BOM object.
4647 """
4748 return Bom_1_4 (** cyclonedx_document )
4849
4950
5051def get_components (bom : Bom_1_4 ):
5152 """
52- Return list of components from CycloneDx BOM.
53+ Return list of components from CycloneDX BOM.
5354 """
5455 return recursive_component_collector (bom .components , [])
5556
5657
5758def bom_attributes_to_dict (cyclonedx_attributes ):
5859 """
59- Return list dict from a list of CycloneDx attributes.
60+ Return list of dict from a list of CycloneDX attributes.
6061 """
6162 if not cyclonedx_attributes :
62- return {}
63+ return []
6364
6465 return [
6566 json .loads (attribute .json (exclude_unset = True , by_alias = True ))
@@ -75,11 +76,10 @@ def recursive_component_collector(root_component_list, collected):
7576 return
7677
7778 for component in root_component_list :
78- extra_data = (
79- bom_attributes_to_dict (component .components )
80- if component .components is not None
81- else {}
82- )
79+ extra_data = {}
80+ if component .components is not None :
81+ extra_data = bom_attributes_to_dict (component .components )
82+
8383 collected .append ({"cdx_package" : component , "nested_components" : extra_data })
8484 recursive_component_collector (component .components , collected )
8585 return collected
@@ -104,12 +104,13 @@ def get_declared_licenses(licenses):
104104 if not licenses :
105105 return ""
106106
107- return "\n " .join (
108- [resolve_license (license ) for license in bom_attributes_to_dict (licenses )]
109- )
107+ resolved_licenses = [
108+ resolve_license (license ) for license in bom_attributes_to_dict (licenses )
109+ ]
110+ return "\n " .join (resolved_licenses )
110111
111112
112- def get_checksums (component : Component ):
113+ def get_checksums (component ):
113114 """
114115 Return dict of all the checksums from a component.
115116 """
@@ -129,40 +130,24 @@ def get_checksums(component: Component):
129130 }
130131
131132
132- def get_external_refrences (external_references ):
133+ def get_external_references (external_references ):
133134 """
134- Return dict of refrence urls from list of `externalRefrences `.
135+ Return dict of reference urls from list of `externalReferences `.
135136 """
136137 if not external_references :
137138 return {}
138139
139- refrences = {
140- "vcs" : [],
141- "issue-tracker" : [],
142- "website" : [],
143- "advisories" : [],
144- "bom" : [],
145- "mailing-list" : [],
146- "social" : [],
147- "chat" : [],
148- "documentation" : [],
149- "support" : [],
150- "distribution" : [],
151- "license" : [],
152- "build-meta" : [],
153- "build-system" : [],
154- "release-notes" : [],
155- "other" : [],
156- }
140+ refrences = defaultdict (lambda : [])
141+
157142 for ref in external_references :
158143 refrences [ref .type .value ].append (ref .url )
159144
160- return { key : value for key , value in refrences . items () if value }
145+ return dict ( refrences )
161146
162147
163148def validate_document (document , schema = CYCLONEDX_JSON_SCHEMA_PATH ):
164149 """
165- CYCLONEDX document validation.
150+ CycloneDX document validation.
166151 """
167152 if isinstance (document , str ):
168153 document = json .loads (document )
0 commit comments