-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathknow-sparql-render
executable file
·76 lines (62 loc) · 2.51 KB
/
know-sparql-render
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
73
74
75
#!/usr/bin/python3
import argparse
from sys import stdin, stdout, stderr
import os.path
from subprocess import Popen, PIPE
from rdflib import Graph, URIRef, Literal
from lxml import etree
from RDFFresnel import Context, ContainerBox
from PKB4Unix import endpoint
argparser = argparse.ArgumentParser(
description=('Render Resources with Fresnel'))
argparser.add_argument('resources', nargs='+', metavar='URI',
help=('Resource to be rendered. Use - to read the URIs from standard input'))
argparser.add_argument('-i', '--instance-graph', metavar='GRAPH', dest='instance_graph',
help=('Restrict instance data to this graph'))
argparser.add_argument('-l', '--lens-graph', metavar='GRAPH', dest='lens_graph',
help=('Graph containing Fresnel Lenses (if not given, all available graphs are searched for lenses)'))
argparser.add_argument('-t', '--transform', metavar='FILE', dest='transform',
help=('The location of a XSL transformation to be applied (using xsltproc) after rendering'))
argparser.add_argument('-T', '--default-transform', dest='default_transform', action='store_true',
help=('Like -t with fresneltoxhtml5.xsl from ~/.local/share/RDFFresnel, /usr/local/share/RDFFresnel or /usr/share/RDFFresnel, depending on which one exists'))
args = argparser.parse_args()
kb = endpoint.kbConnect()
if args.instance_graph:
instances = kb.get_context(URIRef(args.instance_graph))
else:
instances = kb
if args.lens_graph:
lenses = kb.get_context(URIRef(args.lens_graph))
else:
lenses = kb
if args.default_transform:
for p in (
"~/.local/share/RDFFresnel/fresneltoxhtml5.xsl",
"/usr/local/share/RDFFresnel/fresneltoxhtml5.xsl",
"/usr/share/RDFFresnel/fresneltoxhtml5.xsl",
):
os.path.expanduser(p)
if os.path.exists(p):
args.transform = p
break
print("Couldn't find default transformation", file=stderr)
exit(1)
ctx = Context(fresnelGraph=lenses, instanceGraph=instances)
box = ContainerBox(ctx)
for r in args.resources:
if r == '-':
for line in iter(stdin.readline, ''):
r = line.strip()
if line:
box.append(URIRef(r))
else:
box.append(URIRef(r))
box.select()
box.portray()
tree = box.transform()
if args.transform:
pipe = Popen(['xsltproc', args.transform, '-'], stdin=PIPE)
dest = pipe.stdin
else:
dest = stdout.buffer
dest.write(etree.tostring(tree,encoding="UTF-8",xml_declaration=True))