def get_service(self):
     id = self.marmotta.ldp("service")
     response = requests.post(self.marmotta.sparql.select, data=self.sparql.service.ask(id), headers={"Accept":"application/sparql-results+json", "Content-Type":"application/sparql-select"})
     if response.status_code is not 200:
         raise DBError()
     found = JSONResult(response.json()).askAnswer
     if found:
         response = requests.post(self.marmotta.sparql.select, data=self.sparql.service.select(id), headers={"Accept":"application/sparql-results+json", "Content-Type":"application/sparql-select"})
         if response.status_code is not 200:
             raise DBError()
         ds =self.sparql.result_to_dataset(JSONResult(response.json()))
         s_obj = self.RDA.graph_to_object(ds.graph(id))
         return s_obj.pop()
     else:
         return Service(**{
             "providesCollectionPids": False,
             "collectionPidProviderType": False,
             "enforcesAccess": False,
             "supportsPagination": False,
             "asynchronousActions": False,
             "ruleBasedGeneration": False,
             "maxExpansionDepth": -1,
             "providesVersioning": False,
             "supportedCollectionOperations": [],
             "supportedModelTypes": []
         })
 def find(self, ids, type):
     if not isinstance(ids, list):
         ids = [ids]
     for id in ids:
         if not isinstance(id, URIRef):
             raise DBError()
     if not isinstance(type, URIRef):
         raise DBError()
     query = self.queries.find(ids, type)
     return self.request(query, self.server.select)
    def set_member(self, c_id, m_obj):
        if isinstance(m_obj, Model):
            m_obj = [m_obj]
        elif not isinstance(m_obj, list):
            raise ParseError()

        c_ldp_id = self.marmotta.ldp(encoder.encode(c_id))
        collection = self.get_collection(c_id).pop() # 404 if collection not found

        if len(set([m.id for m in m_obj])) is not len(m_obj):
            raise ForbiddenError()
        if not collection.capabilities.membershipIsMutable:
            raise ForbiddenError()
        if collection.capabilities.restrictedToType:
            for m in m_obj:
                if not(hasattr(m,"datatype") and m.datatype in collection.capabilities.restrictedToType):
                    raise ForbiddenError()
        if collection.capabilities.maxLength >= 0:
            size = self.sparql.size(c_ldp_id).bindings.pop().get(Variable('size'))
            if int(size) > collection.capabilities.maxLength-len(m_obj):
                raise ForbiddenError()#"Operation forbidden. Collection of maximum size {} is full.".format(collection.capabilities.maxLength))

        ds = Dataset()
        ldp = ds.graph(identifier=LDP.ns)
        for m in m_obj:
            m_id = self.marmotta.ldp(encoder.encode(c_id)+"/member/"+encoder.encode(m.id))
            member = ds.graph(identifier=m_id)
            member += self.RDA.object_to_graph(member.identifier,m)
            ldp += LDP.add_contains(c_ldp_id+"/member",m_id,False)
        res = self.sparql.insert(ds)
        if res.status_code is not 200:
            raise DBError()
        return m_obj
 def select(self, ids):
     if not isinstance(ids, list):
         ids = [ids]
     for id in ids:
         if not isinstance(id, URIRef):
             raise DBError()
     query = self.queries.select(ids)
     return self.request(query, self.server.select)
Beispiel #5
0
 def __load_json__(self, filename):
     try:
         with open(filename) as filecontent:
             return load(filecontent)
     except FileNotFoundError:
         raise NotFoundError()
     except:
         raise DBError()
Beispiel #6
0
 def del_collection(self, c_id):
     try:
         filename = join(self.d_data, c_id.replace('/', '∕'))
         rmtree(filename)
         return True
     except FileNotFoundError:
         raise NotFoundError()
     except:
         raise DBError()
Beispiel #7
0
 def __write_json__(self, filename, object):
     try:
         os.makedirs(os.path.dirname(filename), exist_ok=True)
         with open(filename, "w") as file:
             dump(object, file)
     except FileNotFoundError:
         raise NotFoundError()
     except:
         raise DBError()
Beispiel #8
0
 def del_member(self, c_id, m_id):
     try:
         filename = join(self.d_data, c_id.replace('/', '∕'),
                         m_id.replace('/', '∕') + '.json')
         remove(filename)
         return True
     except FileNotFoundError:
         raise NotFoundError()
     except:
         raise DBError()
 def set_service(self, s_obj):
     ds = Dataset()
     service = ds.graph(identifier=self.marmotta.ldp("service"))
     service += self.RDA.object_to_graph(service.identifier, s_obj)
     ldp = ds.graph(identifier=LDP.ns)
     ldp += LDP.add_contains(self.marmotta.ldp(),service.identifier,False)
     response = self.sparql.insert(ds)
     if response.status_code is 200:
         return s_obj
     else:
         raise DBError()
 def set_collection(self, c_obj, over_write=False):
     if isinstance(c_obj, Model):
         c_obj = [c_obj]
     elif not isinstance(c_obj, list):
         raise ParseError()
     # create LD collection and declare as ldp:BasicContainer
     ds = Dataset()
     ldp = ds.graph(identifier=LDP.ns)
     for c in c_obj:
         c_id = encoder.encode(c.id)
         collection = ds.graph(identifier=self.marmotta.ldp(c_id))
         collection += self.RDA.object_to_graph(collection.identifier, c)
         ldp += LDP.add_contains(self.marmotta.ldp(), collection.identifier)
         member = ds.graph(identifier=self.marmotta.ldp(c_id+'/member'))
         ldp += LDP.add_contains(collection.identifier, member.identifier)
     ins = self.sparql.insert(ds)
     if ins.status_code is 200:
         return c_obj
     else:
         raise DBError()
 def request(self, query, server):
     if server is self.server.select:
         response = post(server,
                         data=query,
                         headers={
                             "Accept": "application/sparql-results+json",
                             "Content-Type": "application/sparql-select"
                         })
         # print("SPARQL ERROR CODE: ", response.status_code)
     else:
         response = post(server,
                         data=query,
                         headers={
                             "Content-Type":
                             "application/sparql-update; charset=utf-8"
                         })
     if response.status_code is not 200:
         raise DBError()
     else:
         json = {'head': {'vars': []}, 'results': {'bindings': []}}
         try:
             json = response.json()
         finally:
             return SPARQLSet(json, response.status_code)
 def size(self, id):
     if not isinstance(id, URIRef):
         raise DBError()
     query = self.queries.size(id)
     return self.request(query, self.server.select)
 def ask(self, id, type):
     if not isinstance(id, URIRef):
         raise DBError()
     query = self.queries.ask(id, type)
     return self.request(query, self.server.select)
 def delete(self, id):
     if not isinstance(id, URIRef):
         raise DBError()
     query = self.queries.delete(id)
     return self.request(query, self.server.update)
 def insert(self, dataset):
     if not isinstance(dataset, Dataset):
         raise DBError()
     query = self.queries.insert(dataset)
     return self.request(query, self.server.update)
 def list(self, binds=[], filters=[]):
     if not isinstance(binds, list) and not isinstance(filters, list):
         raise DBError()
     query = self.queries.list(binds, filters)
     return self.request(query, self.server.select)