Debian Codegraph @

docs/dev/plugins/other/prompts.rst

raw
1 .. _append_prompt_choices:
2
3 Append Prompt Choices
4 =====================
5
6 Plugins can also append choices to the prompt presented to the user during an
7 import session.
8
9 To do so, add a listener for the ``before_choose_candidate`` event, and return a
10 list of ``PromptChoices`` that represent the additional choices that your plugin
11 shall expose to the user:
12
13 .. code-block:: python
14
15 from beets.plugins import BeetsPlugin
16 from beets.util import PromptChoice
17
18
19 class ExamplePlugin(BeetsPlugin):
20 def __init__(self):
21 super().__init__()
22 self.register_listener(
23 "before_choose_candidate", self.before_choose_candidate_event
24 )
25
26 def before_choose_candidate_event(self, session, task):
27 return [
28 PromptChoice("p", "Print foo", self.foo),
29 PromptChoice("d", "Do bar", self.bar),
30 ]
31
32 def foo(self, session, task):
33 print('User has chosen "Print foo"!')
34
35 def bar(self, session, task):
36 print('User has chosen "Do bar"!')
37
38 The previous example modifies the standard prompt:
39
40 .. code-block:: shell
41
42 # selection (default 1), Skip, Use as-is, as Tracks, Group albums,
43 Enter search, enter Id, aBort?
44
45 by appending two additional options (``Print foo`` and ``Do bar``):
46
47 .. code-block:: shell
48
49 # selection (default 1), Skip, Use as-is, as Tracks, Group albums,
50 Enter search, enter Id, aBort, Print foo, Do bar?
51
52 If the user selects a choice, the ``callback`` attribute of the corresponding
53 ``PromptChoice`` will be called. It is the responsibility of the plugin to check
54 for the status of the import session and decide the choices to be appended: for
55 example, if a particular choice should only be presented if the album has no
56 candidates, the relevant checks against ``task.candidates`` should be performed
57 inside the plugin's ``before_choose_candidate_event`` accordingly.
58
59 Please make sure that the short letter for each of the choices provided by the
60 plugin is not already in use: the importer will emit a warning and discard all
61 but one of the choices using the same letter, giving priority to the core
62 importer prompt choices. As a reference, the following characters are used by
63 the choices on the core importer prompt, and hence should not be used: ``a``,
64 ``s``, ``u``, ``t``, ``g``, ``e``, ``i``, ``b``.
65
66 Additionally, the callback function can optionally specify the next action to be
67 performed by returning a ``importer.Action`` value. It may also return a
68 ``autotag.Proposal`` value to update the set of current proposals to be
69 considered.