Пример #1
0
    def test_get_one_exceptions(self):
        """ Test RessourceValue.one. """
        def values_source():
            return ["1st_obj", "2nd_obj"], ["1st_uriref", "2nd_uriref"]

        instance = ResourceValue(values_source, MockResource(), "some_name")
        self.assertRaises(surf.exc.CardinalityException, instance.get_one)
Пример #2
0
    def test_contains(self):
        """ Test ResourceValue.__contains__. """
        def values_source():
            return ["value_as_surf_object"], ["value_as_uriref"]

        instance = ResourceValue(values_source, MockResource(), "some_name")
        # Test basic membership check.
        self.assertTrue("value_as_surf_object" in instance)

        # Test that "to_rdf" is tried.
        self.assertTrue("value_as_uriref" in instance)
Пример #3
0
    def __setattr__(self, name, value):
        """
        The `set` method - responsible for *caching* the `value` to the
        corresponding object attribute given by `name`.

        .. note: This method sets the state of the resource to *dirty*
        (the `resource` will be persisted if the `commit` `session` method
        is called).

        """
        def make_values_source(values, rdf_values):
            """ Return callable that returns stored values for this attr. """
            def setattr_values_source():
                """ Return stored values for this attribute. """

                return values, rdf_values

            return setattr_values_source

        predicate, direct = attr2rdf(name)
        if predicate:
            rdf_dict = direct and self.__rdf_direct or self.__rdf_inverse
            if not isinstance(value, list):
                value = [value]
            rdf_dict[predicate] = []
            rdf_dict[predicate].extend([self.to_rdf(val) for val in value])
            self.dirty = True

            if type(value) is ResourceValue:
                pass
            else:
                if type(value) not in [list, tuple]:
                    value = [value]

                value = map(value_to_rdf, value)
                values_source = make_values_source(value, rdf_dict[predicate])
                value = ResourceValue(values_source, self, name)

        object.__setattr__(self, name, value)
Пример #4
0
    def __getattr__(self, attr_name):
        """ Retrieve and cache attribute values.

        If attribute name is not in the "ns_predicate" form, an
        `AttributeError` will be raised.


        This method has no impact on the *dirty* state of the object.

        """

        predicate, direct = attr2rdf(attr_name)

        if not predicate:
            raise AttributeError('Not a predicate: %s' % attr_name)

        # Closure for lazy execution.
        def make_values_source(resource, predicate, direct, do_query):
            """ Return callable that loads and returns values. """
            def getattr_values_source():
                """ Load and return values for this attribute. """

                if do_query:
                    store = resource.session[resource.store_key]
                    # Request to triple store
                    values = store.get(resource, predicate, direct)
                    # Instantiate SuRF objects
                    surf_values = resource._lazy(values)
                else:
                    surf_values = []

                # Select triple dictionary for synchronization
                if direct:
                    rdf_dict = resource.__rdf_direct
                else:
                    rdf_dict = resource.__rdf_inverse

                # Save query_contexts
                for i in range(len(surf_values)):
                    if isinstance(surf_values[i], Resource):
                        surf_values[i].query_contexts = self.query_contexts

                # Initial synchronization
                rdf_dict[predicate] = [
                    resource.to_rdf(value) for value in surf_values
                ]

                return surf_values, rdf_dict[predicate]

            return getattr_values_source

        # If resource is fully loaded and still we're here
        # at __getattr__, this must be an empty attribute, so
        # no point querying triple store.
        do_query = not self.__full
        values_source = make_values_source(self, predicate, direct, do_query)

        attr_value = ResourceValue(values_source, self, attr_name)

        # Not using self.__setattr__, that would trigger loading of attributes
        object.__setattr__(self, attr_name, attr_value)
        self.dirty = False

        return attr_value