Skip to content
Snippets Groups Projects
Commit 4d7736c7 authored by Anjali Aggarwal's avatar Anjali Aggarwal
Browse files

schema to all format converter

parent b3210da5
No related branches found
No related tags found
1 merge request!16Resolve "XAS catalysis"
Pipeline #127454 failed
import argparse
import os
from linkml.generators.jsonschemagen import JsonSchemaGenerator
from linkml.generators.jsonldgen import JSONLDGenerator
from linkml.generators.owlgen import OwlSchemaGenerator
def convert_linkml_to_jsonschema(yaml_schema_path, json_schema_output):
# Load the schema using SchemaView
json_schema = JsonSchemaGenerator(yaml_schema_path).serialize()
json_ld = JSONLDGenerator(yaml_schema_path).serialize()
owl_shema = OwlSchemaGenerator(yaml_schema_path).serialize()
# Save JSON schema to file
file_name=json_schema_output.split(".")[0]
with open(f'{file_name}.json', 'w') as file:
file.write(json_schema)
with open(f'{file_name}.jsonld', 'w') as file:
file.write(json_ld)
with open(f'{file_name}.owl', 'w') as file:
file.write(owl_shema)
def main():
parser = argparse.ArgumentParser(description="Convert a LinkML YAML schema to JSON schema.")
parser.add_argument('-i', '--input', type=str, default='scicat/scicat.yaml', help='The path to the LinkML YAML schema file to be read (default: schemas/schema.yaml).')
parser.add_argument('-o', '--output', type=str, default='scicat/output/scicat.json', help='The path to save the JSON schema output (default: output/schema.json).')
args = parser.parse_args()
# Ensure output directory exists
os.makedirs(os.path.dirname(args.output), exist_ok=True)
# Convert LinkML YAML schema to JSON schema
convert_linkml_to_jsonschema(args.input, args.output)
print(f"LinkML YAML schema successfully converted and saved to {args.output}")
if __name__ == "__main__":
main()
# import yaml
# import argparse
# import json
# from linkml_runtime.utils.schemaview import SchemaView
# from linkml_runtime.linkml_model.meta import SchemaDefinition
# # from linkml.generators.jsonschemagen import
# from linkml.generators.jsonschemagen import JsonSchemaGenerator
# def convert_linkml_to_jsonschema(yaml_schema_path, json_schema_output):
# # Load the schema using SchemaView
# schema_view = SchemaView(yaml_schema_path)
# json_schema = JsonSchemaGenerator(schema_view.schema).serialize()
# # Save JSON schema to file
# with open(json_schema_output, 'w') as file:
# file.write(json_schema)
# def main():
# # Initialize the parser
# parser = argparse.ArgumentParser(description="Gen json schema")
# # Add the file argument
# parser.add_argument('-i', '--input', type=str, default='schema.yaml', help='The path to the LinkML YAML schema file to be read (default: schema.yaml).')
# parser.add_argument('-o', '--output', type=str, default='schema.json', help='The path to save the JSON schema output (default: schema.json).')
# # Parse the arguments
# args = parser.parse_args()
# # Read and print the file content
# schema_view = convert_linkml_to_jsonschema(args.input,args.output)
# # json_schema = JsonSchemaGenerator(schema_view.schema).serialize()
# # with open("file.json", "w") as outfile:
# # json.dump(json_schema, outfile)
# if __name__ == "__main__":
# main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment