oldman package¶
oldman.attribute module¶
- class oldman.attribute.OMAttribute(manager, metadata, value_format)[source]¶
Bases: object
An OMAttribute object corresponds to a JSON-LD term that refers to a RDF property.
Technically, the name of the OMAttribute object is a JSON-LD term, namely “a short-hand string that expands to an IRI or a blank node identifier” (cf. the JSON-LD standard) which corresponds here to a RDF property (see OMProperty).
In JSON-LD, the same RDF property may correspond to multiple JSON-LD terms that have different metadata. For instance, a foaf:Person resource may have two attributes for its bio in English and in French. These attributes have two different languages but use the same property bio:olb. Look at the quickstart example to see it in practice.
An OMAttribute object manages the values of every Resource object that depends on a given Model object.
Each value may be :
- None;
- The Python equivalent for a RDF literal (double, string, date, etc.);
- An IRI;
- A collection (set, list and dict) of these types.
Parameters: - manager – ResourceManager object.
- metadata – OMAttributeMetadata object.
- value_format – ValueFormat object that validates the format of values and converts RDF values into regular Python objects.
- check_validity(resource, is_end_user=True)[source]¶
Raises an OMEditError exception if the attribute value assigned to a resource is invalid.
Parameters: - resource – Resource object.
- is_end_user – False when an authorized user (not a regular end-user) wants to force some rights. Defaults to True.
- check_value(value)[source]¶
Checks a new when assigned.
Raises an oldman.exception.OMAttributeTypeCheckError exception if the value is invalid.
Parameters: value – collection or atomic value.
- delete_former_value(resource)[source]¶
Clears the former value that has been replaced.
Parameters: resource – Resource object.
- get(resource)[source]¶
Gets the attribute value of a resource.
Parameters: resource – Resource object. Returns: Atomic value or a generator.
- get_former_value(resource)[source]¶
Gets out the former value that has been replaced.
Parameters: resource – Resource object. Returns: its former attribute value or None.
- get_lightly(resource)[source]¶
Gets the attribute value of a resource in a lightweight manner.
By default, behaves exactly like get(). See the latter function for further details.
- has_value(resource)[source]¶
Tests if the resource attribute has a non-None value.
Parameters: resource – Resource object. Returns: False if the value is None.
- is_valid(resource, is_end_user=True)[source]¶
Tests if the attribute value assigned to a resource is valid.
See check_validity() for further details.
Returns: False if the value assigned to the resource is invalid and True otherwise.
- manager[source]¶
Its ResourceManager object.
- om_property[source]¶
OMProperty to which it belongs.
- other_attributes[source]¶
Other OMAttribute objects of the same property.
- set(resource, value)[source]¶
Sets the attribute value of a resource.
Parameters: - resource – Resource object.
- value – Its value for this attribute.
- to_nt(resource)[source]¶
Converts its current attribute value to N-Triples (NT) triples.
Relies on value_to_nt().
Parameters: resource – Resource object. Returns: N-Triples serialization of its attribute value.
- update_from_graph(resource, sub_graph, initial=False)[source]¶
Updates a resource attribute value by extracting the relevant information from a RDF graph.
Parameters: - resource – Resource object.
- sub_graph – rdflib.Graph object containing the value to extract.
- initial – True when the value is directly from the datastore. Defaults to False.
- value_format[source]¶
ValueFormat object that validates the format of values and converts RDF values into regular Python objects.
- class oldman.attribute.OMAttributeMetadata¶
Bases: tuple
OMAttributeMetadata(name, property, language, jsonld_type, container, reversed)
- container¶
Alias for field number 4
- jsonld_type¶
Alias for field number 3
- language¶
Alias for field number 2
- name¶
Alias for field number 0
- property¶
Alias for field number 1
- reversed¶
Alias for field number 5
- class oldman.attribute.ObjectOMAttribute(manager, metadata, value_format)[source]¶
Bases: oldman.attribute.OMAttribute
An ObjectOMAttribute object is an OMAttribute object that depends on an owl:ObjectProperty.
oldman.exception module¶
- exception oldman.exception.AlreadyAllocatedModelError[source]¶
Bases: oldman.exception.ModelGenerationError
The class IRI or the short name of a new model is already allocated.
- exception oldman.exception.ModelGenerationError[source]¶
Bases: oldman.exception.OMError
Error occured when generating a new model.
- exception oldman.exception.OMAccessError[source]¶
Bases: oldman.exception.OMUserError
Error when accessing objects.
- exception oldman.exception.OMAlreadyDeclaredDatatypeError[source]¶
Bases: oldman.exception.OMAttributeDefError
At least two different datatypes for the same attribute.
You may check the possible datatype inherited from the property (rdfs:range) and the one specified in the JSON-LD context.
- exception oldman.exception.OMAlreadyGeneratedAttributeError[source]¶
Bases: oldman.exception.OMInternalError
Attribute generation occurs only once per SupportedProperty.
You should not try to add metadata or regenerate after that.
- exception oldman.exception.OMAttributeAccessError[source]¶
Bases: oldman.exception.OMAccessError
When such an attribute cannot be identified (is not supported or no model has been found).
- exception oldman.exception.OMAttributeDefError[source]¶
Bases: oldman.exception.OMSchemaError
Inconsistency in the definition of a model class attribute.
- exception oldman.exception.OMAttributeTypeCheckError[source]¶
Bases: oldman.exception.OMEditError
The value assigned to the attribute has wrong type.
- exception oldman.exception.OMClassInstanceError[source]¶
Bases: oldman.exception.OMAccessError
The object is not an instance of the expected RDFS class.
- exception oldman.exception.OMDataStoreError[source]¶
Bases: oldman.exception.OMError
Error detected in the stored data.
- exception oldman.exception.OMDifferentHashlessIRIError[source]¶
Bases: oldman.exception.OMEditError
When creating or updating an object with a different hashless IRI is forbidden.
Blank nodes are not concerned.
- exception oldman.exception.OMEditError[source]¶
Bases: oldman.exception.OMUserError
Runtime errors, occuring when editing or creating an object.
- exception oldman.exception.OMError[source]¶
Bases: exceptions.Exception
Root of exceptions generated by the oldman package.
- exception oldman.exception.OMExpiredMethodDeclarationTimeSlotError[source]¶
Bases: oldman.exception.ModelGenerationError
All methods must be declared before creating a first model.
- exception oldman.exception.OMForbiddenSkolemizedIRIError[source]¶
Bases: oldman.exception.OMEditError
When updating a skolemized IRI from the local domain is forbidden.
- exception oldman.exception.OMHashIriError[source]¶
Bases: oldman.exception.OMAccessError
A hash IRI has been given instead of a hash-less IRI.
- exception oldman.exception.OMInternalError[source]¶
Bases: oldman.exception.OMError
Do not expect it.
- exception oldman.exception.OMObjectNotFoundError[source]¶
Bases: oldman.exception.OMAccessError
When the object is not found.
- exception oldman.exception.OMPropertyDefError[source]¶
Bases: oldman.exception.OMSchemaError
Inconsistency in the definition of a supported property.
- exception oldman.exception.OMPropertyDefTypeError[source]¶
Bases: oldman.exception.OMPropertyDefError
A RDF property cannot be both an ObjectProperty and a DatatypeProperty.
- exception oldman.exception.OMReadOnlyAttributeError[source]¶
Bases: oldman.exception.OMEditError
End-users are not allowed to edit this attribute.
- exception oldman.exception.OMRequiredHashlessIRIError[source]¶
Bases: oldman.exception.OMEditError
No hash-less IRI has been given.
- exception oldman.exception.OMRequiredPropertyError[source]¶
Bases: oldman.exception.OMEditError
A required property has no value.
- exception oldman.exception.OMReservedAttributeNameError[source]¶
Bases: oldman.exception.OMAttributeDefError
Some attribute names are reserved and should not be included in the JSON-LD context.
- exception oldman.exception.OMSPARQLError[source]¶
Bases: oldman.exception.OMAccessError
Invalid SPARQL query given.
- exception oldman.exception.OMSPARQLParseError[source]¶
Bases: oldman.exception.OMInternalError
Invalid SPARQL request.
- exception oldman.exception.OMSchemaError[source]¶
Bases: oldman.exception.ModelGenerationError
Error in the schema graph and/or the JSON-LD context.
Bases: oldman.exception.OMEditError
When updating a resource with new types without explicit authorization.
- exception oldman.exception.OMUndeclaredClassNameError[source]¶
Bases: oldman.exception.ModelGenerationError
The name of the model class should be defined in the JSON-LD context.
- exception oldman.exception.OMUniquenessError[source]¶
Bases: oldman.exception.OMEditError
Attribute uniqueness violation.
Example: IRI illegal reusing.
- exception oldman.exception.OMUserError[source]¶
Bases: oldman.exception.OMError
Error when accessing or editing objects.
- exception oldman.exception.OMWrongResourceError[source]¶
Bases: oldman.exception.OMEditError
Not updating the right object.
- exception oldman.exception.UnsupportedDataStorageFeatureException[source]¶
Bases: oldman.exception.OMDataStoreError
Feature not supported by the data store.
oldman.iri module¶
- class oldman.iri.BlankNodeIriGenerator(hostname=u'localhost')[source]¶
Bases: oldman.iri.PrefixedUUIDIriGenerator
Generates skolem IRIs that denote blank nodes.
Parameters: hostname – Defaults to “localhost”.
- class oldman.iri.IncrementalIriGenerator(prefix, data_store, class_iri, fragment=None)[source]¶
Bases: oldman.iri.IriGenerator
Generates IRIs with short numbers.
Beautiful but slow in concurrent settings. The number generation implies a critical section and a sequence of two SPARQL requests, which represents a significant bottleneck.
Parameters: - prefix – IRI prefix.
- graph – rdflib.Graph object where to store the counter.
- class_iri – IRI of the RDFS class of which new Resource objects are instance of. Usually corresponds to the class IRI of the Model object that owns this generator.
- fragment – IRI fragment to append to the hash-less IRI. Defaults to None.
- class oldman.iri.IriGenerator[source]¶
Bases: object
An IriGenerator object generates the IRIs of some new Resource objects.
- class oldman.iri.PrefixedUUIDIriGenerator(prefix, fragment=None)[source]¶
Bases: oldman.iri.IriGenerator
Uses a prefix, a fragment and a unique UUID1 number to generate IRIs.
Recommended generator because UUID1 is robust and fast (no DB access).
Parameters: - prefix – IRI prefix.
- fragment – IRI fragment to append to the hash-less IRI. Defaults to None.
- class oldman.iri.UUIDFragmentIriGenerator[source]¶
Bases: oldman.iri.IriGenerator
Generates an hashed IRI from a hash-less IRI.
Its fragment is a unique UUID1 number.
oldman.model module¶
- class oldman.model.Model(manager, name, class_iri, ancestry_iris, context, om_attributes, id_generator, methods=None)[source]¶
Bases: object
A Model object represents a RDFS class on the Python side.
It gathers OMAttribute objects and Python methods which are made available to Resource objects that are instances of its RDFS class.
It also creates and retrieves Resource objects that are instances of its RDFS class. It manages an IriGenerator object.
Model creation
Model objects are normally created by a ResourceManager object. Please use the oldman.management.manager.ResourceManager.create_model() method for creating new Model objects.
Parameters: - manager – ResourceManager object that has created this model.
- name – Model name. Usually corresponds to a JSON-LD term or to a class IRI.
- class_iri – IRI of the RDFS class represented by this Model object.
- ancestry_iris – ancestry of the attribute class_iri. Each instance of class_iri is also instance of these classes.
- context – An IRI, a list or a dict that describes the JSON-LD context. See http://www.w3.org/TR/json-ld/#the-context for more details.
- om_attributes – dict of OMAttribute objects. Keys are their names.
- id_generator – IriGenerator object that generates IRIs from new Resource objects.
- methods – dict of Python functions that takes as first argument a Resource object. Keys are the method names. Defaults to {}.
- access_attribute(name)[source]¶
Gets an OMAttribute object.
Used by the Resource class but an end-user should not need to call it.
Parameters: name – Name of the attribute. Returns: The corresponding OMAttribute object.
- all(limit=None, eager=False)[source]¶
Finds every Resource object that is instance of its RDFS class.
Parameters: - limit – Upper bound on the number of solutions returned (SPARQL LIMIT). Positive integer. Defaults to None.
- eager – If True loads all the Resource objects within one single SPARQL query. Defaults to False (lazy).
Returns: A generator of Resource objects.
- context[source]¶
An IRI, a list or a dict that describes the JSON-LD context. See http://www.w3.org/TR/json-ld/#the-context for more details.
- create(id=None, hashless_iri=None, **kwargs)[source]¶
Creates a new resource and saves it.
See new() for more details.
- filter(hashless_iri=None, limit=None, eager=False, pre_cache_properties=None, **kwargs)[source]¶
Finds the Resource objects matching the given criteria.
The class_iri attribute is added to the types.
See oldman.management.finder.ResourceFinder.filter() for further details.
- generate_iri(**kwargs)[source]¶
Generates a new IRI.
Used by the Resource class but an end-user should not need to call it.
Returns: A new IRI.
- get(id=None, hashless_iri=None, **kwargs)[source]¶
Gets the first Resource object matching the given criteria.
The class_iri attribute is added to the types. Also looks if reversed attributes should be considered eagerly.
See oldman.management.finder.Finder.get() for further details.
- is_subclass_of(model)[source]¶
Returns True if its RDFS class is a sub-class (rdfs:subClassOf) of the RDFS class of another model.
Parameters: model – Model object to compare with. Returns: True if is a sub-class of the other model, False otherwise.
- methods[source]¶
dict of Python functions that takes as first argument a Resource object. Keys are the method names.
- new(id=None, hashless_iri=None, **kwargs)[source]¶
Creates a new Resource object without saving it.
The class_iri attribute is added to the types.
See new() for more details.
- om_attributes[source]¶
dict of OMAttribute objects. Keys are their names.
oldman.property module¶
- class oldman.property.OMProperty(manager, property_iri, supporter_class_iri, is_required=False, read_only=False, write_only=False, reversed=False, cardinality=None, property_type=None, domains=None, ranges=None)[source]¶
Bases: object
An OMProperty object represents the support of a RDF property by a RDFS class.
It gathers some OMAttribute objects (usually one).
An OMProperty object is in charge of generating its OMAttribute objects according to the metadata that has been extracted from the schema and JSON-LD context.
A property can be reversed: the Resource object to which the OMAttribute objects will be (indirectly) attached is then the object of this property, not its subject (?o ?p ?s).
Consequently, two OMProperty objects can refer to the same RDF property when one is reversed while the second is not.
Parameters: - manager – ResourceManager object.
- property_iri – IRI of the RDF property.
- supporter_class_iri – IRI of the RDFS class that supports the property.
- is_required – If True instances of the supporter class must assign a value to this property for being valid. Defaults to False.
- read_only – If True, the value of the property cannot be modified by a regular end-user. Defaults to False.
- write_only – If True, the value of the property cannot be read by a regular end-user. Defaults to False.
- reversed – If True, the property is reversed. Defaults to False.
- cardinality – Defaults to None. Not yet supported.
- property_type – String. In OWL, a property is either a DatatypeProperty or an ObjectProperty. Defaults to None (unknown).
- domains – Set of class IRIs that are declared as the RDFS domain of the property. Defaults to set().
- ranges – Set of class IRIs that are declared as the RDFS range of the property. Defaults to set().
- add_attribute_metadata(name, jsonld_type=None, language=None, container=None, reversed=False)[source]¶
Adds metadata about a future OMAttribute object.
Parameters: - name – JSON-LD term representing the attribute.
- jsonld_type – JSON-LD type (datatype IRI or JSON-LD keyword). Defaults to None.
- language – Defaults to None.
- container – JSON-LD container (“@set”, “@list”, “@language” or “@index”). Defaults to None.
- reversed – True if the object and subject in RDF triples should be reversed. Defaults to False.
- add_domain(domain)[source]¶
Declares a RDFS class as part of the domain of the property.
Parameters: domain – IRI of RDFS class.
- add_range(p_range)[source]¶
Declares a RDFS class as part of the range of the property.
Parameters: p_range – IRI of RDFS class.
- default_datatype[source]¶
IRI that is the default datatype of the property.
May be None (if not defined or if the property is an owl:ObjectProperty)
- generate_attributes(attr_format_selector)[source]¶
Generates its OMAttribute objects.
Can be called only once. When called a second time, raises an OMAlreadyGeneratedAttributeError exception.
Parameters: attr_format_selector – ValueFormatSelector object.
- om_attributes[source]¶
Set of OMAttribute objects that depends on this property.
oldman.resource module¶
- class oldman.resource.Resource(manager, id=None, types=None, hashless_iri=None, is_new=True, **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.management.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.
Example:
>>> alice = Resource(manager, 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.create(), oldman.model.Model.new(), oldman.management.manager.ResourceManager.create() or oldman.management.manager.ResourceManager.new() for creating new Resource objects.
Parameters: - manager – ResourceManager object. Gives access to the data_graph (where the triples are stored), the union_graph and the resource_cache.
- 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.
- is_new – When is True and id given, checks that the IRI is not already existing in the union_graph. Defaults to True.
- 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_graph and the resource_cache.
Cascade deletion is done for related resources satisfying the test should_delete_resource().
- full_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_user – False 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.
- full_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: - subgraph – rdflib.Graph object containing the full description of the resource.
- initial – True 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_user – False 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.
- hashless_iri[source]¶
Hash-less IRI of the id attribute. Is obtained by removing the fragment from the IRI.
- 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: model – Model object. Returns: True if the resource is instance of the RDFS class.
- is_valid()[source]¶
Tests if the resource is valid.
Returns: False if the resource is invalid, True otherwise.
- classmethod load_from_graph(manager, id, subgraph, is_new=True)[source]¶
Loads a new Resource object from a sub-graph.
Parameters: - manager – ResourceManager object.
- id – IRI of the resource.
- subgraph – rdflib.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_graph and the resource_cache.
Raises an oldman.exception.OMEditError exception if invalid.
Parameters: is_end_user – False 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.
- 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.
oldman.vocabulary module¶
oldman.vocabulary¶
RDF vocabulary specific to OldMan.
TODO: replace these URNs by URLs.
Parent model prioritization
In RDF, a class is often the child of multiple classes. When the code inherited from these classes (common practise in Object-Oriented Programming) is conflicting, arbitration is necessary.
In this library, we provide a RDF vocabulary to declare priorities for each parent of a given child class. A priority statement is declared as follows:
?cls <urn:oldman:model:ordering:hasPriority> [
<urn:oldman:model:ordering:class> ?parent1 ;
<urn:oldman:model:ordering:priority> 2
].
By default, when no priority is declared for a pair (child, parent), its priority value is set to 0.
- oldman.vocabulary.NEXT_NUMBER_IRI = 'urn:oldman:nextNumber'¶
Used to increment IRIs.