"""PlexRDF: An RDF API that goes beyond what's specified in RDF 1.0. Modified a bit by SBP""" import urllib, string __version__ = '$Revision$' __cvsid__ = '$Id$' __author__ = 'Aaron Swartz , mods: Sean B. Palmer' class Namespace: """A class so namespaced URIs can be abbreviated (like dc.subject).""" def __init__(self, prefix): self.prefix = prefix def __getattr__(self,name): return self.prefix + name class Node: def __init__(self, value, uni=None): self.nodeList = {} if uni: self.universal = uni if type(value) is type(u''): value = literalToURI(value) if value and type(value) != type(''): raise "Must be a u/literal", value if value: self.uri = value def literalToURI(value): """Converts a literal into a data: URI.""" return 'data:,' + str(urllib.quote(value)) def URIToLiteral(uri): """Converts a data: URI into a literal.""" if uri[0:6] != "data:,": raise "Only deals with 'data' URIs" return urllib.unquote(uri[6:]) class Triple(Node): """An RDF triple, the basic unit of association.""" def __init__(self, store, s, p, o): # Terms must be nodes self.subject, self.predicate, self.object = s, p, o def __eq__(self, other): if not isinstance(other, Triple): return self is other if (self.subject == other.subject and self.predicate == other.predicate and self.object == other.object): return 1 else: return 0 class Store: """A generic RDF store.""" def __init__(self): self.tripleList = [] # triples in this store def triple(self, s, p, o): result = Triple(self, s, p, o) self.tripleList.append(result) return result def query(self, s, p, o): if s: s = Node(s) if p: p = Node(p) if o: o = Node(o) results = [] for t in self.tripleList: if ((not s or t.subject is s) and (not p or t.predicate is p) and (not o or t.object is o)): results.append(t) return results