Skip to content

Commands

Python API reference for the tolomeo.commands package and the command errors in tolomeo.exceptions.

A command is a class whose execute coroutine receives a context and returns the response payload. Plugin commands are listed in the plugin's Meta.commands. For the OTA commands and their payloads, see OTA Commands.


Payload Models

tolomeo.commands.CommandPayload

Bases: BaseModel

Base class for the payload models of commands.

Unknown fields are ignored, so callers can keep sending extra keys without breaking a command.

Source code in tolomeo/commands/base.py
class CommandPayload(BaseModel):
    """Base class for the payload models of commands.

    Unknown fields are ignored, so callers can keep sending extra keys
    without breaking a command.

    """

    model_config = ConfigDict(extra="ignore")

CommandStr

tolomeo.commands.CommandStr is a str field type for payload models that also accepts JSON numbers and booleans, stringified with str (123 becomes "123"). None, lists and objects are still rejected, and the field's JSON schema stays {"type": "string"}.


PluginCmd

tolomeo.commands.PluginCmd

Bases: BaseCmd[TCmd]

A command that acts on a plugin.

Source code in tolomeo/commands/plugin/base.py
class PluginCmd(BaseCmd[TCmd]):
    """A command that acts on a plugin."""

tolomeo.commands.PluginCmdContext dataclass

Bases: BaseCmdContext, Generic[P]

Context for a command acting on a plugin.

Attributes:

Name Type Description
plugin P

The plugin the command acts on.

Source code in tolomeo/commands/plugin/base.py
@dataclass
class PluginCmdContext(BaseCmdContext, Generic[P]):
    """Context for a command acting on a plugin.

    Attributes:
        plugin: The plugin the command acts on.

    """

    plugin: P

ServiceCmd

tolomeo.commands.ServiceCmd

Bases: BaseCmd[TCmd]

A command that acts on a service.

Source code in tolomeo/commands/service/base.py
class ServiceCmd(BaseCmd[TCmd]):
    """A command that acts on a service."""

tolomeo.commands.ServiceCmdContext dataclass

Bases: BaseCmdContext, Generic[S]

Context for a command acting on a service.

Attributes:

Name Type Description
service S

The service the command acts on.

Source code in tolomeo/commands/service/base.py
@dataclass
class ServiceCmdContext(BaseCmdContext, Generic[S]):
    """Context for a command acting on a service.

    Attributes:
        service: The service the command acts on.

    """

    service: S

Errors

tolomeo.exceptions.CommandError

Bases: Exception

Base for expected, client-caused command-handling failures.

These represent bad input from a caller (unknown command, bad payload), not internal bugs. Callers may catch this family to log without a stack trace and return a structured error response.

Source code in tolomeo/exceptions.py
class CommandError(Exception):
    """Base for expected, client-caused command-handling failures.

    These represent bad input from a caller (unknown command, bad payload),
    not internal bugs. Callers may catch this family to log without a stack
    trace and return a structured error response.

    """

    def __str__(self) -> str:
        # Return the plain message. Avoids KeyError's quote-wrapping of
        # args[0] so logs and error responses read cleanly.
        return str(self.args[0]) if self.args else super().__str__()

tolomeo.exceptions.MessageFormatError

Bases: CommandError, KeyError

Raised when a required key is missing from a message dict.

Parameters:

Name Type Description Default
key str

The offending key.

required
detail str | None

What is wrong with the message, when it is not simply a missing key. Replaces the default "missing key" text.

None

Attributes:

Name Type Description
key

The offending key.

Source code in tolomeo/exceptions.py
class MessageFormatError(CommandError, KeyError):
    """Raised when a required key is missing from a message dict.

    Args:
        key: The offending key.
        detail: What is wrong with the message, when it is not simply a missing
            key. Replaces the default "missing key" text.

    Attributes:
        key: The offending key.

    """

    def __init__(self, key: str, *, detail: str | None = None):
        reason = detail or f"the required key '{key}' is missing from the dictionary"
        super().__init__(f"Message format error: {reason}")
        self.key = key

