comparison env/lib/python3.9/site-packages/rdflib/tools/rdf2dot.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """
2 A commandline tool for drawing RDF graphs in Graphviz DOT format
3
4 You can draw the graph of an RDF file directly:
5
6 .. code-block: bash
7
8 rdf2dot my_rdf_file.rdf | dot -Tpng | display
9
10 """
11
12 from __future__ import absolute_import
13
14 import rdflib
15 import rdflib.extras.cmdlineutils
16
17 import sys
18 import cgi
19 import collections
20
21 from rdflib import XSD
22
23 LABEL_PROPERTIES = [rdflib.RDFS.label,
24 rdflib.URIRef("http://purl.org/dc/elements/1.1/title"),
25 rdflib.URIRef("http://xmlns.com/foaf/0.1/name"),
26 rdflib.URIRef("http://www.w3.org/2006/vcard/ns#fn"),
27 rdflib.URIRef("http://www.w3.org/2006/vcard/ns#org")
28 ]
29
30 XSDTERMS = [
31 XSD[x] for x in (
32 "anyURI", "base64Binary", "boolean", "byte", "date",
33 "dateTime", "decimal", "double", "duration", "float", "gDay", "gMonth",
34 "gMonthDay", "gYear", "gYearMonth", "hexBinary", "ID", "IDREF",
35 "IDREFS", "int", "integer", "language", "long", "Name", "NCName",
36 "negativeInteger", "NMTOKEN", "NMTOKENS", "nonNegativeInteger",
37 "nonPositiveInteger", "normalizedString", "positiveInteger", "QName",
38 "short", "string", "time", "token", "unsignedByte", "unsignedInt",
39 "unsignedLong", "unsignedShort")]
40
41 EDGECOLOR = "blue"
42 NODECOLOR = "black"
43 ISACOLOR = "black"
44
45
46 def rdf2dot(g, stream, opts={}):
47 """
48 Convert the RDF graph to DOT
49 writes the dot output to the stream
50 """
51
52 fields = collections.defaultdict(set)
53 nodes = {}
54
55 def node(x):
56
57 if x not in nodes:
58 nodes[x] = "node%d" % len(nodes)
59 return nodes[x]
60
61 def label(x, g):
62
63 for labelProp in LABEL_PROPERTIES:
64 l = g.value(x, labelProp)
65 if l:
66 return l
67
68 try:
69 return g.namespace_manager.compute_qname(x)[2]
70 except:
71 return x
72
73 def formatliteral(l, g):
74 v = cgi.escape(l)
75 if l.datatype:
76 return u'"%s"^^%s' % (v, qname(l.datatype, g))
77 elif l.language:
78 return u'"%s"@%s' % (v, l.language)
79 return u'"%s"' % v
80
81 def qname(x, g):
82 try:
83 q = g.compute_qname(x)
84 return q[0] + ":" + q[2]
85 except:
86 return x
87
88 def color(p):
89 return "BLACK"
90
91 stream.write(u"digraph { \n node [ fontname=\"DejaVu Sans\" ] ; \n")
92
93 for s, p, o in g:
94 sn = node(s)
95 if p == rdflib.RDFS.label:
96 continue
97 if isinstance(o, (rdflib.URIRef, rdflib.BNode)):
98 on = node(o)
99 opstr = u"\t%s -> %s [ color=%s, label=< <font point-size='10' " + \
100 u"color='#336633'>%s</font> > ] ;\n"
101 stream.write(opstr % (sn, on, color(p), qname(p, g)))
102 else:
103 fields[sn].add((qname(p, g), formatliteral(o, g)))
104
105 for u, n in nodes.items():
106 stream.write(u"# %s %s\n" % (u, n))
107 f = [u"<tr><td align='left'>%s</td><td align='left'>%s</td></tr>" %
108 x for x in sorted(fields[n])]
109 opstr = u"%s [ shape=none, color=%s label=< <table color='#666666'" + \
110 u" cellborder='0' cellspacing='0' border='1'><tr>" + \
111 u"<td colspan='2' bgcolor='grey'><B>%s</B></td></tr><tr>" + \
112 u"<td href='%s' bgcolor='#eeeeee' colspan='2'>" + \
113 u"<font point-size='10' color='#6666ff'>%s</font></td>" + \
114 u"</tr>%s</table> > ] \n"
115 stream.write(opstr % (n, NODECOLOR, label(u, g), u, u, u"".join(f)))
116
117 stream.write("}\n")
118
119
120 def _help():
121 sys.stderr.write("""
122 rdf2dot.py [-f <format>] files...
123 Read RDF files given on STDOUT, writes a graph of the RDFS schema in DOT
124 language to stdout
125 -f specifies parser to use, if not given,
126
127 """)
128
129
130 def main():
131 rdflib.extras.cmdlineutils.main(rdf2dot, _help)
132
133
134 if __name__ == '__main__':
135 main()