Skip to content

Commit e68d842

Browse files
author
Nicolaus Weidner
committed
[issue-359] Add json writer
Signed-off-by: Nicolaus Weidner <nicolaus.weidner@tngtech.com>
1 parent 7897945 commit e68d842

7 files changed

Lines changed: 236 additions & 0 deletions

File tree

src/writer/json/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+

src/writer/json/json_writer.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
import json
12+
13+
from src.jsonschema.document_converter import DocumentConverter
14+
from src.model.document import Document
15+
16+
17+
class JsonWriter:
18+
converter: DocumentConverter
19+
20+
def __init__(self):
21+
self.converter = DocumentConverter()
22+
23+
def write_document(self, document: Document, file_name: str) -> None:
24+
document_dict = self.converter.convert(document)
25+
with open(file_name, "w") as out:
26+
json.dump(document_dict, out, indent=4)

tests/writer/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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.

tests/writer/json/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"SPDXID": "documentId",
3+
"annotations": [
4+
{
5+
"annotationDate": "2022-12-02T00:00:00Z",
6+
"annotationType": "REVIEW",
7+
"annotator": "Person: reviewerName",
8+
"comment": "reviewComment"
9+
}
10+
],
11+
"comment": "comment",
12+
"creationInfo": {
13+
"created": "2022-12-01T00:00:00Z",
14+
"creators": [
15+
"Tool: tools-python (tools-python@github.com)"
16+
]
17+
},
18+
"dataLicense": "dataLicense",
19+
"externalDocumentRefs": [
20+
{
21+
"externalDocumentId": "docRefId",
22+
"spdxDocument": "externalDocumentUri",
23+
"checksum": {
24+
"algorithm": "SHA1",
25+
"checksumValue": "externalRefSha1"
26+
}
27+
}
28+
],
29+
"hasExtractedLicensingInfo": [
30+
{
31+
"extractedText": "licenseText",
32+
"licenseId": "licenseId"
33+
}
34+
],
35+
"name": "documentName",
36+
"spdxVersion": "spdxVersion",
37+
"documentNamespace": "documentNamespace",
38+
"documentDescribes": [
39+
"packageId",
40+
"fileId"
41+
],
42+
"packages": [
43+
{
44+
"SPDXID": "packageId",
45+
"downloadLocation": "NONE",
46+
"filesAnalyzed": true,
47+
"name": "packageName"
48+
}
49+
],
50+
"files": [
51+
{
52+
"SPDXID": "fileId",
53+
"annotations": [
54+
{
55+
"annotationDate": "2022-12-03T00:00:00Z",
56+
"annotationType": "OTHER",
57+
"annotator": "Tool: toolName",
58+
"comment": "otherComment"
59+
}
60+
],
61+
"checksums": [
62+
{
63+
"algorithm": "SHA1",
64+
"checksumValue": "fileSha1"
65+
}
66+
],
67+
"fileName": "fileName"
68+
}
69+
],
70+
"snippets": [
71+
{
72+
"SPDXID": "snippetId",
73+
"ranges": [
74+
{
75+
"startPointer": {
76+
"reference": "snippetFileId",
77+
"offset": 1
78+
},
79+
"endPointer": {
80+
"reference": "snippetFileId",
81+
"offset": 2
82+
}
83+
}
84+
],
85+
"snippetFromFile": "snippetFileId"
86+
}
87+
],
88+
"relationships": [
89+
{
90+
"spdxElementId": "documentId",
91+
"comment": "relationshipComment",
92+
"relatedSpdxElement": "fileId",
93+
"relationshipType": "DESCRIBES"
94+
},
95+
{
96+
"spdxElementId": "relationshipOriginId",
97+
"relatedSpdxElement": "relationShipTargetId",
98+
"relationshipType": "AMENDS"
99+
}
100+
]
101+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
import json
12+
import os
13+
from datetime import datetime
14+
from pathlib import Path
15+
16+
import pytest
17+
18+
from src.model.actor import Actor, ActorType
19+
from src.model.annotation import Annotation, AnnotationType
20+
from src.model.checksum import ChecksumAlgorithm, Checksum
21+
from src.model.document import CreationInfo, Document
22+
from src.model.external_document_ref import ExternalDocumentRef
23+
from src.model.extracted_licensing_info import ExtractedLicensingInfo
24+
from src.model.file import File
25+
from src.model.package import Package
26+
from src.model.relationship import RelationshipType, Relationship
27+
from src.model.snippet import Snippet
28+
from src.model.spdx_none import SpdxNone
29+
from src.writer.json.json_writer import JsonWriter
30+
31+
32+
@pytest.fixture
33+
def temporary_file_path() -> str:
34+
temporary_file_path = "temp_test_json_writer_output.json"
35+
yield temporary_file_path
36+
os.remove(temporary_file_path)
37+
38+
39+
def test_write_json(temporary_file_path: str):
40+
writer = JsonWriter()
41+
creation_info = CreationInfo("spdxVersion", "documentId", "documentName", "documentNamespace",
42+
[Actor(ActorType.TOOL, "tools-python", "tools-python@github.com")],
43+
datetime(2022, 12, 1), document_comment="comment", data_license="dataLicense",
44+
external_document_refs=[ExternalDocumentRef("docRefId", "externalDocumentUri",
45+
Checksum(ChecksumAlgorithm.SHA1,
46+
"externalRefSha1"))])
47+
package = Package("packageId", "packageName", SpdxNone())
48+
file = File("fileName", "fileId", [Checksum(ChecksumAlgorithm.SHA1, "fileSha1")])
49+
snippet = Snippet("snippetId", "snippetFileId", (1, 2))
50+
document = Document(creation_info, annotations=[
51+
Annotation("documentId", AnnotationType.REVIEW, Actor(ActorType.PERSON, "reviewerName"),
52+
datetime(2022, 12, 2), "reviewComment"),
53+
Annotation("fileId", AnnotationType.OTHER, Actor(ActorType.TOOL, "toolName"), datetime(2022, 12, 3),
54+
"otherComment")],
55+
extracted_licensing_info=[ExtractedLicensingInfo("licenseId", "licenseText")], relationships=[
56+
Relationship(creation_info.spdx_id, RelationshipType.DESCRIBES, "packageId"),
57+
Relationship(creation_info.spdx_id, RelationshipType.DESCRIBES, "fileId", "relationshipComment"),
58+
Relationship("relationshipOriginId", RelationshipType.AMENDS, "relationShipTargetId")], packages=[package],
59+
files=[file], snippets=[snippet])
60+
writer.write_document(document, temporary_file_path)
61+
62+
with open(temporary_file_path) as written_file:
63+
written_json = json.load(written_file)
64+
65+
with open(os.path.join(os.path.dirname(__file__), 'expected_results', 'expected.json')) as expected_file:
66+
expected_json = json.load(expected_file)
67+
68+
assert written_json == expected_json

0 commit comments

Comments
 (0)