Skip to content

Commit 2d73d9f

Browse files
Nicolaus Weidnerarmintaenzertng
authored andcommitted
[issue-359] Extract common method to get all spdx element ids inside a document
Signed-off-by: Nicolaus Weidner <nicolaus.weidner@tngtech.com>
1 parent 7f448b4 commit 2d73d9f

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/document_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) 2022 spdx contributors
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
from typing import List
12+
13+
from src.model.document import Document
14+
15+
16+
def get_contained_spdx_element_ids(document: Document) -> List[str]:
17+
element_ids = [file.spdx_id for file in document.files]
18+
element_ids.extend([package.spdx_id for package in document.packages])
19+
element_ids.extend([snippet.spdx_id for snippet in document.snippets])
20+
return element_ids

src/jsonschema/document_converter.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# limitations under the License.
1111
from typing import Type, Any
1212

13+
from src.document_utils import get_contained_spdx_element_ids
1314
from src.jsonschema.annotation_converter import AnnotationConverter
1415
from src.jsonschema.converter import TypedConverter
1516
from src.jsonschema.creation_info_converter import CreationInfoConverter
@@ -65,9 +66,7 @@ def _get_property_value(self, document: Document, document_property: DocumentPro
6566
return document.creation_info.spdx_id
6667
elif document_property == DocumentProperty.ANNOTATIONS:
6768
# annotations referencing files, packages or snippets will be added to those elements directly
68-
element_ids = [file.spdx_id for file in document.files]
69-
element_ids.extend([package.spdx_id for package in document.packages])
70-
element_ids.extend([snippet.spdx_id for snippet in document.snippets])
69+
element_ids = get_contained_spdx_element_ids(document)
7170
document_annotations = filter(lambda annotation: annotation.spdx_id not in element_ids,
7271
document.annotations)
7372
return [self.annotation_converter.convert(annotation) for annotation in document_annotations] or None

src/validation/spdx_id_validators.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import re
1313
from typing import List
1414

15+
from src.document_utils import get_contained_spdx_element_ids
1516
from src.model.document import Document
1617
from src.model.file import File
1718

@@ -36,10 +37,7 @@ def is_spdx_id_present_in_document(spdx_id: str, document: Document) -> bool:
3637

3738
def get_list_of_all_spdx_ids(document: Document) -> List[str]:
3839
all_spdx_ids_in_document: List[str] = [document.creation_info.spdx_id]
39-
40-
all_spdx_ids_in_document.extend([package.spdx_id for package in document.packages])
41-
all_spdx_ids_in_document.extend([file.spdx_id for file in document.files])
42-
all_spdx_ids_in_document.extend([snippet.spdx_id for snippet in document.snippets])
40+
all_spdx_ids_in_document.extend(get_contained_spdx_element_ids(document))
4341

4442
return all_spdx_ids_in_document
4543

0 commit comments

Comments
 (0)