| 1 |
.. _plugin_events: |
| 2 |
|
| 3 |
Listen for Events |
| 4 |
================= |
| 5 |
|
| 6 |
.. currentmodule:: beets.plugins |
| 7 |
|
| 8 |
Event handlers allow plugins to hook into whenever something happens in beets' |
| 9 |
operations. For instance, a plugin could write a log message every time an album |
| 10 |
is successfully autotagged or update MPD's index whenever the database is |
| 11 |
changed. |
| 12 |
|
| 13 |
You can "listen" for events using :py:meth:`BeetsPlugin.register_listener`. |
| 14 |
Here's an example: |
| 15 |
|
| 16 |
.. code-block:: python |
| 17 |
|
| 18 |
from beets.plugins import BeetsPlugin |
| 19 |
|
| 20 |
|
| 21 |
def loaded(): |
| 22 |
print("Plugin loaded!") |
| 23 |
|
| 24 |
|
| 25 |
class SomePlugin(BeetsPlugin): |
| 26 |
def __init__(self): |
| 27 |
super().__init__() |
| 28 |
self.register_listener("pluginload", loaded) |
| 29 |
|
| 30 |
Note that if you want to access an attribute of your plugin (e.g. ``config`` or |
| 31 |
``log``) you'll have to define a method and not a function. Here is the usual |
| 32 |
registration process in this case: |
| 33 |
|
| 34 |
.. code-block:: python |
| 35 |
|
| 36 |
from beets.plugins import BeetsPlugin |
| 37 |
|
| 38 |
|
| 39 |
class SomePlugin(BeetsPlugin): |
| 40 |
def __init__(self): |
| 41 |
super().__init__() |
| 42 |
self.register_listener("pluginload", self.loaded) |
| 43 |
|
| 44 |
def loaded(self): |
| 45 |
self._log.info("Plugin loaded!") |
| 46 |
|
| 47 |
.. rubric:: Plugin Events |
| 48 |
|
| 49 |
``pluginload`` |
| 50 |
:Parameters: (none) |
| 51 |
:Description: Called after all plugins have been loaded after the ``beet`` |
| 52 |
command starts. |
| 53 |
|
| 54 |
``import`` |
| 55 |
:Parameters: ``lib`` (|Library|), ``paths`` (list of byte paths) |
| 56 |
:Description: Called after the ``import`` command finishes. |
| 57 |
|
| 58 |
``album_imported`` |
| 59 |
:Parameters: ``lib`` (|Library|), ``album`` (|Album|) |
| 60 |
:Description: Called every time the importer finishes adding an album to the |
| 61 |
library. |
| 62 |
|
| 63 |
``album_removed`` |
| 64 |
:Parameters: ``album`` (|Album|) |
| 65 |
:Description: Called every time an album is removed from the library (even |
| 66 |
when its files are not deleted from disk). |
| 67 |
|
| 68 |
``art_set`` |
| 69 |
:Parameters: ``album`` (|Album|) |
| 70 |
:Description: Called after cover art is copied or moved into place for an |
| 71 |
album. |
| 72 |
|
| 73 |
``item_copied`` |
| 74 |
:Parameters: ``item`` (|Item|), ``source`` (path), ``destination`` (path) |
| 75 |
:Description: Called whenever an item file is copied. |
| 76 |
|
| 77 |
``item_imported`` |
| 78 |
:Parameters: ``lib`` (|Library|), ``item`` (|Item|) |
| 79 |
:Description: Called every time the importer adds a singleton to the library |
| 80 |
(not called for full-album imports). |
| 81 |
|
| 82 |
``before_item_moved`` |
| 83 |
:Parameters: ``item`` (|Item|), ``source`` (path), ``destination`` (path) |
| 84 |
:Description: Called with an ``Item`` object immediately before its file is |
| 85 |
moved. |
| 86 |
|
| 87 |
``item_moved`` |
| 88 |
:Parameters: ``item`` (|Item|), ``source`` (path), ``destination`` (path) |
| 89 |
:Description: Called with an ``Item`` object whenever its file is moved. |
| 90 |
|
| 91 |
``item_linked`` |
| 92 |
:Parameters: ``item`` (|Item|), ``source`` (path), ``destination`` (path) |
| 93 |
:Description: Called with an ``Item`` object whenever a symlink is created |
| 94 |
for a file. |
| 95 |
|
| 96 |
``item_hardlinked`` |
| 97 |
:Parameters: ``item`` (|Item|), ``source`` (path), ``destination`` (path) |
| 98 |
:Description: Called with an ``Item`` object whenever a hardlink is created |
| 99 |
for a file. |
| 100 |
|
| 101 |
``item_reflinked`` |
| 102 |
:Parameters: ``item`` (|Item|), ``source`` (path), ``destination`` (path) |
| 103 |
:Description: Called with an ``Item`` object whenever a reflink is created |
| 104 |
for a file. |
| 105 |
|
| 106 |
``item_removed`` |
| 107 |
:Parameters: ``item`` (|Item|) |
| 108 |
:Description: Called with an ``Item`` object every time an item (singleton |
| 109 |
or part of an album) is removed from the library (even when its file is |
| 110 |
not deleted from disk). |
| 111 |
|
| 112 |
``write`` |
| 113 |
:Parameters: ``item`` (|Item|), ``path`` (path), ``tags`` (dict) |
| 114 |
:Description: Called just before a file's metadata is written to disk. |
| 115 |
Handlers may modify ``tags`` or raise ``library.FileOperationError`` to |
| 116 |
abort. |
| 117 |
|
| 118 |
``after_write`` |
| 119 |
:Parameters: ``item`` (|Item|), ``path`` (path) |
| 120 |
:Description: Called after a file's metadata is written to disk. |
| 121 |
|
| 122 |
``import_task_created`` |
| 123 |
:Parameters: ``task`` (|ImportTask|), ``session`` (|ImportSession|) |
| 124 |
:Description: Called immediately after an import task is initialized. May |
| 125 |
return a list (possibly empty) of replacement tasks. |
| 126 |
|
| 127 |
``import_task_start`` |
| 128 |
:Parameters: ``task`` (|ImportTask|), ``session`` (|ImportSession|) |
| 129 |
:Description: Called before an import task begins processing. |
| 130 |
|
| 131 |
``import_task_apply`` |
| 132 |
:Parameters: ``task`` (|ImportTask|), ``session`` (|ImportSession|) |
| 133 |
:Description: Called after metadata changes have been applied in an import |
| 134 |
task (on the UI thread; keep fast). Prefer a pipeline stage otherwise |
| 135 |
(see :ref:`plugin-stage`). |
| 136 |
|
| 137 |
``import_task_before_choice`` |
| 138 |
:Parameters: ``task`` (|ImportTask|), ``session`` (|ImportSession|) |
| 139 |
:Description: Called after candidate search and before deciding how to |
| 140 |
import. May return an importer action (only one handler may return |
| 141 |
non-None). |
| 142 |
|
| 143 |
``import_task_choice`` |
| 144 |
:Parameters: ``task`` (|ImportTask|), ``session`` (|ImportSession|) |
| 145 |
:Description: Called after a decision has been made about an import task. |
| 146 |
Use ``task.choice_flag`` to inspect or change the action. |
| 147 |
|
| 148 |
``import_task_files`` |
| 149 |
:Parameters: ``task`` (|ImportTask|), ``session`` (|ImportSession|) |
| 150 |
:Description: Called after filesystem manipulation (copy/move/write) for an |
| 151 |
import task. |
| 152 |
|
| 153 |
``library_opened`` |
| 154 |
:Parameters: ``lib`` (|Library|) |
| 155 |
:Description: Called after beets starts and initializes the main Library |
| 156 |
object. |
| 157 |
|
| 158 |
``database_change`` |
| 159 |
:Parameters: ``lib`` (|Library|), ``model`` (|Album| or |Item|) |
| 160 |
:Description: A modification has been made to the library database (may not |
| 161 |
yet be committed). |
| 162 |
|
| 163 |
``cli_exit`` |
| 164 |
:Parameters: ``lib`` (|Library|) |
| 165 |
:Description: Called just before the ``beet`` command-line program exits. |
| 166 |
|
| 167 |
``import_begin`` |
| 168 |
:Parameters: ``session`` (|ImportSession|) |
| 169 |
:Description: Called just before a ``beet import`` session starts. |
| 170 |
|
| 171 |
``trackinfo_received`` |
| 172 |
:Parameters: ``info`` (|TrackInfo|) |
| 173 |
:Description: Called after metadata for a track is fetched (e.g., from |
| 174 |
MusicBrainz). Handlers can modify the tags seen by later pipeline stages |
| 175 |
or adjustments (e.g., ``mbsync``). |
| 176 |
|
| 177 |
``albuminfo_received`` |
| 178 |
:Parameters: ``info`` (|AlbumInfo|) |
| 179 |
:Description: Like ``trackinfo_received`` but for album-level metadata. |
| 180 |
|
| 181 |
``album_matched`` |
| 182 |
:Parameters: ``match`` (``AlbumMatch``) |
| 183 |
:Description: Called each time an ``AlbumMatch`` candidate is created while |
| 184 |
importing. This applies to both ID-driven and text-search matching. |
| 185 |
Missing and extra tracks, if any, are included in the match. |
| 186 |
|
| 187 |
``before_choose_candidate`` |
| 188 |
:Parameters: ``task`` (|ImportTask|), ``session`` (|ImportSession|) |
| 189 |
:Description: Called before prompting the user during interactive import. |
| 190 |
May return a list of ``PromptChoices`` to append to the prompt (see |
| 191 |
:ref:`append_prompt_choices`). |
| 192 |
|
| 193 |
``mb_track_extract`` |
| 194 |
:Parameters: ``data`` (dict) |
| 195 |
:Description: Called after metadata is obtained from MusicBrainz for a |
| 196 |
track. Must return a (possibly empty) dict of additional ``field: |
| 197 |
value`` pairs to apply (overwriting existing fields). |
| 198 |
|
| 199 |
``mb_album_extract`` |
| 200 |
:Parameters: ``data`` (dict) |
| 201 |
:Description: Like ``mb_track_extract`` but for album tags. Overwrites tags |
| 202 |
set at the track level with the same field. |
| 203 |
|
| 204 |
``after_convert`` |
| 205 |
:Parameters: ``item`` (|Item|), ``dest`` (path), ``keepnew`` (bool) |
| 206 |
:Description: Called by the :doc:`/plugins/convert` plugin after it |
| 207 |
successfully converts or copies an item. When ``keepnew`` is false, |
| 208 |
``dest`` is the converted file. When ``keepnew`` is true, ``dest`` is |
| 209 |
where the original file was moved and ``item.path`` identifies the |
| 210 |
converted file retained in the library. |
| 211 |
|
| 212 |
``smartplaylist_update`` |
| 213 |
:Parameters: (none) |
| 214 |
:Description: Called by the :doc:`/plugins/smartplaylist` plugin after it |
| 215 |
writes updated playlist files. It is not called in pretend mode. |
| 216 |
|
| 217 |
The included ``mpdupdate`` plugin provides an example use case for event |
| 218 |
listeners. |