Skip to content

Commit 5899850

Browse files
committed
feat: allow custom field overrides when converting
1 parent 219fbfd commit 5899850

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ codemeta = CodeMeta(
101101
author=Person(givenName="Dale", familyName="Earnhardt"),
102102
)
103103

104-
cff = convert("codemeta", "cff", codemeta)
104+
# commit kwarg is an override that can be used to insert
105+
# a custom field into the resulting metadata after conversion
106+
cff = convert("codemeta", "cff", codemeta, commit="abcdef123456789")
105107

106108
print(codemeta.json(indent=True))
107109
# {
@@ -119,6 +121,7 @@ print(cff.yaml())
119121
# message: If you use this software, please cite it using the metadata from this file.
120122
# title: My Project
121123
# type: software
124+
# commit: abcdef123456789
122125
```
123126

124127
<!-- ### As a Github Action -->

codemeticulous/cff/convert.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def extract_main_url_from_codemeta(data: CodeMeta) -> str:
211211
)
212212

213213

214-
def canonical_to_cff(data: CanonicalCodeMeta) -> CitationFileFormat:
214+
def canonical_to_cff(data: CanonicalCodeMeta, **custom_fields) -> CitationFileFormat:
215215
"""Extract all possible Citation File Format fields from a CodeMeta object based
216216
on the CodeMeta crosswalk and return a CitationFileFormat object
217217
"""
@@ -245,6 +245,7 @@ def canonical_to_cff(data: CanonicalCodeMeta) -> CitationFileFormat:
245245
type="software",
246246
url=extract_main_url_from_codemeta(data),
247247
version=data.version,
248+
**custom_fields,
248249
)
249250

250251

codemeticulous/codemeta/convert.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from codemeticulous.codemeta.models import CodeMeta
33

44

5-
def canonical_to_codemeta(data: CanonicalCodeMeta) -> CodeMeta:
6-
return CodeMeta(**data.dict())
5+
def canonical_to_codemeta(data: CanonicalCodeMeta, **custom_fields) -> CodeMeta:
6+
return CodeMeta(**data.dict(), **custom_fields)
77

88

99
def codemeta_to_canonical(data: CodeMeta) -> CanonicalCodeMeta:

codemeticulous/convert.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ def to_canonical(source_format: str, source_data):
4141
return canonical_instance
4242

4343

44-
def from_canonical(target_format: str, canonical_instance):
44+
def from_canonical(target_format: str, canonical_instance, **custom_fields):
4545
canonical_to_target = STANDARDS[target_format]["from_canonical"]
46-
target_instance = canonical_to_target(canonical_instance)
46+
target_instance = canonical_to_target(canonical_instance, **custom_fields)
4747

4848
return target_instance
4949

5050

51-
def convert(source_format: str, target_format: str, source_data):
51+
def convert(source_format: str, target_format: str, source_data, **custom_fields):
5252
# FIXME: add tons of error handling
5353

5454
canonical_instance = to_canonical(source_format, source_data)
55-
return from_canonical(target_format, canonical_instance)
55+
return from_canonical(target_format, canonical_instance, **custom_fields)

codemeticulous/datacite/convert.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def codemeta_language_fileformat_to_datacite_format(
171171
return formats or None
172172

173173

174-
def canonical_to_datacite(data: CanonicalCodeMeta, ignore_existing_doi=False) -> DataciteV45:
174+
def canonical_to_datacite(data: CanonicalCodeMeta, ignore_existing_doi=False, **custom_fields) -> DataciteV45:
175175
primary_doi = (
176176
extract_doi_from_identifier(data.identifier)
177177
if not ignore_existing_doi
@@ -232,6 +232,7 @@ def canonical_to_datacite(data: CanonicalCodeMeta, ignore_existing_doi=False) ->
232232
# codemeta.funding is a plain string, can't really ensure that the string
233233
# is the required name field
234234
# fundingReferences=None,
235+
**custom_fields,
235236
)
236237

237238

0 commit comments

Comments
 (0)