Modules (pyobs.modules)

A module in pyobs is the smalles executable unit. The base class for all modules is Module, so all modules should derive from this class and, usually, implement one or more Interfaces.

Modules are usually not created directly in code, but via a configuration file, which is a YAML file that directly maps to the constructor of the module plus an additional entry class containing the full reference to the class of the module to instantiate.

Take, for instance, the StandAlone class, which has two parameters in its constructor: message and interval. So a valid configuration file would look like this:

class: pyobs.modules.test.Standalone
message: Test
interval: 5

Note that both parameters have default values, so both could be omitted from the configuration. So, this would also work:

class: pyobs.modules.test.Standalone
message: Test

In this case, interval would have to its default value of 10.

Note

Remember that the *args and **kwargs are always forwarded to the super class(es), so the constructor of a module always also provides the parameters from Object and Module.

Quite often, a parameter will accept both an object of a given type and a dict. If this is the case, the dict must be another class definition with a class keyword, referring to a class of the given type. See, for example, the comm parameter of Module: it takes both a dict and a pyobs.comm.Comm. So in a configuration file, we can always specify a Comm object like this:

comm:
    class: pyobs.comm.sleekxmpp.XmppComm
    jid: someone@example.com
    password: secret

An object of type XmppComm (which is a class derived from Comm) will automatically be created.

With a Comm object, proxies to other modules can easily be created (see comm for details):

camera: ICamera = self.comm['camera']
camera.expose()

Sometimes, multiple modules have to run in a single process, so that they can access a common resource. For this case a MultiModule can contain multiple module descriptions.

Module

class Module(name: Optional[str] = None, label: Optional[str] = None, **kwargs: Any)

Bases: pyobs.object.Object, pyobs.interfaces.IModule, pyobs.interfaces.IConfig

Base class for all pyobs modules.

Parameters:
  • name – Name of module. If None, ID from comm object is used.

  • label – Label for module. If None, name is used.

async close() None[source]

Close module.

async execute(method: str, *args: Any, **kwargs: Any) Any[source]

Execute a local method safely with type conversion

All incoming variables in args and kwargs must be of simple type (i.e. int, float, str, bool, tuple) and will be converted to the requested type automatically. All outgoing variables are converted to simple types automatically as well.

Parameters:
  • method – Name of method to execute.

  • *args – Parameters for method.

  • **kwargs – Parameters for method.

Returns:

Response from method call.

Raises:

KeyError – If method does not exist.

async get_config_caps(**kwargs: Any) Dict[str, Tuple[bool, bool, bool]][source]

Returns dict of all config capabilities. First value is whether it has a getter, second is for the setter, third is for a list of possible options..

Returns:

Dict with config caps

async get_config_value(name: str, **kwargs: Any) Any[source]

Returns current value of config item with given name.

Parameters:

name – Name of config item.

Returns:

Current value.

Raises:

ValueError – If config item of given name does not exist.

async get_config_value_options(name: str, **kwargs: Any) List[str][source]

Returns possible values for config item with given name.

Parameters:

name – Name of config item.

Returns:

Possible values.

Raises:

ValueError – If config item of given name does not exist.

async get_error_string(**kwargs: Any) str[source]

Returns description of error, if any.

async get_label(**kwargs: Any) str[source]

Returns label of module.

async get_state(**kwargs: Any) pyobs.utils.enums.ModuleState[source]

Returns current state of module.

async get_version(**kwargs: Any) str[source]

Returns pyobs version of module.

property interfaces: List[Type[pyobs.interfaces.Interface]]

List of implemented interfaces.

async main() None[source]

Main loop for application.

property methods: Dict[str, Tuple[Callable[[...], Any], inspect.Signature]]

List of methods.

name(**kwargs: Any) str[source]

Returns name of module.

async open() None[source]

Open module.

quit() None[source]

Quit module.

async reset_error(**kwargs: Any) bool[source]

Reset error of module, if any. Should be overwritten by derived class to handle error resolution.

async set_config_value(name: str, value: Any, **kwargs: Any) None[source]

Sets value of config item with given name.

Parameters:
  • name – Name of config item.

  • value – New value.

Raises:

ValueError – If config item of given name does not exist or value is invalid.

set_error_string(error: str = '') None[source]

Set error string.

async set_state(state: pyobs.utils.enums.ModuleState, error_string: Optional[str] = None) None[source]

Set state of module.

Parameters:
  • state – New state to set.

  • error_string – If given, set error string.

MultiModule

class MultiModule(modules: Dict[str, Union[pyobs.modules.Module, Dict[str, Any]]], shared: Optional[Dict[str, Union[object, Dict[str, Any]]]] = None, **kwargs: Any)

Bases: pyobs.modules.Module

Wrapper for running multiple modules in a single process.

Parameters:
  • modules – Dictionary with modules.

  • shared – Shared objects between modules.