Skip to main content

High-level classes and functions for interacting with a Fedora repository

Project description

plastron-repo

Fedora repository resources and operations

Repository

There are several class methods on the Repository class available as shortcuts to quickly create a Repository object.

# simple repo, no authentication
from plastron.repo import Repository

repo = Repository.from_url('http://localhost:8080/fcrepo/rest')
# repo with HTTP Basic authentication
from plastron.repo import Repository
from requests.auth import HTTPBasicAuth

repo = Repository.from_url(
    url='http://localhost:8080/fcrepo/rest',
    # any class that implements requests.auth.AuthBase will work here
    auth=HTTPBasicAuth('username', 'password'),
)
# repo with settings from a configuration dictionary
from plastron.repo import Repository

repo = Repository.from_config({
    'REST_ENDPOINT': 'http://localhost:8080/fcrepo/rest',
    'RELPATH': '/',
    'REPO_EXTERNAL_URL': 'http://fcrepo-local/fcrepo/rest',
    'STRUCTURE': 'hierarchical',
    'SERVER_CERT': 'path/to/cert.pem',
    # authentication section
    'FEDORA_USER': 'username',
    'FEDORA_PASSWORD': 'password',
})
# repo with settings from a configuration YAML file;
# the structure of the YAML file is the same as the
# configuration dictionary
from plastron.repo import Repository

repo = Repository.from_config_file('config.yml')

You can always use the full constructor as well.

from requests.auth import HTTPBasicAuth

from plastron.client import Client, Endpoint
from plastron.repo import Repository, RepositoryStructure

repo = Repository(
    client=Client(
        endpoint=Endpoint(
            url='http://localhost:8080/fcrepo/rest',
            default_path='/',
            external_url='http://fcrepo-local/fcrepo/rest',
        ),
        auth=HTTPBasicAuth('username', 'password'),
        structure=RepositoryStructure.HIERARCHICAL,
        server_cert='path/to/cert.pem',
        ua_string='my-client/3.14',
        on_behalf_of='user',
    )
)

Repository Resources

The repository resource classes represent a single URL-addressable HTTP resource that is stored in a repository. The most basic of these is RepositoryResource.

from plastron.repo import Repository, RepositoryResource

repo = Repository.from_url('http://localhost:8080/fcrepo/rest')

# Repository object support indexing with a path
resource = repo['/foo']
assert resource.url == 'http://localhost:8080/fcrepo/rest/foo'
assert isinstance(resource, RepositoryResource)

# this is merely syntactic sugar for the get_resource() method
resource = repo.get_resource('/foo')
assert resource.url == 'http://localhost:8080/fcrepo/rest/foo'
assert isinstance(resource, RepositoryResource)

When getting a resource from a Repository, you can provide a class to instantiate (it defaults to RepositoryResource). So to get an LDP container representation of a resource, use ContainerResource instead.

from plastron.repo import Repository, ContainerResource

repo = Repository.from_url('http://localhost:8080/fcrepo/rest')

container = repo.get_resource('/bar', ContainerResource)
assert container.url == 'http://localhost:8080/fcrepo/rest/bar'
assert isinstance(container, ContainerResource)

# you can also use slice notation to provide the class
container = repo['/bar':ContainerResource]
assert container.url == 'http://localhost:8080/fcrepo/rest/bar'
assert isinstance(container, ContainerResource)

Retrieving Data

None of the methods above actually send any HTTP requests. To connect to the repository and retrieve data, use the read() method on a resource.

RDF Descriptions

Repository resources can be described using RDF resource classes defined using the plastron-rdf library. A resource description is an RDF resource class that shares its underlying RDFLib Graph object with a repository resource. Thus changes to the resource description object will be reflected in the repository resource's graph and will be saved when the repository resource is written back to the repository.

Here is an (extremely simplified) example of retrieving a resource, updating some properties, and writing it back to the repository.

from rdflib import Literal, URIRef

from plastron.rdfmapping.resources import RDFResource
from plastron.repo import Repository, ContainerResource

repo = Repository.from_url('http://localhost:8080/fcrepo/rest')
resource = repo['/obj/123':ContainerResource]
resource.read()

# RDFResource is a very simple RDF description class
# it maps rdf:type to rdf_type and rdfs:label to label
obj = resource.describe(RDFResource)
obj.label = Literal('Digital Object 123')
obj.rdf_type.add(URIRef('http://pcdm.org/models#Object'))

assert obj.has_changes

# sends a PATCH request to the repository with a SPARQL
# update query built from the differences between the
# graph read from the repo and the current graph
resource.update()

Binaries

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plastron_repo-4.4.0.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

plastron_repo-4.4.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file plastron_repo-4.4.0.tar.gz.

File metadata

  • Download URL: plastron_repo-4.4.0.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for plastron_repo-4.4.0.tar.gz
Algorithm Hash digest
SHA256 139ba32bcf5be9d1284a2728910df90967e8eed34e0251969e61919255fc6158
MD5 f6e28ff5ad4c2c6f0d556cc4b224b0b9
BLAKE2b-256 4bd3eb587999b20d3b902c4e446d7c50b652efe6e4e881b8d83d514467cf5ee2

See more details on using hashes here.

File details

Details for the file plastron_repo-4.4.0-py3-none-any.whl.

File metadata

  • Download URL: plastron_repo-4.4.0-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for plastron_repo-4.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f1839abeb8ccde022600bacaf6217f8969b335c6889291cc6f24e5b0b0d2400
MD5 5220a4abe0f841b67f485346a4c3dee1
BLAKE2b-256 4e9882a3e797383ea990a62cde4985687d6a313fe9309b856b380630dca081cd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page