| 1 |
Plugin Dependencies |
| 2 |
=================== |
| 3 |
|
| 4 |
Sometimes you need to be able to share a plugin feature with another. |
| 5 |
For example imagine you have a series of plugin configured the same way, you might |
| 6 |
want to make them depend on a central plugin taking care of the configuration that would |
| 7 |
share it with all the others. |
| 8 |
|
| 9 |
Declaring dependencies |
| 10 |
---------------------- |
| 11 |
|
| 12 |
If you want to be able to use a plugin from another, the later needs to be activated before the former. |
| 13 |
You can ask Errbot to do so by adding a comma separated name list of the plugins your plugin is depending |
| 14 |
on in the **Core** section of your plug file like this: |
| 15 |
|
| 16 |
.. code-block:: ini |
| 17 |
|
| 18 |
[Core] |
| 19 |
Name = MyPlugin |
| 20 |
Module = myplugin |
| 21 |
DependsOn = OtherPlugin1, OtherPlugin2 |
| 22 |
|
| 23 |
Using dependencies |
| 24 |
------------------ |
| 25 |
|
| 26 |
Once a dependent plugin has been declared, you can use it as soon as your plugin is activated. |
| 27 |
|
| 28 |
.. code-block:: python |
| 29 |
|
| 30 |
from errbot import BotPlugin, botcmd |
| 31 |
|
| 32 |
class OtherPlugin1(BotPlugin): |
| 33 |
|
| 34 |
def activate(self): |
| 35 |
self.my_variable = 'hello' |
| 36 |
super().activate() |
| 37 |
|
| 38 |
|
| 39 |
If you want to use it from MyPlugin: |
| 40 |
|
| 41 |
.. code-block:: python |
| 42 |
|
| 43 |
from errbot import BotPlugin, botcmd |
| 44 |
|
| 45 |
class MyPlugin(BotPlugin): |
| 46 |
|
| 47 |
@botcmd |
| 48 |
def hello(self, msg, args): |
| 49 |
return self.get_plugin('OtherPlugin1').my_variable |
| 50 |
|
| 51 |
Important to note: if you want to use a dependent plugin from within activate, you need to be in activated state, for example: |
| 52 |
|
| 53 |
.. code-block:: python |
| 54 |
|
| 55 |
from errbot import BotPlugin, botcmd |
| 56 |
|
| 57 |
class MyPlugin(BotPlugin): |
| 58 |
|
| 59 |
def activate(self): |
| 60 |
super().activate() # <-- needs to be *before* get_plugin |
| 61 |
self.other = self.get_plugin('OtherPlugin1') |
| 62 |
|
| 63 |
@botcmd |
| 64 |
def hello(self, msg, args): |
| 65 |
return self.other.my_variable |
| 66 |
|
| 67 |
|