spdx_tools.spdx.jsonschema.relationship_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.relationship_properties import RelationshipProperty 9from spdx_tools.spdx.model import Document, Relationship 10 11 12class RelationshipConverter(TypedConverter[Relationship]): 13 def _get_property_value( 14 self, relationship: Relationship, relationship_property: RelationshipProperty, document: Document = None 15 ) -> Any: 16 if relationship_property == RelationshipProperty.SPDX_ELEMENT_ID: 17 return relationship.spdx_element_id 18 elif relationship_property == RelationshipProperty.COMMENT: 19 return relationship.comment 20 elif relationship_property == RelationshipProperty.RELATED_SPDX_ELEMENT: 21 return str(relationship.related_spdx_element_id) 22 elif relationship_property == RelationshipProperty.RELATIONSHIP_TYPE: 23 return relationship.relationship_type.name 24 25 def get_json_type(self) -> Type[JsonProperty]: 26 return RelationshipProperty 27 28 def get_data_model_type(self) -> Type[Relationship]: 29 return Relationship
class
RelationshipConverter(spdx_tools.spdx.jsonschema.converter.TypedConverter[spdx_tools.spdx.model.relationship.Relationship]):
13class RelationshipConverter(TypedConverter[Relationship]): 14 def _get_property_value( 15 self, relationship: Relationship, relationship_property: RelationshipProperty, document: Document = None 16 ) -> Any: 17 if relationship_property == RelationshipProperty.SPDX_ELEMENT_ID: 18 return relationship.spdx_element_id 19 elif relationship_property == RelationshipProperty.COMMENT: 20 return relationship.comment 21 elif relationship_property == RelationshipProperty.RELATED_SPDX_ELEMENT: 22 return str(relationship.related_spdx_element_id) 23 elif relationship_property == RelationshipProperty.RELATIONSHIP_TYPE: 24 return relationship.relationship_type.name 25 26 def get_json_type(self) -> Type[JsonProperty]: 27 return RelationshipProperty 28 29 def get_data_model_type(self) -> Type[Relationship]: 30 return Relationship
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.