spdx_tools.spdx.jsonschema.external_document_ref_converter

 1# SPDX-FileCopyrightText: 2022 spdx contributors
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4from beartype.typing import Any, Type
 5
 6from spdx_tools.spdx.jsonschema.checksum_converter import ChecksumConverter
 7from spdx_tools.spdx.jsonschema.converter import TypedConverter
 8from spdx_tools.spdx.jsonschema.external_document_ref_properties import ExternalDocumentRefProperty
 9from spdx_tools.spdx.jsonschema.json_property import JsonProperty
10from spdx_tools.spdx.model import Document, ExternalDocumentRef
11
12
13class ExternalDocumentRefConverter(TypedConverter[ExternalDocumentRef]):
14    checksum_converter: ChecksumConverter
15
16    def __init__(self):
17        self.checksum_converter = ChecksumConverter()
18
19    def _get_property_value(
20        self,
21        external_document_ref: ExternalDocumentRef,
22        external_document_ref_property: ExternalDocumentRefProperty,
23        _document: Document = None,
24    ) -> Any:
25        if external_document_ref_property == ExternalDocumentRefProperty.EXTERNAL_DOCUMENT_ID:
26            return external_document_ref.document_ref_id
27        elif external_document_ref_property == ExternalDocumentRefProperty.SPDX_DOCUMENT:
28            return external_document_ref.document_uri
29        elif external_document_ref_property == ExternalDocumentRefProperty.CHECKSUM:
30            return self.checksum_converter.convert(external_document_ref.checksum)
31
32    def get_json_type(self) -> Type[JsonProperty]:
33        return ExternalDocumentRefProperty
34
35    def get_data_model_type(self) -> Type[ExternalDocumentRef]:
36        return ExternalDocumentRef
14class ExternalDocumentRefConverter(TypedConverter[ExternalDocumentRef]):
15    checksum_converter: ChecksumConverter
16
17    def __init__(self):
18        self.checksum_converter = ChecksumConverter()
19
20    def _get_property_value(
21        self,
22        external_document_ref: ExternalDocumentRef,
23        external_document_ref_property: ExternalDocumentRefProperty,
24        _document: Document = None,
25    ) -> Any:
26        if external_document_ref_property == ExternalDocumentRefProperty.EXTERNAL_DOCUMENT_ID:
27            return external_document_ref.document_ref_id
28        elif external_document_ref_property == ExternalDocumentRefProperty.SPDX_DOCUMENT:
29            return external_document_ref.document_uri
30        elif external_document_ref_property == ExternalDocumentRefProperty.CHECKSUM:
31            return self.checksum_converter.convert(external_document_ref.checksum)
32
33    def get_json_type(self) -> Type[JsonProperty]:
34        return ExternalDocumentRefProperty
35
36    def get_data_model_type(self) -> Type[ExternalDocumentRef]:
37        return ExternalDocumentRef

Base class for all converters between an instance of the tools-python data model and the corresponding dictionary representation, following the json schema. The generic type T is the type according to the data model. Each converter has several methods:

  • get_json_type and get_data_model_type: return the data model type and the corresponding JsonProperty subclass. These methods are abstract in the base class and need to be implemented in subclasses.
  • json_property_name: converts an enum value of a JsonProperty subclass to the corresponding property name in the json schema. The default implementation simply converts from snake case to camel case. Can be overridden in case of exceptions like "SPDXID".
  • convert: converts an instance of type T (one of the data model types) to a dictionary representation. In some cases, the full document is required (see below). The logic should be generic for all types.
  • requires_full_document: indicates whether the full document is required for conversion. Returns False by default, can be overridden as needed for specific types.
  • _get_property_value: Retrieves the value of a specific json property from the data model instance. In some cases, the full document is required.
def get_json_type(self) -> type[spdx_tools.spdx.jsonschema.json_property.JsonProperty]:
33    def get_json_type(self) -> Type[JsonProperty]:
34        return ExternalDocumentRefProperty
def get_data_model_type( self) -> type[spdx_tools.spdx.model.external_document_ref.ExternalDocumentRef]:
36    def get_data_model_type(self) -> Type[ExternalDocumentRef]:
37        return ExternalDocumentRef