Application context

class mundo.api.context.ApplicationContext(
broadcast: Callable,
serialize_plugins: Callable
)

The application context. This class contains references to class instances and other information that should be available to plugins. It is also used internally by e.g. the System class.

The main reason for this separation of application state and the System class is to hide application implementation details from external plugins. Using this context, only the relevant (and safe) information is exposed to external code.

__init__(
broadcast: Callable,
serialize_plugins: Callable
) None

Initializes the application context. Loads the application configuration and instantiates internal classes.

Parameters
  • broadcast – Method used to broadcast events

  • serialize_plugins – Method used to serialize all loaded plugins

property storage: mundo.api.storage.Storage

Returns the internal instance to the Storage class.

Returns

The internal storage instance

property system: mundo.api.config.system.SystemConfig

Returns the internal instance to the SystemConfig class.

Returns

The current system (global) configuration

property session: Optional[mundo.api.config.session.SessionConfig]

Returns the internal instance to the SessionConfig class. This may be None, since a session is not always loaded.

Returns

The current session configuration

property platform: Optional[mundo.api.config.platform.PlatformConfig]

Returns the internal instance to the PlatformConfig class. This may be None, since the platform configuration is based on the loaded session.

Returns

The current session configuration

property controllers: Dict[str, Type[mundo.api.controller.BaseController]]

Returns the dynamically loaded motor controller drivers exposed by activated plugins.

Returns

List of motor controller drivers

property movement_travel_range: float

The maximum physical travel range of the connected motors, specified in nanometers.

Returns

The travel range in nanometers

property movement_tolerance: float

The tolerance of the connected motors. This can be used to define the minimum distance that a motor can travel accurately, as well as validating if a position matches the target position or not.

Returns

The movement tolerance of the connected motors, specified in nanometers. If no motors are connected, 0 is returned

property selected_sample: Optional[mundo.api.sample.Sample]

Returns the currently selected sample, or None if no sample is selected.

property event_listeners: Dict[str, List[Callable]]

A dictionary containing the registered event listeners for each event.

set_connected(
tolerance: float,
travel_range: float
) None

Saves the connection properties to the context and broadcasts the ON_CONNECTED event.

Parameters
  • travel_range – The travel range of the connected motors

  • tolerance – The tolerance (movement accuracy) of the connected motors

set_disconnected() None

Disconnected from the controllers and broadcasts the ON_DISCONNECTED event.

has_saved_connection() bool

Checks if there is saved connection parameters.

Returns

True if the system config contains connection parameters for all axes. False otherwise

clear_controllers() None

Clears the list of loaded controllers. This should be called before reloading plugins.

register_controller(
name: str,
controller: Type[mundo.api.controller.BaseController]
) bool

Register a custom controller that can be used to control the motors. controller must extend the BaseController class and strictly follow the implementation details described in its methods.

This method should be used to dynamically load controller drivers from plugins. The name is a unique identifier and will be used to save the controller type for a platform to a configuration.

Parameters
  • name – The unique name of the controller type

  • controller – The controller class type reference

register_event_listener(
event: mundo.api.events.Event,
fn: Callable
) None

Register an event listener function that should be called when the given event occurs.

Parameters
  • event – The event to listen for

  • fn – The function to call when the event occurs

load_latest_session() None

Attempts to load the previously used session from the system config. If no session could be loaded, a new empty session is created.

new_platform(
filename: str,
save: bool = True
) bool

Creates a new platform with a set filename.

Parameters

filename – The filename of the new platform

load_platform(
filename: Optional[str],
save: bool = True
) bool

Loads the platform configuration.

Parameters
  • filename – The filename of the platform to load, if any

  • save – If the current platform (if any) should be saved to file before being closed

Returns

True if the platform was loaded successfully. False otherwise

load_session(
filename: Optional[str],
save: bool = True
) bool

Loads a session configuration. If the filename is not specified, a new session will be created.

Parameters
  • filename – The filename of the session to load, if any

  • save – If the current platform (if any) should be saved to file before being closed

Returns

True if the session was loaded successfully. False otherwise

save_system() None

Saves the system configuration.

save_platform(
update_session: bool = True
) None

Saves the current platform configuration.

Parameters

update_session – If the session should be updated with the (possible) new platform configuration options

save_session(
file_name: Optional[str] = None
) None

Saves the current session configuration. If a filename is given, the session gets saved as a new session file.

Parameters

file_name – Optional file name for a new session file

save_all() None

Saves all open configurations.

reset() None

Reloads the application context by re-reading the system configuration file, and resetting all other application state.

Note, this method does not save any data to files. It only resets internal variables and state. You should probably not use this method directly, unless you really know what you are doing.

update_sample_state(
sample: mundo.api.sample.Sample,
new_state: mundo.api.sample.SampleState
) None

Updates the state of a sample and broadcasts the ON_SAMPLES_UPDATED event,

Parameters
  • sample – The sample to update the state of

  • new_state – The state which is to be set on ~sample~.

update_samples(
samples: List[mundo.api.sample.Sample]
) None

Broadcasts the ON_SAMPLES_UPDATED event, indicating that samples has been updated, e.g. the state has changed. A list of all updated samples will be sent along with the broadcasted event.

If you want to update the sample state, please use the update_sample_state() method instead.

Parameters

samples – A list of samples that has been updated

enqueue_sample(
sample: mundo.api.sample.Sample,
insert_first=False
) None

Enqueues a sample and broadcasts the ON_QUEUE_UPDATED event.

Parameters
  • sample – The sample to enqueue

  • insert_first – If the sample should be inserted first in the sample queue

remove_sample_from_queue(
index: int,
sample: mundo.api.sample.Sample,
set_done=True
) None

Dequeues a sample and broadcasts the ON_QUEUE_UPDATED, and ON_SAMPLES_UPDATED events.

Parameters
  • index – The index of the sample to dequeue in the sample queue

  • sample – The sample to dequeue

  • set_done – If the sample is done and should update its state

  • all – If all instances of the sample should be dequeued

dequeue_sample() Optional[mundo.api.sample.Sample]

Dequeues a sample and broadcasts the ON_QUEUE_UPDATED, and ON_SAMPLES_UPDATED events.

Returns

The dequeued sample, if any

clear_queue() None

Clears the sample queue and updates the state of the samples. Broadcasts the ON_QUEUE_UPDATED, and ON_SAMPLES_UPDATED events.

update_process_mode(
process_mode: mundo.api.process_status.ProcessMode
) None

Update process_status mode and broadcast the ON_MODE_UPDATE event.

Parameters

process_mode – The mode to update

update_selected_sample(
sample: Optional[mundo.api.sample.Sample],
should_process: bool = False
)

Updates the currently selected sample, and broadasts the ON_SAMPLE_SELECTED event.

Parameters
  • sample – The sample that’s been selected or deselected

  • should_process – Used when a widget or plugin that broadcasts an event also listens to the same event, in order not call unwanted methods.

start_process() None

Starts a process by broadcasting the related events and saving all configuration options.

update_currently_processing(
sample: Optional[mundo.api.sample.Sample]
) None

Sets the currently processing sample.

Parameters

sample – The sample that is currently being processed, if any

stop_process() None

Stops a process by broadcasting the related events.

reset_process() None

Resets a process by broadcasting the related events.