| 1 |
Plugin Development |
| 2 |
================== |
| 3 |
|
| 4 |
Beets plugins are Python modules or packages that extend the core functionality |
| 5 |
of beets. The plugin system is designed to be flexible, allowing developers to |
| 6 |
add virtually any type of features to beets. |
| 7 |
|
| 8 |
For instance you can create plugins that add new commands to the command-line |
| 9 |
interface, listen for events in the beets lifecycle or extend the autotagger |
| 10 |
with new metadata sources. |
| 11 |
|
| 12 |
.. _basic-plugin-setup: |
| 13 |
|
| 14 |
Basic Plugin Setup |
| 15 |
------------------ |
| 16 |
|
| 17 |
A beets plugin is just a Python module or package inside the ``beetsplug`` |
| 18 |
namespace [1]_ package. To create the basic plugin layout, create a directory |
| 19 |
called ``beetsplug`` and add either your plugin module: |
| 20 |
|
| 21 |
.. code-block:: shell |
| 22 |
|
| 23 |
beetsplug/ |
| 24 |
└── myawesomeplugin.py |
| 25 |
|
| 26 |
or your plugin subpackage |
| 27 |
|
| 28 |
.. code-block:: shell |
| 29 |
|
| 30 |
beetsplug/ |
| 31 |
└── myawesomeplugin/ |
| 32 |
├── __init__.py |
| 33 |
└── myawesomeplugin.py |
| 34 |
|
| 35 |
.. attention:: |
| 36 |
|
| 37 |
You do not need to add an ``__init__.py`` file to the ``beetsplug`` |
| 38 |
directory. Python treats your plugin as a namespace package automatically, |
| 39 |
thus we do not depend on ``pkgutil``-based setup in the ``__init__.py`` file |
| 40 |
anymore. |
| 41 |
|
| 42 |
The meat of your plugin goes in ``myawesomeplugin.py``. Every plugin has to |
| 43 |
extend the |BeetsPlugin| abstract base class [2]_ . For instance, a minimal |
| 44 |
plugin without any functionality would look like this: |
| 45 |
|
| 46 |
.. code-block:: python |
| 47 |
|
| 48 |
# beetsplug/myawesomeplugin.py |
| 49 |
from beets.plugins import BeetsPlugin |
| 50 |
|
| 51 |
|
| 52 |
class MyAwesomePlugin(BeetsPlugin): |
| 53 |
pass |
| 54 |
|
| 55 |
.. attention:: |
| 56 |
|
| 57 |
If your plugin is composed of intermediate |BeetsPlugin| subclasses, make |
| 58 |
sure that your plugin is defined *last* in the namespace. We only load the |
| 59 |
last subclass of |BeetsPlugin| we find in your plugin namespace. |
| 60 |
|
| 61 |
To use your new plugin, you need to package [3]_ your plugin and install it into |
| 62 |
your ``beets`` (virtual) environment. To enable your plugin, add it it to the |
| 63 |
beets configuration |
| 64 |
|
| 65 |
.. code-block:: yaml |
| 66 |
|
| 67 |
# config.yaml |
| 68 |
plugins: |
| 69 |
- myawesomeplugin |
| 70 |
|
| 71 |
and you're good to go! |
| 72 |
|
| 73 |
.. [1] Check out `this article`_ and `this Stack Overflow question`_ if you |
| 74 |
haven't heard about namespace packages. |
| 75 |
|
| 76 |
.. [2] Abstract base classes allow us to define a contract which any plugin must |
| 77 |
follow. This is a common paradigm in object-oriented programming, and it |
| 78 |
helps to ensure that plugins are implemented in a consistent way. For more |
| 79 |
information, see for example pep-3119_. |
| 80 |
|
| 81 |
.. [3] There are a variety of packaging tools available for python, for example |
| 82 |
you can use setuptools_ or hatchling_. |
| 83 |
|
| 84 |
.. _hatchling: https://hatch.pypa.io/latest/config/build/#build-system |
| 85 |
|
| 86 |
.. _pep-3119: https://peps.python.org/pep-3119/#rationale |
| 87 |
|
| 88 |
.. _setuptools: https://setuptools.pypa.io/en/latest/userguide/package_discovery.html#finding-simple-packages |
| 89 |
|
| 90 |
.. _this article: https://realpython.com/python-namespace-package/#setting-up-some-namespace-packages |
| 91 |
|
| 92 |
.. _this stack overflow question: https://stackoverflow.com/questions/1675734/how-do-i-create-a-namespace-package-in-python/27586272#27586272 |
| 93 |
|
| 94 |
More information |
| 95 |
---------------- |
| 96 |
|
| 97 |
For more information on writing plugins, feel free to check out the following |
| 98 |
resources: |
| 99 |
|
| 100 |
.. toctree:: |
| 101 |
:maxdepth: 3 |
| 102 |
:includehidden: |
| 103 |
|
| 104 |
commands |
| 105 |
events |
| 106 |
autotagger |
| 107 |
other/index |