11import rdflib
22import json
3+ from pathlib import Path
4+ import argparse
35
4- import os
56
6-
7-
8- def generate_schema_graph (turtle_file : str ) -> rdflib .Graph :
7+ def generate_schema_graph (turtle_file : Path ) -> rdflib .Graph :
98 """
109 Generate rdflib.Graph from a Turtle file.
1110
@@ -15,20 +14,17 @@ def generate_schema_graph(turtle_file: str) -> rdflib.Graph:
1514 Returns:
1615 The rdflib.Graph object containing the data.
1716 """
18- DATA_G = rdflib .Graph ()
19- DATA_G .parse (turtle_file , format = "turtle" )
20-
21- return DATA_G
22-
17+ data_g = rdflib .Graph ()
18+ data_g .parse (turtle_file , format = "turtle" )
19+ return data_g
2320
2421
25-
26- def extract_labels_and_placeholders (DATA_G : rdflib .Graph ) -> dict :
22+ def extract_labels_and_placeholders (data_g : rdflib .Graph ) -> dict :
2723 """
2824 Extract labels and placeholders from rdflib.Graph.
2925
3026 Args:
31- DATA_G : The rdflib.Graph object containing the data.
27+ data_g : The rdflib.Graph object containing the data.
3228
3329 Returns:
3430 The extracted labels and placeholders as a dictionary.
@@ -46,7 +42,7 @@ def extract_labels_and_placeholders(DATA_G: rdflib.Graph) -> dict:
4642 }
4743 }
4844 """
49- result = DATA_G .query (query )
45+ result = data_g .query (query )
5046 schema_json = {
5147 "@type_label" : "Type" ,
5248 "add_modal_button" : "Add {{type}}" ,
@@ -65,9 +61,29 @@ def extract_labels_and_placeholders(DATA_G: rdflib.Graph) -> dict:
6561 schema_json [f"{ slug } _placeholder" ] = str (row .comment )
6662 return schema_json
6763
68- DATA_G = generate_schema_graph (str (os .getcwd ()) + "/build/ontology_combined.ttl" )
6964
70- schema_json = extract_labels_and_placeholders (DATA_G )
71- os .makedirs (str (os .getcwd ()) + "/build/locales/en" , exist_ok = True )
72- with open (str (os .getcwd ()) + "/build/locales/en/schema.json" , "w" ) as f :
73- json .dump (schema_json , f , indent = 4 )
65+ if __name__ == "__main__" :
66+ parser = argparse .ArgumentParser (description = "Extract labels and placeholders from ontology TTL file." )
67+ parser .add_argument (
68+ "--input" ,
69+ required = True ,
70+ type = Path ,
71+ help = "Path to the input Turtle (.ttl) file."
72+ )
73+ parser .add_argument (
74+ "--output" ,
75+ required = True ,
76+ type = Path ,
77+ help = "Path to the output JSON file."
78+ )
79+ args = parser .parse_args ()
80+
81+ print (f"Generating schema from { args .input } ..." )
82+ data_g = generate_schema_graph (args .input )
83+ schema_json = extract_labels_and_placeholders (data_g )
84+
85+ args .output .parent .mkdir (parents = True , exist_ok = True )
86+ with args .output .open ("w" ) as f :
87+ json .dump (schema_json , f , indent = 4 )
88+
89+ print (f"Schema JSON written to { args .output } " )
0 commit comments