oldman.core.resource package

Submodules

oldman.core.resource.reference module

class oldman.core.resource.reference.ResourceReference(subject_resource, attribute, object_resource_or_iri)[source]

Bases: object

TODO: explain

“Subject” and “object” adjectives can be ROUGHLY understood like “subject predicate object”.

By roughly, we mean that we ignore the effect of “inverse property” (an attribute roughly corresponds to a predicate even in practice it can refer to an inverse predicate; we don’t make the distinction here).

attribute
detach()[source]

TODO: explain

get(must_be_attached=True)[source]
is_bound_to_object_resource
object_iri
subject_resource

oldman.core.resource.resource module

class oldman.core.resource.resource.Resource(id, model_manager, session, types=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:
  • id – TODO:describe.
  • model_managerModelManager object. Gives access to its models.
  • types – IRI list or set of the RDFS classes the resource is instance of. Defaults to set().
  • 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.

TODO: update this comment!!!!!

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.
attributes
Returns:An ordered list of list of OMAttribute objects.
check_validity(is_end_user=True)[source]

Checks its validity.

Raises an oldman.exception.OMEditError exception if invalid.

context

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

Derived from oldman.model.Model.context attributes.

former_non_model_types

RDFS classes that were not associated to a Model.

former_types

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!

Gets a related Resource through its session.

If cannot get the resource, return its IRI.

id

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.iri is a skolem IRI and should thus be considered as a blank node.

See is_blank_node() for further details.

Returns:True if id.iri 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

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

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

model_manager

ModelManager object. Gives access to the Model objects.

models

TODO: describe

non_model_types

RDFS classes that are not associated to a Model.

notify_reference(reference, object_resource=None, object_iri=None)[source]

Not for end-users!

TODO: describe

notify_reference_removal(reference)[source]

Not for end-users!

TODO: describe

receive_storage_ack(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.

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

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

update(full_dict, allow_new_type=False, allow_type_removal=False)[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.
  • 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.
Returns:

The Resource object itself.

update_from_graph(subgraph, initial=False, allow_new_type=False, allow_type_removal=False)[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.
  • 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.
Returns:

The Resource object itself.

Module contents