oldman.resource package

Submodules

oldman.resource.manager module

class oldman.resource.manager.ClientResourceManager(data_stores, schema_graph=None, attr_extractor=None, oper_extractor=None, declare_default_operation_functions=True)[source]

TODO: describe

create(id=None, types=None, hashless_iri=None, collection_iri=None, **kwargs)[source]

Creates a new resource and save it in the data_store.

See new() for more details.

declare_method(method, name, class_iri)[source]

Attaches a method to the Resource objects that are instances of a given RDFS class.

Like in Object-Oriented Programming, this method can be overwritten by attaching a homonymous method to a class that has a higher inheritance priority (such as a sub-class).

To benefit from this method (or an overwritten one), Resource objects must be associated to a Model that corresponds to the RDFS class or to one of its subclasses.

Parameters:
  • method – Python function that takes as first argument a Resource object.
  • name – Name assigned to this method.
  • class_iri – Targeted RDFS class. If not overwritten, all the instances (Resource objects) should inherit this method.
filter(types=None, hashless_iri=None, limit=None, eager=False, pre_cache_properties=None, **kwargs)[source]

See oldman.store.datastore.DataStore.filter().

get(id=None, types=None, hashless_iri=None, eager_with_reversed_attributes=True, **kwargs)[source]

See oldman.store.datastore.DataStore.get().

get_model(class_name_or_iri)[source]
import_store_models()[source]

TODO: check possible conflicts with local models.

model_manager[source]
new(id=None, types=None, hashless_iri=None, collection_iri=None, **kwargs)[source]

Creates a new Resource object without saving it in the data_store.

The kwargs dict can contains regular attribute key-values that will be assigned to OMAttribute objects.

Parameters:
  • id – IRI of the new resource. Defaults to None. If not given, the IRI is generated by the IRI generator of the main model.
  • types – IRIs of RDFS classes the resource is instance of. Defaults to None. Note that these IRIs are used to find the models of the resource (see find_models_and_types() for more details).
  • hashless_iri – hash-less IRI that MAY be considered when generating an IRI for the new resource. Defaults to None. Ignored if id is given. Must be None if collection_iri is given.
  • collection_iri – IRI of the controller to which this resource belongs. This information is used to generate a new IRI if no id is given. The IRI generator may ignore it. Defaults to None. Must be None if hashless_iri is given.
Returns:

A new Resource object.

sparql_filter(query)[source]

See oldman.store.datastore.DataStore.sparql_filter().

use_store_model(class_iri, data_store=None)[source]

oldman.resource.resource module

class oldman.resource.resource.ClientResource(resource_manager, model_manager, store, **kwargs)[source]

Bases: oldman.resource.resource.Resource

ClientResource: resource manipulated by the end-user.

Has access to the resource_manager.

Is not serializable.

delete()[source]

Removes the resource from the data_store and its resource_cache.

Cascade deletion is done for related resources satisfying the test should_delete_resource().

Gets a related ClientResource through the resource manager.

classmethod load_from_graph(resource_manager, model_manager, data_store, id, subgraph, is_new=True, collection_iri=None)[source]

Loads a new ClientResource object from a sub-graph.

TODO: update the comments.

Parameters:
  • managerResourceManager object.
  • id – IRI of the resource.
  • subgraphrdflib.Graph object containing triples about the resource.
  • is_new – When is True and id given, checks that the IRI is not already existing in the union_graph. Defaults to True.
Returns:

The Resource object created.

save(is_end_user=True)[source]

Saves it into the data_store and its resource_cache.

Raises an oldman.exception.OMEditError exception if invalid.

Parameters:is_end_userFalse when an authorized user (not a regular end-user) wants to force some rights. Defaults to True. See check_validity() for further details.
Returns:The Resource object itself.
class oldman.resource.resource.Resource(model_manager, data_store, id=None, types=None, hashless_iri=None, collection_iri=None, is_new=True, former_types=None, **kwargs)[source]

Bases: object

A Resource object is a subject-centric representation of a Web resource. A set of Resource objects is equivalent to a RDF graph.

In RDF, a resource is identified by an IRI (globally) or a blank node (locally). Because blank node support is complex and limited (rdflib.plugins.stores.sparqlstore.SPARQLStore stores do not support them), every Resource object has an IRI.

This IRI is either given or generated by a IriGenerator object. Some generators generate recognizable skolem IRIs that are treated as blank nodes when the resource is serialized into JSON, JSON-LD or another RDF format (for external consumption).

A resource is usually instance of some RDFS classes. These classes are grouped in its attribute types. Model objects are found from these classes, by calling the method oldman.resource.manager.ResourceManager.find_models_and_types(). Models give access to Python methods and to OMAttribute objects. Their ordering determines inheritance priorities. The main model is the first one of this list.

Values of OMAttribute objects are accessible and modifiable like ordinary Python attribute values. However, these values are checked so some OMAccessError or OMEditError errors may be raised.

