Debian Codegraph @

docs/user_guide/plugin_development/configuration.rst

raw
1 Configuration
2 =============
3
4 Plugin configuration through the built-in `!config` command
5 -----------------------------------------------------------
6
7 Errbot can keep a simple python object for the configuration of your
8 plugin. This avoids the need for admins to configure settings in
9 some kind of configuration file, instead allowing configuration to
10 happen directly through chat commands.
11
12 In order to enable this feature, you need to provide a configuration
13 template (ie. a config example) by overriding the
14 :meth:`~errbot.botplugin.BotPlugin.get_configuration_template`
15 method. For example, a plugin might request a dictionary with 2
16 entries:
17
18 .. code-block:: python
19
20 from errbot import BotPlugin
21
22 class PluginExample(BotPlugin):
23 def get_configuration_template(self):
24 return {'ID_TOKEN': '00112233445566778899aabbccddeeff',
25 'USERNAME':'changeme'}
26
27 With this in place, an admin will be able to request the default
28 configuration template with `!plugin config PluginExample`. He or she could
29 then give the command
30 `!plugin config PluginExample {'ID_TOKEN' : '00112233445566778899aabbccddeeff', 'USERNAME':'changeme'}`
31 to enable that configuration.
32
33 It will also be possible to recall the configuration template, as
34 well as the config that is actually set, by issuing `!plugin config
35 PluginExample` again.
36
37 Within your code, the config that is set will be in `self.config`:
38
39 .. code-block:: python
40
41 @botcmd
42 def mycommand(self, mess, args):
43 # oh I need my TOKEN !
44 token = self.config['ID_TOKEN']
45
46 .. warning::
47 If there is no configuration set yet, `self.config` will be
48 `None`.
49
50
51 Supplying partial configuration
52 -------------------------------
53
54 Sometimes you want to allow users to only supply a part of the configuration
55 they wish to override from the template instead of having to copy-paste and
56 modify the complete entry.
57
58 This can be achieved by overriding :meth:`~errbot.botplugin.BotPlugin.configure`:
59
60 .. code-block:: python
61
62 from itertools import chain
63
64 CONFIG_TEMPLATE = {'ID_TOKEN': '00112233445566778899aabbccddeeff',
65 'USERNAME':'changeme'}
66
67 def configure(self, configuration):
68 if configuration is not None and configuration != {}:
69 config = dict(chain(CONFIG_TEMPLATE.items(),
70 configuration.items()))
71 else:
72 config = CONFIG_TEMPLATE
73 super(PluginExample, self).configure(config)
74
75 What this achieves is that it creates a Python dictionary object which
76 contains all the values from our `CONFIG_TEMPLATE` and then updates
77 that dictionary with the configuration received when calling the
78 `!config` command. `!config` must be passed a dictionary for this to
79 work.
80
81 If you wish to reset the configuration to its defaults all you need to do is
82 pass an empty dictionary to `!config`.
83
84 You'll now also need to override :meth:`~errbot.botplugin.BotPlugin.get_configuration_template`
85 and return the `CONFIG_TEMPLATE` in that function:
86
87 .. code-block:: python
88
89 def get_configuration_template(self):
90 return CONFIG_TEMPLATE
91
92
93 Using custom configuration checks
94 ---------------------------------
95
96 By default, Errbot will check the supplied configuration against the
97 configuration template, and raise an error if the structure of the
98 two doesn't match.
99
100 You need to override the :meth:`~errbot.botplugin.BotPlugin.check_configuration`
101 method if you wish do some other form of configuration validation.
102 This method will be called automatically when an admin configures
103 your plugin with the `!config` command.
104
105 .. warning::
106 If there is no configuration set yet, it will pass `None` as
107 parameter. Be mindful of this situation.
108
109 Using the partial configuration trick as shown above requires you to
110 override :meth:`~errbot.botplugin.BotPlugin.check_configuration`, so
111 at a minimum you'll need this:
112
113 .. code-block:: python
114
115 def check_configuration(self, configuration):
116 pass
117
118 We suggest that you at least do some validation instead of nothing but
119 that is up to you.