Skip to content

Commit 9597b24

Browse files
authored
Merge pull request #16 from satra/enh/prefixes
allow for additional prefixes or a modified context
2 parents caaf5be + daf87ac commit 9597b24

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

reproschema/cli.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,19 @@ def validate(shapefile, path):
5757
default="n-triples",
5858
show_default=True,
5959
)
60+
@click.option(
61+
"--prefixfile", default=None, type=click.Path(exists=True, dir_okay=False)
62+
)
63+
@click.option(
64+
"--contextfile", default=None, type=click.Path(exists=True, dir_okay=False)
65+
)
6066
@click.argument("path", nargs=1, type=str)
61-
def convert(path, format):
67+
def convert(path, format, prefixfile, contextfile):
6268
if not (path.startswith("http") or os.path.exists(path)):
6369
raise ValueError(f"{path} must be a URL or an existing file or directory")
6470
from .jsonldutils import to_newformat
6571

66-
print(to_newformat(path, format))
72+
print(to_newformat(path, format, prefixfile, contextfile))
6773

6874

6975
@main.command()

reproschema/jsonldutils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def validate_data(data, shape_file_path):
7575
return conforms, v_text
7676

7777

78-
def to_newformat(path, format):
78+
def to_newformat(path, format, prefixfile=None, contextfile=None):
7979
"""Convert a JSONLD document to n-triples format
8080
8181
Since PyLD requires an http url, a local server is started to serve the
@@ -87,6 +87,11 @@ def to_newformat(path, format):
8787
A local path or remote url to convert to n-triples
8888
format: str of enum
8989
Returned format jsonld, n-triples, turtle
90+
prefixfile: str
91+
Prefixes to use when converting to turtle (ignored otherwise)
92+
contextfile: str
93+
Context to use for compaction when returning jsonld. If not provided,
94+
a jsonld graph is returned.
9095
9196
Returns
9297
-------
@@ -99,6 +104,10 @@ def to_newformat(path, format):
99104
raise ValueError(f"{format} not in {supported_formats}")
100105
data = load_file(path)
101106
if format == "jsonld":
107+
if contextfile is not None:
108+
with open(contextfile) as fp:
109+
context = json.load(fp)
110+
data = jsonld.compact(data, context)
102111
return json.dumps(data, indent=2)
103112
kwargs = {"algorithm": "URDNA2015", "format": "application/n-quads"}
104113
nt = jsonld.normalize(data, kwargs)
@@ -112,5 +121,10 @@ def to_newformat(path, format):
112121
g.bind("nidm", "http://purl.org/nidash/nidm#")
113122
g.bind("skos", "http://www.w3.org/2004/02/skos/core#")
114123
g.bind("prov", "http://www.w3.org/ns/prov#")
124+
if prefixfile is not None:
125+
with open(prefixfile) as fp:
126+
prefixes = json.load(fp)
127+
for key, value in prefixes.items():
128+
g.bind(key, value)
115129
g.parse(data=nt, format="nt")
116130
return g.serialize(format=format).decode()

0 commit comments

Comments
 (0)