1- # Code by Brave AI
1+ import json
2+ import sys
3+
4+ from cffconvert import Citation
25from pydantic import ValidationError
36from pydantic_yaml import to_yaml_str
4- import json
7+
8+ from crosswalks .codemeta_to_citation_cff import (
9+ get_citation_file_format_from_codemeta_software ,
10+ get_pydantic_model_from_dict ,
11+ )
512from models .codemeta .v3_1 .codemeta_pydantic import Software
6- from crosswalks .codemeta_to_citation_cff import get_citation_file_format_from_codemeta_software , get_pydantic_model_from_dict
7- import sys
8- import subprocess
913
1014status = 1
1115
1216def 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 )
17+ ret = False
18+ try :
19+ # Initialize the Citation object with raw text instead of a file path
20+ citation = Citation (cffstr = citation_cff_yaml_str )
21+
22+ # Run the built-in validation check
23+ citation .validate ()
24+
25+ ret = True
26+ except Exception as e :
27+ print (f"ERROR: cffconvert error occurred during parsing: { e } " , file = sys .stderr )
2028
2129 return ret
2230
@@ -42,7 +50,7 @@ def convert(codemeta_path: str):
4250 citation_cff = get_citation_file_format_from_codemeta_software (codemeta_software )
4351
4452 if citation_cff :
45- citation_cff_yaml_str = to_yaml_str (citation_cff , exclude_none = True )
53+ citation_cff_yaml_str = to_yaml_str (citation_cff , by_alias = True , exclude_none = True )
4654 print (citation_cff_yaml_str )
4755
4856 if validate_cff (citation_cff_yaml_str ):
0 commit comments