spdx_tools.spdx.jsonschema.creation_info_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.datetime_conversions import datetime_to_iso_string
 7from spdx_tools.spdx.jsonschema.converter import TypedConverter
 8from spdx_tools.spdx.jsonschema.creation_info_properties import CreationInfoProperty
 9from spdx_tools.spdx.jsonschema.json_property import JsonProperty
10from spdx_tools.spdx.jsonschema.optional_utils import apply_if_present
11from spdx_tools.spdx.model import CreationInfo, Document
12
13
14class CreationInfoConverter(TypedConverter[CreationInfo]):
15    def get_data_model_type(self) -> Type[CreationInfo]:
16        return CreationInfo
17
18    def get_json_type(self) -> Type[JsonProperty]:
19        return CreationInfoProperty
20
21    def _get_property_value(
22        self, creation_info: CreationInfo, creation_info_property: CreationInfoProperty, _document: Document = None
23    ) -> Any:
24        if creation_info_property == CreationInfoProperty.CREATED:
25            return datetime_to_iso_string(creation_info.created)
26        elif creation_info_property == CreationInfoProperty.CREATORS:
27            return [creator.to_serialized_string() for creator in creation_info.creators] or None
28        elif creation_info_property == CreationInfoProperty.LICENSE_LIST_VERSION:
29            return apply_if_present(str, creation_info.license_list_version)
30        elif creation_info_property == CreationInfoProperty.COMMENT:
31            return creation_info.creator_comment
15class CreationInfoConverter(TypedConverter[CreationInfo]):
16    def get_data_model_type(self) -> Type[CreationInfo]:
17        return CreationInfo
18
19    def get_json_type(self) -> Type[JsonProperty]:
20        return CreationInfoProperty
21
22    def _get_property_value(
23        self, creation_info: CreationInfo, creation_info_property: CreationInfoProperty, _document: Document = None
24    ) -> Any:
25        if creation_info_property == CreationInfoProperty.CREATED:
26            return datetime_to_iso_string(creation_info.created)
27        elif creation_info_property == CreationInfoProperty.CREATORS:
28            return [creator.to_serialized_string() for creator in creation_info.creators] or None
29        elif creation_info_property == CreationInfoProperty.LICENSE_LIST_VERSION:
30            return apply_if_present(str, creation_info.license_list_version)
31        elif creation_info_property == CreationInfoProperty.COMMENT:
32            return creation_info.creator_comment

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.document.CreationInfo]:
16    def get_data_model_type(self) -> Type[CreationInfo]:
17        return CreationInfo
def get_json_type(self) -> type[spdx_tools.spdx.jsonschema.json_property.JsonProperty]:
19    def get_json_type(self) -> Type[JsonProperty]:
20        return CreationInfoProperty