2024-07-10 23:20:06 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
2024-09-05 16:45:22 +01:00
|
|
|
"""
|
2024-11-08 18:14:14 +00:00
|
|
|
Connects to the graph query service and dumps all graph edges in Turtle
|
|
|
|
|
format.
|
2024-09-05 16:45:22 +01:00
|
|
|
"""
|
|
|
|
|
|
2024-07-10 23:20:06 +01:00
|
|
|
import rdflib
|
|
|
|
|
import io
|
2024-09-05 16:45:22 +01:00
|
|
|
import sys
|
2025-01-02 19:49:22 +00:00
|
|
|
import argparse
|
|
|
|
|
import os
|
2024-09-05 16:45:22 +01:00
|
|
|
|
2025-01-02 19:49:22 +00:00
|
|
|
from trustgraph.api import Api, Uri
|
2024-09-05 16:45:22 +01:00
|
|
|
|
2025-01-02 19:49:22 +00:00
|
|
|
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
|
|
|
|
default_user = 'trustgraph'
|
|
|
|
|
default_collection = 'default'
|
2024-09-05 16:45:22 +01:00
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
def show_graph(url, flow_id, user, collection):
|
2024-09-05 16:45:22 +01:00
|
|
|
|
2025-05-05 11:09:18 +01:00
|
|
|
api = Api(url).flow().id(flow_id)
|
2025-01-02 19:49:22 +00:00
|
|
|
|
2025-05-05 11:09:18 +01:00
|
|
|
rows = api.triples_query(
|
2025-01-02 19:49:22 +00:00
|
|
|
s=None, p=None, o=None,
|
2025-05-03 10:39:53 +01:00
|
|
|
user=user, collection=collection,
|
2025-01-02 19:49:22 +00:00
|
|
|
limit=10_000)
|
2024-09-05 16:45:22 +01:00
|
|
|
|
|
|
|
|
g = rdflib.Graph()
|
|
|
|
|
|
|
|
|
|
for row in rows:
|
|
|
|
|
|
2025-01-02 19:49:22 +00:00
|
|
|
sv = rdflib.term.URIRef(row.s)
|
|
|
|
|
pv = rdflib.term.URIRef(row.p)
|
2024-09-05 16:45:22 +01:00
|
|
|
|
2025-01-02 19:49:22 +00:00
|
|
|
if isinstance(row.o, Uri):
|
2024-09-05 16:45:22 +01:00
|
|
|
|
|
|
|
|
# Skip malformed URLs with spaces in
|
2025-01-02 19:49:22 +00:00
|
|
|
if " " in row.o:
|
2024-09-05 16:45:22 +01:00
|
|
|
continue
|
|
|
|
|
|
2025-01-02 19:49:22 +00:00
|
|
|
ov = rdflib.term.URIRef(row.o)
|
|
|
|
|
|
2024-09-05 16:45:22 +01:00
|
|
|
else:
|
2025-01-02 19:49:22 +00:00
|
|
|
|
|
|
|
|
ov = rdflib.term.Literal(row.o)
|
2024-09-05 16:45:22 +01:00
|
|
|
|
|
|
|
|
g.add((sv, pv, ov))
|
|
|
|
|
|
|
|
|
|
g.serialize(destination="output.ttl", format="turtle")
|
|
|
|
|
|
|
|
|
|
buf = io.BytesIO()
|
|
|
|
|
|
|
|
|
|
g.serialize(destination=buf, format="turtle")
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-09-05 16:45:22 +01:00
|
|
|
sys.stdout.write(buf.getvalue().decode("utf-8"))
|
2024-07-10 23:20:06 +01:00
|
|
|
|
|
|
|
|
|
2024-09-05 16:45:22 +01:00
|
|
|
def main():
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-09-05 16:45:22 +01:00
|
|
|
parser = argparse.ArgumentParser(
|
2024-11-08 18:14:14 +00:00
|
|
|
prog='tg-graph-to-turtle',
|
2024-09-05 16:45:22 +01:00
|
|
|
description=__doc__,
|
|
|
|
|
)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-09-05 16:45:22 +01:00
|
|
|
parser.add_argument(
|
2025-01-02 19:49:22 +00:00
|
|
|
'-u', '--api-url',
|
|
|
|
|
default=default_url,
|
|
|
|
|
help=f'API URL (default: {default_url})',
|
|
|
|
|
)
|
|
|
|
|
|
2025-05-03 10:39:53 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
'-f', '--flow-id',
|
|
|
|
|
default="0000",
|
|
|
|
|
help=f'Flow ID (default: 0000)'
|
|
|
|
|
)
|
|
|
|
|
|
2025-01-02 19:49:22 +00:00
|
|
|
parser.add_argument(
|
|
|
|
|
'-U', '--user',
|
|
|
|
|
default=default_user,
|
|
|
|
|
help=f'User ID (default: {default_user})'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'-C', '--collection',
|
|
|
|
|
default=default_collection,
|
|
|
|
|
help=f'Collection ID (default: {default_collection})'
|
2024-09-05 16:45:22 +01:00
|
|
|
)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-09-05 16:45:22 +01:00
|
|
|
args = parser.parse_args()
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-09-05 16:45:22 +01:00
|
|
|
try:
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2025-01-02 19:49:22 +00:00
|
|
|
show_graph(
|
2025-05-03 10:39:53 +01:00
|
|
|
url = args.api_url,
|
|
|
|
|
flow_id = args.flow_id,
|
|
|
|
|
user = args.user,
|
|
|
|
|
collection = args.collection,
|
2025-01-02 19:49:22 +00:00
|
|
|
)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-09-05 16:45:22 +01:00
|
|
|
except Exception as e:
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-09-05 16:45:22 +01:00
|
|
|
print("Exception:", e, flush=True)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-09-05 16:45:22 +01:00
|
|
|
main()
|
2024-07-10 23:20:06 +01:00
|
|
|
|