examples

Reference backend implementations — small, dependency-free, copyable samples that satisfy otto’s backend contracts and are conformance-verified in otto’s own test suite.

In-memory reference LabRepository (sample).

A teaching/reference host-source backend: it holds a mapping of lab name to a list of host dicts and builds real hosts via otto.storage.create_host_from_dict(). It needs no files or network, so it runs inside doctests and the conformance suite, and SUT authors can copy it as a starting point.

Register it from an init module and select it by name:

from otto.storage import register_lab_repository
from otto.examples.lab_repository import ExampleLabRepository

register_lab_repository("example", ExampleLabRepository)

then in .otto/settings.toml:

[lab]
backend = "example"

Direct usage:

>>> from otto.examples.lab_repository import ExampleLabRepository
>>> repo = ExampleLabRepository()
>>> repo.list_labs()
['east', 'west']
>>> lab = repo.load_lab("east")
>>> lab.name
'east'
>>> len(lab.hosts)
1
class otto.examples.lab_repository.ExampleLabRepository(*, repo_dir: Path | None = None, labs: dict[str, list[dict[str, Any]]] | None = None)

Bases: object

In-memory LabRepository reference backend.

Parameters

repo_dirPath | None

Accepted for factory/registry uniformity — otto.storage.build_lab_repository() constructs a custom backend as cls(repo_dir=..., **kwargs). This in-memory sample has no files to resolve, so it is ignored.

labsdict[str, list[dict]] | None

Optional mapping of lab name to host dicts. Defaults to a small built-in demo dataset.

load_lab(name: str, preferences: dict[str, dict[str, Any]] | None = None) Lab
list_labs() list[str]

In-memory reference ReservationBackend (sample).

A teaching/reference reservation backend backed by a plain user -> resources mapping. It needs no files or network, demonstrates multi-holder who_reserved and the optional SupportsUsernameCompletion capability, and is conformance-verified in otto’s own suite.

Register it from an init module and select it by name:

from otto.reservations import register_reservation_backend
from otto.examples.reservations import ExampleReservationBackend

register_reservation_backend("example", ExampleReservationBackend)

then in .otto/settings.toml:

[reservations]
backend = "example"

Direct usage:

>>> from otto.examples.reservations import ExampleReservationBackend
>>> backend = ExampleReservationBackend()
>>> backend.backend_name()
'example'
>>> backend.who_reserved("shared")
['alice', 'bob']
>>> sorted(backend.get_reserved_resources("alice"))
['lab-a', 'shared']
>>> backend.list_usernames()
['alice', 'bob']
class otto.examples.reservations.ExampleReservationBackend(*, url: str | None = None, reservations: dict[str, list[str]] | None = None)

Bases: object

In-memory ReservationBackend reference backend.

Also implements the optional SupportsUsernameCompletion capability.

Parameters

urlstr | None

Accepted for factory uniformity — otto.reservations.build_backend() may call cls(url=url, ...). This in-memory sample ignores it.

reservationsdict[str, list[str]] | None

Optional mapping of username to the resources they hold. Defaults to a small built-in demo dataset.

get_reserved_resources(username: str) set[str]
who_reserved(resource: str) list[str]
backend_name() str
list_usernames() list[str]