Skip to content

Commit 7aaff77

Browse files
feat: conversion now includes an additional validation of the cff output with cffconvert
1 parent 900e686 commit 7aaff77

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ htmlcov/
2929
# repo-specific
3030
models/citation_cff/v1_2_0/schema.json
3131
tests/data/out
32+
/tmp

codemeta_convert.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,21 @@
55
from models.codemeta.v3_1.codemeta_pydantic import Software
66
from crosswalks.codemeta_to_citation_cff import get_citation_file_format_from_codemeta_software, get_pydantic_model_from_dict
77
import sys
8+
import subprocess
89

910
status = 1
1011

12+
def validate_cff(citation_cff_yaml_str):
13+
# Assuming the CITATION.cff file is in the current directory
14+
result = subprocess.run(["cffconvert", "--validate"], capture_output=True, text=True)
15+
16+
ret = result.returncode == 0
17+
18+
if not ret:
19+
print("ERROR: cffconvert validation failed.", result.stderr, file=sys.stderr)
20+
21+
return ret
22+
1123
def convert(codemeta_path: str):
1224
# codemeta_path = 'tests/data/in/codemeta-codemeta-3.0.json'
1325
# codemeta_path = 'tests/data/in/codemeta-isicily.json'
@@ -21,7 +33,7 @@ def convert(codemeta_path: str):
2133
with open(codemeta_path, 'r') as f:
2234
codemeta_data = json.load(f)
2335
except Exception as e:
24-
print(f'ERROR: {e}', sys.stderr)
36+
print(f'ERROR: {e}', file=sys.stderr)
2537

2638
if codemeta_data:
2739
codemeta_software = get_pydantic_model_from_dict(codemeta_data, Software)
@@ -30,8 +42,11 @@ def convert(codemeta_path: str):
3042
citation_cff = get_citation_file_format_from_codemeta_software(codemeta_software)
3143

3244
if citation_cff:
33-
print(to_yaml_str(citation_cff, exclude_none=True))
34-
ret = 0
45+
citation_cff_yaml_str = to_yaml_str(citation_cff, exclude_none=True)
46+
print(citation_cff_yaml_str)
47+
48+
if validate_cff(citation_cff_yaml_str):
49+
ret = 0
3550

3651
return ret
3752

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ pyld==3.1.0 # TODO: check what this is needed for
33
datamodel-code-generator==0.66.0 # to convert cff json-schema into Pydantic models
44
pydantic_yaml==1.7.0 # to serialise cff model instance into yaml
55
pydantic2-schemaorg==0.3.0 # schema.org classes already encoded as Pydantic model
6-
6+
cffconvert==2.0.0 # for CITATION.cff -> bibtex output

0 commit comments

Comments
 (0)