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)
"none": ["No license attached", ""] } license_list = { "photo": (uri, "Photo license:"), "meta": ("", "RDF Metadata license:") } for j in license_list: print license_list[j][1] count = 0 keys = licenses.keys() keys.sort() for i in keys: count += 1 print "%s: %s" % (i, licenses[i][0]) licenses[i].append(count) license = raw_input("[by-nc-sa] > ") if not license: license = "by-nc-sa" if license != "none": mem_model.add((URIRef(license_list[j][0]), URIRef("http://web.resource.org/cc/rights"), URIRef(licenses[license][1]))) mem_model.prefix_mapping("foaf", "http://xmlns.com/foaf/0.1/") mem_model.prefix_mapping("dc", "http://purl.org/dc/elements/1.1/") mem_model.prefix_mapping("cc", "http://web.resource.org/cc/") mem_model.prefix_mapping("cyc", "http://opencyc.sourceforge.net/daml/cyc.daml#") mem_model.prefix_mapping("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#") mem_model.save("file.rdf") print mem_model.serialize()
"by-nc-sa":["Attribution-NonCommercial-ShareAlike 2.0","http://creativecommons.org/licenses/by-nc-sa/2.0/"], "by-sa":["Attribution-ShareAlike 2.0","http://creativecommons.org/licenses/by-sa/2.0/"], "nd":["NoDerivs 1.0","http://creativecommons.org/licenses/nd/1.0/"], "nd-nc":["NoDerivs-NonCommercial 1.0","http://creativecommons.org/licenses/nd-nc/1.0/"], "nc":["NonCommercial 1.0","http://creativecommons.org/licenses/nc/1.0/"], "nc-sa":["NonCommercial-ShareAlike 1.0","http://creativecommons.org/licenses/nc-sa/1.0/"], "sa":["ShareAlike 1.0","http://creativecommons.org/licenses/sa/1.0/"], "none":["No license attached",""] } license_list = { "photo": (uri, "Photo license:"), "meta": ("", "RDF Metadata license:") } for j in license_list: print license_list[j][1] count = 0 keys = licenses.keys() keys.sort() for i in keys: count+=1 print "%s: %s"%(i, licenses[i][0]) licenses[i].append(count) license = raw_input("[by-nc-sa] > ") if not license: license = "by-nc-sa" if license != "none": mem_model.add((URIRef(license_list[j][0]), URIRef("http://web.resource.org/cc/rights"), URIRef(licenses[license][1]))) mem_model.prefix_mapping("foaf", "http://xmlns.com/foaf/0.1/") mem_model.prefix_mapping("dc", "http://purl.org/dc/elements/1.1/") mem_model.prefix_mapping("cc", "http://web.resource.org/cc/") mem_model.prefix_mapping("cyc", "http://opencyc.sourceforge.net/daml/cyc.daml#") mem_model.prefix_mapping("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#") mem_model.save("file.rdf") print mem_model.serialize()
elif type == 'application/rdf+xml': content_type = 'application/rdf+xml' out_format = 'rdfxml' elif type in ['text/*', 'text/html']: content_type = 'text/html' out_format = 'html' elif type == 'text/plain': content_type = 'text/plain' out_format = 'ntriples' else: content_type = 'text/html' out_format = 'html' print 'Content-Type: %s\r' % content_type print '\r' if out_format != 'html': print convert(graph2.serialize(format='xml'), 'rdfxml', out_format) else: print 'The resource identified by this URI is described' print 'by the following RDF graph:' print '<p>' print '<code>' text = convert(graph2.serialize(format='xml'), 'rdfxml', 'ntriples') text = '&'.join(text.split('&')) text = '<'.join(text.split('<')) print text print '</code>'
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. store.save("foaf.rdf") # Let's show off the serializers print "RDF Serializations:" # Serialize as XML print "--- start: rdf-xml ---" print store.serialize() print "--- end: rdf-xml ---\n" # Serialize as NTriples print "--- start: ntriples ---" print store.serialize(format="nt") print "--- end: ntriples ---\n"