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 Interface
s.
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: str | None = None, label: str | None = None, own_comm: bool = True, additional_config_variables: List[str] | None = None, **kwargs: Any)
Bases:
Object
,IModule
,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.
own_comm – If True, module owns comm and opens/closes it.
additional_config_variables – List of additional variable names available to remote config getter/setter.
- 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_state(**kwargs: Any) ModuleState [source]
Returns current state of module.
- property methods: Dict[str, Tuple[Callable[[...], Any], Signature, Dict[Any, Any]]]
List of methods.
- property name: str
Returns name of 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.
- async set_state(state: ModuleState, error_string: str | None = None) None [source]
Set state of module.
- Parameters:
state – New state to set.
error_string – If given, set error string.