Debian Codegraph @

docs/dev/plugins/other/import.rst

raw
1 .. _plugin-stage:
2
3 Add Import Pipeline Stages
4 ==========================
5
6 Many plugins need to add high-latency operations to the import workflow. For
7 example, a plugin that fetches lyrics from the Web would, ideally, not block the
8 progress of the rest of the importer. Beets allows plugins to add stages to the
9 parallel import pipeline.
10
11 Each stage is run in its own thread. Plugin stages run after metadata changes
12 have been applied to a unit of music (album or track) and before file
13 manipulation has occurred (copying and moving files, writing tags to disk).
14 Multiple stages run in parallel but each stage processes only one task at a time
15 and each task is processed by only one stage at a time.
16
17 Plugins provide stages as functions that take two arguments: ``config`` and
18 ``task``, which are ``ImportSession`` and ``ImportTask`` objects (both defined
19 in ``beets.importer``). Add such a function to the plugin's ``import_stages``
20 field to register it:
21
22 .. code-block:: python
23
24 from beets.importer import ImportSession, ImportTask
25 from beets.plugins import BeetsPlugin
26
27
28 class ExamplePlugin(BeetsPlugin):
29
30 def __init__(self):
31 super().__init__()
32 self.import_stages = [self.stage]
33
34 def stage(self, session: ImportSession, task: ImportTask):
35 print("Importing something!")
36
37 It is also possible to request your function to run early in the pipeline by
38 adding the function to the plugin's ``early_import_stages`` field instead:
39
40 .. code-block:: python
41
42 self.early_import_stages = [self.stage]
43
44 .. _extend-query:
45
46 Extend the Query Syntax
47 -----------------------
48
49 You can add new kinds of queries to beets' :doc:`query syntax
50 </reference/query>`. There are two ways to add custom queries: using a prefix
51 and using a name. Prefix-based query extension can apply to *any* field, while
52 named queries are not associated with any field. For example, beets already
53 supports regular expression queries, which are indicated by a colon
54 prefix---plugins can do the same.
55
56 For either kind of query extension, define a subclass of the ``Query`` type from
57 the ``beets.dbcore.query`` module. Then:
58
59 - To define a prefix-based query, define a ``queries`` method in your plugin
60 class. Return from this method a dictionary mapping prefix strings to query
61 classes.
62 - To define a named query, defined dictionaries named either ``item_queries`` or
63 ``album_queries``. These should map names to query types. So if you use ``{
64 "foo": FooQuery }``, then the query ``foo:bar`` will construct a query like
65 ``FooQuery("bar")``.
66
67 For prefix-based queries, you will want to extend ``FieldQuery``, which
68 implements string comparisons on fields. To use it, create a subclass inheriting
69 from that class and override the ``value_match`` class method. (Remember the
70 ``@classmethod`` decorator!) The following example plugin declares a query using
71 the ``@`` prefix to delimit exact string matches. The plugin will be used if we
72 issue a command like ``beet ls @something`` or ``beet ls artist:@something``:
73
74 .. code-block:: python
75
76 from beets.plugins import BeetsPlugin
77 from beets.dbcore import FieldQuery
78
79
80 class ExactMatchQuery(FieldQuery):
81 @classmethod
82 def value_match(self, pattern, val):
83 return pattern == val
84
85
86 class ExactMatchPlugin(BeetsPlugin):
87 def queries(self):
88 return {"@": ExactMatchQuery}