spdx_tools.spdx.jsonschema.checksum_converter

 1# SPDX-FileCopyrightText: 2022 spdx contributors
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4from beartype.typing import Type
 5
 6from spdx_tools.spdx.jsonschema.checksum_properties import ChecksumProperty
 7from spdx_tools.spdx.jsonschema.converter import TypedConverter
 8from spdx_tools.spdx.jsonschema.json_property import JsonProperty
 9from spdx_tools.spdx.model import Checksum, ChecksumAlgorithm, Document
10
11
12class ChecksumConverter(TypedConverter[Checksum]):
13    def get_data_model_type(self) -> Type[Checksum]:
14        return Checksum
15
16    def get_json_type(self) -> Type[JsonProperty]:
17        return ChecksumProperty
18
19    def _get_property_value(
20        self, checksum: Checksum, checksum_property: ChecksumProperty, _document: Document = None
21    ) -> str:
22        if checksum_property == ChecksumProperty.ALGORITHM:
23            return algorithm_to_json_string(checksum.algorithm)
24        elif checksum_property == ChecksumProperty.CHECKSUM_VALUE:
25            return checksum.value
26
27
28def algorithm_to_json_string(algorithm: ChecksumAlgorithm) -> str:
29    name_with_dash: str = algorithm.name.replace("_", "-")
30    if "BLAKE2B" in name_with_dash:
31        return name_with_dash.replace("BLAKE2B", "BLAKE2b")
32    return name_with_dash
13class ChecksumConverter(TypedConverter[Checksum]):
14    def get_data_model_type(self) -> Type[Checksum]:
15        return Checksum
16
17    def get_json_type(self) -> Type[JsonProperty]:
18        return ChecksumProperty
19
20    def _get_property_value(
21        self, checksum: Checksum, checksum_property: ChecksumProperty, _document: Document = None
22    ) -> str:
23        if checksum_property == ChecksumProperty.ALGORITHM:
24            return algorithm_to_json_string(checksum.algorithm)
25        elif checksum_property == ChecksumProperty.CHECKSUM_VALUE:
26            return checksum.value

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_data_model_type(self) -> type[spdx_tools.spdx.model.checksum.Checksum]:
14    def get_data_model_type(self) -> Type[Checksum]:
15        return Checksum
def get_json_type(self) -> type[spdx_tools.spdx.jsonschema.json_property.JsonProperty]:
17    def get_json_type(self) -> Type[JsonProperty]:
18        return ChecksumProperty
def algorithm_to_json_string(algorithm: spdx_tools.spdx.model.checksum.ChecksumAlgorithm) -> str:
29def algorithm_to_json_string(algorithm: ChecksumAlgorithm) -> str:
30    name_with_dash: str = algorithm.name.replace("_", "-")
31    if "BLAKE2B" in name_with_dash:
32        return name_with_dash.replace("BLAKE2B", "BLAKE2b")
33    return name_with_dash