spdx_tools.spdx.writer.rdf.file_writer

 1# SPDX-FileCopyrightText: 2023 spdx contributors
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4from beartype.typing import Dict
 5from rdflib import RDF, RDFS, Graph, Literal, URIRef
 6
 7from spdx_tools.spdx.casing_tools import snake_case_to_camel_case
 8from spdx_tools.spdx.model import File
 9from spdx_tools.spdx.rdfschema.namespace import SPDX_NAMESPACE
10from spdx_tools.spdx.writer.rdf.checksum_writer import add_checksum_to_graph
11from spdx_tools.spdx.writer.rdf.license_expression_writer import add_license_expression_or_none_or_no_assertion
12from spdx_tools.spdx.writer.rdf.writer_utils import add_namespace_to_spdx_id, add_optional_literal
13
14
15def add_file_to_graph(file: File, graph: Graph, doc_namespace: str, external_doc_ref_to_namespace: Dict[str, str]):
16    file_resource = URIRef(add_namespace_to_spdx_id(file.spdx_id, doc_namespace, external_doc_ref_to_namespace))
17    graph.add((file_resource, RDF.type, SPDX_NAMESPACE.File))
18    graph.add((file_resource, SPDX_NAMESPACE.fileName, Literal(file.name)))
19    for file_type in file.file_types:
20        graph.add(
21            (
22                file_resource,
23                SPDX_NAMESPACE.fileType,
24                SPDX_NAMESPACE[f"fileType_{snake_case_to_camel_case(file_type.name)}"],
25            )
26        )
27
28    for checksum in file.checksums:
29        add_checksum_to_graph(checksum, graph, file_resource)
30
31    add_license_expression_or_none_or_no_assertion(
32        file.license_concluded, graph, file_resource, SPDX_NAMESPACE.licenseConcluded, doc_namespace
33    )
34    add_license_expression_or_none_or_no_assertion(
35        file.license_info_in_file, graph, file_resource, SPDX_NAMESPACE.licenseInfoInFile, doc_namespace
36    )
37
38    add_optional_literal(file.license_comment, graph, file_resource, SPDX_NAMESPACE.licenseComments)
39    add_optional_literal(file.copyright_text, graph, file_resource, SPDX_NAMESPACE.copyrightText)
40    add_optional_literal(file.comment, graph, file_resource, RDFS.comment)
41    add_optional_literal(file.notice, graph, file_resource, SPDX_NAMESPACE.noticeText)
42    for contributor in file.contributors:
43        graph.add((file_resource, SPDX_NAMESPACE.fileContributor, Literal(contributor)))
44    for attribution_text in file.attribution_texts:
45        graph.add((file_resource, SPDX_NAMESPACE.attributionText, Literal(attribution_text)))
def add_file_to_graph( file: spdx_tools.spdx.model.file.File, graph: rdflib.graph.Graph, doc_namespace: str, external_doc_ref_to_namespace: dict[str, str]):
16def add_file_to_graph(file: File, graph: Graph, doc_namespace: str, external_doc_ref_to_namespace: Dict[str, str]):
17    file_resource = URIRef(add_namespace_to_spdx_id(file.spdx_id, doc_namespace, external_doc_ref_to_namespace))
18    graph.add((file_resource, RDF.type, SPDX_NAMESPACE.File))
19    graph.add((file_resource, SPDX_NAMESPACE.fileName, Literal(file.name)))
20    for file_type in file.file_types:
21        graph.add(
22            (
23                file_resource,
24                SPDX_NAMESPACE.fileType,
25                SPDX_NAMESPACE[f"fileType_{snake_case_to_camel_case(file_type.name)}"],
26            )
27        )
28
29    for checksum in file.checksums:
30        add_checksum_to_graph(checksum, graph, file_resource)
31
32    add_license_expression_or_none_or_no_assertion(
33        file.license_concluded, graph, file_resource, SPDX_NAMESPACE.licenseConcluded, doc_namespace
34    )
35    add_license_expression_or_none_or_no_assertion(
36        file.license_info_in_file, graph, file_resource, SPDX_NAMESPACE.licenseInfoInFile, doc_namespace
37    )
38
39    add_optional_literal(file.license_comment, graph, file_resource, SPDX_NAMESPACE.licenseComments)
40    add_optional_literal(file.copyright_text, graph, file_resource, SPDX_NAMESPACE.copyrightText)
41    add_optional_literal(file.comment, graph, file_resource, RDFS.comment)
42    add_optional_literal(file.notice, graph, file_resource, SPDX_NAMESPACE.noticeText)
43    for contributor in file.contributors:
44        graph.add((file_resource, SPDX_NAMESPACE.fileContributor, Literal(contributor)))
45    for attribution_text in file.attribution_texts:
46        graph.add((file_resource, SPDX_NAMESPACE.attributionText, Literal(attribution_text)))