コード例 #1
0
 def reset(self):
     self.g = None
     if self.uri:
         self.g = ConjunctiveGraph(identifier=self.uri)
     else:
         self.g = ConjunctiveGraph()
     self.namespaces = {}
     self.urihelper = URIHelper(self.namespaces)
     #add defaults
     for prefix, ns in NAMESPACES.iteritems():
         self.add_namespace(prefix, ns)
コード例 #2
0
ファイル: rdfobject.py プロジェクト: benosteen/RDFobject
 def reset(self):
     self.namespaces = {}
     self.triples = set([])
     self.types = set([])
     self.g = None
     self.altered = False
     self._cached = False
     #add defaults
     self.urihelper = URIHelper(self.namespaces)
     self.add_namespace(u'rdf', u'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
     self.add_namespace(u'rdfs', u"http://www.w3.org/2000/01/rdf-schema#")
     self.add_namespace(u'dcterms', u"http://purl.org/dc/terms/")
     self.add_namespace(u'ov', u'http://open.vocab.org/terms/')
     self.add_namespace(u'ore', u'http://www.openarchives.org/ore/terms/')
コード例 #3
0
ファイル: rdfobject.py プロジェクト: benosteen/RDFobject
class RDFobject(object):
    def __init__(self, uri=None):
        self.reset()
        if uri:
            self.set_uri(uri)
    
    def reset(self):
        self.namespaces = {}
        self.triples = set([])
        self.types = set([])
        self.g = None
        self.altered = False
        self._cached = False
        #add defaults
        self.urihelper = URIHelper(self.namespaces)
        self.add_namespace(u'rdf', u'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
        self.add_namespace(u'rdfs', u"http://www.w3.org/2000/01/rdf-schema#")
        self.add_namespace(u'dcterms', u"http://purl.org/dc/terms/")
        self.add_namespace(u'ov', u'http://open.vocab.org/terms/')
        self.add_namespace(u'ore', u'http://www.openarchives.org/ore/terms/')
    
    @_cause_new_revision
    def add_type(self, uri_of_type):
        uri = self.urihelper.parse_uri(uri_of_type)
        self.types.add(uri)

    def set_type(self, uri_of_type):
        """Clears all other types to set this to be singularly-typed"""
        self.types = set([])
        self.add_type(uri_of_type)

    @_cause_new_revision
    def del_type(self, uri_of_type):
        uri = self.urihelper.parse_uri(uri_of_type)
        if uri in self.types:
            self.types.remove(uri)
    
    def set_uri(self, uri):
        self.uri = self.urihelper.parse_uri(uri)
    
    def add_namespace(self, prefix, uri):
        self.namespaces[prefix] = self.urihelper.get_namespace(uri)
        if prefix not in self.urihelper.namespaces:
            self.urihelper.namespaces[prefix] = self.urihelper.get_namespace(uri)

    def del_namespace(self, prefix):
        if prefix in self.namespaces:
            del self.namespaces[prefix]

    def triple_exists(self, predicate, objectRef):
        if not isinstance(objectRef, URIRef) and not isinstance(objectRef, Literal):
            objectRef = self.urihelper.parse_uri(objectRef, return_Literal_not_Exception=True)
        predicate_uri = self.urihelper.parse_uri(predicate)
        return (predicate_uri, objectRef) in self.triples

    def list_objects(self, predicate):
        predicate_uri = self.urihelper.parse_uri(predicate)
        return [x[1] for x in self.triples if x[0] == predicate_uri]

    @_cause_new_revision    
    def add_triple(self, predicate, objectRef):
        if not isinstance(objectRef, URIRef) and not isinstance(objectRef, Literal):
            objectRef = self.urihelper.parse_uri(objectRef, return_Literal_not_Exception=True)
        predicate_uri = self.urihelper.parse_uri(predicate)
        
        if (predicate_uri, objectRef) not in self.triples:
            if predicate_uri == NAMESPACES['rdf']['type']:
                self.add_type(objectRef)
            else:
                self.triples.add((predicate_uri, objectRef))

    @_cause_new_revision    
    def del_triple(self, predicate, objectRef=None):
        predicate_uri = self.urihelper.parse_uri(predicate)
        if objectRef:
            if not isinstance(objectRef, URIRef) and not isinstance(objectRef, Literal):
                objectRef = self.urihelper.parse_uri(objectRef, return_Literal_not_Exception=True)
            if (predicate_uri, objectRef) in self.triples:
                self.triples.remove((predicate_uri, objectRef))
        else:
            self.triples = set([t for t in self.triples if t[0] != predicate_uri])

    @_cause_new_revision
    def add_n3_triple(self, text):
        if NTHREE_PL_P.search(text):
            prefix, predicate, objectRef = NTHREE_PL_P.search(text).groups()
            objectRef = Literal(objectRef)
            self.add_triple(prefix, predicate, objectRef)
        elif NTHREE_PURI_P.search(text):
            prefix, predicate, obj_prefix, objectRef = NTHREE_PURI_P.search(text).groups()
            if obj_prefix not in ['http', 'info', 'urn', 'ftp', 'https']:
                self.add_triple(self.urihelper.uriref_shorthand_uri(prefix, predicate), self.urihelper.uriref_shorthand_uri(obj_prefix, objectRef))
            else:
                self.add_triple( self.urihelper.uriref_shorthand_uri(prefix, predicate), self.urihelper.parse_uri(":".join([obj_prefix, objectRef]), return_Literal_not_Exception=True) )
        else:
            raise N3NotUnderstoodException()

    def from_string(self, uri, text, format="xml", encoding="utf-8"):
        self.reset()
        self.set_uri(uri)
        t = TextInputSource(text, system_id=uri)
        t.setEncoding(encoding)
        g = ConjunctiveGraph(identifier=self.uri)
        g = g.parse(t, format)
        for prefix, ns in g.namespaces():
            self.add_namespace(prefix, ns)
        for s,p,o in g.triples((self.uri, None, None)):
            self.add_triple(p, o)

    
    def from_url(self, url, uri=None, format="xml",  encoding="utf-8"):
        self.reset()
        if not uri:
            self.set_uri(url)
        else:
            self.set_uri(uri)
        g = ConjunctiveGraph(identifier=self.uri)
        g = g.parse(url, format)
        for prefix, ns in g.namespaces():
            self.add_namespace(prefix, ns)
        for s,p,o in g.triples((self.uri, None, None)):
            self.add_triple(p, o)
    
    def get_graph(self):
        if not self.uri:
            raise URINotSetException()
        g = ConjunctiveGraph(identifier=self.uri)
        
        for ns in self.namespaces:
            g.bind(ns, self.namespaces[ns])
        #add global prefix bindings
        for ns in self.urihelper.namespaces:
            g.bind(ns, self.urihelper.namespaces[ns])
            
        # Add type(s)
        for type in self.types:
            g.add((self.uri, self.namespaces['rdf']['type'], type))
        
        for triple in self.triples:
            g.add((self.uri, triple[0], triple[1]))
        return g

    def list_triples(self):
        """output a list of the triple tuples - Should really make this a generator, but anyway."""
        triples = []
        # Add type(s)
        for type in self.types:
            triples.append((self.uri, self.namespaces['rdf']['type'], type))
        
        for triple in self.triples:
            triples.append((self.uri, triple[0], triple[1]))
        return triples

    @_cause_new_revision
    def add_rdfobject(self, rdfobject):
        """ ** NOTE ** This only adds the property and object pairs, and discards the subject """
        self._add_graph(rdfobject.get_graph())

    def _add_graph(self, g):
        """ ** NOTE ** This only adds the property and object pairs, and discards the subject """
        for prefix, ns in g.namespaces():
            self.add_namespace(prefix, ns)
        for s,p,o in g.triples((None, None, None)):
            self.add_triple(p, o)

    @_cause_new_revision
    def munge_graph(self, g):
        """ This will only add the triples that have this object's subject as their subject. """
        for prefix, ns in g.namespaces():
            self.add_namespace(prefix, ns) 
        for s,p,o in g.triples((self.uri, None, None)):
            self.add_triple(p, o)
    
    def __str__(self):
        return self.to_string('xml')
    
    def to_string(self, format="xml"):
        return self.get_graph().serialize(format=format, encoding="utf-8")+"\n"
コード例 #4
0
class ManifestHelper(object):
    def __init__(self, uri=None):
        self.uri = None
        if uri:
            self.uri = uri
        self.reset()
    
    def reset(self):
        self.g = None
        if self.uri:
            self.g = ConjunctiveGraph(identifier=self.uri)
        else:
            self.g = ConjunctiveGraph()
        self.namespaces = {}
        self.urihelper = URIHelper(self.namespaces)
        #add defaults
        for prefix, ns in NAMESPACES.iteritems():
            self.add_namespace(prefix, ns)
    
    def from_string(self, textfile, format="xml", encoding="utf-8"):
        self.reset()
        self.g.parse(textfile, format)
        return
    
    def triple_exists(self, s, p, o):
        if not type(self.g).__name__ in ['ConjunctiveGraph', 'Graph']:
            return False        
        if s == '*':
            s = None
        if p == '*':
            p = None
        if o == '*':
            o = None

        if not isinstance(s, URIRef) and not isinstance(s, BNode) and not s == None:
            s = self.urihelper.get_uriref(s)
        
        if not isinstance(p, URIRef) and not p == None:
            p = self.urihelper.parse_uri(p)

        if not isinstance(o, URIRef) and not isinstance(o, Literal) and not isinstance(o, BNode) and not o == None:
            if not isinstance(o, basestring):
                o = unicode(o)
            o = self.urihelper.parse_uri(o, return_Literal_not_Exception=True)
             
        count = 0
        for ans_s, ans_p, ans_o in self.g.triples((s, p, o)):
            count += 1
        if count > 0:
            return True
        else:
            return False 
    
    def list_objects(self, s, p):
        objects = []
        if not type(self.g).__name__ in ['ConjunctiveGraph', 'Graph']:
            return objects
        if s == '*':
            s = None
        if p == '*':
            p = None

        if not isinstance(s, URIRef) and not isinstance(s, BNode) and not s == None:
            s = self.urihelper.get_uriref(s)
        
        if not isinstance(p, URIRef) and not p == None:
            p = self.urihelper.parse_uri(p)

        for o in self.g.objects(s, p):
            objects.append(o)
        return objects
    
    def add_triple(self, s, p, o):
        if not isinstance(s, URIRef) and not isinstance(s, BNode):
            s = self.urihelper.get_uriref(s)
        
        if not isinstance(p, URIRef):
            p = self.urihelper.parse_uri(p)

        if not isinstance(o, URIRef) and not isinstance(o, Literal) and not isinstance(o, BNode):
            if not isinstance(o, basestring):
                o = unicode(o)
            o = self.urihelper.parse_uri(o, return_Literal_not_Exception=True)

        self.g.add((s, p, o))
        self.g.commit()
        return
    
    def add_namespace(self, prefix, uri):
        if not isinstance (prefix, basestring):
            raise TypeError('Add namespace: prefix is not of type string or unicode') 

        if not isinstance(uri, (URIRef, Namespace)):
            if not isinstance(uri, basestring):
                raise TypeError('Add namespace: namespace is not of type string or unicode') 

        if not isinstance(prefix, unicode):
            prefix = unicode(prefix)

        if isinstance(uri, basestring) and not isinstance(uri, unicode):
            uri = unicode(uri)

        self.namespaces[prefix] = self.urihelper.get_namespace(uri)
        if prefix not in self.urihelper.namespaces:
            self.urihelper.namespaces[prefix] = self.urihelper.get_namespace(uri)
        self.g.bind(prefix, self.namespaces[prefix])
        return
    
    def del_namespace(self, prefix, ns):
        if prefix in self.namespaces:
            del self.namespaces[prefix]
        return
    
    def del_triple(self, s, p, o=None):
        if not type(self.g).__name__ in ['ConjunctiveGraph', 'Graph']:
            return
        if s == '*':
            s = None
        if p == '*':
            p = None
        if o == '*':
            o = None

        if not isinstance(s, URIRef) and not isinstance(s, BNode) and not s == None:
            s = self.urihelper.get_uriref(s)
        
        if not isinstance(p, URIRef) and not p == None:
            p = self.urihelper.parse_uri(p)

        if not isinstance(o, URIRef) and not isinstance(o, Literal) and not isinstance(o, BNode) and not o == None:
            if not isinstance(o, basestring):
                o = unicode(o)
            o = self.urihelper.parse_uri(o, return_Literal_not_Exception=True)
        self.g.remove((s, p, o))
        return
    
    def get_graph(self):
        return self.g
    
    def to_string(self, format="xml"):
        if type(self.g).__name__ in ['ConjunctiveGraph', 'Graph'] and len(self.g)>0:
            self.g.commit()
            ans_str = self.g.serialize(format=format, encoding="utf-8")+"\n"
            return ans_str
        else:
            return u'<?xml version="1.0" encoding="UTF-8"?>\n'