/usr/share/pyshared/autobahn/prefixmap.py is in python-autobahn 0.5.14-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | ###############################################################################
##
## Copyright 2011 Tavendo GmbH
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
###############################################################################
class PrefixMap:
"""
Provides a two-way mapping between CURIEs (Compact URI Expressions) and
full URIs. See http://www.w3.org/TR/curie/.
"""
def __init__(self):
self.index = {}
self.rindex = {}
## add a couple of well-know prefixes
##
#self.set("owl", "http://www.w3.org/2002/07/owl#")
#self.set("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
#self.set("rdfs", "http://www.w3.org/2000/01/rdf-schema#")
#self.set("rdfa", "http://www.w3.org/ns/rdfa#")
#self.set("xhv", "http://www.w3.org/1999/xhtml/vocab#")
#self.set("xml", "http://www.w3.org/XML/1998/namespace")
#self.set("xsd", "http://www.w3.org/2001/XMLSchema#")
def get(self, prefix):
"""
Returns the URI for the prefix or None if prefix has no mapped URI.
:param prefix: Prefix to map.
:type prefix: str
:returns: str -- Mapped URI for prefix or None.
"""
return self.index.get(prefix, None)
def set(self, prefix, uri):
"""
Set mapping of prefix to URI.
:param prefix: Prefix to be mapped.
:type prefix: str
:param uri: URI the prefix is to be mapped to.
:type uri: str
"""
self.index[prefix] = uri
self.rindex[uri] = prefix
def setDefault(self, uri):
"""
Set default URI mapping of empty prefix (prefix of length 0).
:param uri: URI the empty prefix to be mapped to (i.e. :label should map to uri:label).
:type str
"""
self.set("", uri)
def remove(self, prefix):
"""
Remove mapping of prefix to URI.
:param prefix: Prefix for which mapping should be removed.
:type str
"""
uri = index.get(index, None)
if uri:
del self.index[prefix]
del self.rindex[uri]
def resolve(self, curie):
"""
Resolve given CURIE to full URI.
:param curie: CURIE (i.e. "rdf:label").
:type curie: str
:returns: str -- Full URI for CURIE or None.
"""
i = curie.find(":")
if i > 0:
prefix = curie[:i]
if self.index.has_key(prefix):
return self.index[prefix] + curie[i+1:]
return None
def resolveOrPass(self, curieOrUri):
"""
Resolve given CURIE/URI and return string verbatim if cannot be resolved.
:param curieOrUri: CURIE or URI.
:type curieOrUri: str
:returns: str -- Full URI for CURIE or original string.
"""
u = self.resolve(curieOrUri)
if u:
return u
else:
return curieOrUri
def shrink(self, uri):
"""
Shrink given URI to CURIE. If no appropriate prefix mapping is available,
return original URI.
:param uri: URI to shrink.
:type uri: str
:returns str -- CURIE or original URI.
"""
for i in xrange(len(uri), 1, -1):
u = uri[:i]
p = self.rindex.get(u, None)
if p:
return p + ":" + uri[i:]
return uri
if __name__ == '__main__':
m = PrefixMap()
print m.resolve("http://www.w3.org/1999/02/22-rdf-syntax-ns#label")
print m.resolve("rdf:label")
print m.resolve("foobar:label")
print m.shrink("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
print m.shrink("http://www.w3.org/1999/02/22-rdf-syntax-ns#label")
print m.shrink("http://foobar.org#label")
|