@@ -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