forked from linkeddata/swap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeprefixizer.py
56 lines (47 loc) · 1.64 KB
/
deprefixizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""deprefixizer
When we are done with this, it will not have any prefixes
"""
import re
import uripath
def absolutize(uri, baseURI):
return uripath.join(baseURI, uri)
def deprefixize(inTokens, baseURI, prefixMap, qName, uri, fn=None):
"""remove @keywords designations
"""
prefixMode = 0
for token in inTokens:
if token[0:1] == '#':
continue
if prefixMode == 0:
if token == '@prefix':
prefixMode = 1
else:
if qName.match(token):
n = token.find(':')
prefix = token[:n]
if prefix == '_' and '_' not in prefixMap:
yield token
else:
yield '<' + prefixMap[prefix] + token[n+1:] + '>'
elif uri.match(token):
yield '<' + absolutize(token[1:-1], baseURI) + '>'
else:
yield token
elif prefixMode == 1:
if not qName.match(token):
raise ValueError('Expected: qName')
prefixMode = token[0:token.find(':')]
elif prefixMode == 2:
if token != '.':
raise ValueError('Expected: "." ')
prefixMode = 0
else:
if not uri.match(token):
raise ValueError('Expected: uri')
prefixMap[prefixMode] = absolutize(token[1:-1], baseURI)
if fn:
fn(prefixMode, absolutize(token[1:-1], baseURI))
prefixMode = 2
#if __name__ == '__main__':
# import sys
# dekeywordize(sys.stdin, sys.stdout)