| 1 |
Read Configuration Options |
| 2 |
========================== |
| 3 |
|
| 4 |
Plugins can configure themselves using the ``config.yaml`` file. You can read |
| 5 |
configuration values in two ways. The first is to use ``self.config`` within |
| 6 |
your plugin class. This gives you a view onto the configuration values in a |
| 7 |
section with the same name as your plugin's module. For example, if your plugin |
| 8 |
is in ``greatplugin.py``, then ``self.config`` will refer to options under the |
| 9 |
``greatplugin:`` section of the config file. |
| 10 |
|
| 11 |
For example, if you have a configuration value called "foo", then users can put |
| 12 |
this in their ``config.yaml``: |
| 13 |
|
| 14 |
:: |
| 15 |
|
| 16 |
greatplugin: |
| 17 |
foo: bar |
| 18 |
|
| 19 |
To access this value, say ``self.config['foo'].get()`` at any point in your |
| 20 |
plugin's code. The ``self.config`` object is a *view* as defined by the Confuse_ |
| 21 |
library. |
| 22 |
|
| 23 |
.. _confuse: https://confuse.readthedocs.io/en/latest/ |
| 24 |
|
| 25 |
If you want to access configuration values *outside* of your plugin's section, |
| 26 |
import the ``config`` object from the ``beets`` module. That is, just put ``from |
| 27 |
beets import config`` at the top of your plugin and access values from there. |
| 28 |
|
| 29 |
If your plugin provides configuration values for sensitive data (e.g., |
| 30 |
passwords, API keys, ...), you should add these to the config so they can be |
| 31 |
redacted automatically when users dump their config. This can be done by setting |
| 32 |
each value's ``redact`` flag, like so: |
| 33 |
|
| 34 |
:: |
| 35 |
|
| 36 |
self.config['password'].redact = True |