codemeticulous 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codemeticulous/__init__.py +82 -0
- codemeticulous/cff/__init__.py +19 -0
- codemeticulous/cff/convert.py +462 -0
- codemeticulous/cff/identifiers.py +17 -0
- codemeticulous/cff/models.py +1696 -0
- codemeticulous/cff/schema.py +16 -0
- codemeticulous/cff/schemas/1.2.0/schema.json +1882 -0
- codemeticulous/cff/types.py +23 -0
- codemeticulous/cli.py +669 -0
- codemeticulous/codemeta/__init__.py +20 -0
- codemeticulous/codemeta/convert.py +1130 -0
- codemeticulous/codemeta/migrate.py +369 -0
- codemeticulous/codemeta/models.py +92 -0
- codemeticulous/collection.py +111 -0
- codemeticulous/conversion.py +40 -0
- codemeticulous/convert.py +281 -0
- codemeticulous/datacite/__init__.py +10 -0
- codemeticulous/datacite/convert.py +572 -0
- codemeticulous/datacite/models.py +562 -0
- codemeticulous/datacite/schemas/4.5/include/datacite-contributorType-v4.xsd +35 -0
- codemeticulous/datacite/schemas/4.5/include/datacite-dateType-v4.xsd +25 -0
- codemeticulous/datacite/schemas/4.5/include/datacite-descriptionType-v4.xsd +19 -0
- codemeticulous/datacite/schemas/4.5/include/datacite-funderIdentifierType-v4.xsd +16 -0
- codemeticulous/datacite/schemas/4.5/include/datacite-nameType-v4.xsd +10 -0
- codemeticulous/datacite/schemas/4.5/include/datacite-numberType-v4.xsd +12 -0
- codemeticulous/datacite/schemas/4.5/include/datacite-relatedIdentifierType-v4.xsd +34 -0
- codemeticulous/datacite/schemas/4.5/include/datacite-relationType-v4.xsd +53 -0
- codemeticulous/datacite/schemas/4.5/include/datacite-resourceType-v4.xsd +45 -0
- codemeticulous/datacite/schemas/4.5/include/datacite-titleType-v4.xsd +14 -0
- codemeticulous/datacite/schemas/4.5/include/xml.xsd +286 -0
- codemeticulous/datacite/schemas/4.5/metadata.xsd +711 -0
- codemeticulous/datacite/schemas/4.6/include/datacite-contributorType-v4.xsd +37 -0
- codemeticulous/datacite/schemas/4.6/include/datacite-dateType-v4.xsd +27 -0
- codemeticulous/datacite/schemas/4.6/include/datacite-descriptionType-v4.xsd +19 -0
- codemeticulous/datacite/schemas/4.6/include/datacite-funderIdentifierType-v4.xsd +16 -0
- codemeticulous/datacite/schemas/4.6/include/datacite-nameType-v4.xsd +10 -0
- codemeticulous/datacite/schemas/4.6/include/datacite-numberType-v4.xsd +12 -0
- codemeticulous/datacite/schemas/4.6/include/datacite-relatedIdentifierType-v4.xsd +37 -0
- codemeticulous/datacite/schemas/4.6/include/datacite-relationType-v4.xsd +57 -0
- codemeticulous/datacite/schemas/4.6/include/datacite-resourceType-v4.xsd +49 -0
- codemeticulous/datacite/schemas/4.6/include/datacite-titleType-v4.xsd +14 -0
- codemeticulous/datacite/schemas/4.6/include/xml.xsd +286 -0
- codemeticulous/datacite/schemas/4.6/metadata.xsd +712 -0
- codemeticulous/datacite/schemas/4.6/schema.json +628 -0
- codemeticulous/datacite/schemas/4.7/include/datacite-contributorType-v4.xsd +37 -0
- codemeticulous/datacite/schemas/4.7/include/datacite-dateType-v4.xsd +27 -0
- codemeticulous/datacite/schemas/4.7/include/datacite-descriptionType-v4.xsd +19 -0
- codemeticulous/datacite/schemas/4.7/include/datacite-funderIdentifierType-v4.xsd +16 -0
- codemeticulous/datacite/schemas/4.7/include/datacite-nameType-v4.xsd +10 -0
- codemeticulous/datacite/schemas/4.7/include/datacite-numberType-v4.xsd +12 -0
- codemeticulous/datacite/schemas/4.7/include/datacite-relatedIdentifierType-v4.xsd +40 -0
- codemeticulous/datacite/schemas/4.7/include/datacite-relationType-v4.xsd +59 -0
- codemeticulous/datacite/schemas/4.7/include/datacite-resourceType-v4.xsd +52 -0
- codemeticulous/datacite/schemas/4.7/include/datacite-titleType-v4.xsd +14 -0
- codemeticulous/datacite/schemas/4.7/include/xml.xsd +286 -0
- codemeticulous/datacite/schemas/4.7/metadata.xsd +715 -0
- codemeticulous/datacite/schemas/NOTICE.md +11 -0
- codemeticulous/datacite/schemas/__init__.py +18 -0
- codemeticulous/datacite/xml.py +325 -0
- codemeticulous/formats.py +50 -0
- codemeticulous/identifiers.py +75 -0
- codemeticulous/licenses.py +26 -0
- codemeticulous/mixins.py +30 -0
- codemeticulous/models.py +119 -0
- codemeticulous/provenance.py +345 -0
- codemeticulous/py.typed +0 -0
- codemeticulous/serialization.py +18 -0
- codemeticulous/skills/__init__.py +291 -0
- codemeticulous/skills/_windows.py +53 -0
- codemeticulous/skills/research-software-metadata/SKILL.md +41 -0
- codemeticulous/skills/research-software-metadata/references/collection-contract.md +69 -0
- codemeticulous/skills/research-software-metadata/references/report-template.md +37 -0
- codemeticulous/skills/research-software-metadata/references/source-discovery.md +73 -0
- codemeticulous/skills/research-software-metadata/references/supported-formats.md +101 -0
- codemeticulous/standards.py +26 -0
- codemeticulous/targets.py +145 -0
- codemeticulous/utils.py +48 -0
- codemeticulous/validation.py +218 -0
- codemeticulous-0.1.0.dist-info/METADATA +160 -0
- codemeticulous-0.1.0.dist-info/RECORD +84 -0
- codemeticulous-0.1.0.dist-info/WHEEL +5 -0
- codemeticulous-0.1.0.dist-info/entry_points.txt +2 -0
- codemeticulous-0.1.0.dist-info/licenses/LICENSE +674 -0
- codemeticulous-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
from .conversion import (
|
|
2
|
+
ConversionError,
|
|
3
|
+
ConversionIssue,
|
|
4
|
+
ConversionResult,
|
|
5
|
+
Transformation,
|
|
6
|
+
)
|
|
7
|
+
from .provenance import (
|
|
8
|
+
AcquisitionMethod,
|
|
9
|
+
CollectedSoftwareMetadata,
|
|
10
|
+
CollectionStatus,
|
|
11
|
+
Evidence,
|
|
12
|
+
FieldCollection,
|
|
13
|
+
SourceReference,
|
|
14
|
+
collection_json_schema,
|
|
15
|
+
validate_collection,
|
|
16
|
+
)
|
|
17
|
+
from .convert import convert
|
|
18
|
+
from .formats import (
|
|
19
|
+
CffVersion,
|
|
20
|
+
CodeMetaVersion,
|
|
21
|
+
DataCiteVersion,
|
|
22
|
+
SoftwareMetadataVersion,
|
|
23
|
+
MetadataFormat,
|
|
24
|
+
)
|
|
25
|
+
from .targets import (
|
|
26
|
+
Cff,
|
|
27
|
+
CodeMetaJson,
|
|
28
|
+
CodeMetaJsonV4,
|
|
29
|
+
DataCiteJson,
|
|
30
|
+
DataCiteXml,
|
|
31
|
+
SoftwareMetadataJson,
|
|
32
|
+
Target,
|
|
33
|
+
)
|
|
34
|
+
from .serialization import serialize
|
|
35
|
+
from .validation import validate_document, validate_file
|
|
36
|
+
from .models import (
|
|
37
|
+
Agent,
|
|
38
|
+
Affiliation,
|
|
39
|
+
Contribution,
|
|
40
|
+
Identifier,
|
|
41
|
+
License,
|
|
42
|
+
RelatedResource,
|
|
43
|
+
SoftwareMetadata,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
__all__ = [
|
|
47
|
+
"AcquisitionMethod",
|
|
48
|
+
"Agent",
|
|
49
|
+
"Affiliation",
|
|
50
|
+
"Contribution",
|
|
51
|
+
"Cff",
|
|
52
|
+
"CffVersion",
|
|
53
|
+
"CodeMetaJson",
|
|
54
|
+
"CodeMetaJsonV4",
|
|
55
|
+
"CodeMetaVersion",
|
|
56
|
+
"DataCiteJson",
|
|
57
|
+
"DataCiteVersion",
|
|
58
|
+
"DataCiteXml",
|
|
59
|
+
"CollectedSoftwareMetadata",
|
|
60
|
+
"CollectionStatus",
|
|
61
|
+
"ConversionError",
|
|
62
|
+
"ConversionIssue",
|
|
63
|
+
"ConversionResult",
|
|
64
|
+
"Evidence",
|
|
65
|
+
"FieldCollection",
|
|
66
|
+
"convert",
|
|
67
|
+
"Identifier",
|
|
68
|
+
"License",
|
|
69
|
+
"RelatedResource",
|
|
70
|
+
"SourceReference",
|
|
71
|
+
"SoftwareMetadata",
|
|
72
|
+
"SoftwareMetadataJson",
|
|
73
|
+
"SoftwareMetadataVersion",
|
|
74
|
+
"MetadataFormat",
|
|
75
|
+
"Target",
|
|
76
|
+
"Transformation",
|
|
77
|
+
"collection_json_schema",
|
|
78
|
+
"serialize",
|
|
79
|
+
"validate_document",
|
|
80
|
+
"validate_collection",
|
|
81
|
+
"validate_file",
|
|
82
|
+
]
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from .convert import software_metadata_to_cff
|
|
2
|
+
from .identifiers import (
|
|
3
|
+
CffIdentifier,
|
|
4
|
+
DoiIdentifier,
|
|
5
|
+
OtherIdentifier,
|
|
6
|
+
SwhIdentifier,
|
|
7
|
+
UrlIdentifier,
|
|
8
|
+
)
|
|
9
|
+
from .models import CitationFileFormat
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"CffIdentifier",
|
|
13
|
+
"CitationFileFormat",
|
|
14
|
+
"DoiIdentifier",
|
|
15
|
+
"OtherIdentifier",
|
|
16
|
+
"SwhIdentifier",
|
|
17
|
+
"UrlIdentifier",
|
|
18
|
+
"software_metadata_to_cff",
|
|
19
|
+
]
|
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
"""Conversion from the neutral metadata model to Citation File Format."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from datetime import date
|
|
6
|
+
import re
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from jsonschema.exceptions import ValidationError as SchemaValidationError
|
|
10
|
+
from pydantic import ValidationError
|
|
11
|
+
|
|
12
|
+
from codemeticulous.cff.schema import cff_validator
|
|
13
|
+
from codemeticulous.conversion import ConversionError, ConversionIssue, ConversionResult
|
|
14
|
+
from codemeticulous.identifiers import normalize_doi, orcid_url
|
|
15
|
+
from codemeticulous.licenses import parse_license_expression
|
|
16
|
+
from codemeticulous.models import (
|
|
17
|
+
Agent,
|
|
18
|
+
Identifier,
|
|
19
|
+
License,
|
|
20
|
+
RelatedResource,
|
|
21
|
+
SoftwareMetadata,
|
|
22
|
+
)
|
|
23
|
+
from codemeticulous.cff.identifiers import (
|
|
24
|
+
CffIdentifier,
|
|
25
|
+
DoiIdentifier,
|
|
26
|
+
OtherIdentifier,
|
|
27
|
+
SwhIdentifier,
|
|
28
|
+
UrlIdentifier,
|
|
29
|
+
)
|
|
30
|
+
from codemeticulous.cff.models import (
|
|
31
|
+
CitationFileFormat,
|
|
32
|
+
Entity,
|
|
33
|
+
Person,
|
|
34
|
+
Reference,
|
|
35
|
+
ReferenceType,
|
|
36
|
+
SpdxLicenseId,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
_DOI = re.compile(
|
|
40
|
+
r"^(?:https?://(?:dx\.)?doi\.org/)?(10\.\d{4,9}(?:\.\d+)?/[A-Za-z0-9:/_;\-.()\[\]\\]+)$"
|
|
41
|
+
)
|
|
42
|
+
_SWH = re.compile(r"^swh:1:(?:snp|rel|rev|dir|cnt):[0-9a-fA-F]{40}$")
|
|
43
|
+
_SPDX = {item.value for item in SpdxLicenseId}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _issue(path: str, message: str) -> ConversionIssue:
|
|
47
|
+
return ConversionIssue(path=path, message=message)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _agent(agent: Agent, path: str, issues: list[ConversionIssue]) -> Person | Entity:
|
|
51
|
+
kwargs: dict[str, Any] = {"name": agent.name}
|
|
52
|
+
if agent.email:
|
|
53
|
+
kwargs["email"] = agent.email
|
|
54
|
+
if agent.url:
|
|
55
|
+
kwargs["website"] = agent.url
|
|
56
|
+
for index, identifier in enumerate(agent.identifiers):
|
|
57
|
+
orcid = orcid_url(identifier.value, identifier.scheme)
|
|
58
|
+
if orcid and "orcid" not in kwargs:
|
|
59
|
+
kwargs["orcid"] = orcid
|
|
60
|
+
else:
|
|
61
|
+
issues.append(
|
|
62
|
+
_issue(
|
|
63
|
+
f"{path}.identifiers[{index}]",
|
|
64
|
+
"omitted agent identifier because CFF exports only the first valid ORCID",
|
|
65
|
+
)
|
|
66
|
+
)
|
|
67
|
+
if agent.kind == "person":
|
|
68
|
+
kwargs.pop("name")
|
|
69
|
+
if agent.affiliations:
|
|
70
|
+
kwargs["affiliation"] = agent.affiliations[0].name
|
|
71
|
+
for index in range(1, len(agent.affiliations)):
|
|
72
|
+
issues.append(
|
|
73
|
+
_issue(
|
|
74
|
+
f"{path}.affiliations[{index}]",
|
|
75
|
+
"omitted additional affiliation because CFF accepts one affiliation name per person",
|
|
76
|
+
)
|
|
77
|
+
)
|
|
78
|
+
for index, affiliation in enumerate(agent.affiliations):
|
|
79
|
+
if affiliation.identifier:
|
|
80
|
+
issues.append(
|
|
81
|
+
_issue(
|
|
82
|
+
f"{path}.affiliations[{index}].identifier",
|
|
83
|
+
"omitted affiliation identifier because CFF accepts affiliation names only",
|
|
84
|
+
)
|
|
85
|
+
)
|
|
86
|
+
if agent.given_names:
|
|
87
|
+
kwargs["given_names"] = " ".join(agent.given_names)
|
|
88
|
+
if agent.family_names:
|
|
89
|
+
kwargs["family_names"] = " ".join(agent.family_names)
|
|
90
|
+
if not agent.given_names and not agent.family_names:
|
|
91
|
+
kwargs["given_names"] = agent.name
|
|
92
|
+
return Person(**kwargs)
|
|
93
|
+
if agent.kind == "organization":
|
|
94
|
+
for index, affiliation in enumerate(agent.affiliations):
|
|
95
|
+
issues.append(
|
|
96
|
+
_issue(
|
|
97
|
+
f"{path}.affiliations[{index}]",
|
|
98
|
+
"omitted affiliation because CFF entities do not have an affiliation field",
|
|
99
|
+
)
|
|
100
|
+
)
|
|
101
|
+
if affiliation.identifier:
|
|
102
|
+
issues.append(
|
|
103
|
+
_issue(
|
|
104
|
+
f"{path}.affiliations[{index}].identifier",
|
|
105
|
+
"omitted affiliation identifier because CFF accepts affiliation names only",
|
|
106
|
+
)
|
|
107
|
+
)
|
|
108
|
+
return Entity(**kwargs)
|
|
109
|
+
issues.append(_issue(path, "mapped unknown agent kind to a CFF entity"))
|
|
110
|
+
for index, affiliation in enumerate(agent.affiliations):
|
|
111
|
+
issues.append(
|
|
112
|
+
_issue(
|
|
113
|
+
f"{path}.affiliations[{index}]",
|
|
114
|
+
"omitted affiliation because CFF entities do not have an affiliation field",
|
|
115
|
+
)
|
|
116
|
+
)
|
|
117
|
+
if affiliation.identifier:
|
|
118
|
+
issues.append(
|
|
119
|
+
_issue(
|
|
120
|
+
f"{path}.affiliations[{index}].identifier",
|
|
121
|
+
"omitted affiliation identifier because CFF accepts affiliation names only",
|
|
122
|
+
)
|
|
123
|
+
)
|
|
124
|
+
return Entity(**kwargs)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _identifier(identifier: Identifier, path: str) -> CffIdentifier:
|
|
128
|
+
scheme = (identifier.scheme or "").lower()
|
|
129
|
+
value = identifier.value
|
|
130
|
+
normalized_doi = normalize_doi(value)
|
|
131
|
+
if scheme == "doi":
|
|
132
|
+
match = _DOI.match(normalized_doi or "")
|
|
133
|
+
if not match:
|
|
134
|
+
raise ConversionError(f"{path}: invalid DOI")
|
|
135
|
+
return DoiIdentifier(type="doi", value=match.group(1))
|
|
136
|
+
if scheme in {"swh", "softwareheritage"}:
|
|
137
|
+
return SwhIdentifier(type="swh", value=value)
|
|
138
|
+
if scheme in {"url", "uri"}:
|
|
139
|
+
return UrlIdentifier(type="url", value=value)
|
|
140
|
+
if scheme == "other":
|
|
141
|
+
return OtherIdentifier(type="other", value=value)
|
|
142
|
+
match = _DOI.match(normalized_doi or "")
|
|
143
|
+
if match:
|
|
144
|
+
return DoiIdentifier(type="doi", value=match.group(1))
|
|
145
|
+
if _SWH.match(value):
|
|
146
|
+
return SwhIdentifier(type="swh", value=value)
|
|
147
|
+
if value.startswith(("http://", "https://")):
|
|
148
|
+
return UrlIdentifier(type="url", value=value)
|
|
149
|
+
return OtherIdentifier(type="other", value=value)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _spdx_id(item: License) -> str | None:
|
|
153
|
+
label = _spdx_value(item.identifier or item.name)
|
|
154
|
+
if item.url is None:
|
|
155
|
+
return label
|
|
156
|
+
linked = _spdx_value(item.url)
|
|
157
|
+
if (item.identifier or label) and label != linked:
|
|
158
|
+
return None
|
|
159
|
+
return linked
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _spdx_value(candidate: str | None) -> str | None:
|
|
163
|
+
if not candidate:
|
|
164
|
+
return None
|
|
165
|
+
for prefix in ("https://spdx.org/licenses/", "http://spdx.org/licenses/"):
|
|
166
|
+
if candidate.startswith(prefix):
|
|
167
|
+
candidate = candidate.removeprefix(prefix).removesuffix(".html")
|
|
168
|
+
break
|
|
169
|
+
return candidate if candidate in _SPDX else None
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _licenses(
|
|
173
|
+
metadata: SoftwareMetadata, issues: list[ConversionIssue]
|
|
174
|
+
) -> tuple[str | list[str] | None, str | None]:
|
|
175
|
+
def omit(reason: str, path: str = "licenses") -> tuple[None, None]:
|
|
176
|
+
issues.append(
|
|
177
|
+
_issue(
|
|
178
|
+
path,
|
|
179
|
+
f"omitted CFF license fields because of {reason}. Consult the source's written licensing terms",
|
|
180
|
+
)
|
|
181
|
+
)
|
|
182
|
+
return None, None
|
|
183
|
+
|
|
184
|
+
licenses = metadata.licenses
|
|
185
|
+
for index, item in enumerate(licenses):
|
|
186
|
+
known_ids = {
|
|
187
|
+
identifier
|
|
188
|
+
for value in (item.identifier, item.name, item.url)
|
|
189
|
+
if (identifier := _spdx_value(value)) is not None
|
|
190
|
+
}
|
|
191
|
+
if len(known_ids) > 1:
|
|
192
|
+
return omit("conflicting SPDX identifiers", f"licenses[{index}]")
|
|
193
|
+
identifiers = [_spdx_id(item) for item in licenses]
|
|
194
|
+
if len(licenses) == 1:
|
|
195
|
+
item = licenses[0]
|
|
196
|
+
if (
|
|
197
|
+
item.url
|
|
198
|
+
and _spdx_value(item.identifier or item.name)
|
|
199
|
+
and identifiers[0] is None
|
|
200
|
+
):
|
|
201
|
+
if metadata.license_statement:
|
|
202
|
+
return omit(
|
|
203
|
+
"an unverified SPDX identifier for the license URL",
|
|
204
|
+
"licenses[0]",
|
|
205
|
+
)
|
|
206
|
+
issues.append(
|
|
207
|
+
_issue(
|
|
208
|
+
"licenses[0]",
|
|
209
|
+
"kept the license URL and omitted the unverified SPDX identifier",
|
|
210
|
+
)
|
|
211
|
+
)
|
|
212
|
+
return None, item.url
|
|
213
|
+
alternatives = list(
|
|
214
|
+
dict.fromkeys(
|
|
215
|
+
identifier for identifier in identifiers if identifier is not None
|
|
216
|
+
)
|
|
217
|
+
)
|
|
218
|
+
relationship = metadata.license_relationship
|
|
219
|
+
statement = parse_license_expression(metadata.license_statement)
|
|
220
|
+
if statement:
|
|
221
|
+
operator, declared_ids = statement
|
|
222
|
+
if operator is None and len(declared_ids) > 1:
|
|
223
|
+
return omit(
|
|
224
|
+
"a combination of AND and OR in the licensing terms",
|
|
225
|
+
"license_statement",
|
|
226
|
+
)
|
|
227
|
+
recorded_ids = {
|
|
228
|
+
identifier or item.identifier
|
|
229
|
+
for item, identifier in zip(licenses, identifiers)
|
|
230
|
+
}
|
|
231
|
+
if set(declared_ids) != recorded_ids or relationship not in (None, operator):
|
|
232
|
+
return omit(
|
|
233
|
+
"a disagreement between the license statement and the canonical licenses",
|
|
234
|
+
"license_statement",
|
|
235
|
+
)
|
|
236
|
+
relationship = operator
|
|
237
|
+
|
|
238
|
+
written_terms = (metadata.license_statement and not statement) or any(
|
|
239
|
+
item.description for item in licenses
|
|
240
|
+
)
|
|
241
|
+
if len(licenses) > 1:
|
|
242
|
+
if len(alternatives) == 1 and None not in identifiers and not written_terms:
|
|
243
|
+
return alternatives[0], None
|
|
244
|
+
if relationship is None:
|
|
245
|
+
return omit("unclear terms about the relationship between the licenses")
|
|
246
|
+
if relationship == "AND":
|
|
247
|
+
return omit("terms that require AND while CFF license lists mean OR")
|
|
248
|
+
if None in identifiers:
|
|
249
|
+
return omit("at least one alternative with no CFF 1.2.0 SPDX identifier")
|
|
250
|
+
if written_terms:
|
|
251
|
+
issues.append(
|
|
252
|
+
_issue(
|
|
253
|
+
"licenses",
|
|
254
|
+
"exported the SPDX alternatives without the source's written licensing terms",
|
|
255
|
+
)
|
|
256
|
+
)
|
|
257
|
+
return alternatives, None
|
|
258
|
+
|
|
259
|
+
if written_terms:
|
|
260
|
+
if licenses and not alternatives and licenses[0].url:
|
|
261
|
+
return None, licenses[0].url
|
|
262
|
+
return omit("written terms that require the complete license statement")
|
|
263
|
+
if alternatives:
|
|
264
|
+
return alternatives[0], None
|
|
265
|
+
if licenses:
|
|
266
|
+
if licenses[0].url:
|
|
267
|
+
return None, licenses[0].url
|
|
268
|
+
issues.append(
|
|
269
|
+
_issue(
|
|
270
|
+
"licenses[0]",
|
|
271
|
+
"omitted license because CFF requires a supported SPDX identifier or a license URL",
|
|
272
|
+
)
|
|
273
|
+
)
|
|
274
|
+
return None, None
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def _reference(
|
|
278
|
+
resource: RelatedResource, path: str, issues: list[ConversionIssue]
|
|
279
|
+
) -> Reference | None:
|
|
280
|
+
if resource.relation not in {"cites", "requires"}:
|
|
281
|
+
issues.append(
|
|
282
|
+
_issue(
|
|
283
|
+
path,
|
|
284
|
+
f"omitted relation {resource.relation!r} because CFF conversion does not support it",
|
|
285
|
+
)
|
|
286
|
+
)
|
|
287
|
+
return None
|
|
288
|
+
if not resource.title:
|
|
289
|
+
issues.append(
|
|
290
|
+
_issue(f"{path}.title", "omitted reference because CFF requires a title")
|
|
291
|
+
)
|
|
292
|
+
return None
|
|
293
|
+
if not resource.creators:
|
|
294
|
+
issues.append(
|
|
295
|
+
_issue(f"{path}.creators", "omitted reference because CFF requires authors")
|
|
296
|
+
)
|
|
297
|
+
return None
|
|
298
|
+
reference_type = ReferenceType.from_canonical(resource.resource_type or "")
|
|
299
|
+
if reference_type is None:
|
|
300
|
+
reference_type = ReferenceType.generic
|
|
301
|
+
if resource.resource_type:
|
|
302
|
+
issues.append(
|
|
303
|
+
_issue(
|
|
304
|
+
f"{path}.resource_type",
|
|
305
|
+
"mapped unsupported resource type to a generic CFF reference",
|
|
306
|
+
)
|
|
307
|
+
)
|
|
308
|
+
if resource.relation == "requires":
|
|
309
|
+
issues.append(
|
|
310
|
+
_issue(
|
|
311
|
+
f"{path}.relation",
|
|
312
|
+
"exported dependency as a CFF reference without its dependency relation",
|
|
313
|
+
)
|
|
314
|
+
)
|
|
315
|
+
kwargs: dict[str, Any] = {
|
|
316
|
+
"title": resource.title,
|
|
317
|
+
"type": reference_type,
|
|
318
|
+
"authors": [
|
|
319
|
+
_agent(agent, f"{path}.creators[{i}]", issues)
|
|
320
|
+
for i, agent in enumerate(resource.creators)
|
|
321
|
+
],
|
|
322
|
+
}
|
|
323
|
+
if resource.url:
|
|
324
|
+
kwargs["url"] = resource.url
|
|
325
|
+
if resource.identifier:
|
|
326
|
+
mapped = _identifier(resource.identifier, f"{path}.identifier")
|
|
327
|
+
if mapped.type == "doi":
|
|
328
|
+
kwargs["doi"] = mapped.value
|
|
329
|
+
elif mapped.type == "url":
|
|
330
|
+
kwargs["url"] = mapped.value
|
|
331
|
+
else:
|
|
332
|
+
issues.append(
|
|
333
|
+
_issue(
|
|
334
|
+
f"{path}.identifier",
|
|
335
|
+
"omitted identifier because CFF references do not support its type",
|
|
336
|
+
)
|
|
337
|
+
)
|
|
338
|
+
return Reference(**kwargs)
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
def _convert(value: SoftwareMetadata) -> ConversionResult[CitationFileFormat]:
|
|
342
|
+
"""Convert neutral software metadata to CFF, retaining deterministic issues."""
|
|
343
|
+
if not value.creators:
|
|
344
|
+
raise ConversionError("creators: CFF requires at least one author")
|
|
345
|
+
|
|
346
|
+
issues: list[ConversionIssue] = []
|
|
347
|
+
unsupported = (
|
|
348
|
+
("publisher", value.publisher),
|
|
349
|
+
("contributors", value.contributors),
|
|
350
|
+
("category", value.category),
|
|
351
|
+
("publication_year", value.publication_year),
|
|
352
|
+
("date_created", value.date_created),
|
|
353
|
+
("date_modified", value.date_modified),
|
|
354
|
+
("programming_languages", value.programming_languages),
|
|
355
|
+
("formats", value.formats),
|
|
356
|
+
("sizes", value.sizes),
|
|
357
|
+
("release_notes", value.release_notes),
|
|
358
|
+
("issue_tracker", value.issue_tracker),
|
|
359
|
+
("continuous_integration", value.continuous_integration),
|
|
360
|
+
("unmapped_properties", value.unmapped_properties),
|
|
361
|
+
)
|
|
362
|
+
for path, item in unsupported:
|
|
363
|
+
if item:
|
|
364
|
+
issues.append(
|
|
365
|
+
_issue(path, "omitted field because CFF conversion does not support it")
|
|
366
|
+
)
|
|
367
|
+
identifiers: list[CffIdentifier] = []
|
|
368
|
+
doi: str | None = None
|
|
369
|
+
for index, item in enumerate(value.identifiers):
|
|
370
|
+
mapped = _identifier(item, f"identifiers[{index}]")
|
|
371
|
+
if mapped.type == "doi" and doi is None:
|
|
372
|
+
doi = mapped.value
|
|
373
|
+
else:
|
|
374
|
+
if mapped.type == "doi":
|
|
375
|
+
issues.append(
|
|
376
|
+
_issue(
|
|
377
|
+
f"identifiers[{index}]",
|
|
378
|
+
"kept additional DOI in the CFF identifiers list",
|
|
379
|
+
)
|
|
380
|
+
)
|
|
381
|
+
identifiers.append(mapped)
|
|
382
|
+
|
|
383
|
+
license_, license_url = _licenses(value, issues)
|
|
384
|
+
url = value.url or value.download_url
|
|
385
|
+
if value.url and value.download_url:
|
|
386
|
+
issues.append(
|
|
387
|
+
_issue(
|
|
388
|
+
"download_url",
|
|
389
|
+
"omitted download URL because the landing-page URL takes precedence in CFF",
|
|
390
|
+
)
|
|
391
|
+
)
|
|
392
|
+
elif value.download_url:
|
|
393
|
+
issues.append(
|
|
394
|
+
_issue(
|
|
395
|
+
"download_url",
|
|
396
|
+
"used the download URL as the CFF url without identifying it as a download",
|
|
397
|
+
)
|
|
398
|
+
)
|
|
399
|
+
|
|
400
|
+
references: list[Reference] = []
|
|
401
|
+
for index, relation in enumerate(value.relations):
|
|
402
|
+
reference = _reference(relation, f"relations[{index}]", issues)
|
|
403
|
+
if reference is not None:
|
|
404
|
+
references.append(reference)
|
|
405
|
+
|
|
406
|
+
date_released: date | None = value.date_released # type: ignore[assignment]
|
|
407
|
+
if isinstance(value.date_released, str):
|
|
408
|
+
try:
|
|
409
|
+
date_released = date.fromisoformat(value.date_released)
|
|
410
|
+
except ValueError as exc:
|
|
411
|
+
raise ConversionError(
|
|
412
|
+
"date_released: value is not a valid ISO date"
|
|
413
|
+
) from exc
|
|
414
|
+
|
|
415
|
+
payload: dict[str, Any] = {
|
|
416
|
+
"cff_version": "1.2.0",
|
|
417
|
+
"message": "If you use this software, please cite it using the metadata from this file.",
|
|
418
|
+
"title": value.title,
|
|
419
|
+
"type": "software",
|
|
420
|
+
"abstract": value.description,
|
|
421
|
+
"authors": [
|
|
422
|
+
_agent(agent, f"creators[{i}]", issues)
|
|
423
|
+
for i, agent in enumerate(value.creators)
|
|
424
|
+
],
|
|
425
|
+
"commit": value.revision,
|
|
426
|
+
"date_released": date_released,
|
|
427
|
+
"doi": doi,
|
|
428
|
+
"identifiers": identifiers or None,
|
|
429
|
+
"version": value.version,
|
|
430
|
+
"keywords": value.keywords or None,
|
|
431
|
+
"license": license_,
|
|
432
|
+
"license_url": license_url,
|
|
433
|
+
"repository": value.repository,
|
|
434
|
+
"repository_code": value.repository_code,
|
|
435
|
+
"repository_artifact": value.repository_artifact,
|
|
436
|
+
"url": url,
|
|
437
|
+
"references": references or None,
|
|
438
|
+
}
|
|
439
|
+
result = CitationFileFormat(**payload)
|
|
440
|
+
try:
|
|
441
|
+
cff_validator().validate(
|
|
442
|
+
result.model_dump(mode="json", by_alias=True, exclude_none=True)
|
|
443
|
+
)
|
|
444
|
+
except SchemaValidationError as exc:
|
|
445
|
+
raise ConversionError(
|
|
446
|
+
f"Invalid CFF output at {exc.json_path}: {exc.message}"
|
|
447
|
+
) from exc
|
|
448
|
+
return ConversionResult(value=result, issues=tuple(issues))
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def software_metadata_to_cff(
|
|
452
|
+
value: SoftwareMetadata,
|
|
453
|
+
) -> ConversionResult[CitationFileFormat]:
|
|
454
|
+
try:
|
|
455
|
+
return _convert(value)
|
|
456
|
+
except ConversionError:
|
|
457
|
+
raise
|
|
458
|
+
except ValidationError as exc:
|
|
459
|
+
raise ConversionError(f"Unable to construct CitationFileFormat: {exc}") from exc
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
__all__ = ["software_metadata_to_cff"]
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""Identifier variants accepted by CFF."""
|
|
2
|
+
|
|
3
|
+
from typing import TypeAlias
|
|
4
|
+
|
|
5
|
+
from .models import DoiIdentifier, OtherIdentifier, SwhIdentifier, UrlIdentifier
|
|
6
|
+
|
|
7
|
+
CffIdentifier: TypeAlias = (
|
|
8
|
+
DoiIdentifier | UrlIdentifier | SwhIdentifier | OtherIdentifier
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"CffIdentifier",
|
|
13
|
+
"DoiIdentifier",
|
|
14
|
+
"OtherIdentifier",
|
|
15
|
+
"SwhIdentifier",
|
|
16
|
+
"UrlIdentifier",
|
|
17
|
+
]
|