forked from spdx/tools-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_converter.py
More file actions
70 lines (61 loc) · 3.28 KB
/
file_converter.py
File metadata and controls
70 lines (61 loc) · 3.28 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# SPDX-FileCopyrightText: 2022 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
from beartype.typing import Any, Type
from spdx_tools.spdx.jsonschema.annotation_converter import AnnotationConverter
from spdx_tools.spdx.jsonschema.checksum_converter import ChecksumConverter
from spdx_tools.spdx.jsonschema.converter import TypedConverter
from spdx_tools.spdx.jsonschema.file_properties import FileProperty
from spdx_tools.spdx.jsonschema.json_property import JsonProperty
from spdx_tools.spdx.jsonschema.optional_utils import apply_if_present
from spdx_tools.spdx.model import Document, File
class FileConverter(TypedConverter[File]):
annotation_converter: AnnotationConverter
checksum_converter: ChecksumConverter
def __init__(self):
self.annotation_converter = AnnotationConverter()
self.checksum_converter = ChecksumConverter()
def json_property_name(self, file_property: FileProperty) -> str:
if file_property == FileProperty.SPDX_ID:
return "SPDXID"
return super().json_property_name(file_property)
def _get_property_value(self, file: Any, file_property: FileProperty, document: Document | None = None) -> Any:
if file_property == FileProperty.SPDX_ID:
return file.spdx_id
elif file_property == FileProperty.ANNOTATIONS:
file_annotations = filter(lambda annotation: annotation.spdx_id == file.spdx_id, document.annotations)
return [self.annotation_converter.convert(annotation) for annotation in file_annotations] or None
elif file_property == FileProperty.ARTIFACT_OFS:
# Deprecated property, automatically converted during parsing
pass
elif file_property == FileProperty.ATTRIBUTION_TEXTS:
return file.attribution_texts or None
elif file_property == FileProperty.CHECKSUMS:
return [self.checksum_converter.convert(checksum) for checksum in file.checksums] or None
elif file_property == FileProperty.COMMENT:
return file.comment
elif file_property == FileProperty.COPYRIGHT_TEXT:
return apply_if_present(str, file.copyright_text)
elif file_property == FileProperty.FILE_CONTRIBUTORS:
return file.contributors or None
elif file_property == FileProperty.FILE_DEPENDENCIES:
# Deprecated property, automatically converted during parsing
pass
elif file_property == FileProperty.FILE_NAME:
return file.name
elif file_property == FileProperty.FILE_TYPES:
return [file_type.name for file_type in file.file_types] or None
elif file_property == FileProperty.LICENSE_COMMENTS:
return file.license_comment
elif file_property == FileProperty.LICENSE_CONCLUDED:
return apply_if_present(str, file.license_concluded)
elif file_property == FileProperty.LICENSE_INFO_IN_FILES:
return [str(license_expression) for license_expression in file.license_info_in_file] or None
elif file_property == FileProperty.NOTICE_TEXT:
return file.notice
def get_json_type(self) -> Type[JsonProperty]:
return FileProperty
def get_data_model_type(self) -> Type[File]:
return File
def requires_full_document(self) -> bool:
return True