magscope.scripting#

Utilities for registering and executing scripted automation flows.

This module provides the runtime that powers MagScope’s lightweight automation system. Users describe a sequence of actions in a script file by instantiating Script and adding IPC Command instances. The resulting steps are validated against ScriptRegistry to ensure that each call is valid before being executed by ScriptManager.

Only methods decorated with register_script_command() are exposed to the script environment. Script execution runs in its own manager process and communicates with other parts of the application through the standard IPC mechanism.

Attributes#

Classes#

ScriptStep

Structured representation of a single scripted action.

ScriptCommandRegistration

Metadata binding a script-visible method to its IPC command type.

Script

Container that records the steps of a user-authored script.

ScriptRegistry

Tracks scriptable methods that managers expose to the scripting API.

ScriptStatus

Lifecycle stages of a script managed by ScriptManager.

ScriptManager

Process that coordinates script execution and forwards IPC messages.

Module Contents#

magscope.scripting.logger[source]#
class magscope.scripting.ScriptStep[source]#

Structured representation of a single scripted action.

command: magscope.ipc_commands.Command[source]#
wait: bool = False[source]#
class magscope.scripting.ScriptCommandRegistration[source]#

Metadata binding a script-visible method to its IPC command type.

cls_name: str[source]#
meth_name: str[source]#
command_type: type[magscope.ipc_commands.Command][source]#
callable: Callable[source]#
class magscope.scripting.Script[source]#

Container that records the steps of a user-authored script.

steps: list[ScriptStep] = [][source]#
append(command: magscope.ipc_commands.Command, *, wait: bool = False)[source]#

Append an IPC command to the script.

class magscope.scripting.ScriptRegistry[source]#

Tracks scriptable methods that managers expose to the scripting API.

avoided_names = ['sentinel', 'send_ipc'][source]#
_methods: dict[type[magscope.ipc_commands.Command], ScriptCommandRegistration][source]#
__call__(command_type: type[magscope.ipc_commands.Command]) ScriptCommandRegistration[source]#

Return the registered callable for command_type.

Raises:

ValueError: If command_type has not been registered.

register_class_methods(cls)[source]#

Inspect cls for scriptable methods and add them to the registry.

check_script(script: Iterable[ScriptStep], *, command_registry=None)[source]#

Validate a compiled script before it is executed.

Checks include verifying that the method exists, arguments bind against the callable signature, and that reserved flags such as wait have the correct types. When command_registry is provided, the command must also map to a registered IPC handler so that ScriptManager can dispatch it.

static _collect_script_registrations(cls)[source]#

Yield scriptable methods declared on cls and its bases.

static get_class_name(cls)[source]#

Return the class name for a class or instance.

class magscope.scripting.ScriptStatus[source]#

Bases: enum.StrEnum

Lifecycle stages of a script managed by ScriptManager.

EMPTY = 'Empty'[source]#
LOADED = 'Loaded'[source]#
RUNNING = 'Running'[source]#
PAUSED = 'Paused'[source]#
FINISHED = 'Finished'[source]#
ERROR = 'Error'[source]#
class magscope.scripting.ScriptManager[source]#

Bases: magscope.processes.ManagerProcessBase

Process that coordinates script execution and forwards IPC messages.

_script: list[ScriptStep] = [][source]#
_script_index: int = 0[source]#
_script_length: int = 0[source]#
script_registry[source]#
_script_status: ScriptStatus[source]#
_script_waiting: bool = False[source]#
_script_sleep_duration: float | None = None[source]#
_script_sleep_start: float = 0[source]#
setup()[source]#

Initialise process state.

Currently no special setup is required, but the hook is retained for symmetry with other ManagerProcessBase implementations.

do_main_loop()[source]#

Main loop executed by the process infrastructure.

start_script()[source]#

Start the currently loaded script from the beginning.

pause_script()[source]#

Pause the running script.

resume_script()[source]#

Resume a script that was previously paused.

load_script(path)[source]#

Load and validate a script from path.

The script file is executed in an isolated namespace. Exactly one Script instance must be created in that file; its recorded steps are copied locally after validation.

_execute_script_step(step: ScriptStep)[source]#

Dispatch a single script step to its owning manager.

update_waiting()[source]#

Let the script resume after waiting for a previous step to finish.

start_sleep(duration: float)[source]#

Pause the script for duration seconds.

_do_sleep()[source]#

Check whether the scripted sleep period has elapsed.

_send_script_step_update(current_step: int | None, *, description: str | None = None)[source]#

Notify the GUI of the current script position.

static _format_script_step(step: ScriptStep) str[source]#

Return a user-friendly description of a script step.

_set_script_status(status)[source]#

Notify the GUI that the script status has changed.

_handle_script_error(message: str, *, details: str | None)[source]#

Report a script error to the GUI and transition to the error state.