This abstract class accepts two concrete classes: StoreResource and ClientResource. The former is serializable and can be saved directly by the datastore while the latter has to be converted into a StoreResource so as to be saved.

Example:

>>> alice = StoreResource(model_manager, data_store, types=["http://schema.org/Person"], name=u"Alice")
>>> alice.id
u'http://localhost/persons/1'
>>> alice.name
u'Alice'
>>> alice.save()
>>> alice.name = "Alice A."
>>> print alice.to_jsonld()
{
   "@context": "http://localhost/person.jsonld",
   "id": "http://localhost/persons/1",
   "types": [
              "http://schema.org/Person"
            ],
   "name": "Alice A."
}
>>> alice.name = 5
oldman.exception.OMAttributeTypeCheckError: 5 is not a (<type 'str'>, <type 'unicode'>)

Resource creation

Resource objects are normally created by a Model or a ResourceManager object. Please use the methods oldman.model.model.Model.create(), oldman.model.Model.new(), oldman.resource.manager.ResourceManager.create() or oldman.resource.manager.ResourceManager.new() for creating new Resource objects.

Parameters:
  • model_managerModelManager object. Gives access to its models.
  • data_storeDataStore object. Datastore that has authority on this resource.
  • id – IRI of the resource. If not given, this IRI is generated by the main model. Defaults to None.
  • types – IRI list or set of the RDFS classes the resource is instance of. Defaults to set().
  • hashless_iri – Hash-less IRI that is given to the main model for generating a new IRI if no id is given. The IRI generator may ignore it. Defaults to None. Must be None if collection_iri is given.
  • collection_iri – IRI of the controller to which this resource belongs. This information is used to generate a new IRI if no id is given. The IRI generator may ignore it. Defaults to None. Must be None if hashless_iri is given.
  • is_new – When is True and id given, checks that the IRI is not already existing in the data_store. Defaults to True.
  • former_types – IRI list or set of the RDFS classes the resource was instance of. Defaults to set().
  • kwargs – values indexed by their attribute names.
add_type(additional_type)[source]

Declares that the resource is instance of another RDFS class.

Note that it may introduce a new model to the list and change its ordering.

Parameters:additional_type – IRI or JSON-LD term identifying a RDFS class.
check_validity()[source]

Checks its validity.

Raises an oldman.exception.OMEditError exception if invalid.

context[source]

An IRI, a list or a dict that describes the JSON-LD context.

Derived from oldman.model.Model.context attributes.

delete()[source]

Removes the resource from the data_store and its resource_cache.

Cascade deletion is done for related resources satisfying the test should_delete_resource().

former_non_model_types[source]

RDFS classes that were not associated to a Model.

former_types[source]

Not for end-users

get_attribute(attribute_name)[source]

Not for the end-user!

get_lightly(attribute_name)[source]

If the attribute corresponds to an owl:ObjectProperty, returns a IRI or None. Otherwise (if is a datatype), returns the value.

get_operation(http_method)[source]

TODO: describe

Not for end-users! Must be implemented by concrete classes.

If cannot get the resource, return its IRI.

hashless_iri[source]

Hash-less IRI of the id attribute. Is obtained by removing the fragment from the IRI.

id[source]

IRI that identifies the resource.

in_same_document(other_resource)[source]

Tests if two resources have the same hash-less IRI.

Returns:True if these resources are in the same document.
is_blank_node()[source]

Tests if id is a skolem IRI and should thus be considered as a blank node.

See is_blank_node() for further details.

Returns:True if id is a locally skolemized IRI.
is_instance_of(model)[source]

Tests if the resource is instance of the RDFS class of the model.

Parameters:modelModel object.
Returns:True if the resource is instance of the RDFS class.
is_new[source]

True if the resource has never been saved.

is_valid()[source]

Tests if the resource is valid.

Returns:False if the resource is invalid, True otherwise.
local_context[source]

Context that is locally accessible but that may not be advertised in the JSON-LD serialization.

model_manager[source]

ModelManager object. Gives access to the Model objects.

models[source]

TODO: describe

non_model_types[source]

RDFS classes that are not associated to a Model.

receive_id(id)[source]

Receives the permanent ID assigned by the store. Useful when the permanent ID is given by an external server.

Replaces the temporary ID of the resource.

save(is_end_user=True)[source]

Saves it into the data_store and its resource_cache.

Raises an oldman.exception.OMEditError exception if invalid.

Parameters:is_end_userFalse when an authorized user (not a regular end-user) wants to force some rights. Defaults to True. See check_validity() for further details.
Returns:The Resource object itself.
store[source]

DataStore object.

to_dict(remove_none_values=True, include_different_contexts=False, ignored_iris=None)[source]

Serializes the resource into a JSON-like dict.

