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()
def to_rdf_etree(sources): graph = ConjunctiveGraph() for source in sources: graph.load(source, format=guess_format(source)) io = StringIO() graph.serialize(io, format="pretty-xml") io.seek(0) return etree.parse(io)
from rdflib.graph import ConjunctiveGraph from oort.util.deps import json from oort.gluon import Profile from oort.gluon.parser import to_rdf from oort.gluon.serializer import to_tree if __name__ == '__main__': from sys import argv, stdin args = argv[1:] fname = args.pop(0) if args else "" profile_fname = args.pop(0) if args else None lang = args.pop(0) if args else None if not fname or fname.endswith('.json'): infile = open(fname) if fname else stdin tree = json.loads( "".join(l for l in infile if not l.strip().startswith('//'))) graph = to_rdf(tree) print graph.serialize(format="n3") else: graph = ConjunctiveGraph() from rdfextras.tools.pathutils import guess_format graph.parse(fname, format=guess_format(fname)) profile = Profile(json.load(open(profile_fname)) ) if profile_fname else None tree = to_tree(graph, profile, lang) print json.dumps(tree, indent=4, separators=(',',': '), sort_keys=True, check_circular=False)
from oort.util.deps import json from oort.gluon import Profile from oort.gluon.parser import to_rdf from oort.gluon.serializer import to_tree if __name__ == '__main__': from sys import argv, stdin args = argv[1:] fname = args.pop(0) if args else "" profile_fname = args.pop(0) if args else None lang = args.pop(0) if args else None if not fname or fname.endswith('.json'): infile = open(fname) if fname else stdin tree = json.loads("".join(l for l in infile if not l.strip().startswith('//'))) graph = to_rdf(tree) print graph.serialize(format="n3") else: graph = ConjunctiveGraph() from rdfextras.tools.pathutils import guess_format graph.parse(fname, format=guess_format(fname)) profile = Profile(json.load( open(profile_fname))) if profile_fname else None tree = to_tree(graph, profile, lang) print json.dumps(tree, indent=4, separators=(',', ': '), sort_keys=True, check_circular=False)
if not args: print "USAGE: %s FILE [rdf...]" % p.basename(cmd) print "Where FILE is a local copy of <https://lagen.nu/1976:725>. Get it by doing e.g.:" print " $ /usr/bin/curl -sk 'https://lagen.nu/1976:725' > /tmp/sfs-1976_725.xhtml" print print "If additional local rdf files are supplied, a diff of the " \ "extracted data and the supplied data is output (instead of just the " \ "extracted data)." exit() docpath = args[0] graph = fsdoc_to_graph(docpath) from rdfextras.tools.pathutils import guess_format cmp_graph = Graph() for fpath in args[1:]: cmp_graph.load(fpath, format=guess_format(fpath)) if cmp_graph: from rdflib.compare import graph_diff in_both, in_first, in_second = graph_diff(graph, cmp_graph) print "# %s new statements:" % len(in_first) for pfx, uri in graph.namespaces(): in_first.bind(pfx, uri) print in_first.serialize(format='n3') else: print "# Nothing to compare against. New RDF is:" print graph.serialize(format='n3')