spdx_tools.spdx.writer.rdf.rdf_writer
1# SPDX-FileCopyrightText: 2023 spdx contributors 2# 3# SPDX-License-Identifier: Apache-2.0 4from beartype.typing import IO, Dict 5from rdflib import DOAP, Graph 6from rdflib.compare import to_isomorphic 7 8from spdx_tools.spdx.model import Document 9from spdx_tools.spdx.rdfschema.namespace import POINTER_NAMESPACE, SPDX_NAMESPACE 10from spdx_tools.spdx.writer.rdf.annotation_writer import add_annotation_to_graph 11from spdx_tools.spdx.writer.rdf.creation_info_writer import add_creation_info_to_graph 12from spdx_tools.spdx.writer.rdf.extracted_licensing_info_writer import add_extracted_licensing_info_to_graph 13from spdx_tools.spdx.writer.rdf.file_writer import add_file_to_graph 14from spdx_tools.spdx.writer.rdf.package_writer import add_package_to_graph 15from spdx_tools.spdx.writer.rdf.relationship_writer import add_relationship_to_graph 16from spdx_tools.spdx.writer.rdf.snippet_writer import add_snippet_to_graph 17from spdx_tools.spdx.writer.write_utils import validate_and_deduplicate 18 19 20def write_document_to_stream( 21 document: Document, stream: IO[bytes], validate: bool = True, drop_duplicates: bool = True 22): 23 document = validate_and_deduplicate(document, validate, drop_duplicates) 24 graph = Graph() 25 doc_namespace = document.creation_info.document_namespace 26 external_doc_ref_to_namespace: Dict[str, str] = { 27 external_doc_ref.document_ref_id: external_doc_ref.document_uri 28 for external_doc_ref in document.creation_info.external_document_refs 29 } 30 doc_node = add_creation_info_to_graph(document.creation_info, graph) 31 for annotation in document.annotations: 32 add_annotation_to_graph(annotation, graph, doc_namespace, external_doc_ref_to_namespace) 33 34 for file in document.files: 35 add_file_to_graph(file, graph, doc_namespace, external_doc_ref_to_namespace) 36 37 for package in document.packages: 38 add_package_to_graph(package, graph, doc_namespace, external_doc_ref_to_namespace) 39 40 for relationship in document.relationships: 41 add_relationship_to_graph(relationship, graph, doc_namespace, external_doc_ref_to_namespace) 42 43 for snippet in document.snippets: 44 add_snippet_to_graph(snippet, graph, doc_namespace, external_doc_ref_to_namespace) 45 46 for extracted_licensing_info in document.extracted_licensing_info: 47 add_extracted_licensing_info_to_graph(extracted_licensing_info, graph, doc_node, doc_namespace) 48 49 graph = to_isomorphic(graph) 50 graph.bind("spdx", SPDX_NAMESPACE) 51 graph.bind("doap", DOAP) 52 graph.bind("ptr", POINTER_NAMESPACE) 53 graph.serialize(stream, "pretty-xml", encoding="UTF-8", max_depth=100) 54 55 56def write_document_to_file(document: Document, file_name: str, validate: bool = True, drop_duplicates: bool = True): 57 with open(file_name, "wb") as out: 58 write_document_to_stream(document, out, validate, drop_duplicates)
def
write_document_to_stream( document: spdx_tools.spdx.model.document.Document, stream: IO[bytes], validate: bool = True, drop_duplicates: bool = True):
21def write_document_to_stream( 22 document: Document, stream: IO[bytes], validate: bool = True, drop_duplicates: bool = True 23): 24 document = validate_and_deduplicate(document, validate, drop_duplicates) 25 graph = Graph() 26 doc_namespace = document.creation_info.document_namespace 27 external_doc_ref_to_namespace: Dict[str, str] = { 28 external_doc_ref.document_ref_id: external_doc_ref.document_uri 29 for external_doc_ref in document.creation_info.external_document_refs 30 } 31 doc_node = add_creation_info_to_graph(document.creation_info, graph) 32 for annotation in document.annotations: 33 add_annotation_to_graph(annotation, graph, doc_namespace, external_doc_ref_to_namespace) 34 35 for file in document.files: 36 add_file_to_graph(file, graph, doc_namespace, external_doc_ref_to_namespace) 37 38 for package in document.packages: 39 add_package_to_graph(package, graph, doc_namespace, external_doc_ref_to_namespace) 40 41 for relationship in document.relationships: 42 add_relationship_to_graph(relationship, graph, doc_namespace, external_doc_ref_to_namespace) 43 44 for snippet in document.snippets: 45 add_snippet_to_graph(snippet, graph, doc_namespace, external_doc_ref_to_namespace) 46 47 for extracted_licensing_info in document.extracted_licensing_info: 48 add_extracted_licensing_info_to_graph(extracted_licensing_info, graph, doc_node, doc_namespace) 49 50 graph = to_isomorphic(graph) 51 graph.bind("spdx", SPDX_NAMESPACE) 52 graph.bind("doap", DOAP) 53 graph.bind("ptr", POINTER_NAMESPACE) 54 graph.serialize(stream, "pretty-xml", encoding="UTF-8", max_depth=100)
def
write_document_to_file( document: spdx_tools.spdx.model.document.Document, file_name: str, validate: bool = True, drop_duplicates: bool = True):