tolomeo.exceptions.UnknownCommandError

Bases: CommandError, ValueError

Raised when a command name is not registered.

Parameters:

Name Type Description Default
command str

The unknown command name.

required

Attributes:

Name Type Description
command

The unknown command name.

Source code in tolomeo/exceptions.py
class UnknownCommandError(CommandError, ValueError):
    """Raised when a command name is not registered.

    Args:
        command: The unknown command name.

    Attributes:
        command: The unknown command name.

    """

    def __init__(self, command: str):
        super().__init__(f"Unknown command error: {command}")
        self.command = command

tolomeo.exceptions.MissingPluginIdError

Bases: CommandError, ValueError

Raised when a command payload has no plugin 'id'.

Source code in tolomeo/exceptions.py
class MissingPluginIdError(CommandError, ValueError):
    """Raised when a command payload has no plugin 'id'."""

    def __init__(self) -> None:
        super().__init__("Missing plugin 'id' in command payload.")

tolomeo.exceptions.PluginNotFoundError

Bases: CommandError, KeyError

Raised when no plugin is registered for the given id.

Parameters:

Name Type Description Default
plugin_id str

The id used for lookup.

required
Source code in tolomeo/exceptions.py
class PluginNotFoundError(CommandError, KeyError):
    """Raised when no plugin is registered for the given id.

    Args:
        plugin_id: The id used for lookup.

    """

    def __init__(self, plugin_id: str):
        super().__init__(f"Plugin with id '{plugin_id}' not found.")
        self.plugin_id = plugin_id

tolomeo.exceptions.InvalidPayloadError

Bases: CommandError, ValueError

Raised when a command payload does not match what the command expects.

Parameters:

Name Type Description Default
command str

The name of the command that rejected the payload.

required
details str

What is wrong with the payload.

required

Attributes:

Name Type Description
command

The name of the command that rejected the payload.

details

What is wrong with the payload.

Source code in tolomeo/exceptions.py
class InvalidPayloadError(CommandError, ValueError):
    """Raised when a command payload does not match what the command expects.

    Args:
        command: The name of the command that rejected the payload.
        details: What is wrong with the payload.

    Attributes:
        command: The name of the command that rejected the payload.
        details: What is wrong with the payload.

    """

    def __init__(self, command: str, details: str):
        super().__init__(f"Invalid payload for '{command}': {details}")
        self.command = command
        self.details = details

    @classmethod
    def from_validation_error(cls, command: str, error: ValidationError) -> InvalidPayloadError:
        """Build the error from a Pydantic validation failure.

        Args:
            command: The name of the command that rejected the payload.
            error: The validation failure, one `loc: message` entry per issue.

        Returns:
            The error, with every issue listed in `details`.

        """
        details = "; ".join(
            f"{'.'.join(str(part) for part in e['loc']) or '<payload>'}: {e['msg']}" for e in error.errors()
        )
        return cls(command, details)

from_validation_error(command, error) classmethod

Build the error from a Pydantic validation failure.

Parameters:

Name Type Description Default
command str

The name of the command that rejected the payload.

required
error ValidationError

The validation failure, one loc: message entry per issue.

required

Returns:

Type Description
InvalidPayloadError

The error, with every issue listed in details.

Source code in tolomeo/exceptions.py
@classmethod
def from_validation_error(cls, command: str, error: ValidationError) -> InvalidPayloadError:
    """Build the error from a Pydantic validation failure.

    Args:
        command: The name of the command that rejected the payload.
        error: The validation failure, one `loc: message` entry per issue.

    Returns:
        The error, with every issue listed in `details`.

    """
    details = "; ".join(
        f"{'.'.join(str(part) for part in e['loc']) or '<payload>'}: {e['msg']}" for e in error.errors()
    )
    return cls(command, details)