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)
def parse_and_serialize(input_files, input_format, guess, outfile, output_format, ns_bindings, store_conn=STORE_CONNECTION, store_type=STORE_TYPE): store = plugin.get(store_type, Store)() store.open(store_conn) graph = Graph(store) for prefix, uri in ns_bindings.items(): graph.namespace_manager.bind(prefix, uri, override=False) for fpath in input_files: use_format, kws = _format_and_kws(input_format) if fpath == '-': fpath = sys.stdin elif not input_format and guess: use_format = guess_format(fpath) or DEFAULT_INPUT_FORMAT graph.parse(fpath, format=use_format, **kws) if outfile: output_format, kws = _format_and_kws(output_format) graph.serialize( destination=outfile, format=output_format, base=None, **kws) store.rollback()