forked from spdx/tools-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecksum_converter.py
More file actions
32 lines (25 loc) · 1.21 KB
/
checksum_converter.py
File metadata and controls
32 lines (25 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# SPDX-FileCopyrightText: 2022 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
from beartype.typing import Type
from spdx_tools.spdx.jsonschema.checksum_properties import ChecksumProperty
from spdx_tools.spdx.jsonschema.converter import TypedConverter
from spdx_tools.spdx.jsonschema.json_property import JsonProperty
from spdx_tools.spdx.model import Checksum, ChecksumAlgorithm, Document
class ChecksumConverter(TypedConverter[Checksum]):
def get_data_model_type(self) -> Type[Checksum]:
return Checksum
def get_json_type(self) -> Type[JsonProperty]:
return ChecksumProperty
def _get_property_value(
self, checksum: Checksum, checksum_property: ChecksumProperty, _document: Document | None = None
) -> str:
if checksum_property == ChecksumProperty.ALGORITHM:
return algorithm_to_json_string(checksum.algorithm)
elif checksum_property == ChecksumProperty.CHECKSUM_VALUE:
return checksum.value
def algorithm_to_json_string(algorithm: ChecksumAlgorithm) -> str:
name_with_dash: str = algorithm.name.replace("_", "-")
if "BLAKE2B" in name_with_dash:
return name_with_dash.replace("BLAKE2B", "BLAKE2b")
return name_with_dash