Parameters:
  • remove_none_values – If True, None values are not inserted into the dict. Defaults to True.
  • include_different_contexts – If True local contexts are given to sub-resources. Defaults to False.
  • ignored_iris – List of IRI of resources that should not be included in the dict. Defaults to set().
Returns:

A dict describing the resource.

to_json(remove_none_values=True, ignored_iris=None)[source]

Serializes the resource into pure JSON (not JSON-LD).

Parameters:
  • remove_none_values – If True, None values are not inserted into the dict. Defaults to True.
  • ignored_iris – List of IRI of resources that should not be included in the dict. Defaults to set().
Returns:

A JSON-encoded string.

to_jsonld(remove_none_values=True, include_different_contexts=False, ignored_iris=None)[source]

Serializes the resource into JSON-LD.

Parameters:
  • remove_none_values – If True, None values are not inserted into the dict. Defaults to True.
  • include_different_contexts – If True local contexts are given to sub-resources. Defaults to False.
  • ignored_iris – List of IRI of resources that should not be included in the dict. Defaults to set().
Returns:

A JSON-LD encoded string.

to_rdf(rdf_format='turtle')[source]

Serializes the resource into RDF.

Parameters:rdf_format – content-type or keyword supported by RDFlib. Defaults to “turtle”.
Returns:A string in the chosen RDF format.
types[source]

IRI list of the RDFS classes the resource is instance of.

update(full_dict, is_end_user=True, allow_new_type=False, allow_type_removal=False, save=True)[source]

Updates the resource from a flat dict.

By flat, we mean that sub-resources are only represented by their IRIs: there is no nested sub-object structure.

This dict is supposed to be exhaustive, so absent value is removed. Some sub-resources may thus be deleted like if there were a cascade deletion.

Parameters:
  • full_dict – Flat dict containing the attribute values to update.
  • is_end_userFalse when an authorized user (not a regular end-user) wants to force some rights. Defaults to True. See check_validity() for further details.
  • allow_new_type

    If True, new types can be added. Please keep in mind that type change can:

    • Modify the behavior of the resource by changing its model list.
    • Interfere with the SPARQL requests using instance tests.

    If enabled, this may represent a major security concern. Defaults to False.

  • allow_type_removal – If True, new types can be removed. Same security concerns than above. Defaults to False.
  • save – If True calls save() after updating. Defaults to True.
Returns:

The Resource object itself.

update_from_graph(subgraph, initial=False, is_end_user=True, allow_new_type=False, allow_type_removal=False, save=True)[source]

Similar to full_update() but with a RDF graph instead of a Python dict.

Parameters:
  • subgraphrdflib.Graph object containing the full description of the resource.
  • initialTrue when the subgraph comes from the data_graph and is thus used to load Resource object from the triple store. Defaults to False.
  • is_end_userFalse when an authorized user (not a regular end-user) wants to force some rights. Defaults to True. See check_validity() for further details.
  • allow_new_type – If True, new types can be added. Defaults to False. See full_update() for explanations about the security concerns.
  • allow_type_removal – If True, new types can be removed. Same security concerns than above. Defaults to False.
  • save – If True calls save() after updating. Defaults to True.
Returns:

The Resource object itself.

class oldman.resource.resource.StoreResource(model_manager, data_store, id=None, types=None, hashless_iri=None, collection_iri=None, is_new=True, former_types=None, **kwargs)[source]

Bases: oldman.resource.resource.Resource

StoreResource: resource manipulated by the data store.

End-users should not manipulate it.

Is serializable (pickable).

delete()[source]

Removes the resource from the data_store and its resource_cache.

Cascade deletion is done for related resources satisfying the test should_delete_resource().

Gets a related StoreResource by calling the datastore directly.

classmethod load_from_graph(model_manager, data_store, id, subgraph, is_new=True, collection_iri=None)[source]

Loads a new StoreResource object from a sub-graph.

TODO: update the comments.

Parameters:
  • managerResourceManager object.
  • id – IRI of the resource.
  • subgraphrdflib.Graph object containing triples about the resource.
  • is_new – When is True and id given, checks that the IRI is not already existing in the union_graph. Defaults to True.
Returns:

The Resource object created.

save(is_end_user=True)[source]

Saves it into the data_store and its resource_cache.

Raises an oldman.exception.OMEditError exception if invalid.

Parameters:is_end_userFalse when an authorized user (not a regular end-user) wants to force some rights. Defaults to True. See check_validity() for further details.
Returns:The Resource object itself.
oldman.resource.resource.is_blank_node(iri)[source]

Tests if id is a locally skolemized IRI.

External skolemized blank nodes are not considered as blank nodes.

Parameters:iri – IRI of the resource.
Returns:True if is a blank node.
oldman.resource.resource.should_delete_resource(resource)[source]

Tests if a resource should be deleted.

Parameters:resourceResource object to evaluate.
Returns:True if it should be deleted.

Module contents