def __set__(self, obj, newvals): log.debug("SET with descriptor value %s of type %s" % (newvals, type(newvals))) if not isinstance(newvals, (list, tuple)): raise AttributeError("to set a rdfList you must pass in a " + "list (it can be a list of one)") try: oldvals = obj.__dict__[self.name] except KeyError: oldvals = [] obj.__dict__[self.name] = oldvals oldhead = obj.db.value(obj.resUri, self.pred) # This is a stack style where retrevial is oppisite of how # it starts out # newnode = RDF.nil # for value in newvals: # almostnewnode = newnode # newnode = BNode() # obj.db.add((newnode, RDF.first, value2object(value))) # obj.db.add((newnode, RDF.rest, almostnewnode)) if not newvals: newhead = RDF.nil else: newhead = BNode() newtail = newhead oldtail = None for value in newvals: if oldtail: obj.db.add((oldtail, RDF.rest, newtail)) obj.db.add((newtail, RDF.first, value2object(value))) oldtail = newtail newtail = BNode() obj.db.add((oldtail, RDF.rest, RDF.nil)) obj.db.set((obj.resUri, self.pred, newhead)) if oldhead: rdfSubject(oldhead)._remove(db=obj.db) obj.__dict__[self.name] = copy(newvals)
def __set__(self, obj, newvals): log.debug("SET with descriptor value %s of type %s" % (newvals, type(newvals))) if not isinstance(newvals, (list, tuple)): raise AttributeError("to set a rdfList you must pass in" + " a list (it can be a list of one)") seq = obj.db.value(obj.resUri, self.pred) if not seq: seq = BNode() obj.db.add((obj.resUri, self.pred, seq)) obj.db.add((seq, RDF.type, RDF.Seq)) for s, p, o in obj.db.triples((seq, None, None)): if p.startswith(RDF["_"]): obj.db.remove((s, p, o)) if isinstance(o, BNode) and o not in newvals: rdfSubject(o)._remove(db=obj.db) for i in range(len(newvals)): obj.db.add((seq, RDF["_%i" % (i + 1)], value2object(newvals[i]))) obj.__dict__[self.name] = copy(newvals)
def __set__(self, obj, newvals): log.debug("SET with descriptor value %s of type %s" % (newvals, type(newvals))) if not isinstance(newvals, (list, tuple)): raise AttributeError("to set a rdfList you must pass in" + " a list (it can be a list of one)") seq = obj.db.value(obj.resUri, self.pred) if not seq: seq = BNode() obj.db.add((obj.resUri, self.pred, seq)) obj.db.add((seq, RDF.type, RDF.Seq)) for s, p, o in obj.db.triples((seq, None, None)): if p.startswith(RDF['_']): obj.db.remove((s, p, o)) if isinstance(o, BNode) and o not in newvals: rdfSubject(o)._remove(db=obj.db) for i in range(len(newvals)): obj.db.add((seq, RDF['_%i' % (i + 1)], value2object(newvals[i]))) obj.__dict__[self.name] = copy(newvals)
def _deserialize(self, data): """ data has the pattern "<uri>" """ uri = data[1:-1] # We have to retrieve the type to rebuild the object attr = self.__dict__['field'] # Be careful when orig = None !!!!! orig = getattr(attr.model, attr.name) if None == orig: return rdfSubject(rdflib.term.URIRef(uri)) elif isinstance(orig, list): # rdfalchemy mapper gives me the solution rt = attr.model.__class__.__dict__[attr.name].range_type from rdfalchemy.orm import mapper alch_map = mapper() try: cls = alch_map[str(rt)] return cls(rdflib.term.URIRef(uri)) except: rdfSubject(rdflib.term.URIRef(uri)) else: return type(orig)(rdflib.term.URIRef(uri))