Skip to content

bioamla.operations

bioamla.operations

bioamla.operations — the index of everything bioamla can do, shared by its front ends.

An operation is a named unit of work (detect.energy, audio.features, ...) with a typed request, a function and a typed result. The CLI builds requests from command options; the server builds them from HTTP requests; both call :func:run.

Operations are thin adapters over the public library: each maps its request onto calls to documented domain functions (e.g. :func:bioamla.detect.detect_file) and returns their result. Anything an operation can do, a script can do by calling the library directly. See Workspaces, remote work and agent skills.

Operations are declared in their domain packages with :func:operation and loaded on demand, so listing or running one never imports unrelated domains.

Example

from bioamla import operations result = operations.run("detect.energy", {"file": "rec.wav", "threshold_db": -25}) result.count

OperationCancelled

Bases: BioamlaError

The operation stopped because its caller asked it to.

PathField

Marks a request field that names a file or folder; see :data:PathStr.

The field also accepts a :class:~pathlib.Path (stored as a string), and in the request's JSON Schema it gets "format": "path".

OperationContext dataclass

What a front end supplies to an operation run.

Attributes:

Name Type Description
workspace Any

The workspace the operation runs in, if any.

on_progress Callable[[int, int], None] | None

on_progress(done, total) for long operations.

cancelled Callable[[], bool]

Returns True when the caller wants the operation to stop.

path_check Callable[[str], str]

Maps a request path to the path to use, raising if it isn't allowed. The default (local runs) allows anything and returns it as given.

secrets dict[str, str]

Credentials for this run (e.g. {"EBIRD_API_KEY": ...} from a command-line flag). Never part of a request; an operation falls back to the environment where it runs.

on_notice Callable[[str, str], None] | None

on_notice(level, message) for things to tell the caller while the operation runs ("info" or "warning").

notice

notice(level: str, message: str) -> None

Tell the caller something (a no-op without on_notice).

secret

secret(name: str) -> str | None

The credential name from :attr:secrets, else the environment.

resolve_path

resolve_path(path: str | None) -> str | None

path after the context's check (None stays None).

check_cancelled

check_cancelled() -> None

Raise :class:OperationCancelled if the caller asked to stop.

progress

progress(done: int, total: int) -> None

Report progress (a no-op without on_progress), then honour a cancel.

Operation dataclass

A registered operation.

Attributes:

Name Type Description
name str

Dotted name, e.g. detect.energy.

request type

The request class (a validated config with a JSON Schema).

func Callable[[Any, OperationContext], Any]

func(request, context) -> result.

kind OperationKind

quick (answer directly) or job (long; run in the background).

remote bool

Whether a server may run it (False for e.g. local playback).

summary str

One-line description.

path_fields

path_fields() -> tuple[str, ...]

The request's fields that name files or folders.

operation

operation(
    name: str,
    *,
    request: type,
    kind: OperationKind = "quick",
    remote: bool = True,
) -> Callable[[Callable], Callable]

Register the decorated function as the operation name.

The function takes (request, context) and returns the result; it should be a thin adapter over public library functions.

get

get(name: str) -> Operation

The operation called name, loading its domain on first use.

Raises:

Type Description
NotFoundError

If there is no such operation.

DependencyError

If its domain needs an extra that isn't installed.

all_operations

all_operations() -> tuple[list[Operation], dict[str, str]]

Every operation that can load here, by name, and the domains that can't (prefix -> why), e.g. because an extra isn't installed.

path_fields cached

path_fields(request_type: type) -> tuple[str, ...]

Names of the fields of request_type that name files or folders.

Raises:

Type Description
TypeError

If a :data:PathStr sits where :func:run can't check it (in a nested config, a dict, ...).

build_request

build_request(op: Operation, data: Any) -> Any

data (a dict, or already a request) as op's validated request.

Raises:

Type Description
ConfigValidationError

If it doesn't fit the request type.

check_setting_key

check_setting_key(key: str) -> str

key if it names a request field of an operation (detect.energy.threshold_db), the form a workspace setting takes.

Raises:

Type Description
InvalidInputError

If it doesn't (no such operation or field, or the operation's domain needs an extra that isn't installed).

prepare

prepare(
    name: str,
    request: Any,
    context: OperationContext | None = None,
) -> Any

request as the operation name would run it: validated, with every path field through context.resolve_path. :func:run does this first; a front end that runs the operation later (e.g. as a job) can call it to reject a bad request straight away.

Raises:

Type Description
NotFoundError

If there's no such operation.

ConfigValidationError

If the request is invalid.

BioamlaError

Whatever the context's path check raises.

run

run(
    name: str,
    request: Any,
    context: OperationContext | None = None,
) -> Any

Run the operation name on request (a request object or a dict).

Every path field goes through context.resolve_path first.

Raises:

Type Description
NotFoundError

If there's no such operation.

ConfigValidationError

If the request is invalid.

BioamlaError

Whatever the operation raises.