@@ -75,7 +75,7 @@ def validate_data(data, shape_file_path):
75
75
return conforms , v_text
76
76
77
77
78
- def to_newformat (path , format ):
78
+ def to_newformat (path , format , prefixfile = None , contextfile = None ):
79
79
"""Convert a JSONLD document to n-triples format
80
80
81
81
Since PyLD requires an http url, a local server is started to serve the
@@ -87,6 +87,11 @@ def to_newformat(path, format):
87
87
A local path or remote url to convert to n-triples
88
88
format: str of enum
89
89
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.
90
95
91
96
Returns
92
97
-------
@@ -99,6 +104,10 @@ def to_newformat(path, format):
99
104
raise ValueError (f"{ format } not in { supported_formats } " )
100
105
data = load_file (path )
101
106
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 )
102
111
return json .dumps (data , indent = 2 )
103
112
kwargs = {"algorithm" : "URDNA2015" , "format" : "application/n-quads" }
104
113
nt = jsonld .normalize (data , kwargs )
@@ -112,5 +121,10 @@ def to_newformat(path, format):
112
121
g .bind ("nidm" , "http://purl.org/nidash/nidm#" )
113
122
g .bind ("skos" , "http://www.w3.org/2004/02/skos/core#" )
114
123
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 )
115
129
g .parse (data = nt , format = "nt" )
116
130
return g .serialize (format = format ).decode ()
0 commit comments