瀏覽代碼

Separate LPP-NR and LDP-RS modules.

Stefano Cossu 7 年之前
父節點
當前提交
511acaa7b4
共有 4 個文件被更改,包括 132 次插入195 次删除
  1. 4 2
      lakesuperior/endpoints/ldp.py
  2. 13 0
      lakesuperior/model/ldp_nr.py
  3. 115 0
      lakesuperior/model/ldp_rs.py
  4. 0 193
      lakesuperior/model/ldpr.py

+ 4 - 2
lakesuperior/endpoints/ldp.py

@@ -1,7 +1,9 @@
 from flask import Blueprint, request
 
-from lakesuperior.model.ldpr import Ldpr, Ldpc, LdpNr, \
-        InvalidResourceError, ResourceNotExistsError
+from lakesuperior.model.ldpr import InvalidResourceError, \
+        ResourceNotExistsError
+from lakesuperior.model.ldp_rs import Ldpc, LdpRs
+from lakesuperior.model.ldp_nr import LdpNr
 
 
 # Blueprint for LDP REST API. This is what is usually found under `/rest/` in

+ 13 - 0
lakesuperior/model/ldp_nr.py

@@ -0,0 +1,13 @@
+from lakesuperior.core.namespaces import ns_collection as nsc
+from lakesuperior.model.ldpr import Ldpr, transactional, must_exist
+
+class LdpNr(Ldpr):
+    '''LDP-NR (Non-RDF Source).
+
+    Definition: https://www.w3.org/TR/ldp/#ldpnr
+    '''
+    pass
+
+
+
+

+ 115 - 0
lakesuperior/model/ldp_rs.py

@@ -0,0 +1,115 @@
+from lakesuperior.core.namespaces import ns_collection as nsc
+from lakesuperior.model.ldpr import Ldpr, transactional, must_exist
+from lakesuperior.util.translator import Translator
+
+class LdpRs(Ldpr):
+    '''LDP-RS (LDP RDF source).
+
+    Definition: https://www.w3.org/TR/ldp/#ldprs
+    '''
+    base_types = {
+        nsc['ldp'].RDFSource
+    }
+
+    std_headers = {
+        'Accept-Post' : {
+            'text/turtle',
+            'text/rdf+n3',
+            'text/n3',
+            'application/rdf+xml',
+            'application/n-triples',
+            'application/ld+json',
+            'multipart/form-data',
+            'application/sparql-update',
+        },
+        'Accept-Patch' : {
+            'application/sparql-update',
+        },
+    }
+
+
+    def head(self):
+        '''
+        Return values for the headers.
+        '''
+        headers = self.rdfly.headers
+
+        for t in self.ldp_types:
+            headers['Link'].append('{};rel="type"'.format(t.identifier.n3()))
+
+        return headers
+
+
+    def get(self, inbound=False, children=True, srv_mgd=True):
+        '''
+        https://www.w3.org/TR/ldp/#ldpr-HTTP_GET
+        '''
+        im_rsrc = self.rdfly.out_rsrc(inbound)
+        if not len(im_rsrc.graph):
+            raise ResourceNotExistsError()
+
+        return Translator.globalize_rsrc(im_rsrc)
+
+
+    @transactional
+    @must_exist
+    def patch(self, data):
+        '''
+        https://www.w3.org/TR/ldp/#ldpr-HTTP_PATCH
+        '''
+        ts = Literal(arrow.utcnow(), datatype=XSD.dateTime)
+
+        self.rdfly.patch_rsrc(self.urn, data, ts)
+
+        self.rdfly.ds.add((self.urn, nsc['fcrepo'].lastUpdated, ts))
+        self.rdfly.ds.add((self.urn, nsc['fcrepo'].lastUpdatedBy,
+                Literal('BypassAdmin')))
+
+
+
+class Ldpc(LdpRs):
+    '''LDPC (LDP Container).'''
+
+    def __init__(self, uuid):
+        super().__init__(uuid)
+        self.base_types.update({
+            nsc['ldp'].Container,
+        })
+
+
+
+
+class LdpBc(Ldpc):
+    '''LDP-BC (LDP Basic Container).'''
+    def __init__(self, uuid):
+        super().__init__(uuid)
+        self.base_types.update({
+            nsc['ldp'].BasicContainer,
+        })
+
+
+
+class LdpDc(Ldpc):
+    '''LDP-DC (LDP Direct Container).'''
+
+    def __init__(self, uuid):
+        super().__init__(uuid)
+        self.base_types.update({
+            nsc['ldp'].DirectContainer,
+        })
+
+
+
+class LdpIc(Ldpc):
+    '''LDP-IC (LDP Indirect Container).'''
+
+    def __init__(self, uuid):
+        super().__init__(uuid)
+        self.base_types.update({
+            nsc['ldp'].IndirectContainer,
+        })
+
+
+
+
+

+ 0 - 193
lakesuperior/model/ldpr.py

@@ -363,29 +363,6 @@ class Ldpr(metaclass=ABCMeta):
 
     ## LDP METHODS ##
 
