# RDF resources

class Resource:

    def __init__(self, base, uri=""):
        self._uri = base + uri

    def __repr__(self):
        return "Resource(\"%s\")" % self._uri

    def __str__(self):
        return "<%s>" % self._uri

    def __hash__(self):
        return hash(self._uri)

    def __eq__(self, other):
        return isinstance(other, Resource) and self._uri == other._uri

    def __ne__(self, other):
        return not self.__eq__(other)

    def uri(self):
        return self._uri

class Prefix (Resource):

    def __getattr__(self, attr):
        res = Prefix(self._uri, attr)
        self.__dict__[attr] = res
        return res

    # %%% Is there a less brittle way of doing this?
    # For example, overriding the constructor instead.
    def __setattr__(self, attr, val):
        if attr != '_uri':
            self.__dict__[attr] = Prefix(self._uri, val)
        else:
            self.__dict__[attr] = val

# A resource type for 'blank' nodes, i.e., ones that aren't supposed t
# be globally addressable.

class Blank (Resource):
    _counter = 0
    
    def __init__(self):
        Resource.__init__(self, "blank:", str(Blank._counter))
        Blank._counter = Blank._counter + 1


# Finally, some common namespaces

fuschia = Prefix('http://www.squaremobius.net/fuschia/schema#')

rdf =  Prefix("rdf:") # Look it up!
rdfs = Prefix("rdfs:") # %%% Look it up!
