Example #1
0
 def create_from_graph(cls, uri, graph, **kwds):
     #----------------------------------------------
     """
 We can't call the 'create_from_graph()' for the appropriate
 class as leads to recursion...
 """
     self = None
     if graph.contains(rdf.Statement(uri, RDF.type, Interval.metaclass)):
         self = Interval(uri, None, **kwds)
     elif graph.contains(rdf.Statement(uri, RDF.type, Instant.metaclass)):
         self = Instant(uri, None, **kwds)
     if self is not None:
         self.add_metadata(graph)
         self.graph = graph
     return self
Example #2
0
  def add_metadata(self, graph):
  #-----------------------------
    """
    Set attributes from RDF triples in a graph.

    :param graph: A graph of RDF statements.
    :type graph: :class:`~biosignalml.rdf.Graph`
    """
    if self.metaclass is not None and graph.contains(rdf.Statement(self.uri, rdf.RDF.type, self.metaclass)):
      for stmt in graph.get_statements(rdf.Statement(self.uri, None, None)):
        for metaclass in [getattr(cls, 'metaclass', None)
                            for cls in self.__class__.__mro__ if cls != object]:
          s, attr, v, fn = self.rdfmap.metadata(metaclass, stmt)
          #logging.debug("%s: %s = '%s'", self.uri, attr, v)  ###
          if attr is not None:
            self._assign(attr, v, fn)
            break
Example #3
0
 def metadata_as_stream(self):
 #----------------------------
   '''
   Return a stream of RDF statements about ourselves.
   '''
   if self.metaclass:
     yield rdf.Statement(self.node, rdf.RDF.type, self.metaclass)
     for s in self.rdfmap.statement_stream(self): yield s
Example #4
0
 def get_resource_as_graph(self, uri, rtype, graph_uri=None):
 #-----------------------------------------------------------
   if graph_uri is None:
     graph_uri = uri
     ### Following can give an error from Virtuoso...
     text = self._sparqlstore.construct('<%(uri)s> ?p ?o',
             '''graph <%(pgraph)s> { ?g a <%(gtype)s> MINUS { [] prv:precededBy ?g }}
                graph ?g { <%(uri)s> a <%(rtype)s> ; ?p ?o }''',
             params=dict(pgraph=self._provenance_uri, gtype=self._graphtype, uri=uri, rtype=rtype),
             prefixes=dict(prv=PRV.prefix), format=rdf.Format.RDFXML)
   else:
     text = self._sparqlstore.construct('<%(uri)s> ?p ?o',
             '<%(uri)s> a <%(rtype)s> ; ?p ?o',
             params=dict(uri=uri, rtype=rtype),
             graph=graph_uri, format=rdf.Format.RDFXML)
   resource = rdf.Graph.create_from_string(graph_uri, text, rdf.Format.RDFXML)
   return resource if resource.contains(rdf.Statement(uri, RDF.type, rtype)) else None
Example #5
0
class MetaData(tornado.web.RequestHandler):
    #==========================================

    SUPPORTED_METHODS = ("GET", "HEAD", "PUT", "POST")  ##, "DELETE")

    def check_xsrf_cookie(self):
        #---------------------------
        """Don't check XSRF token for POSTs."""
        pass

    @user.capable(user.ACTION_VIEW)
    def get(self, **kwds):
        #----------------------
        if hasattr(self, 'full_uri'): name = self.full_uri
        else:
            name = self.request.uri.split('/',
                                          3)[3]  # Starts with '/frontend/rdf/'
        # If the resource is a named graph then do we return the graph as RDF?
        repo = options.repository
        uri = name.split('#', 1)[0].split('?', 1)[0]
        graph_uri = repo.get_resource_graph_uri(uri)
        if graph_uri is None:
            if uri == '': graph_uri = repo.uri
            elif repo.has_provenance(uri):
                graph_uri = uri
                uri = ''
        accept = resource.parse_accept(self.request.headers)
        if 'text/turtle' in accept or 'application/x-turtle' in accept:
            format = rdf.Format.TURTLE
        elif 'application/json' in accept:
            format = rdf.Format.JSON
        else:
            format = rdf.Format.RDFXML
        ## 415 Unsupported Media Type if accept is not */* nor something we can serve...
        self.set_header('Vary',
                        'Accept')  # Let caches know we've used Accept header
        self.set_header('Content-Type', rdf.Format.mimetype(format))
        self.write(
            options.repository.describe(uri, graph=graph_uri, format=format))

    @user.capable(user.ACTION_EXTEND)
    def put(self, **kwds):
        #---------------------
        if not hasattr(self, 'full_uri'):
            self.set_status(405)  # Method not allowed
            return
        rec_uri = self.full_uri.split('#', 1)[0].split('?', 1)[0]
        ## check uri doesn't exist?? Or authorisation to overwrite??
        try:
            g = rdf.Graph.create_from_string(
                rec_uri, self.request.body,
                self.request.headers.get('Content-Type', rdf.Format.RDFXML))
        except Exception, msg:
            logging.error('Cannot create RDF graph -- syntax errors? %s', msg)
            self.set_status(400)
            return
        if not g.contains(rdf.Statement(rec_uri, rdf.RDF.type,
                                        BSML.Recording)):
            logging.error("Metadata doesn't describe a bsml:Recording")
            self.set_status(400)
            return

        format = str(g.get_object(rec_uri, rdf.DCT.format))
        if format is None or format == formats.BSMLRecording.MIMETYPE:
            RecordingClass = formats.HDF5Recording
            g.set_subject_property(rec_uri, rdf.DCT.format,
                                   formats.HDF5Recording.MIMETYPE)
        else:
            RecordingClass = formats.CLASSES.get(format)
            if RecordingClass is None:
                logging.error(
                    "Repository doesn't support requested Recording class")
                self.set_status(400)
                return
        rec = RecordingClass.create_from_graph(rec_uri, g)
        rec.close()

        # Actual file gets assigned a name and created when data is first streamed to it.
        # Don't set hdf5 metadata block -- this is for export_hdf5 (see below).

        graph_uri = options.repository.add_recording_graph(
            rec_uri, g.serialise(), self.user)
        self.set_status(201)  # Created
        self.set_header('Location', str(graph_uri))