def test_rdf_datetime() -> None: """Affirm that datetime objects can be serialized in makerdf().""" ldr = Loader({}) ctx: ContextType = { "id": "@id", "location": {"@id": "@id", "@type": "@id"}, "bar": "http://example.com/bar", "ex": "http://example.com/", } ldr.add_context(ctx) ra: CommentedMap = cast( CommentedMap, ldr.resolve_all( cmap( { "id": "foo", "bar": {"id": "baz"}, } ), "http://example.com", )[0], ) ra["s:dateCreated"] = datetime.datetime(2020, 10, 8) g = makerdf(None, ra, ctx) g.serialize(destination=stdout(), format="n3") g2 = makerdf(None, CommentedSeq([ra]), ctx) g2.serialize(destination=stdout(), format="n3")
def test_scoped_id() -> None: ldr = Loader({}) ctx = { "id": "@id", "location": {"@id": "@id", "@type": "@id"}, "bar": "http://example.com/bar", "ex": "http://example.com/", } # type: ContextType ldr.add_context(ctx) ra, _ = ldr.resolve_all( cmap({"id": "foo", "bar": {"id": "baz"}}), "http://example.com" ) assert { "id": "http://example.com/#foo", "bar": {"id": "http://example.com/#foo/baz"}, } == ra g = makerdf(None, ra, ctx) print(g.serialize(format="n3")) ra, _ = ldr.resolve_all( cmap({"location": "foo", "bar": {"location": "baz"}}), "http://example.com", checklinks=False, ) assert { "location": "http://example.com/foo", "bar": {"location": "http://example.com/baz"}, } == ra g = makerdf(None, ra, ctx) print(g.serialize(format="n3")) ra, _ = ldr.resolve_all( cmap({"id": "foo", "bar": {"location": "baz"}}), "http://example.com", checklinks=False, ) assert { "id": "http://example.com/#foo", "bar": {"location": "http://example.com/baz"}, } == ra g = makerdf(None, ra, ctx) print(g.serialize(format="n3")) ra, _ = ldr.resolve_all( cmap({"location": "foo", "bar": {"id": "baz"}}), "http://example.com", checklinks=False, ) assert { "location": "http://example.com/foo", "bar": {"id": "http://example.com/#baz"}, } == ra g = makerdf(None, ra, ctx) print(g.serialize(format="n3"))
def expand_cwl(cwl, uri, g): try: document_loader = Loader( {"cwl": "https://w3id.org/cwl/cwl#", "id": "@id"}) cwl = yaml.load(cwl) document_loader, avsc_names, processobj, metadata, uri = validate_document( document_loader, cwl, uri, strict=False) jsonld_context.makerdf(uri, processobj, document_loader.ctx, graph=g) sys.stderr.write("\n%s: imported ok\n" % (uri)) except Exception as e: sys.stderr.write("\n%s: %s\n" % (uri, e))
def expand_cwl(cwl, uri, g): try: document_loader = Loader({ "cwl": "https://w3id.org/cwl/cwl#", "id": "@id" }) cwl = yaml.load(cwl) document_loader, avsc_names, processobj, metadata, uri = validate_document( document_loader, cwl, uri, strict=False) jsonld_context.makerdf(uri, processobj, document_loader.ctx, graph=g) sys.stderr.write("\n%s: imported ok\n" % (uri)) except Exception as e: sys.stderr.write("\n%s: %s\n" % (uri, e))
def main(): parser = argparse.ArgumentParser() parser.add_argument("swagger") parser.add_argument("annotations") parser.add_argument("url") parser.add_argument("--dockstore-fixup", action="store_true", default=False) parser.add_argument("--print-rdf", action="store_true", default=False) parser.add_argument("--serve", action="store_true", default=False) parser.add_argument("--fuseki-path", type=str, default=".") args = parser.parse_args() with open(args.annotations) as f2: annotations = yaml.load(f2) with open(args.swagger) as f: sld = swg2salad.swg2salad(yaml.load(f), annotations) sld["$base"] = "http://ga4gh.org/schemas/tool-registry-schemas" sld["name"] = "file://" + os.path.realpath(args.swagger) document_loader, avsc_names, schema_metadata, metaschema_loader = load_schema( sld) txt = document_loader.fetch_text( urlparse.urljoin("file://" + os.getcwd() + "/", args.url)) r = yaml.load(txt) if args.dockstore_fixup: dockstore_fixup(r) validate_doc(avsc_names, r, document_loader, True) sys.stderr.write("API returned valid response\n") toolreg = Namespace("http://ga4gh.org/schemas/tool-registry-schemas#") td = Namespace( "http://ga4gh.org/schemas/tool-registry-schemas#ToolDescriptor/") if args.print_rdf or args.serve: g = jsonld_context.makerdf(args.url, r, document_loader.ctx) for s, _, o in g.triples((None, td["type"], Literal("CWL"))): for _, _, d in g.triples((s, toolreg["descriptor"], None)): expand_cwl(d, unicode(s), g) if args.print_rdf: print(g.serialize(format="turtle")) if args.serve: t = tempfile.NamedTemporaryFile(suffix=".ttl") g.serialize(t, format="turtle") t.flush() subprocess.check_call( ["./fuseki-server", "--file=" + t.name, "/tools"], cwd=args.fuseki_path)
def main(): parser = argparse.ArgumentParser() parser.add_argument("swagger") parser.add_argument("annotations") parser.add_argument("url") parser.add_argument("--print-rdf", action="store_true", default=False) parser.add_argument("--serve", action="store_true", default=False) parser.add_argument("--fuseki-path", type=str, default=".") args = parser.parse_args() warnings.simplefilter('ignore', yaml.error.UnsafeLoaderWarning) with open(args.annotations) as f2: annotations = yaml.load(f2) with open(args.swagger) as f: sld = swg2salad.swg2salad(yaml.load(f), annotations) sld["$base"] = "http://ga4gh.org/schemas/tool-registry-schemas" sld["name"] = "file://" + os.path.realpath(args.swagger) document_loader, avsc_names, schema_metadata, metaschema_loader = load_schema( cmap(sld)) txt = document_loader.fetch_text( urlparse.urljoin( "file://" + os.getcwd() + "/", args.url)) r = yaml.load(txt) validate_doc(avsc_names, r, document_loader, True) sys.stderr.write("API returned valid response\n") toolreg = Namespace("http://ga4gh.org/schemas/tool-registry-schemas#") td = Namespace( "http://ga4gh.org/schemas/tool-registry-schemas#ToolDescriptor/") if args.print_rdf or args.serve: g = jsonld_context.makerdf(args.url, r, document_loader.ctx) for s, _, o in g.triples((None, td["type"], Literal("CWL"))): for _, _, d in g.triples((s, toolreg["descriptor"], None)): expand_cwl(d, unicode(s), g) if args.print_rdf: print(g.serialize(format="turtle")) if args.serve: t = tempfile.NamedTemporaryFile(suffix=".ttl") g.serialize(t, format="turtle") t.flush() subprocess.check_call( ["./fuseki-server", "--file=" + t.name, "/tools"], cwd=args.fuseki_path)
def visitor(t): makerdf(t["id"], t, ctx, graph=g)
def visitor(t: CommentedMap) -> None: makerdf(t["id"], t, ctx, graph=g)
def test_scoped_id(self): ldr = schema_salad.ref_resolver.Loader({}) ctx = { "id": "@id", "location": { "@id": "@id", "@type": "@id" }, "bar": "http://example.com/bar", "ex": "http://example.com/" } ldr.add_context(ctx) ra, _ = ldr.resolve_all(cmap({ "id": "foo", "bar": { "id": "baz" } }), "http://example.com") self.assertEqual( { 'id': 'http://example.com/#foo', 'bar': { 'id': 'http://example.com/#foo/baz' }, }, ra) g = makerdf(None, ra, ctx) print(g.serialize(format="n3")) ra, _ = ldr.resolve_all(cmap({ "location": "foo", "bar": { "location": "baz" } }), "http://example.com", checklinks=False) self.assertEqual( { 'location': 'http://example.com/foo', 'bar': { 'location': 'http://example.com/baz' }, }, ra) g = makerdf(None, ra, ctx) print(g.serialize(format="n3")) ra, _ = ldr.resolve_all(cmap({ "id": "foo", "bar": { "location": "baz" } }), "http://example.com", checklinks=False) self.assertEqual( { 'id': 'http://example.com/#foo', 'bar': { 'location': 'http://example.com/baz' }, }, ra) g = makerdf(None, ra, ctx) print(g.serialize(format="n3")) ra, _ = ldr.resolve_all(cmap({ "location": "foo", "bar": { "id": "baz" } }), "http://example.com", checklinks=False) self.assertEqual( { 'location': 'http://example.com/foo', 'bar': { 'id': 'http://example.com/#baz' }, }, ra) g = makerdf(None, ra, ctx) print(g.serialize(format="n3"))
def test_scoped_id(self): ldr = schema_salad.ref_resolver.Loader({}) ctx = { "id": "@id", "location": { "@id": "@id", "@type": "@id" }, "bar": "http://example.com/bar", "ex": "http://example.com/" } ldr.add_context(ctx) ra, _ = ldr.resolve_all({ "id": "foo", "bar": { "id": "baz" } }, "http://example.com") self.assertEqual({'id': 'http://example.com/#foo', 'bar': { 'id': 'http://example.com/#foo/baz'}, }, ra) g = makerdf(None, ra, ctx) print(g.serialize(format="n3")) ra, _ = ldr.resolve_all({ "location": "foo", "bar": { "location": "baz" } }, "http://example.com", checklinks=False) self.assertEqual({'location': 'http://example.com/foo', 'bar': { 'location': 'http://example.com/baz'}, }, ra) g = makerdf(None, ra, ctx) print(g.serialize(format="n3")) ra, _ = ldr.resolve_all({ "id": "foo", "bar": { "location": "baz" } }, "http://example.com", checklinks=False) self.assertEqual({'id': 'http://example.com/#foo', 'bar': { 'location': 'http://example.com/baz'}, }, ra) g = makerdf(None, ra, ctx) print(g.serialize(format="n3")) ra, _ = ldr.resolve_all({ "location": "foo", "bar": { "id": "baz" } }, "http://example.com", checklinks=False) self.assertEqual({'location': 'http://example.com/foo', 'bar': { 'id': 'http://example.com/#baz'}, }, ra) g = makerdf(None, ra, ctx) print(g.serialize(format="n3"))
def visitor(t): # type: (CommentedMap) -> None makerdf(t["id"], t, ctx, graph=g)
def visitor(t): # type: (MutableMapping[str, Any]) -> None makerdf(t["id"], t, ctx, graph=g)