Debian Codegraph @

src/rdflib_endpoint/__main__.py

raw
1 import glob
2 import sys
3 from typing import List
4
5 import click
6 import uvicorn
7 from rdflib import Dataset
8
9 from rdflib_endpoint import SparqlEndpoint
10
11
12 @click.group()
13 def cli() -> None:
14 """Quickly serve RDF files as SPARQL endpoint with RDFLib Endpoint"""
15
16
17 @cli.command(help="Serve a local RDF file as a SPARQL endpoint by default on http://0.0.0.0:8000/sparql")
18 @click.argument("files", nargs=-1)
19 @click.option("--host", default="localhost", help="Host of the SPARQL endpoint")
20 @click.option("--port", default=8000, help="Port of the SPARQL endpoint")
21 @click.option("--store", default="default", help="Store used by RDFLib: default or Oxigraph")
22 @click.option("--enable-update", is_flag=True, help="Enable SPARQL updates")
23 def serve(files: List[str], host: str, port: int, store: str, enable_update: bool) -> None:
24 run_serve(files, host, port, store, enable_update)
25
26
27 def run_serve(files: List[str], host: str, port: int, store: str = "default", enable_update: bool = False) -> None:
28 if store == "oxigraph":
29 store = store.capitalize()
30 g = Dataset(store=store, default_union=True)
31 for glob_file in files:
32 file_list = glob.glob(glob_file)
33 for file in file_list:
34 g.parse(file)
35 click.echo(
36 click.style("INFO", fg="green")
37 + ": 📥️ Loaded triples from "
38 + click.style(str(file), bold=True)
39 + ", for a total of "
40 + click.style(str(len(g)), bold=True)
41 )
42
43 app = SparqlEndpoint(
44 graph=g,
45 enable_update=enable_update,
46 example_query="""PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
47 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
48 SELECT * WHERE {
49 ?s ?p ?o .
50 } LIMIT 100""",
51 )
52 uvicorn.run(app, host=host, port=port)
53
54
55 @cli.command(help="Merge and convert local RDF files to another format easily")
56 @click.argument("files", nargs=-1)
57 @click.option("--output", default="localhost", help="Host of the SPARQL endpoint")
58 @click.option("--store", default="default", help="Store used by RDFLib: default or Oxigraph")
59 def convert(files: List[str], output: str, store: str) -> None:
60 run_convert(files, output, store)
61
62
63 def run_convert(files: List[str], output: str, store: str = "default") -> None:
64 if store == "oxigraph":
65 store = store.capitalize()
66 g = Dataset(store=store, default_union=True)
67 for glob_file in files:
68 file_list = glob.glob(glob_file)
69 for file in file_list:
70 g.parse(file)
71 click.echo(
72 click.style("INFO", fg="green")
73 + ": 📥️ Loaded triples from "
74 + click.style(str(file), bold=True)
75 + ", for a total of "
76 + click.style(str(len(g)), bold=True)
77 )
78
79 out_format = "ttl"
80 if output.endswith(".nt"):
81 out_format = "nt"
82 elif output.endswith(".xml") or output.endswith(".rdf"):
83 out_format = "xml"
84 elif output.endswith(".json") or output.endswith(".jsonld"):
85 out_format = "json-ld"
86 elif output.endswith(".trig"):
87 out_format = "trig"
88
89 g.serialize(output, format=out_format)
90
91
92 if __name__ == "__main__":
93 sys.exit(cli())