eulfedora – Python objects to interact with the Fedora Commons repository

Server objects

Repository & Resource Index

class eulfedora.server.Repository(root=None, username=None, password=None, request=None)

Pythonic interface to a single Fedora Commons repository instance.

default_object_type

Default type to use for methods that return fedora objects - DigitalObject

find_objects(terms=None, type=None, chunksize=None, **kwargs)

Find objects in Fedora. Find query should be generated via keyword args, based on the fields in Fedora documentation. By default, the query uses a contains (~) search for all search terms. Calls ApiFacade.findObjects(). Results seem to return consistently in ascending PID order.

Example usage - search for all objects where the owner contains ‘jdoe’:

repository.find_objects(ownerId='jdoe')

Supports all search operators provided by Fedora findObjects query (exact, gt, gte, lt, lte, and contains). To specify the type of query for a particular search term, call find_objects like this:

repository.find_objects(ownerId__exact='lskywalker')
repository.find_objects(date__gt='20010302')
Parameters:
  • type – type of objects to return; defaults to DigitalObject
  • chunksize – number of objects to return at a time
Return type:

generator for list of objects

get_next_pid(namespace=None, count=None)

Request next available pid or pids from Fedora, optionally in a specified namespace. Calls ApiFacade.getNextPID().

Deprecated since version 0.14: Mint pids for new objects with eulfedora.models.DigitalObject.get_default_pid() instead, or call ApiFacade.getNextPID() directly.

Parameters:
  • namespace – (optional) get the next pid in the specified pid namespace; otherwise, Fedora will return the next pid in the configured default namespace.
  • count – (optional) get the specified number of pids; by default, returns 1 pid
Return type:

string or list of strings

get_object(pid=None, type=None, create=None)

Initialize a single object from Fedora, or create a new one, with the same Fedora configuration and credentials.

Parameters:
  • pid – pid of the object to request, or a function that can be called to get one. if not specified, get_next_pid() will be called if a pid is needed
  • type – type of object to return; defaults to DigitalObject
Return type:

single object of the type specified

Create :

boolean: create a new object? (if not specified, defaults to False when pid is specified, and True when it is not)

get_objects_with_cmodel(cmodel_uri, type=None)

Find objects in Fedora with the specified content model.

Parameters:
  • cmodel_uri – content model URI (should be full URI in info:fedora/pid:### format)
  • type – type of object to return (e.g., class:DigitalObject)
Return type:

list of objects

ingest(text, log_message=None)

Ingest a new object into Fedora. Returns the pid of the new object on success. Calls ApiFacade.ingest().

Parameters:
  • text – full text content of the object to be ingested
  • log_message – optional log message
Return type:

string

purge_object(pid, log_message=None)

Purge an object from Fedora. Calls ApiFacade.purgeObject().

Parameters:
  • pid – pid of the object to be purged
  • log_message – optional log message
Return type:

boolean

risearch

instance of ResourceIndex, with the same root url and credentials

search_fields

fields that can be searched against in find_objects()

search_fields_aliases

human-readable aliases for oddly-named fedora search fields

class eulfedora.server.ResourceIndex(opener)

Python object for accessing Fedora’s Resource Index.

RISEARCH_FLUSH_ON_QUERY

Specify whether or not RI search queries should specify flush=true to obtain the most recent results. If flush is specified to the query method, that takes precedence.

Irrelevant if Fedora RIsearch is configured with syncUpdates = True.

find_statements(query, language='spo', type='triples', flush=None)

Run a query in a format supported by the Fedora Resource Index (e.g., SPO os Sparql) and return the results.

Parameters:
  • query – query as a string
  • language – query language to use; defaults to ‘spo’
  • type – type of query - tuples or triples; defaults to ‘triples’
  • flush – flush results to get recent changes; defaults to False
Return type:

rdflib.ConjunctiveGraph when type is triples; list of dictionaries (keys based on return fields) when type is tuples

get_objects(subject, predicate)

Search for all subjects related to the specified subject and predicate.

Parameters:
  • subject
  • object
Return type:

generator of RDF statements

get_predicates(subject, object)

Search for all subjects related to the specified subject and object.

Parameters:
  • subject
  • object
Return type:

generator of RDF statements

get_subjects(predicate, object)

Search for all subjects related to the specified predicate and object.

Parameters:
  • predicate
  • object
Return type:

generator of RDF statements

sparql_query(query, flush=None)

Run a Sparql query.

Parameters:query – sparql query string
Return type:list of dictionary

Create and run a subject-predicate-object (SPO) search. Any search terms that are not specified will be replaced as a wildcard in the query.

Parameters:
  • subject – optional subject to search
  • predicate – optional predicate to search
  • object – optional object to search
Return type:

rdflib.ConjunctiveGraph

spoencode(val)

Encode search terms for an SPO query.

Parameters:val – string to be encoded
Return type:string

Django-integration

views Fedora views

Management commands

The following management commands will be available when you include eulfedora in your django INSTALLED_APPS and rely on the existdb settings described above.

For more details on these commands, use manage.py <command> help

  • syncrepo - load simple content models and fixture object to the configured fedora repository

eulfedora Template tags

eulfedora adds custom template tags for use in templates.

fedora_access

Catch fedora failures and permission errors encountered during template rendering:

{% load fedora %}

{% fedora_access %}
   <p>Try to access data on fedora objects which could be
     <span class='{{ obj.inaccessible_ds.content.field }}'>inaccessible</span>
     or when fedora is
     <span class='{{ obj.regular_ds.content.other_field }}'>down</span>.</p>
{% permission_denied %}
   <p>Fall back to this content if the main body results in a permission
     error while trying to access fedora data.</p>
{% fedora_failed %}
   <p>Fall back to this content if the main body runs into another error
     while trying to access fedora data.</p>
{% end_fedora_access %}

The permission_denied and fedora_failed sections are optional. If only permission_denied is present then non-permission errors will result in the entire block rendering empty. If only fedora_failed is present then that section will be used for all errors whether permission-related or not. If neither is present then all errors will result in the entire block rendering empty.

Note that when Django’s TEMPLATE_DEBUG setting is on, it precludes all error handling and displays the Django exception screen for all errors, including fedora errors, even if you use this template tag. To disable this Django internal functionality and see the effects of the fedora_access tag, add the following to your Django settings:

TEMPLATE_DEBUG = False

testutil Unittest utilities