spdx_tools.spdx.jsonschema.package_verification_code_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.json_property import JsonProperty
 8from spdx_tools.spdx.jsonschema.package_verification_code_properties import PackageVerificationCodeProperty
 9from spdx_tools.spdx.model import Document, PackageVerificationCode
10
11
12class PackageVerificationCodeConverter(TypedConverter[PackageVerificationCode]):
13    def _get_property_value(
14        self,
15        verification_code: PackageVerificationCode,
16        verification_code_property: PackageVerificationCodeProperty,
17        document: Document = None,
18    ) -> Any:
19        if verification_code_property == PackageVerificationCodeProperty.PACKAGE_VERIFICATION_CODE_EXCLUDED_FILES:
20            return verification_code.excluded_files or None
21        elif verification_code_property == PackageVerificationCodeProperty.PACKAGE_VERIFICATION_CODE_VALUE:
22            return verification_code.value
23
24    def get_json_type(self) -> Type[JsonProperty]:
25        return PackageVerificationCodeProperty
26
27    def get_data_model_type(self) -> Type[PackageVerificationCode]:
28        return PackageVerificationCode
13class PackageVerificationCodeConverter(TypedConverter[PackageVerificationCode]):
14    def _get_property_value(
15        self,
16        verification_code: PackageVerificationCode,
17        verification_code_property: PackageVerificationCodeProperty,
18        document: Document = None,
19    ) -> Any:
20        if verification_code_property == PackageVerificationCodeProperty.PACKAGE_VERIFICATION_CODE_EXCLUDED_FILES:
21            return verification_code.excluded_files or None
22        elif verification_code_property == PackageVerificationCodeProperty.PACKAGE_VERIFICATION_CODE_VALUE:
23            return verification_code.value
24
25    def get_json_type(self) -> Type[JsonProperty]:
26        return PackageVerificationCodeProperty
27
28    def get_data_model_type(self) -> Type[PackageVerificationCode]:
29        return PackageVerificationCode

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]:
25    def get_json_type(self) -> Type[JsonProperty]:
26        return PackageVerificationCodeProperty
def get_data_model_type(self) -> type[spdx_tools.spdx.model.package.PackageVerificationCode]:
28    def get_data_model_type(self) -> Type[PackageVerificationCode]:
29        return PackageVerificationCode