Example #1
0
class rdfStore:
    """
    Provides RDF parsing and output functions for CC license and work
    definitions.
    """
    
    def __init__(self):
        # initialize the TripleStore for managing RDF
        self.store = TripleStore()
        
    def parse(self, rdf):
        """
        Parse the given String, rdf, into it's component triples.
        """

        self.store.parse( StringInputSource (rdf) )

    def subjects(self):
        """A generator which successivly returns each subject contained in
        the store, wrapped in an instance of rdfDict."""

        for subject in self.store.subjects():
            yield rdfDict(subject, store=self.store)
        
    def output(self):
        """
        Return a string containing the RDF representation of the
        licenses and works."""

        if self.store is not None:
            rdf = cStringIO.StringIO()
            self.store.serialize(stream=rdf)
            return rdf.getvalue()
        else:
            return ""

    __str__ = output

    def append(self, newItem):
        """
        Adds a new work or license to the RDF store.
        """

        # make sure the stores aren't the same
        if (newItem.store is not self.store):

            # add each triple from the newItem's store to this store
            for triple in newItem.store.triples():
                self.store.add(triple)
Example #2
0
    return people


if __name__ == "__main__":
    mem_model = TripleStore()
    wordlist = get_words()
    FOAF = Namespace("http://xmlns.com/foaf/0.1/")
    GEO = Namespace("http://www.w3.org/2003/01/geo/wgs84_pos#")
    RDF = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
    DC = Namespace("http://purl.org/dc/elements/1.1/")
    CYC = Namespace("http://opencyc.sourceforge.net/daml/cyc.daml#")
    print "Image Annotation Tool"
    print "Enter the URI of a photo to annotate:"
    uri = raw_input("> ")
    mem_model.add((URIRef(uri),
                   URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
                   URIRef("http://xmlns.com/foaf/0.1/Image")))
    print "Should I add a thumbnail for this image, by adding '.sized' before the extension?"
    thumbnail = raw_input("> ")
    if thumbnail:
        thumb = "%ssized.%s" % (uri[:-3], uri[-3:])
        mem_model.add((URIRef(uri), FOAF['thumbnail'], URIRef(thumb)))
    print "Enter a title for the photo:"
    title = raw_input("> ")
    mem_model.add(
        (URIRef(uri), URIRef("http://purl.org/dc/elements/1.1/title"),
         Literal(title)))
    print "Enter a description for the photo:"
    description = raw_input("> ")
    mem_model.add(
        (URIRef(uri), URIRef("http://purl.org/dc/elements/1.1/description"),
Example #3
0
                for i in available:
                    people[source][prop].append(i)
    return people

if __name__ == "__main__":  
    mem_model = TripleStore()
    wordlist = get_words()
    FOAF = Namespace("http://xmlns.com/foaf/0.1/")
    GEO = Namespace("http://www.w3.org/2003/01/geo/wgs84_pos#")
    RDF = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
    DC = Namespace("http://purl.org/dc/elements/1.1/")
    CYC = Namespace("http://opencyc.sourceforge.net/daml/cyc.daml#")
    print "Image Annotation Tool"
    print "Enter the URI of a photo to annotate:"
    uri = raw_input("> ")
    mem_model.add((URIRef(uri), URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), URIRef("http://xmlns.com/foaf/0.1/Image")))
    print "Should I add a thumbnail for this image, by adding '.sized' before the extension?"
    thumbnail = raw_input("> ")
    if thumbnail:
        thumb = "%ssized.%s"%(uri[:-3],uri[-3:])
        mem_model.add((URIRef(uri), FOAF['thumbnail'], URIRef(thumb)))
    print "Enter a title for the photo:"
    title = raw_input("> ")
    mem_model.add((URIRef(uri), URIRef("http://purl.org/dc/elements/1.1/title"), Literal(title)))
    print "Enter a description for the photo:"
    description = raw_input("> ")
    mem_model.add((URIRef(uri), URIRef("http://purl.org/dc/elements/1.1/description"), Literal(description)))
    while 1:
        print "Photo Creator?"
        person = raw_input("> ")
        people = get_people(person)
    try:
        bnode = graph.objects(graphNode, FOAF["primaryTopic"]).next()
    except StopIteration:
        print 'Content-Type: text/plain\r'
        print '\r'
        print 'no primary topic given'

    graph2 = TripleStore()

    for (s, p, o) in graph.triples((None, None, None)):
        if (s, p, o) == (graphNode, FOAF["primaryTopic"], bnode): continue

        if s == bnode: s = thisURI
        if o == bnode: o = thisURI

        graph2.add((s, p, o))

    http_accept = os.environ['HTTP_ACCEPT']
    accepts = []  # pairs (quality, type)

    for acc in http_accept.split(','):
        parts = acc.split(';')
        type = parts[0].strip().lower()

        for part in parts[1:]:
            eq = part.find('=')
            if eq >= 0 and part[:eq].strip() == 'q':
                quality = float(part[eq + 1:])
                break
            else:
                quality = 1
Example #5
0
# Create a namespace object for the Friend of a friend namespace.
FOAF = Namespace("http://xmlns.com/foaf/0.1/")

store = TripleStore()

store.prefix_mapping("dc", "http://http://purl.org/dc/elements/1.1/")
store.prefix_mapping("foaf", "http://xmlns.com/foaf/0.1/")
 
# Create an identifier to use as the subject for Donna.
donna = BNode()

from rdflib.constants import DATATYPE

# Add triples using store's add method.
store.add((donna, TYPE, FOAF["Person"]))
store.add((donna, FOAF["nick"], Literal("donna")))
store.add((donna, FOAF["name"], Literal("Donna Fales")))

# Iterate over triples in store and print them out.
print "--- printing raw triples ---"
for s, p, o in store:
    print s, p, o
    
# For each foaf:Person in the store print out its mbox property.
print "--- printing mboxes ---"
for person in store.subjects(TYPE, FOAF["Person"]):
    for mbox in store.objects(person, FOAF["mbox"]):
        print mbox

# Serialize the store as RDF/XML to the file foaf.rdf.
Example #6
0
class rdfDict:
    """
    Provides a dictionary-like wrapper around a set of RDF triples with a
    common subject.  In addition to the standard dictionary interface
    provides convenience methods for manipulating the triples.
    """
    def __init__(self, subject, rdfStore=None):
        """
        Creates a new rdfDict given the particular subject and an optional
        TripleStore (rdfStore).  If no TripleStore is provided, creates a
        new one.
        """

        # store the subject
        self.subject = subject

        # store or create the triple store
        if rdfStore is None:
            self.store = TripleStore()
        else:
            self.store = rdfStore

    def __str__(self):
        """
        Return the string representation of this instance using an algorithm
        inspired by the Dublic Core dumb-down method.
        """

	output_str = ''
        pairs = []
	type_item = None
        
        for key in self:
	    if (key.find("#type") != -1): 
	      type_item = key
	    else:
	      pairs.append((key, self.getAll(key)))

	output_str = str(self.getFirst(type_item)) + ": "

	for pair in pairs:
	   output_str = '%s %s: %s;' % (output_str,
                                        str(pair[0]),
                                        ", ".join([str(n) for n in pair[1]]) ) 

	return output_str

    def about(self):
        """
        Return the subject used to create the instance (usually equivalent
        to rdf:about.
        """
        return self.subject

    def __getvalues(self, key):
        """
        Returns a list of values for a particular key; coerces BNodes to
        rdfDicts and mangles Literals with language attributes (if available).
        """

        values = [ n for n in self.store.objects(subject=self.subject,
                                                 predicate=key)
                   ]

        result = []
        
        for value in values:
            if (isinstance(value, BNode)):
                # coerce to rdfDict
                result.append(rdfDict(value, self.store))
            else:
                result.append(value)

        return result
                            
    def __getitem__(self, key):
        """
        Return the object described in RDF with a subject of self.subject
        and a predicate of key.  If more than one match is found, raises
        an AmbiguousKeyError.
        """

        result = self.__getvalues(key)

        if (len(result) == 0):
            # no item was found, throw an exception
            raise KeyError()
        elif (len(result) > 1):
            # check if there is more than one option
            raise AmbiguousKeyError()
        else:
            # otherwise return object value
            return result[0]

    def getFirst(self, key):
        """
        Returns the first object having the predicate key.  If no match is
        found returns None.
        """

        if ( (self.subject, URIRef(key), None) in self.store):
            return self.__getvalues(key)[0]
        else:
            return None
        
    def getAll(self, key):
        """
        Returns a list of objects which have a predicate of key.  The list
        may be empty or contain only a single element.
        """

	return self.__getvalues(key)

    def __setitem__(self, key, value):
        """
        Adds an RDF triple of the values (self.subject, key, value); any
        objects with the same subject/predicate are replaced.
        """

        if (self.subject, key, none) in self.store:
            del self[key]

        self.store.add( (self.subject, key, value) )
        
    def add (self, key, value):
        """
        Adds an RDF triple consisting of the subject, key and value.
        """
        
        self.store.add( (self.subject, key, value) )
        
    def addAll (self, key, values):
        """
        Adds the list of objects in values with the same subject and predicate.
        """
        
        for value in values:
            self.add (key, value)
            
    def __len__(self):
        """
        Returns the number of predicate-object pairs associated with the
        subject self.subject.
        """

        return len(self.store)

    def __delitem__(self, key):
        """
        Removes all items with the given key.
        """

        if (self.subject, key, None) in self.store:
            self.store.remove( (self.subject, key, None) )
        else:
            raise KeyError()

    def remove (self, key, value):
        """
        Removes a specific key-value pair.
        """
        
        self.store.remove( (self.subject, key, value) )

    def __iter__(self):
        """
        Returns an iterator over the unique keys (predicates)
        for the given subject.
        """

        return iter(sets.Set(self.store.predicates(subject=self.subject)))

    iterkeys = __iter__

    def __contains__(self, key):
        """
        Returns true if the given key appears as a predicate of the subject.
        """

        return ( (self.subject, key, None) in self.store )