def add(identifier, annotation): # parse id (allow annotations of an ARK or part of an ARK) # push this off till next phase and require all assertions # to be at the object rather than file level pass # make sure id exists if not identity.exists(identifier): return False annotations = storage.get_or_create_file(identifier, "about.nt") # get_or_create annotations file from storage # hardcode this filename to about.ttl? # block out b.c. RDF lib was to sloooow # #annotations = storage.get_or_create_file(identifier, "about.n3") # # # instantiate a graph # g = rdflib.ConjunctiveGraph() # # if annotations: # # pull existing annotations into in-memory rdf graph # g.parse(StringIO.StringIO(annotations), format="n3") # # # assume client sends list of strings/tuples (KLUDGE) # for assertion in annotation: # ns = rdflib.Namespace(assertion[1][1]) # ns_label = assertion[1][0] # triple = (rdflib.URIRef(assertion[0]), # ns[assertion[1][2]], # rdflib.Literal(assertion[2])) # # # attach annotation to annotations file # g.bind(ns_label, ns) # g.add(triple) # # # write annotation to central datastore # # push this off till future iteration # __rdfstore_write(triple, ns_label, ns) # temp filx until we get rdf store working better if not annotations: annotations = "" for assertion in annotation: trip = '<%s> <%s> "%s" .\n' % (assertion[0], assertion[1], assertion[2]) RDFMask().create_mask(assertion[0], os.path.basename(assertion[1]), assertion[2]) annotations += trip # validate annotations (don't write garbage) # maybe rdflib is doing this for us in g.parse & g.add? pass # write annotations file using storage service # this should ask storage to up the repo version # storage.put_file(identifier, "about.n3", g.serialize(format="n3")) storage.put_file(identifier, "about.nt", annotations) return True
def new_annotation_file(p): # create a new set of annotations to store in about.nt trips = RDFMask.objects.filter(phile=p) annotations = "" for t in trips: x = '<%s> <%s> "%s" .\n' % (t.phile.identifier, t.triple_predicate, t.triple_object) annotations += x # overwrite existing about.nt with new triples # put_file, will update git storage.put_file(trips[0].phile.identifier, "about.nt", annotations) #repo = git.Repo(trips[0].phile.path) #storage.commit_version(repo.index, "updated metadata " + key + "=>" + val) return True