1.2. eventstreamr2.lib.amp.mixins — Service Mixins¶
This module provides some basic mixins. All the mixin classes are new-style classes which means that they support and rely on the correct calls to super().
To ensure that the mixins are called(some Twisted classes are still classic class) place them before the Twisted classes when creating a class, like the example below:
class MyService(InternalServiceMixin, twisted.application.service.Service):
pass
1.2.1. InternalServiceMixin — Mixin for a better service¶
This mixin provides the common code for storing the reactor and the configuration manager(as well as any more that are required). It also has methods to create new services with these values. All new services should use this class.
- class eventstreamr2.lib.amp.mixins.InternalServiceMixin(*args, **kwargs)[source]¶
A base class that provides easier implementation of sub-services. It automatically initilises the configuration manager and reactor passed in.
To use simple extend this class and define your init method as shown:
def __init__(self, ..., **kwargs): super(..., self).__init__(self, **kwargs) ...
Variables: - _config_mgr – A configuration manager instance which contains all the configurations.
- _reactor – A twisted reactor instance to be used for this service. Use this over importing twisted.internet.reactor as it allows for easier testing.
- __init__(*args, **kwargs)[source]¶
This method initilises the service with the configuration manager and reactor passed in as keyword args.
Parameters: - _config_mgr – The configuration manager for this service(KW args only)
- _reactor – The reactor for this service(KW args only)
- _init_new_service(service_class, *args, **kwargs)[source]¶
Create a new service using this service’s configuration manager and reactor. Additional parameters after the service_class will be passed through to the class’s constructor.
Parameters: - service_class – The service class to create.
- *args – The positional arguments to send to the service class’s constructor.
- **kwargs – The keyword arguments to send to the service class’s constructor. Note that _config_mgr and _reactor are always overwritten.
1.2.2. CommandRegistrationServiceMixin — Mixin for easier commands¶
This mixin provides an easier method of assigning callbacks to commands. An example of using this class is provided in the tutorials
- class eventstreamr2.lib.amp.mixins.CommandRegistrationServiceMixin(helper, command_responder_pairs, *args, **kwargs)[source]¶
This mixin allows services to dictate the responders for a set of commands; then have them automatically registered and de-registered when the service is started and stoped.
Todo
Add methods to change the command bindings on the fly.
Todo
Consider changing this to use a class variable for the responder_pairs and then binding them to self when the service is started.
- __init__(helper, command_responder_pairs, *args, **kwargs)[source]¶
Simply provide the configuration helper and then a list
Parameters: - helper (eventstreamr2.lib.commands.ConfigurationHelper) – The configuration helper the commands provided are attached to.
- command_responder_pairs (list(tuple(Command, function))) – A list of 2-tuples mapping a command to a function.
- startService()[source]¶
Registers the command-responder pairs given in the constructor with the configuration helper also given in the constructor.
- startService()[source]
Registers the command-responder pairs given in the constructor with the configuration helper also given in the constructor.
1.2.3. Polling Mixins¶
Todo
Develop and document a new polling mixin. Preferably one that is easy to use.
Todo
Write a tutorial for the polling mixins.