Repository navigation
Expand file tree
/
Copy pathaboutcode.py
More file actions
61 lines (53 loc) · 1.58 KB
/
Copy pathaboutcode.py
File metadata and controls
61 lines (53 loc) · 1.58 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
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# DejaCode is a trademark of nexB Inc.
# SPDX-License-Identifier: AGPL-3.0-only
# See https://github.com/aboutcode-org/dejacode for support or download.
# See https://aboutcode.org for more information about AboutCode FOSS projects.
#
"""
Serialize data as an ABOUT file.
https://github.com/aboutcode-org/aboutcode-toolkit/blob/develop/SPECIFICATION.rst
"""
import saneyaml
# Supported fields, in the order they are written in the ABOUT file
ABOUT_FIELD_NAMES = [
"about_resource",
"name",
"version",
"download_url",
"description",
"homepage_url",
"package_url",
"notes",
"license_expression",
"copyright",
"notice_file",
"notice_url",
"redistribute",
"attribute",
"track_changes",
"owner",
"owner_url",
"contact",
"vcs_repository",
"checksum_md5",
"checksum_sha1",
"licenses",
]
def normalize_text(text):
"""Return `text` with LF line endings, without trailing spaces and surrounding blank lines."""
return "\n".join(line.rstrip() for line in text.splitlines()).strip()
def dumps(about_data):
"""
Return the `about_data` dict as an ABOUT file YAML string.
Fields are written in the `ABOUT_FIELD_NAMES` order, empty and False values are skipped.
"""
about_fields = {}
for field_name in ABOUT_FIELD_NAMES:
value = about_data.get(field_name)
if isinstance(value, str):
value = normalize_text(value)
if value:
about_fields[field_name] = value
return saneyaml.dump(about_fields)