wei.modules.rest_module

REST Module Convenience Class

Classes

RESTModule([arg_parser, description, model, ...])

A convenience class for creating REST-powered WEI modules.

class wei.modules.rest_module.RESTModule(arg_parser: ArgumentParser | None = None, description: str = '', model: str | None = None, interface: str = 'wei_rest_node', actions: List[ModuleAction] | None = None, resource_pools: List[Any] | None = None, admin_commands: Set[AdminCommands] | None = None, name: str | None = None, host: str | None = '0.0.0.0', port: int | None = 2000, about: ModuleAbout | None = None, **kwargs)

A convenience class for creating REST-powered WEI modules.

__init__(arg_parser: ArgumentParser | None = None, description: str = '', model: str | None = None, interface: str = 'wei_rest_node', actions: List[ModuleAction] | None = None, resource_pools: List[Any] | None = None, admin_commands: Set[AdminCommands] | None = None, name: str | None = None, host: str | None = '0.0.0.0', port: int | None = 2000, about: ModuleAbout | None = None, **kwargs)

Creates an instance of the RESTModule class

about: ModuleAbout | None = None

A ModuleAbout object that describes the module. This is used to provide information about the module to user’s and WEI. Will be generated from attributes if not set.

action(**kwargs)

Decorator to add an action to the module. This decorator can be used to define actions that the module can perform.

Parameters:
  • `name – str` The name of the action. If not provided, the name of the function will be used.

  • `description – str` A description of the action. If not provided, the function’s docstring will be used.

static action_handler(state: State, action: ActionRequest) StepResponse | StepFileResponse

This function is called whenever an action is requested from the module. It should return a StepResponse object that indicates the success or failure of the action. Note: If overridden, this function should handle all actions for the module (i.e. any actions defined using the decorator should also be handled here).

actions: List[ModuleAction] = []

A list of actions that the module can perform.

admin_commands: Set[AdminCommands] = {}

A list of admin commands supported by the module.

arg_parser: ArgumentParser | None = None

An argparse.ArgumentParser object that can be used to parse command line arguments. If not set in the constructor, a default will be used.

cancel()

Decorator to add cancellation functionality to the module

description: str = ''

A description of the module and the devices/resources it controls.

error: str | None = None

Any error message that has occurred during the module’s operation.

static exception_handler(state: State, exception: Exception, error_message: str | None = None)

This function is called whenever a module encounters or throws an irrecoverable exception. It should handle the exception (print errors, do any logging, etc.) and set the module status to ERROR.

static get_action_lock(state: State, action: ActionRequest, blocking: bool = True)

This function is used to ensure the module only performs actions when it is safe to do so. In most cases, this means ensuring the instrument is not currently acting and then setting the module’s status to BUSY to prevent other actions from being taken for the duration. This can be overridden by the developer to provide more specific behavior.

interface: str = 'wei_rest_node'

The interface used by the module.

lock()

Decorator to add locking functionality to the module

model: str | None = None

The model of instrument or resource this module manages.

name: str | None = None

A unique name for this particular instance of this module. This is required, and should generally be set by the command line.

pause()

Decorator to add pause functionality to the module

pre_locked_status = 'INIT'

The status of the module before it was locked

pre_paused_status = 'INIT'

The status of the module before it was paused

static release_action_lock(state: State, action: ActionRequest)

Releases the lock on the module. This should be called after an action is completed. This can be overridden by the developer to provide more specific behavior.

reset()

Decorator to add reset functionality to the module

resource_pools: List[Any] = []

A list of resource pools used by the module.

resume()

Decorator to add resume functionality to the module

safety_stop()

Decorator to add safety_stop functionality to the module

shutdown()

Decorator to add a shutdown_handler to the module

start()

Starts the REST server-based module

startup()

Decorator to add a startup_handler to the module

static startup_thread(state: State)

Runs the startup function for the module in a non-blocking thread, with error handling

state_handler()

Decorator to add custom logic for the published state on the /state endpoint. This should return a dictionary of the module’s current state that is compliant with the wei.types.module_types.ModuleState model.

status: ModuleStatus = 'INIT'

The current status of the module.

unlock()

Decorator to add unlocking functionality to the module

wei_version: str | None = '0.7.2'

The version of WEI that this module is compatible with.