-    def head(self):
-        '''
-        Return values for the headers.
-        '''
-        headers = self.rdfly.headers
-
-        for t in self.ldp_types:
-            headers['Link'].append('{};rel="type"'.format(t.identifier.n3()))
-
-        return headers
-
-
-    def get(self, inbound=False):
-        '''
-        https://www.w3.org/TR/ldp/#ldpr-HTTP_GET
-        '''
-        im_rsrc = self.rdfly.out_rsrc(inbound)
-        if not len(im_rsrc.graph):
-            raise ResourceNotExistsError()
-
-        return Translator.globalize_rsrc(im_rsrc)
-
-
     @transactional
     def post(self, data, format='text/turtle'):
         '''
@@ -496,33 +473,6 @@ class Ldpr(metaclass=ABCMeta):
         return None
 
 
-    #def _find_first_ancestor(self):
-    #    '''
-    #    Find by logic and triplestore queries the first existing resource by
-    #    traversing a path hierarchy upwards.
-
-    #    @return rdflib.term.URIRef
-    #    '''
-    #    path_components = self.uuid.split('/')
-
-    #    if len(path_components) < 2:
-    #        return None
-
-    #    # Build search list, e.g. for a/b/c/d/e would be a/b/c/d, a/b/c, a/b, a
-    #    search_order = accumulate(
-    #        reversed(search_order)[1:],
-    #        func=lambda x,y : x + '/' + y
-    #    )
-
-    #    for cmp in search_order:
-    #        if self.rdfly.ask_rsrc_exists(ns['fcres'].cmp):
-    #            return urn
-    #        else:
-    #            self._create_pairtree_node(cmp)
-
-    #    return None
-
-
     def _create_pairtree(self, uri, child_uri):
         '''
         Create a pairtree node with a containment statement.
@@ -550,146 +500,3 @@ class Ldpr(metaclass=ABCMeta):
 
 
 
-    #def _splice_in(self, parent):
-    #    '''
-    #    Insert the new resource between a container and its child.
-
-    #    If a resource is inserted between two resources that already have a
-    #    containment relationship, e.g. inserting `<a/b>` where
-    #    `<a> ldp:contains <a/b/c>` exists, the existing containment
-    #    relationship must be broken in order to insert the resource in between.
-
-    #    NOTE: This method only removes the containment relationship between the
-    #    old parent (`<a>` in the example above) and old child (`<a/b/c>`) and
-    #    sets a new one between the new parent and child (`<a/b>` and
-    #    `<a/b/c>`). The relationship between `<a>` and `<a/b>` is set
-    #    separately.
-
-    #    @param rdflib.resource.Resource parent The parent resource. This
-    #    includes the root node.
-    #    '''
-    #    # For some reason, initBindings (which adds a VALUES statement in the
-    #    # query) does not work **just for `?new`**. `BIND` is necessary along
-    #    # with a format() function.
-    #    q = '''
-    #    SELECT ?child {{
-    #      ?p ldp:contains ?child .
-    #      FILTER ( ?child != <{}> ) .
-    #      FILTER STRSTARTS(str(?child), "{}") .
-    #    }}
-    #    LIMIT 1
-    #    '''.format(self.urn)
-    #    qres = self.rsrc.graph.query(q, initBindings={'p' : parent.identifier})
-
-    #    if not qres:
-    #        return
-
-    #    child_urn = qres.next()[0]
-
-    #    parent.remove(nsc['ldp'].contains, child_urn)
-    #    self.src.add(nsc['ldp'].contains, child_urn)
-
-
-    #def find_lost_children(self):
-    #    '''
-    #    If the parent was created after its children and has to find them!
-    #    '''
-    #    q = '''
-    #    SELECT ?child {
-
-
-
-
-class LdpRs(Ldpr):
-    '''LDP-RS (LDP RDF source).
-
-    Definition: https://www.w3.org/TR/ldp/#ldprs
-    '''
-    base_types = {
-        nsc['ldp'].RDFSource
-    }
-
-    std_headers = {
-        'Accept-Post' : {
-            'text/turtle',
-            'text/rdf+n3',
-            'text/n3',
-            'application/rdf+xml',
-            'application/n-triples',
-            'application/ld+json',
-            'multipart/form-data',
-            'application/sparql-update',
-        },
-        'Accept-Patch' : {
-            'application/sparql-update',
-        },
-    }
-
-    @transactional
-    @must_exist
-    def patch(self, data):
-        '''
-        https://www.w3.org/TR/ldp/#ldpr-HTTP_PATCH
-        '''
-        ts = Literal(arrow.utcnow(), datatype=XSD.dateTime)
-
-        self.rdfly.patch_rsrc(self.urn, data, ts)
-
-        self.rdfly.ds.add((self.urn, nsc['fcrepo'].lastUpdated, ts))
-        self.rdfly.ds.add((self.urn, nsc['fcrepo'].lastUpdatedBy,
-                Literal('BypassAdmin')))
-
-
-class LdpNr(LdpRs):
-    '''LDP-NR (Non-RDF Source).
-
-    Definition: https://www.w3.org/TR/ldp/#ldpnr
-    '''
-    pass
-
-
-
-class Ldpc(LdpRs):
-    '''LDPC (LDP Container).'''
-
-    def __init__(self, uuid):
-        super().__init__(uuid)
-        self.base_types.update({
-            nsc['ldp'].Container,
-        })
-
-
-
-
-class LdpBc(Ldpc):
-    '''LDP-BC (LDP Basic Container).'''
-    def __init__(self, uuid):
-        super().__init__(uuid)
-        self.base_types.update({
-            nsc['ldp'].BasicContainer,
-        })
-
-
-
-class LdpDc(Ldpc):
-    '''LDP-DC (LDP Direct Container).'''
-
-    def __init__(self, uuid):
-        super().__init__(uuid)
-        self.base_types.update({
-            nsc['ldp'].DirectContainer,
-        })
-
-
-
-class LdpIc(Ldpc):
-    '''LDP-IC (LDP Indirect Container).'''
-
-    def __init__(self, uuid):
-        super().__init__(uuid)
-        self.base_types.update({
-            nsc['ldp'].IndirectContainer,
-        })
-
-
-