spdx_tools.spdx.jsonschema.external_package_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.converter import TypedConverter 7from spdx_tools.spdx.jsonschema.external_package_ref_properties import ExternalPackageRefProperty 8from spdx_tools.spdx.jsonschema.json_property import JsonProperty 9from spdx_tools.spdx.model import Document, ExternalPackageRef 10 11 12class ExternalPackageRefConverter(TypedConverter[ExternalPackageRef]): 13 def _get_property_value( 14 self, 15 external_ref: ExternalPackageRef, 16 external_ref_property: ExternalPackageRefProperty, 17 document: Document = None, 18 ) -> Any: 19 if external_ref_property == ExternalPackageRefProperty.COMMENT: 20 return external_ref.comment 21 elif external_ref_property == ExternalPackageRefProperty.REFERENCE_CATEGORY: 22 return external_ref.category.name 23 elif external_ref_property == ExternalPackageRefProperty.REFERENCE_LOCATOR: 24 return external_ref.locator 25 elif external_ref_property == ExternalPackageRefProperty.REFERENCE_TYPE: 26 return external_ref.reference_type 27 28 def get_json_type(self) -> Type[JsonProperty]: 29 return ExternalPackageRefProperty 30 31 def get_data_model_type(self) -> Type[ExternalPackageRef]: 32 return ExternalPackageRef
class
ExternalPackageRefConverter(spdx_tools.spdx.jsonschema.converter.TypedConverter[spdx_tools.spdx.model.package.ExternalPackageRef]):
13class ExternalPackageRefConverter(TypedConverter[ExternalPackageRef]): 14 def _get_property_value( 15 self, 16 external_ref: ExternalPackageRef, 17 external_ref_property: ExternalPackageRefProperty, 18 document: Document = None, 19 ) -> Any: 20 if external_ref_property == ExternalPackageRefProperty.COMMENT: 21 return external_ref.comment 22 elif external_ref_property == ExternalPackageRefProperty.REFERENCE_CATEGORY: 23 return external_ref.category.name 24 elif external_ref_property == ExternalPackageRefProperty.REFERENCE_LOCATOR: 25 return external_ref.locator 26 elif external_ref_property == ExternalPackageRefProperty.REFERENCE_TYPE: 27 return external_ref.reference_type 28 29 def get_json_type(self) -> Type[JsonProperty]: 30 return ExternalPackageRefProperty 31 32 def get_data_model_type(self) -> Type[ExternalPackageRef]: 33 return ExternalPackageRef
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.