|
1 | 1 | import json
|
2 | 2 | import urlparse
|
3 | 3 | from schema_salad.ref_resolver import Loader
|
| 4 | +from schema_salad.jsonld_context import makerdf |
4 | 5 | from rdflib import Graph, plugin, URIRef
|
5 | 6 | from rdflib.serializer import Serializer
|
6 | 7 | from typing import Any, Union, Dict, IO
|
7 | 8 |
|
8 |
| -def makerdf(workflow, wf, ctx): |
9 |
| - # type: (Union[str, unicode], Union[List[Dict[unicode, Any]], Dict[unicode, Any]], Loader.ContextType) -> Graph |
10 |
| - prefixes = {} |
11 |
| - for k,v in ctx.iteritems(): |
12 |
| - if isinstance(v, dict): |
13 |
| - url = v["@id"] |
14 |
| - else: |
15 |
| - url = v |
16 |
| - doc_url, frg = urlparse.urldefrag(url) |
17 |
| - if "/" in frg: |
18 |
| - p, _ = frg.split("/") |
19 |
| - prefixes[p] = u"%s#%s/" % (doc_url, p) |
20 |
| - |
21 |
| - if isinstance(wf, list): |
22 |
| - for entry in wf: |
23 |
| - entry["@context"] = ctx |
24 |
| - else: |
25 |
| - wf["@context"] = ctx |
26 |
| - g = Graph().parse(data=json.dumps(wf), format='json-ld', location=workflow) |
27 |
| - |
28 |
| - # Bug in json-ld loader causes @id fields to be added to the graph |
29 |
| - for s,p,o in g.triples((None, URIRef("@id"), None)): |
30 |
| - g.remove((s, p, o)) |
31 |
| - |
32 |
| - for k2,v2 in prefixes.iteritems(): |
33 |
| - g.namespace_manager.bind(k2, v2) |
34 |
| - |
35 |
| - return g |
36 |
| - |
37 | 9 | def printrdf(workflow, wf, ctx, sr, stdout):
|
38 | 10 | # type: (Union[str, unicode], Union[List[Dict[unicode, Any]], Dict[unicode, Any]], Loader.ContextType, str, IO[Any]) -> None
|
39 | 11 | stdout.write(makerdf(workflow, wf, ctx).serialize(format=sr))
|
|
0 commit comments