Skip to content

Commit 44e4b1b

Browse files
committed
fix mk keys url compat
1 parent 59b021e commit 44e4b1b

File tree

6 files changed

+192
-177
lines changed

6 files changed

+192
-177
lines changed

src/json2rdf/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = "71" # shoud equal `git rev-list --count master`
1+
__version__ = "72" # shoud equal `git rev-list --count master`
22
# can roll back to 0 if errors
33
from .json2rdf import json2rdf, j2r

src/json2rdf/json2rdf.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class anonID(ID): ...
4646
# subject_key = subject_keys[0]
4747
# @classproperty 'deprecated'
4848
# def subject_key
49-
object_keys = {'refid',}
49+
object_keys = {'refid', 'idref' }
5050

5151
class list:
5252
key = '__rdftype__'
@@ -202,16 +202,21 @@ def __str__(self) -> str:
202202
_ = _ + super().__str__()
203203
return _
204204

205+
from urllib.parse import quote as uq
205206
@classmethod
206207
def triple(cls, s, p, o):
207208
m = {True: 'true', False:'false', None: '\"null\"'} # not rdf:nil which is specific to a rdf:List
208209
from types import NoneType
209210
# SUBJECT
210211
assert(isinstance(s, Identification.ID))
211212
if type(s) is Identification.ID:
213+
s = str(s)
214+
s = cls.uq(s)
212215
s = f'{cls.list.id_prefix}:{s}'
213216
else:
214217
assert(type(s) is Identification.anonID)
218+
s = str(s)
219+
s = cls.uq(s)
215220
s = f'_:{s}'
216221
# special list/seq handling
217222
if p == Identification.list.key and o == Identification.list.value:
@@ -223,10 +228,7 @@ def triple(cls, s, p, o):
223228
p = f'rdf:_{p}'
224229
else:
225230
assert(isinstance(p, str))
226-
p = p.replace(' ', '_')
227-
# create legal by dropping non alpha num
228-
# url encodeing?
229-
p = ''.join(c for c in p if c.isalnum() or c == '_')
231+
p = cls.uq(p)
230232
p = f'{cls.list.key_prefix}:{p}'
231233

232234
# OBJECT
@@ -250,9 +252,13 @@ def triple(cls, s, p, o):
250252
o = m[o]
251253
elif isinstance(o, Identification.ID):
252254
if type(o) is Identification.ID:
255+
o = str(o)
256+
o = cls.uq(o)
253257
o = f'{cls.list.id_prefix}:{o}'
254258
else:
255259
assert(type(o) is Identification.anonID)
260+
o = str(o)
261+
o = cls.uq(o)
256262
o = f'_:{o}'
257263
else:
258264
o = str(o)

test/test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ def json():
1111
'dwid': {
1212
'id': 33,
1313
'k': 'v'
14+
},
15+
'dwbadid': {
16+
'id': "sdfsdf \" : ! @ # ",
17+
'refid': "sdfsdf \" : ! @ # ",
18+
"horrible key @ *$ \" ": "!@#$ sdf ",
19+
"key_w_us": "has nice key",
20+
"CamelCase": "has nice key",
1421
}
1522
}
1623

test/test/.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/test/test.ttl

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
22
prefix id: <urn:example:id:>
33
prefix prefix: <urn:example:prefix:>
4-
prefix meta: <urn:example:meta:>
54

6-
_:2380581804288 prefix:k _:2380627818112.
7-
_:2380616502144 rdf:_0 _:2380627815424.
8-
_:2380616502144 rdf:type rdf:Seq.
9-
_:2380627776832 prefix:dwid id:33.
10-
_:2380627776832 prefix:ed _:2380627818880.
11-
_:2380627776832 prefix:el _:2380627818944.
12-
_:2380627776832 prefix:k _:2380616502144.
13-
_:2380627776832 prefix:kk _:2380581804288.
14-
_:2380627776832 prefix:l _:2380627819072.
15-
_:2380627815424 prefix:k 3.
16-
_:2380627815424 prefix:kk "dil".
17-
_:2380627815424 prefix:kkk true.
18-
_:2380627818112 prefix:k "did".
19-
_:2380627818944 rdf:type rdf:Seq.
20-
_:2380627819072 rdf:_0 1.
21-
_:2380627819072 rdf:_1 2.
22-
_:2380627819072 rdf:_2 3.
23-
_:2380627819072 rdf:_3 "a".
24-
_:2380627819072 rdf:_4 "b".
25-
_:2380627819072 rdf:_5 "c".
26-
_:2380627819072 rdf:type rdf:Seq.
5+
_:2406051654912 prefix:dwbadid id:sdfsdf%20%22%20%3A%20%21%20%40%20%23%20.
6+
_:2406051654912 prefix:dwid id:33.
7+
_:2406051654912 prefix:ed _:2406089670336.
8+
_:2406051654912 prefix:el _:2406089670400.
9+
_:2406051654912 prefix:k _:2406089764352.
10+
_:2406051654912 prefix:kk _:2406052732160.
11+
_:2406051654912 prefix:l _:2406089670528.
12+
_:2406052732160 prefix:k _:2406089530496.
13+
_:2406089530496 prefix:k "did".
14+
_:2406089670400 rdf:type rdf:Seq.
15+
_:2406089670528 rdf:_0 1.
16+
_:2406089670528 rdf:_1 2.
17+
_:2406089670528 rdf:_2 3.
18+
_:2406089670528 rdf:_3 "a".
19+
_:2406089670528 rdf:_4 "b".
20+
_:2406089670528 rdf:_5 "c".
21+
_:2406089670528 rdf:type rdf:Seq.
22+
_:2406089764352 rdf:_0 _:2406089764416.
23+
_:2406089764352 rdf:type rdf:Seq.
24+
_:2406089764416 prefix:k 3.
25+
_:2406089764416 prefix:kk "dil".
26+
_:2406089764416 prefix:kkk true.
2727
id:33 prefix:id 33.
28-
id:33 prefix:k "v".
28+
id:33 prefix:k "v".
29+
id:sdfsdf%20%22%20%3A%20%21%20%40%20%23%20 prefix:CamelCase "has nice key".
30+
id:sdfsdf%20%22%20%3A%20%21%20%40%20%23%20 prefix:horrible%20key%20%40%20%2A%24%20%22%20 "!@#$ sdf ".
31+
id:sdfsdf%20%22%20%3A%20%21%20%40%20%23%20 prefix:id "sdfsdf \" : ! @ # ".
32+
id:sdfsdf%20%22%20%3A%20%21%20%40%20%23%20 prefix:key_w_us "has nice key".
33+
id:sdfsdf%20%22%20%3A%20%21%20%40%20%23%20 prefix:refid id:sdfsdf%20%22%20%3A%20%21%20%40%20%23%20.

0 commit comments

Comments
 (0)