Beispiel #1
0
 def _get_manifest(self, id, part_id, file_uri, version = False):
     if not self.storeclient.exists(id):
         raise ObjectNotFoundException
     if not version:
         version = self._get_latest_part(id, part_id)
     part_name = "%s%s%s" % (part_id, self.prefix, version)
     m = Manifest(file_uri)
     if version >= 1:
         f = self.storeclient.get_stream(id, part_id, part_name,streamable=False)
         m.from_string(f.decode('utf-8'))
     return m
Beispiel #2
0
 def _reset(self, uri, manifest_uri):
     self.parts = set([])
     self.parts_objs = {}
     self.root = RDFobject(uri)
     self.types = self.root.types
     if self.root.uri:
         self.uri = self.root.uri
     if manifest_uri:
         self.root.add_triple("dcterms:requires", manifest_uri)
     self.manifest = Manifest(manifest_uri)
Beispiel #3
0
 def load_manifest(self, create_if_nonexistent=True):
     self.manifest = self.obj.getManifest()
     g = self.manifest.get_graph()
     if isinstance(g, ConjunctiveGraph):
         for s,p,o in g.triples(( None, NAMESPACES['foaf']['primaryTopic'], self.root.uri)):
             if s not in self.parts:
                 self.parts.add(s)
                 if s.startswith(self.obj.uri):
                     part_id = s[len(self.obj.uri)+1:]
                     r = Manifest(s)
                     r.from_string(self.obj.get_part(part_id))
                     self.parts_objs[s] = r
                 elif s.startswith("http://"):
                     r = Manifest(s)
                     r.from_url(s)
                     self.parts_objs[s] = r
Beispiel #4
0
class Entity(object):
    def __init__(self, uri=None, manifest_uri=None):
        self._reset(uri, manifest_uri)
        self.uh = URIHelper()
    
    def _reset(self, uri, manifest_uri):
        self.parts = set([])
        self.parts_objs = {}
        self.root = RDFobject(uri)
        self.types = self.root.types
        if self.root.uri:
            self.uri = self.root.uri
        if manifest_uri:
            self.root.add_triple("dcterms:requires", manifest_uri)
        self.manifest = Manifest(manifest_uri)
        
    def add_namespaces(self, ns_dict):
        """Convenience method for adding namespaces"""
        for prefix in ns_dict:
            self.add_namespace(prefix, ns_dict[prefix])
        
    def add_namespace(self, prefix, ns):
        self.uh.add_namespace(prefix, ns)
        self.root.add_namespace(prefix, ns)
        self.manifest.add_namespace(prefix, ns)
        for part_uri in self.parts:
            self.parts_objs[part_uri].add_namespace(prefix, ns)
        
    def del_namespace(self, prefix):
        self.root.del_namespace(prefix)
        self.manifest.del_namespace(prefix)
        
    def add_triple(self, s,p,o, create_item=True):
        s = self.uh.parse_uri(s)
        if s == self.uri:
            self.root.add_triple(p,o)
        else:
            self.manifest.add_triple(s,p,o, create_item)

    def del_triple(self, s,p,o=None):
        s = self.uh.parse_uri(s)
        if s == self.uri:
            self.root.del_triple(p,o)
        else:
            self.manifest.del_triple(s,p,o)

    def add_type(self, uritype):
        self.root.add_type(uritype)

    def del_type(self, uritype):
        self.root.del_type(uritype)

    def list_aggregates(self):
        g = self.root.get_graph()
        agg = []
        for _,_,o in g.triples((None, self.uh.parse_uri("ore:aggregates"), None)):
            agg.append(o)
        return agg

    def __str__(self, format="xml"):
        return self.to_string(format)

    def get_graph(self):
        g = self.root.get_graph()
        g_m = self.manifest.get_graph()
        if g_m:
            for prefix, ns in g_m.namespaces():
                g.bind(prefix, ns)
            for s,p,o in g_m.triples((None, None, None)):
                g.add((s, p, o))
        return g
    
    def to_string(self, format="xml"):
        return self.get_graph().serialize(format=format, encoding="utf-8") + u"\n"

    def from_string(self, root_uri, rdfstring, format="xml"):
        t = TextInputSource(rdfstring)
        g = ConjunctiveGraph()
        g = g.parse(t, format)
        for prefix, ns in g.namespaces():
            self.add_namespace(prefix ,ns)
        for s,p,o in g.triples((None, None, None)):
            self.add_triple(s,p,o)

    def namedgraphid_to_uri(self, graph_id):
        return "%s/%s" % (self.uri, graph_id)

    def add_named_graph(self, graph_id, valid_from=datetime.now(), valid_until=None):
        uri = self.namedgraphid_to_uri(graph_id)
        if self.uh.parse_uri(uri) not in self.parts:
            r = Manifest(uri)
            
            self.parts.add(r.uri)
            self.parts_objs[r.uri] = r
            self.add_triple(r.uri, "rdf:type", "http://www.w3.org/2004/03/trix/rdfg-1/Graph")
            self.add_triple(r.uri, "dc:format", "application/rdf+xml")
            self.add_triple(r.uri, 'foaf:primaryTopic', self.uri)
            self.add_triple(r.uri, "dcterms:created", datetime.now())
            if valid_from:
                self.add_triple(r.uri, "ov:validFrom", valid_from)
            if valid_until:
                self.add_triple(r.uri, "ov:validUntil", valid_until)
            self.add_triple(self.uri, "ore:aggregates", r.uri)
            self.add_triple(r.uri, "dcterms:isPartOf", self.uri)
            return r
        else:
            return self.parts_objs[self.uh.parse_uri(uri)]

    def get_named_graph(self, graph_id, create_if_necessary=False):
        uri = self.namedgraphid_to_uri(graph_id)
        if self.uh.parse_uri(uri) not in self.parts:
            raise NamedGraphNotFoundException
        else:
            return self.parts_objs[self.uh.parse_uri(uri)]

    def del_named_graph(self, graph_id):
        uri = self.uh.parse_uri("%s/%s" % (self.uri, graph_id))
        if uri in parts:
            self.parts.remove(uri)
            del self.parts_objs[uri]
            self.del_triple(self.uri, "ore:aggregates", uri)
            self.manifest.del_item(uri)
Beispiel #5
0
# -*- coding: utf-8 -*-
import os, sys 
# add rdfobject to the path, if running in the test dir 
if os.path.isdir(os.path.join(os.getcwd(), 'rdfobject')):
    sys.path.append(os.getcwd())
else:
    parent_dir = os.path.abspath(os.path.join(os.getcwd(), '..'))
    if os.path.isdir(os.path.join(parent_dir, 'rdfobject')):
        sys.path.append(parent_dir)
    else:
        print "Test must be run in either the test directory or the directory above it" 
        quit                                

from rdfobject.constructs import Manifest, ItemAlreadyExistsException, ItemDoesntExistException

m = Manifest()

RELSINT = """<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
   xmlns:dataset="http://vocab.ouls.ox.ac.uk/dataset/scheme#"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>
  <rdf:Description rdf:about="info:fedora/uuid:8a360038-2282-11de-9609-000e2ed68b2b/TEXT_DESC">
    <dc:title>TEXT_DESC</dc:title>
    <dc:description>Data description</dc:description>
  </rdf:Description>
  <rdf:Description rdf:about="info:fedora/uuid:23e0e1ca-2284-11de-9609-000e2ed68b2b/raw.wav">
    <dc:title>raw.wav</dc:title>

    <dc:description>the original recording, in Microsoft WAV format.