Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made ClosedNamespace (and _RDFNamespace) inherit from Namespace #551

Merged
merged 1 commit into from
Nov 28, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions rdflib/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ def __new__(cls, value):
rt = unicode.__new__(cls, value, 'utf-8')
return rt


@property
def title(self):
return URIRef(self + 'title')

def term(self, name):
# need to handle slices explicitly because of __getitem__ override
return URIRef(self + (name if isinstance(name, basestring) else ''))
Expand Down Expand Up @@ -148,27 +143,34 @@ def __repr__(self):



class ClosedNamespace(object):
class ClosedNamespace(Namespace):
"""
A namespace with a closed list of members

Trying to create terms not listen is an error
"""

def __init__(self, uri, terms):
self.uri = uri
self.__uris = {}
def __new__(cls, uri, terms):
rt = Namespace.__new__(cls, uri)

rt.__uris = {}
for t in terms:
self.__uris[t] = URIRef(self.uri + t)
rt.__uris[t] = URIRef(rt + t)

return rt

def term(self, name):
uri = self.__uris.get(name)
if uri is None:
raise Exception(
"term '%s' not in namespace '%s'" % (name, self.uri))
"term '%s' not in namespace '%s'" % (name, self))
else:
return uri

@property
def uri(self): # support legacy code from before ClosedNamespace extended unicode
return self

def __getitem__(self, key, default=None):
return self.term(key)

Expand All @@ -178,21 +180,18 @@ def __getattr__(self, name):
else:
return self.term(name)

def __str__(self):
return str(self.uri)

def __repr__(self):
return """rdf.namespace.ClosedNamespace('%s')""" % str(self.uri)
return """rdf.namespace.ClosedNamespace('%s')""" % self


class _RDFNamespace(ClosedNamespace):
"""
Closed namespace for RDF terms
"""
def __init__(self):
super(_RDFNamespace, self).__init__(
URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#"),
terms=[

def __new__(cls):
return ClosedNamespace.__new__(cls, "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
terms=[
# Syntax Names
"RDF", "Description", "ID", "about", "parseType",
"resource", "li", "nodeID", "datatype",
Expand All @@ -213,10 +212,11 @@ def __init__(self):
"XMLLiteral", "HTML", "langString"]
)


def term(self, name):
try:
i = int(name)
return URIRef("%s_%s" % (self.uri, i))
return URIRef("%s_%s" % (self, i))
except ValueError:
return super(_RDFNamespace, self).term(name)

Expand Down