comparison env/lib/python3.7/site-packages/rdflib/extras/cmdlineutils.py @ 5:9b1c78e6ba9c draft default tip

"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
author shellac
date Mon, 01 Jun 2020 08:59:25 -0400
parents 79f47841a781
children
comparison
equal deleted inserted replaced
4:79f47841a781 5:9b1c78e6ba9c
1 import sys
2 import time
3 import getopt
4 import rdflib
5 import codecs
6
7 from rdflib.util import guess_format
8
9
10 def _help():
11 sys.stderr.write("""
12 program.py [-f <format>] [-o <output>] [files...]
13 Read RDF files given on STDOUT - does something to the resulting graph
14 If no files are given, read from stdin
15 -o specifies file for output, if not given stdout is used
16 -f specifies parser to use, if not given it is guessed from extension
17
18 """)
19
20
21 def main(target, _help=_help, options="", stdin=True):
22 """
23 A main function for tools that read RDF from files given on commandline
24 or from STDIN (if stdin parameter is true)
25 """
26
27 args, files = getopt.getopt(sys.argv[1:], "hf:o:" + options)
28 dargs = dict(args)
29
30 if "-h" in dargs:
31 _help()
32 sys.exit(-1)
33
34 g = rdflib.Graph()
35
36 if "-f" in dargs:
37 f = dargs["-f"]
38 else:
39 f = None
40
41 if "-o" in dargs:
42 sys.stderr.write("Output to %s\n" % dargs["-o"])
43 out = codecs.open(dargs["-o"], "w", "utf-8")
44 else:
45 out = sys.stdout
46
47 start = time.time()
48 if len(files) == 0 and stdin:
49 sys.stderr.write("Reading from stdin as %s..." % f)
50 g.load(sys.stdin, format=f)
51 sys.stderr.write("[done]\n")
52 else:
53 size = 0
54 for x in files:
55 if f is None:
56 f = guess_format(x)
57 start1 = time.time()
58 sys.stderr.write("Loading %s as %s... " % (x, f))
59 g.load(x, format=f)
60 sys.stderr.write("done.\t(%d triples\t%.2f seconds)\n" %
61 (len(g) - size, time.time() - start1))
62 size = len(g)
63
64 sys.stderr.write("Loaded a total of %d triples in %.2f seconds.\n" %
65 (len(g), time.time() - start))
66
67 target(g, out, args)