-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodemeta_convert.py
More file actions
72 lines (52 loc) · 1.99 KB
/
Copy pathcodemeta_convert.py
File metadata and controls
72 lines (52 loc) · 1.99 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
62
63
64
65
66
67
68
69
70
71
72
import json
import sys
from cffconvert import Citation
from pydantic import ValidationError
from pydantic_yaml import to_yaml_str
from crosswalks.codemeta_to_citation_cff import (
get_citation_file_format_from_codemeta_software,
get_pydantic_model_from_dict,
)
from models.codemeta.v3_1.codemeta_pydantic import Software
status = 1
def validate_cff(citation_cff_yaml_str):
ret = False
try:
# Initialize the Citation object with raw text instead of a file path
citation = Citation(cffstr=citation_cff_yaml_str)
# Run the built-in validation check
citation.validate()
ret = True
except Exception as e:
print(f"ERROR: cffconvert error occurred during parsing: {e}", file=sys.stderr)
return ret
def convert(codemeta_path: str):
# codemeta_path = 'tests/data/in/codemeta-codemeta-3.0.json'
# codemeta_path = 'tests/data/in/codemeta-isicily.json'
ret = 1
codemeta_data = None
# Load and parse the JSON file
try:
with open(codemeta_path, 'r') as f:
codemeta_data = json.load(f)
except Exception as e:
print(f'ERROR: {e}', file=sys.stderr)
if codemeta_data:
codemeta_software = get_pydantic_model_from_dict(codemeta_data, Software)
if codemeta_software:
citation_cff = get_citation_file_format_from_codemeta_software(codemeta_software)
if citation_cff:
citation_cff_yaml_str = to_yaml_str(citation_cff, by_alias=True, exclude_none=True)
print(citation_cff_yaml_str)
if validate_cff(citation_cff_yaml_str):
ret = 0
return ret
import argparse
parser = argparse.ArgumentParser(description='Codemeta converter')
parser.add_argument('--file', '-f',
default='codemeta.json',
help='Path to the input file (default: input.txt)')
args = parser.parse_args()
if args.file:
status = convert(args.file)
sys.exit(status)