spdx_tools.spdx.jsonschema.extracted_licensing_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.jsonschema.converter import TypedConverter
 7from spdx_tools.spdx.jsonschema.extracted_licensing_info_properties import ExtractedLicensingInfoProperty
 8from spdx_tools.spdx.jsonschema.json_property import JsonProperty
 9from spdx_tools.spdx.jsonschema.optional_utils import apply_if_present
10from spdx_tools.spdx.model import Document, ExtractedLicensingInfo
11
12
13class ExtractedLicensingInfoConverter(TypedConverter[ExtractedLicensingInfo]):
14    def _get_property_value(
15        self,
16        extracted_licensing_info: ExtractedLicensingInfo,
17        extracted_licensing_info_property: ExtractedLicensingInfoProperty,
18        document: Document = None,
19    ) -> Any:
20        if extracted_licensing_info_property == ExtractedLicensingInfoProperty.COMMENT:
21            return extracted_licensing_info.comment
22        elif extracted_licensing_info_property == ExtractedLicensingInfoProperty.EXTRACTED_TEXT:
23            return extracted_licensing_info.extracted_text
24        elif extracted_licensing_info_property == ExtractedLicensingInfoProperty.LICENSE_ID:
25            return extracted_licensing_info.license_id
26        elif extracted_licensing_info_property == ExtractedLicensingInfoProperty.NAME:
27            return apply_if_present(str, extracted_licensing_info.license_name)
28        elif extracted_licensing_info_property == ExtractedLicensingInfoProperty.SEE_ALSOS:
29            return extracted_licensing_info.cross_references or None
30
31    def get_json_type(self) -> Type[JsonProperty]:
32        return ExtractedLicensingInfoProperty
33
34    def get_data_model_type(self) -> Type[ExtractedLicensingInfo]:
35        return ExtractedLicensingInfo
14class ExtractedLicensingInfoConverter(TypedConverter[ExtractedLicensingInfo]):
15    def _get_property_value(
16        self,
17        extracted_licensing_info: ExtractedLicensingInfo,
18        extracted_licensing_info_property: ExtractedLicensingInfoProperty,
19        document: Document = None,
20    ) -> Any:
21        if extracted_licensing_info_property == ExtractedLicensingInfoProperty.COMMENT:
22            return extracted_licensing_info.comment
23        elif extracted_licensing_info_property == ExtractedLicensingInfoProperty.EXTRACTED_TEXT:
24            return extracted_licensing_info.extracted_text
25        elif extracted_licensing_info_property == ExtractedLicensingInfoProperty.LICENSE_ID:
26            return extracted_licensing_info.license_id
27        elif extracted_licensing_info_property == ExtractedLicensingInfoProperty.NAME:
28            return apply_if_present(str, extracted_licensing_info.license_name)
29        elif extracted_licensing_info_property == ExtractedLicensingInfoProperty.SEE_ALSOS:
30            return extracted_licensing_info.cross_references or None
31
32    def get_json_type(self) -> Type[JsonProperty]:
33        return ExtractedLicensingInfoProperty
34
35    def get_data_model_type(self) -> Type[ExtractedLicensingInfo]:
36        return ExtractedLicensingInfo

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]:
32    def get_json_type(self) -> Type[JsonProperty]:
33        return ExtractedLicensingInfoProperty
def get_data_model_type( self) -> type[spdx_tools.spdx.model.extracted_licensing_info.ExtractedLicensingInfo]:
35    def get_data_model_type(self) -> Type[ExtractedLicensingInfo]:
36        return ExtractedLicensingInfo