| 1 |
[Advanced] Storage Plugin development |
| 2 |
===================================== |
| 3 |
|
| 4 |
A storage plugin is the glue code that tells Errbot how to store the persistent data the plugins and the bot itself are |
| 5 |
producing. Starting with Errbot 3.3.0, storage plugins can be developed out of the main repository. This documentation |
| 6 |
is there to guide you making a new storage plugin so you can connect Errbot to your favorite database. |
| 7 |
|
| 8 |
|
| 9 |
Architecture |
| 10 |
------------ |
| 11 |
|
| 12 |
Storage plugins are instantiated in 2 stages. |
| 13 |
|
| 14 |
The first stage is storage plugin discovery and is similar to normal bot plugins: |
| 15 |
|
| 16 |
* Errbot scans `errbot/storage` and `config.BOT_EXTRA_STORAGE_PLUGINS_DIR` for `.plug` files pointing |
| 17 |
to plugins implementing :class:`~errbot.storage.base.StoragePluginBase`. |
| 18 |
* Once the correct plugin from `config.STORAGE` is found, it is built with the bot config as its `__init__` parameter. |
| 19 |
* By calling `super().__init__` on :class:`~errbot.storage.base.StoragePluginBase`, Errbot will populate |
| 20 |
`self._storage_config` from `config.STORAGE_CONFIG`. This configuration should contain the custom parameters needed by |
| 21 |
your plugin to be able to connect to your database/storage ie. url, port, path, credentials ... You need to document |
| 22 |
them clearly so your users can set `config.STORAGE_CONFIG` correctly. |
| 23 |
* As you can see in :class:`~errbot.storage.base.StoragePluginBase`, you just have to implement the `open` method there. |
| 24 |
|
| 25 |
The second stage is opening the storage, which is done using the `open` method: |
| 26 |
|
| 27 |
* Various parts of Errbot may need separate key/value storage, the `open` method has a namespace to track those. |
| 28 |
For example, the internal :class:`~errbot.plugin_manager.BotPluginManager` will open the namespace `core` to store the |
| 29 |
bot plugins and their config, the installed repos, etc. |
| 30 |
* `open` needs to return a :class:`~errbot.storage.base.StorageBase`, which exposes the various actions that Errbot can |
| 31 |
call on the storage (set, get, ...). |
| 32 |
* You don't need to track the lifecycle of the storage, it will be enforced externally: no double-close, double-open, |
| 33 |
`get` after close, etc. |
| 34 |
|
| 35 |
Plugins are :class:`collections.MutableMapping` and use :class:`~errbot.storage.StoreMixin` as an adapter from the |
| 36 |
mapping accessors to the :class:`~errbot.storage.base.StorageBase` implementation. |
| 37 |
|
| 38 |
|
| 39 |
Testing |
| 40 |
------- |
| 41 |
|
| 42 |
Storage plugins are completely independent from Errbot itself. It should be easy to instantiate and test them |
| 43 |
externally. |
| 44 |
|
| 45 |
|
| 46 |
Example |
| 47 |
------- |
| 48 |
|
| 49 |
You can have a look at the internal shelf implementation :class:`~errbot.storage.shelf.ShelfStorage` |