diff options
Diffstat (limited to 'rdflib/tools/rdf2dot.py')
-rw-r--r-- | rdflib/tools/rdf2dot.py | 109 |
1 files changed, 77 insertions, 32 deletions
diff --git a/rdflib/tools/rdf2dot.py b/rdflib/tools/rdf2dot.py index 84b251a1..9b622b66 100644 --- a/rdflib/tools/rdf2dot.py +++ b/rdflib/tools/rdf2dot.py @@ -20,23 +20,60 @@ import collections from rdflib import XSD -LABEL_PROPERTIES = [rdflib.RDFS.label, - rdflib.URIRef("http://purl.org/dc/elements/1.1/title"), - rdflib.URIRef("http://xmlns.com/foaf/0.1/name"), - rdflib.URIRef("http://www.w3.org/2006/vcard/ns#fn"), - rdflib.URIRef("http://www.w3.org/2006/vcard/ns#org") - ] +LABEL_PROPERTIES = [ + rdflib.RDFS.label, + rdflib.URIRef("http://purl.org/dc/elements/1.1/title"), + rdflib.URIRef("http://xmlns.com/foaf/0.1/name"), + rdflib.URIRef("http://www.w3.org/2006/vcard/ns#fn"), + rdflib.URIRef("http://www.w3.org/2006/vcard/ns#org"), +] XSDTERMS = [ - XSD[x] for x in ( - "anyURI", "base64Binary", "boolean", "byte", "date", - "dateTime", "decimal", "double", "duration", "float", "gDay", "gMonth", - "gMonthDay", "gYear", "gYearMonth", "hexBinary", "ID", "IDREF", - "IDREFS", "int", "integer", "language", "long", "Name", "NCName", - "negativeInteger", "NMTOKEN", "NMTOKENS", "nonNegativeInteger", - "nonPositiveInteger", "normalizedString", "positiveInteger", "QName", - "short", "string", "time", "token", "unsignedByte", "unsignedInt", - "unsignedLong", "unsignedShort")] + XSD[x] + for x in ( + "anyURI", + "base64Binary", + "boolean", + "byte", + "date", + "dateTime", + "decimal", + "double", + "duration", + "float", + "gDay", + "gMonth", + "gMonthDay", + "gYear", + "gYearMonth", + "hexBinary", + "ID", + "IDREF", + "IDREFS", + "int", + "integer", + "language", + "long", + "Name", + "NCName", + "negativeInteger", + "NMTOKEN", + "NMTOKENS", + "nonNegativeInteger", + "nonPositiveInteger", + "normalizedString", + "positiveInteger", + "QName", + "short", + "string", + "time", + "token", + "unsignedByte", + "unsignedInt", + "unsignedLong", + "unsignedShort", + ) +] EDGECOLOR = "blue" NODECOLOR = "black" @@ -73,10 +110,10 @@ def rdf2dot(g, stream, opts={}): def formatliteral(l, g): v = cgi.escape(l) if l.datatype: - return u'"%s"^^%s' % (v, qname(l.datatype, g)) + return u""%s"^^%s" % (v, qname(l.datatype, g)) elif l.language: - return u'"%s"@%s' % (v, l.language) - return u'"%s"' % v + return u""%s"@%s" % (v, l.language) + return u""%s"" % v def qname(x, g): try: @@ -88,7 +125,7 @@ def rdf2dot(g, stream, opts={}): def color(p): return "BLACK" - stream.write(u"digraph { \n node [ fontname=\"DejaVu Sans\" ] ; \n") + stream.write(u'digraph { \n node [ fontname="DejaVu Sans" ] ; \n') for s, p, o in g: sn = node(s) @@ -96,40 +133,48 @@ def rdf2dot(g, stream, opts={}): continue if isinstance(o, (rdflib.URIRef, rdflib.BNode)): on = node(o) - opstr = u"\t%s -> %s [ color=%s, label=< <font point-size='10' " + \ - u"color='#336633'>%s</font> > ] ;\n" + opstr = ( + u"\t%s -> %s [ color=%s, label=< <font point-size='10' " + + u"color='#336633'>%s</font> > ] ;\n" + ) stream.write(opstr % (sn, on, color(p), qname(p, g))) else: fields[sn].add((qname(p, g), formatliteral(o, g))) for u, n in nodes.items(): stream.write(u"# %s %s\n" % (u, n)) - f = [u"<tr><td align='left'>%s</td><td align='left'>%s</td></tr>" % - x for x in sorted(fields[n])] - opstr = u"%s [ shape=none, color=%s label=< <table color='#666666'" + \ - u" cellborder='0' cellspacing='0' border='1'><tr>" + \ - u"<td colspan='2' bgcolor='grey'><B>%s</B></td></tr><tr>" + \ - u"<td href='%s' bgcolor='#eeeeee' colspan='2'>" + \ - u"<font point-size='10' color='#6666ff'>%s</font></td>" + \ - u"</tr>%s</table> > ] \n" + f = [ + u"<tr><td align='left'>%s</td><td align='left'>%s</td></tr>" % x + for x in sorted(fields[n]) + ] + opstr = ( + u"%s [ shape=none, color=%s label=< <table color='#666666'" + + u" cellborder='0' cellspacing='0' border='1'><tr>" + + u"<td colspan='2' bgcolor='grey'><B>%s</B></td></tr><tr>" + + u"<td href='%s' bgcolor='#eeeeee' colspan='2'>" + + u"<font point-size='10' color='#6666ff'>%s</font></td>" + + u"</tr>%s</table> > ] \n" + ) stream.write(opstr % (n, NODECOLOR, label(u, g), u, u, u"".join(f))) stream.write("}\n") def _help(): - sys.stderr.write(""" + sys.stderr.write( + """ rdf2dot.py [-f <format>] files... Read RDF files given on STDOUT, writes a graph of the RDFS schema in DOT language to stdout -f specifies parser to use, if not given, -""") +""" + ) def main(): rdflib.extras.cmdlineutils.main(rdf2dot, _help) -if __name__ == '__main__': +if __name__ == "__main__": main() |