spdx_tools.spdx.jsonschema.annotation_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.annotation_properties import AnnotationProperty 8from spdx_tools.spdx.jsonschema.converter import TypedConverter 9from spdx_tools.spdx.jsonschema.json_property import JsonProperty 10from spdx_tools.spdx.model import Annotation, Document 11 12 13class AnnotationConverter(TypedConverter[Annotation]): 14 def _get_property_value( 15 self, annotation: Annotation, annotation_property: AnnotationProperty, document: Document = None 16 ) -> Any: 17 if annotation_property == AnnotationProperty.ANNOTATION_DATE: 18 return datetime_to_iso_string(annotation.annotation_date) 19 elif annotation_property == AnnotationProperty.ANNOTATION_TYPE: 20 return annotation.annotation_type.name 21 elif annotation_property == AnnotationProperty.ANNOTATOR: 22 return annotation.annotator.to_serialized_string() 23 elif annotation_property == AnnotationProperty.COMMENT: 24 return annotation.annotation_comment 25 26 def get_json_type(self) -> Type[JsonProperty]: 27 return AnnotationProperty 28 29 def get_data_model_type(self) -> Type[Annotation]: 30 return Annotation
class
AnnotationConverter(spdx_tools.spdx.jsonschema.converter.TypedConverter[spdx_tools.spdx.model.annotation.Annotation]):
14class AnnotationConverter(TypedConverter[Annotation]): 15 def _get_property_value( 16 self, annotation: Annotation, annotation_property: AnnotationProperty, document: Document = None 17 ) -> Any: 18 if annotation_property == AnnotationProperty.ANNOTATION_DATE: 19 return datetime_to_iso_string(annotation.annotation_date) 20 elif annotation_property == AnnotationProperty.ANNOTATION_TYPE: 21 return annotation.annotation_type.name 22 elif annotation_property == AnnotationProperty.ANNOTATOR: 23 return annotation.annotator.to_serialized_string() 24 elif annotation_property == AnnotationProperty.COMMENT: 25 return annotation.annotation_comment 26 27 def get_json_type(self) -> Type[JsonProperty]: 28 return AnnotationProperty 29 30 def get_data_model_type(self) -> Type[Annotation]: 31 return Annotation
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.