Connector Plugins¶
Overview¶
The last plugin class is the Connector plugin. This plugin class allows for the saving or passing off of the final result. Once all other plugins have completed their tasks, the final result is sent to the loaded connector plugins for handling. For example, a connector plugin may save results to disk, ElasticSearch, or even pass them off to a queueing system such as RabbitMQ.
Connector plugins can be defined multiple ways. In these examples, we will use the
filedir
connector plugin, allowing results to be saved to disk.
From stoq.cfg
:
[core]
connectors = filedir
Note
Multiple plugins can be defined separated by a comma.
From the command line:
$ stoq run -C filedir [...]
Note
Multiple plugins can be defined by simply adding the plugin name
Or, when instantiating the Stoq()
class:
>>> import stoq
>>> connectors = ['filedir']
>>> s = Stoq(connectors=connectors, [...])
Writing a plugin¶
A connector plugin must be a subclass of the ConnectorPlugin
class.
As with any plugin, a configuration file must also exist and be properly configured.
Example¶
from typing import Dict, Optional
from configparser import ConfigParser
from stoq.data_classes import StoqResponse
from stoq.plugins import ConnectorPlugin
class ExampleConnector(ConnectorPlugin):
def __init__(self, config: ConfigParser, plugin_opts: Optional[Dict]) -> None:
super().__init__(config, plugin_opts)
self.output_file = config.get(
'options', 'output_file', fallback='/tmp/stoqresult.txt')
def save(self, response: StoqResponse) -> None:
with open(f'{self.output_file}', 'w') as result:
result.write(response)