summaryrefslogtreecommitdiff
path: root/rdflib/tools
diff options
context:
space:
mode:
authorNicholas Car <nicholas.car@surroundaustralia.com>2020-05-16 21:00:24 +1000
committerNicholas Car <nicholas.car@surroundaustralia.com>2020-05-16 21:00:24 +1000
commit0be6f6039479ce29cf71b11e76be54e186130036 (patch)
tree897d208525a7e8952fb496151db074d49dcdeb3a /rdflib/tools
parent2a8d70824e1b4caf0c606074a44ac3a15fa72718 (diff)
downloadrdflib-0be6f6039479ce29cf71b11e76be54e186130036.tar.gz
blacked all python files
Diffstat (limited to 'rdflib/tools')
-rw-r--r--rdflib/tools/csv2rdf.py110
-rw-r--r--rdflib/tools/graphisomorphism.py44
-rw-r--r--rdflib/tools/rdf2dot.py109
-rw-r--r--rdflib/tools/rdfpipe.py127
-rw-r--r--rdflib/tools/rdfs2dot.py88
5 files changed, 307 insertions, 171 deletions
diff --git a/rdflib/tools/csv2rdf.py b/rdflib/tools/csv2rdf.py
index ef246a73..cec1005c 100644
--- a/rdflib/tools/csv2rdf.py
+++ b/rdflib/tools/csv2rdf.py
@@ -24,7 +24,7 @@ import rdflib
from rdflib import RDF, RDFS
from rdflib.namespace import split_uri
-__all__ = ['CSV2RDF']
+__all__ = ["CSV2RDF"]
HELP = """
csv2rdf.py \
@@ -124,23 +124,21 @@ def index(l, i):
def csv_reader(csv_data, dialect=csv.excel, **kwargs):
- csv_reader = csv.reader(csv_data,
- dialect=dialect, **kwargs)
+ csv_reader = csv.reader(csv_data, dialect=dialect, **kwargs)
for row in csv_reader:
# decode UTF-8 back to Unicode, cell by cell:
- yield [str(cell, 'utf-8', errors='replace') for cell in row]
+ yield [str(cell, "utf-8", errors="replace") for cell in row]
def prefixuri(x, prefix, class_=None):
if prefix:
- r = rdflib.URIRef(
- prefix + quote(
- x.encode("utf8").replace(" ", "_"), safe=""))
+ r = rdflib.URIRef(prefix + quote(x.encode("utf8").replace(" ", "_"), safe=""))
else:
r = rdflib.URIRef(x)
uris[x] = (r, class_)
return r
+
# meta-language for config
@@ -235,8 +233,7 @@ class NodeSplit(NodeMaker):
self.f = rdflib.Literal
if not callable(self.f):
raise Exception("Function passed to split is not callable!")
- return [
- self.f(y.strip()) for y in x.split(self.sep) if y.strip() != ""]
+ return [self.f(y.strip()) for y in x.split(self.sep) if y.strip() != ""]
def range(self):
if self.f and isinstance(self.f, NodeMaker):
@@ -283,16 +280,17 @@ def _config_split(sep=None, f=None):
return NodeSplit(sep, f)
-config_functions = {"ignore": _config_ignore,
- "uri": _config_uri,
- "literal": _config_literal,
- "float": _config_float,
- "int": _config_int,
- "date": _config_date,
- "split": _config_split,
- "replace": _config_replace,
- "bool": _config_bool,
- }
+config_functions = {
+ "ignore": _config_ignore,
+ "uri": _config_uri,
+ "literal": _config_literal,
+ "float": _config_float,
+ "int": _config_int,
+ "date": _config_date,
+ "split": _config_split,
+ "replace": _config_replace,
+ "bool": _config_bool,
+}
def column(v):
@@ -307,7 +305,7 @@ class CSV2RDF(object):
self.CLASS = None
self.BASE = None
self.PROPBASE = None
- self.IDENT = 'auto'
+ self.IDENT = "auto"
self.LABEL = None
self.DEFINECLASS = False
self.SKIP = 0
@@ -317,7 +315,7 @@ class CSV2RDF(object):
self.COLUMNS = {}
self.PROPS = {}
- self.OUT = codecs.getwriter("utf-8")(sys.stdout, errors='replace')
+ self.OUT = codecs.getwriter("utf-8")(sys.stdout, errors="replace")
self.triples = 0
@@ -340,8 +338,7 @@ class CSV2RDF(object):
self.BASE = rdflib.Namespace("http://example.org/instances/")
if not self.PROPBASE:
- warnings.warn(
- "No property base given, using http://example.org/property/")
+ warnings.warn("No property base given, using http://example.org/property/")
self.PROPBASE = rdflib.Namespace("http://example.org/props/")
# skip lines at the start
@@ -350,8 +347,7 @@ class CSV2RDF(object):
# read header line
header_labels = list(csvreader.next())
- headers = dict(
- enumerate([self.PROPBASE[toProperty(x)] for x in header_labels]))
+ headers = dict(enumerate([self.PROPBASE[toProperty(x)] for x in header_labels]))
# override header properties if some are given
for k, v in self.PROPS.items():
headers[k] = v
@@ -364,27 +360,34 @@ class CSV2RDF(object):
h, l = headers[i], header_labels[i]
if h == "" or l == "":
continue
- if self.COLUMNS.get(i, self.DEFAULT) == 'ignore':
+ if self.COLUMNS.get(i, self.DEFAULT) == "ignore":
continue
self.triple(h, RDF.type, RDF.Property)
self.triple(h, RDFS.label, rdflib.Literal(toPropertyLabel(l)))
self.triple(h, RDFS.domain, self.CLASS)
- self.triple(h, RDFS.range,
- self.COLUMNS.get(i, default_node_make).range())
+ self.triple(
+ h, RDFS.range, self.COLUMNS.get(i, default_node_make).range()
+ )
rows = 0
for l in csvreader:
try:
- if self.IDENT == 'auto':
+ if self.IDENT == "auto":
uri = self.BASE["%d" % rows]
else:
- uri = self.BASE["_".join([quote(x.encode(
- "utf8").replace(" ", "_"), safe="")
- for x in index(l, self.IDENT)])]
+ uri = self.BASE[
+ "_".join(
+ [
+ quote(x.encode("utf8").replace(" ", "_"), safe="")
+ for x in index(l, self.IDENT)
+ ]
+ )
+ ]
if self.LABEL:
- self.triple(uri, RDFS.label, rdflib.Literal(
- " ".join(index(l, self.LABEL))))
+ self.triple(
+ uri, RDFS.label, rdflib.Literal(" ".join(index(l, self.LABEL)))
+ )
if self.CLASS:
# type triple
@@ -392,8 +395,8 @@ class CSV2RDF(object):
for i, x in enumerate(l):
x = x.strip()
- if x != '':
- if self.COLUMNS.get(i, self.DEFAULT) == 'ignore':
+ if x != "":
+ if self.COLUMNS.get(i, self.DEFAULT) == "ignore":
continue
try:
o = self.COLUMNS.get(i, rdflib.Literal)(x)
@@ -405,15 +408,17 @@ class CSV2RDF(object):
except Exception as e:
warnings.warn(
- "Could not process value for column " +
- "%d:%s in row %d, ignoring: %s " % (
- i, headers[i], rows, e.message))
+ "Could not process value for column "
+ + "%d:%s in row %d, ignoring: %s "
+ % (i, headers[i], rows, e.message)
+ )
rows += 1
if rows % 100000 == 0:
sys.stderr.write(
- "%d rows, %d triples, elapsed %.2fs.\n" % (
- rows, self.triples, time.time() - start))
+ "%d rows, %d triples, elapsed %.2fs.\n"
+ % (rows, self.triples, time.time() - start)
+ )
except:
sys.stderr.write("Error processing line: %d\n" % rows)
raise
@@ -432,8 +437,7 @@ class CSV2RDF(object):
self.triple(c, RDF.type, RDFS.Class)
self.OUT.close()
- sys.stderr.write(
- "Converted %d rows into %d triples.\n" % (rows, self.triples))
+ sys.stderr.write("Converted %d rows into %d triples.\n" % (rows, self.triples))
sys.stderr.write("Took %.2f seconds.\n" % (time.time() - start))
@@ -443,8 +447,19 @@ def main():
opts, files = getopt.getopt(
sys.argv[1:],
"hc:b:p:i:o:Cf:l:s:d:D:",
- ["out=", "base=", "delim=", "propbase=", "class=", "default="
- "ident=", "label=", "skip=", "defineclass", "help"])
+ [
+ "out=",
+ "base=",
+ "delim=",
+ "propbase=",
+ "class=",
+ "default=" "ident=",
+ "label=",
+ "skip=",
+ "defineclass",
+ "help",
+ ],
+ )
opts = dict(opts)
if "-h" in opts or "--help" in opts:
@@ -534,9 +549,8 @@ def main():
if csv2rdf.CLASS and ("-C" in opts or "--defineclass" in opts):
csv2rdf.DEFINECLASS = True
- csv2rdf.convert(
- csv_reader(fileinput.input(files), delimiter=csv2rdf.DELIM))
+ csv2rdf.convert(csv_reader(fileinput.input(files), delimiter=csv2rdf.DELIM))
-if __name__ == '__main__':
+if __name__ == "__main__":
main()
diff --git a/rdflib/tools/graphisomorphism.py b/rdflib/tools/graphisomorphism.py
index abc84fa1..a073d7d9 100644
--- a/rdflib/tools/graphisomorphism.py
+++ b/rdflib/tools/graphisomorphism.py
@@ -69,39 +69,47 @@ class IsomorphicTestableGraph(Graph):
def main():
import sys
from optparse import OptionParser
- usage = '''usage: %prog [options] file1 file2 ... fileN'''
+
+ usage = """usage: %prog [options] file1 file2 ... fileN"""
op = OptionParser(usage=usage)
- op.add_option('-s', '--stdin', action='store_true', default=False,
- help='Load from STDIN as well')
- op.add_option('--format',
- default='xml',
- dest='inputFormat',
- metavar='RDF_FORMAT',
- choices=['xml', 'trix', 'n3', 'nt', 'rdfa'],
- help="The format of the RDF document(s) to compare" +
- "One of 'xml','n3','trix', 'nt', " +
- "or 'rdfa'. The default is %default")
+ op.add_option(
+ "-s",
+ "--stdin",
+ action="store_true",
+ default=False,
+ help="Load from STDIN as well",
+ )
+ op.add_option(
+ "--format",
+ default="xml",
+ dest="inputFormat",
+ metavar="RDF_FORMAT",
+ choices=["xml", "trix", "n3", "nt", "rdfa"],
+ help="The format of the RDF document(s) to compare"
+ + "One of 'xml','n3','trix', 'nt', "
+ + "or 'rdfa'. The default is %default",
+ )
(options, args) = op.parse_args()
graphs = []
graph2FName = {}
if options.stdin:
- graph = IsomorphicTestableGraph().parse(
- sys.stdin, format=options.inputFormat)
+ graph = IsomorphicTestableGraph().parse(sys.stdin, format=options.inputFormat)
graphs.append(graph)
- graph2FName[graph] = '(STDIN)'
+ graph2FName[graph] = "(STDIN)"
for fn in args:
- graph = IsomorphicTestableGraph().parse(
- fn, format=options.inputFormat)
+ graph = IsomorphicTestableGraph().parse(fn, format=options.inputFormat)
graphs.append(graph)
graph2FName[graph] = fn
checked = set()
for graph1, graph2 in combinations(graphs, 2):
if (graph1, graph2) not in checked and (graph2, graph1) not in checked:
assert graph1 == graph2, "%s != %s" % (
- graph2FName[graph1], graph2FName[graph2])
+ graph2FName[graph1],
+ graph2FName[graph2],
+ )
-if __name__ == '__main__':
+if __name__ == "__main__":
main()
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'&quot;%s&quot;^^%s' % (v, qname(l.datatype, g))
+ return u"&quot;%s&quot;^^%s" % (v, qname(l.datatype, g))
elif l.language:
- return u'&quot;%s&quot;@%s' % (v, l.language)
- return u'&quot;%s&quot;' % v
+ return u"&quot;%s&quot;@%s" % (v, l.language)
+ return u"&quot;%s&quot;" % 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()
diff --git a/rdflib/tools/rdfpipe.py b/rdflib/tools/rdfpipe.py
index 88cd99e9..4be352a0 100644
--- a/rdflib/tools/rdfpipe.py
+++ b/rdflib/tools/rdfpipe.py
@@ -22,14 +22,20 @@ from rdflib.serializer import Serializer
from rdflib.util import guess_format
+DEFAULT_INPUT_FORMAT = "xml"
+DEFAULT_OUTPUT_FORMAT = "n3"
-DEFAULT_INPUT_FORMAT = 'xml'
-DEFAULT_OUTPUT_FORMAT = 'n3'
-
-def parse_and_serialize(input_files, input_format, guess,
- outfile, output_format, ns_bindings,
- store_conn="", store_type=None):
+def parse_and_serialize(
+ input_files,
+ input_format,
+ guess,
+ outfile,
+ output_format,
+ ns_bindings,
+ store_conn="",
+ store_type=None,
+):
if store_type:
store = plugin.get(store_type, Store)()
@@ -44,7 +50,7 @@ def parse_and_serialize(input_files, input_format, guess,
for fpath in input_files:
use_format, kws = _format_and_kws(input_format)
- if fpath == '-':
+ if fpath == "-":
fpath = sys.stdin
elif not input_format and guess:
use_format = guess_format(fpath) or DEFAULT_INPUT_FORMAT
@@ -52,7 +58,7 @@ def parse_and_serialize(input_files, input_format, guess,
if outfile:
output_format, kws = _format_and_kws(output_format)
- kws.setdefault('base', None)
+ kws.setdefault("base", None)
graph.serialize(destination=outfile, format=output_format, **kws)
if store:
@@ -75,15 +81,15 @@ def _format_and_kws(fmt):
('fmt', {'a': 'b:c'})
"""
fmt, kws = fmt, {}
- if fmt and ':' in fmt:
- fmt, kwrepr = fmt.split(':', 1)
- for kw in kwrepr.split(','):
- if '=' in kw:
- k, v = kw.split('=')
+ if fmt and ":" in fmt:
+ fmt, kwrepr = fmt.split(":", 1)
+ for kw in kwrepr.split(","):
+ if "=" in kw:
+ k, v = kw.split("=")
kws[k] = v
- elif kw.startswith('-'):
+ elif kw.startswith("-"):
kws[kw[1:]] = False
- elif kw.startswith('+'):
+ elif kw.startswith("+"):
kws[kw[1:]] = True
else: # same as "+"
kws[kw] = True
@@ -96,62 +102,78 @@ def make_option_parser():
kw_example = "FORMAT:(+)KW1,-KW2,KW3=VALUE"
oparser = OptionParser(
- "%prog [-h] [-i INPUT_FORMAT] [-o OUTPUT_FORMAT] " +
- "[--ns=PFX=NS ...] [-] [FILE ...]",
- description=__doc__.strip() + (
+ "%prog [-h] [-i INPUT_FORMAT] [-o OUTPUT_FORMAT] "
+ + "[--ns=PFX=NS ...] [-] [FILE ...]",
+ description=__doc__.strip()
+ + (
" Reads file system paths, URLs or from stdin if '-' is given."
- " The result is serialized to stdout."),
- version="%prog " + "(using rdflib %s)" % rdflib.__version__)
+ " The result is serialized to stdout."
+ ),
+ version="%prog " + "(using rdflib %s)" % rdflib.__version__,
+ )
oparser.add_option(
- '-i', '--input-format',
+ "-i",
+ "--input-format",
type=str, # default=DEFAULT_INPUT_FORMAT,
help="Format of the input document(s)."
- " Available input formats are: %s." % parser_names +
- " If no format is given, it will be " +
- "guessed from the file name extension." +
- " Keywords to parser can be given after format like: %s." % kw_example,
- metavar="INPUT_FORMAT")
+ " Available input formats are: %s." % parser_names
+ + " If no format is given, it will be "
+ + "guessed from the file name extension."
+ + " Keywords to parser can be given after format like: %s." % kw_example,
+ metavar="INPUT_FORMAT",
+ )
oparser.add_option(
- '-o', '--output-format',
- type=str, default=DEFAULT_OUTPUT_FORMAT,
+ "-o",
+ "--output-format",
+ type=str,
+ default=DEFAULT_OUTPUT_FORMAT,
help="Format of the graph serialization."
- " Available output formats are: %s."
- % serializer_names +
- " Default format is: '%default'." +
- " Keywords to serializer can be given after format like: %s." %
- kw_example,
- metavar="OUTPUT_FORMAT")
+ " Available output formats are: %s." % serializer_names
+ + " Default format is: '%default'."
+ + " Keywords to serializer can be given after format like: %s." % kw_example,
+ metavar="OUTPUT_FORMAT",
+ )
oparser.add_option(
- '--ns',
- action="append", type=str,
+ "--ns",
+ action="append",
+ type=str,
help="Register a namespace binding (QName prefix to a base URI). "
"This can be used more than once.",
- metavar="PREFIX=NAMESPACE")
+ metavar="PREFIX=NAMESPACE",
+ )
oparser.add_option(
- '--no-guess', dest='guess',
- action='store_false', default=True,
- help="Don't guess format based on file suffix.")
+ "--no-guess",
+ dest="guess",
+ action="store_false",
+ default=True,
+ help="Don't guess format based on file suffix.",
+ )
oparser.add_option(
- '--no-out',
- action='store_true', default=False,
- help="Don't output the resulting graph " +
- "(useful for checking validity of input).")
+ "--no-out",
+ action="store_true",
+ default=False,
+ help="Don't output the resulting graph "
+ + "(useful for checking validity of input).",
+ )
oparser.add_option(
- '-w', '--warn',
- action='store_true', default=False,
- help="Output warnings to stderr (by default only critical errors).")
+ "-w",
+ "--warn",
+ action="store_true",
+ default=False,
+ help="Output warnings to stderr (by default only critical errors).",
+ )
return oparser
-def _get_plugin_names(kind): return ", ".join(
- p.name for p in plugin.plugins(kind=kind))
+def _get_plugin_names(kind):
+ return ", ".join(p.name for p in plugin.plugins(kind=kind))
def main():
@@ -170,7 +192,7 @@ def main():
ns_bindings = {}
if opts.ns:
for ns_kw in opts.ns:
- pfx, uri = ns_kw.split('=')
+ pfx, uri = ns_kw.split("=")
ns_bindings[pfx] = uri
outfile = sys.stdout.buffer
@@ -178,8 +200,9 @@ def main():
if opts.no_out:
outfile = None
- parse_and_serialize(args, opts.input_format, opts.guess,
- outfile, opts.output_format, ns_bindings)
+ parse_and_serialize(
+ args, opts.input_format, opts.guess, outfile, opts.output_format, ns_bindings
+ )
if __name__ == "__main__":
diff --git a/rdflib/tools/rdfs2dot.py b/rdflib/tools/rdfs2dot.py
index 7135fe62..4c31516f 100644
--- a/rdflib/tools/rdfs2dot.py
+++ b/rdflib/tools/rdfs2dot.py
@@ -21,14 +21,52 @@ import collections
from rdflib import XSD, RDF, RDFS
-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")]
+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",
+ )
+]
EDGECOLOR = "blue"
NODECOLOR = "black"
@@ -60,7 +98,7 @@ def rdfs2dot(g, stream, opts={}):
pass # bnodes and some weird URIs cannot be split
return l
- stream.write(u"digraph { \n node [ fontname=\"DejaVu Sans\" ] ; \n")
+ stream.write(u'digraph { \n node [ fontname="DejaVu Sans" ] ; \n')
for x in g.subjects(RDF.type, RDFS.Class):
n = node(x)
@@ -72,7 +110,8 @@ def rdfs2dot(g, stream, opts={}):
for x in g.subjects(RDF.type, RDF.Property):
for a, b in itertools.product(
- g.objects(x, RDFS.domain), g.objects(x, RDFS.range)):
+ g.objects(x, RDFS.domain), g.objects(x, RDFS.range)
+ ):
if b in XSDTERMS or b == RDFS.Literal:
l = label(b, g)
if b == RDFS.Literal:
@@ -81,35 +120,42 @@ def rdfs2dot(g, stream, opts={}):
else:
# if a in nodes and b in nodes:
stream.write(
- "\t%s -> %s [ color=%s, label=\"%s\" ];\n" % (
- node(a), node(b), EDGECOLOR, label(x, g)))
+ '\t%s -> %s [ color=%s, label="%s" ];\n'
+ % (node(a), node(b), EDGECOLOR, label(x, g))
+ )
for u, n in nodes.items():
stream.write(u"# %s %s\n" % (u, n))
- f = [u"<tr><td align='left'>%s</td><td>%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>" + \
- u"</tr>%s</table> > ] \n"
+ f = [
+ u"<tr><td align='left'>%s</td><td>%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>"
+ + u"</tr>%s</table> > ] \n"
+ )
stream.write(opstr % (n, NODECOLOR, label(u, g), u"".join(f)))
stream.write("}\n")
def _help():
- sys.stderr.write("""
+ sys.stderr.write(
+ """
rdfs2dot.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(rdfs2dot, _help)
-if __name__ == '__main__':
+if __name__ == "__main__":
main()