1
1
# -*- coding: UTF-8 -*-
2
2
import json
3
+ from functools import partial
3
4
from rdflib import ConjunctiveGraph
4
5
from rdflib .compare import isomorphic
5
- from rdflib .plugins .parsers .jsonld import to_rdf
6
+ from rdflib .parser import InputSource
7
+ from rdflib .plugins .parsers .jsonld import to_rdf , JsonLDParser
6
8
from rdflib .plugins .serializers .jsonld import from_rdf
7
9
from rdflib .plugins .shared .jsonld .keys import CONTEXT , GRAPH
8
10
@@ -16,26 +18,69 @@ def _preserving_nodeid(self, bnode_context=None):
16
18
return bNode (self .eat (r_nodeid ).group (1 ))
17
19
18
20
21
+
22
+
19
23
DEFAULT_PARSER_VERSION = 1.0
20
24
21
25
26
+ def make_fake_urlinputsource (input_uri , format = None , suite_base = None , options = {}):
27
+ local_url = input_uri .replace ("https://w3c.github.io/json-ld-api/tests/" , "./" )
28
+ try :
29
+ f = open (local_url , "rb" )
30
+ except FileNotFoundError :
31
+ f = None
32
+ source = InputSource (input_uri )
33
+ source .setPublicId (input_uri )
34
+ source .setByteStream (f )
35
+ source .url = input_uri
36
+ source .links = []
37
+ if local_url .endswith ((".jsonld" , ".jldt" )):
38
+ source .content_type = "application/ld+json"
39
+ else :
40
+ source .content_type = "application/json"
41
+ source .format = format
42
+ if options :
43
+ if "httpLink" in options :
44
+ source .links .append (options ["httpLink" ])
45
+ if "contentType" in options :
46
+ source .content_type = options ['contentType' ]
47
+ if "redirectTo" in options :
48
+ redir = suite_base + options ['redirectTo' ]
49
+ local_redirect = redir .replace ("https://w3c.github.io/json-ld-api/tests/" , "./" )
50
+ if f :
51
+ f .close ()
52
+ try :
53
+ f = open (local_redirect , "rb" )
54
+ except FileNotFoundError :
55
+ f = None
56
+ source .setByteStream (f )
57
+ source .url = redir
58
+ source .setPublicId (redir )
59
+ source .setSystemId (redir )
60
+ return source
61
+
22
62
def do_test_json (suite_base , cat , num , inputpath , expectedpath , context , options ):
23
63
input_uri = suite_base + inputpath
24
- input_obj = _load_json (inputpath )
25
64
input_graph = ConjunctiveGraph ()
26
- to_rdf (
27
- input_obj ,
28
- input_graph ,
29
- base = input_uri ,
30
- context_data = context ,
31
- generalized_rdf = True ,
32
- )
65
+ if cat == "remote-doc" :
66
+ input_src = make_fake_urlinputsource (input_uri , format = "json-ld" , suite_base = suite_base , options = options )
67
+ p = JsonLDParser ()
68
+ p .parse (input_src , input_graph , base = input_src .getPublicId (), context_data = context , generalized_rdf = True )
69
+ else :
70
+ input_obj = _load_json (inputpath )
71
+ to_rdf (
72
+ input_obj ,
73
+ input_graph ,
74
+ base = input_uri ,
75
+ context_data = context ,
76
+ generalized_rdf = True ,
77
+ )
33
78
expected_json = _load_json (expectedpath )
34
79
use_native_types = True # CONTEXT in input_obj
35
80
result_json = from_rdf (
36
81
input_graph ,
37
82
context ,
38
- base = input_uri ,
83
+ base = "./" , # deliberately set base different to the input base
39
84
use_native_types = options .get ("useNativeTypes" , use_native_types ),
40
85
use_rdf_type = options .get ("useRdfType" , False ),
41
86
)
@@ -72,14 +117,19 @@ def do_test_parser(suite_base, cat, num, inputpath, expectedpath, context, optio
72
117
version = 1.1
73
118
elif requested_version == "json-ld-1.0" :
74
119
version = 1.0
75
- to_rdf (
76
- input_obj ,
77
- result_graph ,
78
- context_data = context ,
79
- base = options .get ("base" , input_uri ),
80
- version = version ,
81
- generalized_rdf = options .get ("produceGeneralizedRdf" , False ),
82
- )
120
+ if cat == "remote-doc" :
121
+ input_src = make_fake_urlinputsource (input_uri , format = "json-ld" , options = options )
122
+ p = JsonLDParser ()
123
+ p .parse (input_src , result_graph , base = input_uri , context_data = context , generalized_rdf = True )
124
+ else :
125
+ to_rdf (
126
+ input_obj ,
127
+ result_graph ,
128
+ context_data = context ,
129
+ base = options .get ("base" , input_uri ),
130
+ version = version ,
131
+ generalized_rdf = options .get ("produceGeneralizedRdf" , False ),
132
+ )
83
133
assert isomorphic (result_graph , expected_graph ), "Expected:\n %s\n Got:\n %s" % (
84
134
expected_graph .serialize (),
85
135
result_graph .serialize (),
0 commit comments