| 1 |
Hook Plugin |
| 2 |
=========== |
| 3 |
|
| 4 |
Internally, beets uses *events* to tell plugins when something happens. For |
| 5 |
example, one event fires when the importer finishes processes a song, and |
| 6 |
another triggers just before the ``beet`` command exits. The ``hook`` plugin |
| 7 |
lets you run commands in response to these events. |
| 8 |
|
| 9 |
.. _hook-configuration: |
| 10 |
|
| 11 |
Configuration |
| 12 |
------------- |
| 13 |
|
| 14 |
To configure the plugin, make a ``hook`` section in your configuration file. The |
| 15 |
available options are: |
| 16 |
|
| 17 |
- **hooks**: A list of events and the commands to run (see |
| 18 |
:ref:`individual-hook-configuration`). Default: Empty. |
| 19 |
|
| 20 |
.. _individual-hook-configuration: |
| 21 |
|
| 22 |
Configuring Each Hook |
| 23 |
~~~~~~~~~~~~~~~~~~~~~ |
| 24 |
|
| 25 |
Each element under ``hooks`` should have these keys: |
| 26 |
|
| 27 |
- **event**: The name of the event that will trigger this hook. See the |
| 28 |
:ref:`plugin events <plugin_events>` documentation for a list of possible |
| 29 |
values. |
| 30 |
- **command**: The command to run when this hook executes. |
| 31 |
|
| 32 |
.. _command-substitution: |
| 33 |
|
| 34 |
Command Substitution |
| 35 |
~~~~~~~~~~~~~~~~~~~~ |
| 36 |
|
| 37 |
Commands can access the parameters of events using `Python string formatting`_. |
| 38 |
Use ``{name}`` in your command and the plugin will substitute it with the named |
| 39 |
value. The name can also refer to a field, as in ``{album.path}``. |
| 40 |
|
| 41 |
.. _python string formatting: https://peps.python.org/pep-3101/ |
| 42 |
|
| 43 |
You can find a list of all available events and their arguments in the |
| 44 |
:ref:`plugin events <plugin_events>` documentation. |
| 45 |
|
| 46 |
Example Configuration |
| 47 |
--------------------- |
| 48 |
|
| 49 |
.. code-block:: yaml |
| 50 |
|
| 51 |
hook: |
| 52 |
hooks: |
| 53 |
# Output on exit: |
| 54 |
# beets just exited! |
| 55 |
# have a nice day! |
| 56 |
- event: cli_exit |
| 57 |
command: echo "beets just exited!" |
| 58 |
- event: cli_exit |
| 59 |
command: echo "have a nice day!" |
| 60 |
|
| 61 |
# Output on item import: |
| 62 |
# importing "<file_name_here>" |
| 63 |
# Where <file_name_here> is the item being imported |
| 64 |
- event: item_imported |
| 65 |
command: echo "importing \"{item.path}\"" |
| 66 |
|
| 67 |
# Output on write: |
| 68 |
# writing to "<file_name_here>" |
| 69 |
# Where <file_name_here> is the file being written to |
| 70 |
- event: write |
| 71 |
command: echo "writing to {path}" |