Skip to content

Commit fe7c49f

Browse files
authored
Merge pull request #45 from common-workflow-language/improve-makerdf
Bugfix makerdf (helper function for converting to RDF).
2 parents 94d9847 + 3b414e7 commit fe7c49f

File tree

5 files changed

+26
-21
lines changed

5 files changed

+26
-21
lines changed

schema_salad/jsonld_context.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,11 @@ def fix_jsonld_ids(obj, ids):
188188
for entry in obj:
189189
fix_jsonld_ids(entry, ids)
190190

191-
def makerdf(workflow, wf, ctx):
192-
# type: (Union[str, unicode], Union[List[Dict[unicode, Any]], Dict[unicode, Any]], Loader.ContextType) -> Graph
191+
def makerdf(workflow, wf, ctx, graph=None):
192+
# type: (Union[str, unicode], Union[List[Dict[unicode, Any]], Dict[unicode, Any]], Loader.ContextType, Graph) -> Graph
193193
prefixes = {}
194194
idfields = []
195-
for k,v in ctx.iteritems():
195+
for k, v in ctx.iteritems():
196196
if isinstance(v, dict):
197197
url = v["@id"]
198198
else:
@@ -201,26 +201,29 @@ def makerdf(workflow, wf, ctx):
201201
idfields.append(k)
202202
doc_url, frg = urlparse.urldefrag(url)
203203
if "/" in frg:
204-
p, _ = frg.split("/")
204+
p = frg.split("/")[0]
205205
prefixes[p] = u"%s#%s/" % (doc_url, p)
206206

207+
fix_jsonld_ids(wf, idfields)
208+
209+
if graph is None:
210+
g = Graph()
211+
else:
212+
g = graph
213+
207214
if isinstance(wf, list):
208-
wf = {
209-
"@context": ctx,
210-
"@graph": wf
211-
}
215+
for w in wf:
216+
w["@context"] = ctx
217+
g.parse(data=json.dumps(w), format='json-ld', location=workflow)
212218
else:
213219
wf["@context"] = ctx
214-
215-
fix_jsonld_ids(wf, idfields)
216-
217-
g = Graph().parse(data=json.dumps(wf), format='json-ld', location=workflow)
220+
g.parse(data=json.dumps(wf), format='json-ld', location=workflow)
218221

219222
# Bug in json-ld loader causes @id fields to be added to the graph
220-
for s,p,o in g.triples((None, URIRef("@id"), None)):
221-
g.remove((s, p, o))
223+
for sub, pred, obj in g.triples((None, URIRef("@id"), None)):
224+
g.remove((sub, pred, obj))
222225

223-
for k2,v2 in prefixes.iteritems():
226+
for k2, v2 in prefixes.iteritems():
224227
g.namespace_manager.bind(k2, v2)
225228

226229
return g

schema_salad/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import argparse
33
import logging
44
import sys
5+
import traceback
56
import pkg_resources # part of setuptools
67
from . import schema
78
from . import jsonld_context
@@ -103,7 +104,7 @@ def main(argsl=None): # type: (List[str]) -> int
103104
schema_raw_doc, schema_uri)
104105
except (validate.ValidationException) as e:
105106
_logger.error("Schema `%s` failed link checking:\n%s",
106-
args.schema, e, exc_info=(e if args.debug else False))
107+
args.schema, e, exc_info=(True if args.debug else False))
107108
_logger.debug("Index is %s", metaschema_loader.idx.keys())
108109
_logger.debug("Vocabulary is %s", metaschema_loader.vocab.keys())
109110
return 1
@@ -148,7 +149,8 @@ def main(argsl=None): # type: (List[str]) -> int
148149

149150
if isinstance(avsc_names, Exception):
150151
_logger.error("Schema `%s` error:\n%s", args.schema,
151-
avsc_names, exc_info=(avsc_names if args.debug else False))
152+
avsc_names, exc_info=((type(avsc_names), avsc_names,
153+
None) if args.debug else None))
152154
if args.print_avro:
153155
print(json.dumps(avsc_obj, indent=4))
154156
return 1

schema_salad/ref_resolver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def resolve_all(self, document, base_url, file_base=None, checklinks=True):
553553
document[key], _ = loader.resolve_all(
554554
val, base_url, file_base=file_base, checklinks=False)
555555
except validate.ValidationException as v:
556-
_logger.warn("loader is %s", id(loader), exc_info=v)
556+
_logger.warn("loader is %s", id(loader), exc_info=True)
557557
raise validate.ValidationException("(%s) (%s) Validation error in field %s:\n%s" % (
558558
id(loader), file_base, key, validate.indent(str(v))))
559559

@@ -577,7 +577,7 @@ def resolve_all(self, document, base_url, file_base=None, checklinks=True):
577577
val, base_url, file_base=file_base, checklinks=False)
578578
i += 1
579579
except validate.ValidationException as v:
580-
_logger.warn("failed", exc_info=v)
580+
_logger.warn("failed", exc_info=True)
581581
raise validate.ValidationException("(%s) (%s) Validation error in position %i:\n%s" % (
582582
id(loader), file_base, i, validate.indent(str(v))))
583583

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
extras_require = {} # TODO: to be removed when the above is added
4242

4343
setup(name='schema-salad',
44-
version='1.14',
44+
version='1.15',
4545
description='Schema Annotations for Linked Avro Data (SALAD)',
4646
long_description=open(README).read(),
4747
author='Common workflow language working group',

typeshed/2.7/rdflib/graph.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Graph(Node):
2525
def close(self, commit_pending_transaction=False): ...
2626
def add(self, __tuple_arg_2: Tuple[Node, Node, Node]) -> None: ...
2727
def addN(self, quads): ...
28-
def remove(self, __tuple_arg_2): ...
28+
def remove(self, __tuple_arg_2: Tuple[Union[AnyStr, URIRef], AnyStr, Union[AnyStr, URIRef]]) -> None: ...
2929
def triples(self, __tuple_arg_2: Tuple[Union[AnyStr, URIRef], AnyStr, Union[AnyStr, URIRef]]) -> Iterator[Tuple[AnyStr, AnyStr, AnyStr]]: ...
3030
def __getitem__(self, item): ...
3131
def __len__(self): ...

0 commit comments

Comments
 (0)