File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed
Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ import json
2+ from typing import AnyStr
3+
4+ from ogc .na .util import load_yaml
5+ from pyld import jsonld
6+ from rdflib import Graph
7+
8+ from ogc .bblocks .models import TransformMetadata , Transformer
9+ from ogc .na import ingest_json
10+
11+ transform_type = 'jsonld-frame'
12+
13+ default_inputs = [
14+ 'text/turtle' ,
15+ 'rdf/xml' ,
16+ 'application/ld+json' ,
17+ ]
18+
19+ default_outputs = [
20+ 'application/ld+json' ,
21+ ]
22+
23+ class JsonLdFrameTransformer (Transformer ):
24+
25+ def __init__ (self ):
26+ super ().__init__ ([transform_type ], default_inputs , default_outputs )
27+
28+ def do_transform (self , metadata : TransformMetadata ) -> AnyStr | None :
29+ frame = json .loads (metadata .transform_content )
30+
31+ if metadata .source_mime_type == 'application/ld+json' :
32+ input_doc = json .loads (metadata .input_data )
33+ else :
34+ data_graph = Graph ().parse (data = metadata .input_data , format = metadata .source_mime_type )
35+ input_doc = json .loads (data_graph .serialize (format = 'json-ld' ))
36+
37+ framed = jsonld .frame (input_doc , frame )
38+
39+ if metadata .target_mime_type == 'application/json' :
40+ if '@graph' in framed :
41+ framed = framed ['@graph' ]
42+ else :
43+ framed .pop ('@context' , None )
44+
45+ return json .dumps (framed , indent = 2 )
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ jq~=1.6
1414pathvalidate == 3.2.0
1515jsonpointer ~= 2.4
1616lxml ~= 5.0.0
17+ pyld
1718
1819# mkdocs
1920mkdocs >= 1.4.2
You can’t perform that action at this time.
0 commit comments