diff env/lib/python3.7/site-packages/rdflib/extras/cmdlineutils.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/env/lib/python3.7/site-packages/rdflib/extras/cmdlineutils.py	Sat May 02 07:14:21 2020 -0400
@@ -0,0 +1,67 @@
+import sys
+import time
+import getopt
+import rdflib
+import codecs
+
+from rdflib.util import guess_format
+
+
+def _help():
+    sys.stderr.write("""
+program.py [-f <format>] [-o <output>] [files...]
+Read RDF files given on STDOUT - does something to the resulting graph
+If no files are given, read from stdin
+-o specifies file for output, if not given stdout is used
+-f specifies parser to use, if not given it is guessed from extension
+
+""")
+
+
+def main(target, _help=_help, options="", stdin=True):
+    """
+    A main function for tools that read RDF from files given on commandline
+    or from STDIN (if stdin parameter is true)
+    """
+
+    args, files = getopt.getopt(sys.argv[1:], "hf:o:" + options)
+    dargs = dict(args)
+
+    if "-h" in dargs:
+        _help()
+        sys.exit(-1)
+
+    g = rdflib.Graph()
+
+    if "-f" in dargs:
+        f = dargs["-f"]
+    else:
+        f = None
+
+    if "-o" in dargs:
+        sys.stderr.write("Output to %s\n" % dargs["-o"])
+        out = codecs.open(dargs["-o"], "w", "utf-8")
+    else:
+        out = sys.stdout
+
+    start = time.time()
+    if len(files) == 0 and stdin:
+        sys.stderr.write("Reading from stdin as %s..." % f)
+        g.load(sys.stdin, format=f)
+        sys.stderr.write("[done]\n")
+    else:
+        size = 0
+        for x in files:
+            if f is None:
+                f = guess_format(x)
+            start1 = time.time()
+            sys.stderr.write("Loading %s as %s... " % (x, f))
+            g.load(x, format=f)
+            sys.stderr.write("done.\t(%d triples\t%.2f seconds)\n" %
+                             (len(g) - size, time.time() - start1))
+            size = len(g)
+
+    sys.stderr.write("Loaded a total of %d triples in %.2f seconds.\n" %
+                     (len(g), time.time() - start))
+
+    target(g, out, args)