Exemple #1
0
    def set(self, graph):
        """
        Load the `resource` from a `graph`. The `graph` must be a `rdflib`
        `ConjunctiveGraph` or `Graph`

        .. note: Currently the method does not support *lazy loading*
                 as `load` does.

        """

        #TODO: must make this __lazy ... see how
        attrs = {}
        for s, p, o in graph:
            attr_name = None
            value = None
            if unicode(s) == unicode(self.subject):
                attr_name = rdf2attr(p, True)
                #value = self.__lazy([o])
                value = o
            elif unicode(o) == unicode(self.subject):
                attr_name = rdf2attr(p, False)
                #value = self.__lazy([s])
                value = s

            if attr_name:
                if attr_name not in attrs:
                    attrs[attr_name] = value
                elif attr_name in attrs and type(attrs[attr_name]) is not list:
                    attrs[attr_name] = [attrs[attr_name]]
                    attrs[attr_name].append(value)
                else:
                    attrs[attr_name].append(value)

        for attr_name in attrs:
            setattr(self, attr_name, attrs[attr_name])
Exemple #2
0
    def set(self, graph):
        """
        Load the `resource` from a `graph`. The `graph` must be a `rdflib`
        `ConjunctiveGraph` or `Graph`

        .. note: Currently the method does not support *lazy loading*
                 as `load` does.

        """

        #TODO: must make this __lazy ... see how
        attrs = {}
        for s, p, o in graph:
            attr_name = None
            value = None
            if str(s) == str(self.subject):
                attr_name = rdf2attr(p, True)
                #value = self.__lazy([o])
                value = o
            elif str(o) == str(self.subject):
                attr_name = rdf2attr(p, False)
                #value = self.__lazy([s])
                value = s

            if attr_name:
                if attr_name not in attrs:
                    attrs[attr_name] = value
                elif attr_name in attrs and type(attrs[attr_name]) is not list:
                    attrs[attr_name] = [attrs[attr_name]]
                    attrs[attr_name].append(value)
                else:
                    attrs[attr_name].append(value)

        for attr_name in attrs:
            setattr(self, attr_name, attrs[attr_name])
Exemple #3
0
    def __getitem__(self, attr_name):
        """ Dictionary-style attribute access. """

        if isinstance(attr_name, URIRef):
            attr_name = rdf2attr(attr_name, True)

        return getattr(self, attr_name)
Exemple #4
0
    def __getitem__(self, attr_name):
        """ Dictionary-style attribute access. """

        if isinstance(attr_name, URIRef):
            attr_name = rdf2attr(attr_name, True)

        return getattr(self, attr_name)
Exemple #5
0
    def test_rdf2attr_surf(self):
        """ Test rdf2attr with SURF namespace.
        
        This once had a bug which generated attribute names
        like "__fallback_namespace_label" 
        instead of "surf_label".
        
        """

        uri = "http://code.google.com/p/surfrdf/label"
        self.assertEqual(rdf2attr(uri, True), "surf_label")
Exemple #6
0
 def test_rdf2attr_surf(self):
     """ Test rdf2attr with SURF namespace.
     
     This once had a bug which generated attribute names
     like "__fallback_namespace_label" 
     instead of "surf_label".
     
     """
     
     uri = "http://code.google.com/p/surfrdf/label"
     self.assertEqual(rdf2attr(uri, True), "surf_label")
Exemple #7
0
    def __set_predicate_values(self, results, direct):
        """ set the prediate - value(s) to the resource using lazy loading,
        `results` is a dict under the form:
        {'predicate':{'value':[concept,concept],...},...}.

        """

        for p, v in list(results.items()):
            attr = rdf2attr(p, direct)
            value = self._lazy(v)
            if value:
                self.__setattr__(attr, value)
Exemple #8
0
    def __set_predicate_values(self, results, direct):
        """ set the prediate - value(s) to the resource using lazy loading,
        `results` is a dict under the form:
        {'predicate':{'value':[concept,concept],...},...}.

        """

        for p, v in results.items():
            attr = rdf2attr(p, direct)
            value = self._lazy(v)
            if value:
                self.__setattr__(attr, value)
Exemple #9
0
    def query_attribute(self, attribute_name):
        """ Return ResultProxy for querying attribute values. """

        # If we want to get john.foaf_knows values, we have to formulate
        # query like friends = get_by(is_foaf_knows_of = john), thus the
        # attribute name inversion
        uri, direct = attr2rdf(attribute_name)
        inverse_attribute_name = unicode(rdf2attr(uri, not direct))

        store = self.session[self.store_key]
        proxy = ResultProxy(store = store,
                            instancemaker = self.__instancemaker)

        kwargs = {inverse_attribute_name : self.subject}
        return proxy.get_by(**kwargs)
Exemple #10
0
    def query_attribute(self, attribute_name):
        """ Return ResultProxy for querying attribute values. """

        # If we want to get john.foaf_knows values, we have to formulate
        # query like friends = get_by(is_foaf_knows_of = john), thus the
        # attribute name inversion
        uri, direct = attr2rdf(attribute_name)

        # We'll be using inverse_attribute_name as keyword argument.
        # Python 2.6.2 and older don't allow unicode keyword arguments,
        # so we revert back to str().
        inverse_attribute_name = str(rdf2attr(uri, not direct))

        store = self.session[self.store_key]
        proxy = ResultProxy(store=store,
                            instance_factory=self.__instance_factory)

        kwargs = {inverse_attribute_name: self.subject}
        return proxy.get_by(**kwargs)
Exemple #11
0
    def query_attribute(self, attribute_name):
        """ Return ResultProxy for querying attribute values. """

        # If we want to get john.foaf_knows values, we have to formulate
        # query like friends = get_by(is_foaf_knows_of = john), thus the
        # attribute name inversion
        uri, direct = attr2rdf(attribute_name)
        
        # We'll be using inverse_attribute_name as keyword argument.
        # Python 2.6.2 and older doesn't allow unicode keyword arguments, 
        # so we do str().
        inverse_attribute_name = str(rdf2attr(uri, not direct))

        store = self.session[self.store_key]
        proxy = ResultProxy(store = store,
                            instancemaker = self.__instancemaker)

        kwargs = {inverse_attribute_name : self.subject}
        return proxy.get_by(**kwargs)
Exemple #12
0
 def test_rdf2attr(self):
     uri = "http://www.w3.org/2000/01/rdf-schema#label"
     self.assertEqual(rdf2attr(uri, True), "rdfs_label")
def test_rdf2attr():
    uri = "http://www.w3.org/2000/01/rdf-schema#label"
    assert rdf2attr(uri, True) == "rdfs_label"
Exemple #14
0
 def test_rdf2attr(self):
     uri = "http://www.w3.org/2000/01/rdf-schema#label"
     self.assertEqual(rdf2attr(uri, True), "rdfs_label")
Exemple #15
0
def test_rdf2attr():
    uri = "http://www.w3.org/2000/01/rdf-schema#label"
    assert rdf2attr(uri, True) == "rdfs_label"