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