Debian Codegraph @

docs/plugins/inline.rst

raw
1 Inline Plugin
2 =============
3
4 The ``inline`` plugin lets you use Python to customize your path formats. Using
5 it, you can define template fields in your beets configuration file and refer to
6 them from your template strings in the ``paths:`` section (see
7 :doc:`/reference/config/`).
8
9 To use the ``inline`` plugin, enable it in your configuration (see
10 :ref:`using-plugins`). Then, make a ``item_fields:`` block in your config file.
11 Under this key, every line defines a new template field; the key is the name of
12 the field (you'll use the name to refer to the field in your templates) and the
13 value is a Python expression or function body. The Python code has all of a
14 track's normal fields in scope, so you can refer to these attributes (such as
15 ``artist`` or ``title``) as Python variables. The Python code also has direct
16 access to the item or album object as ``db_obj``. This allows use of computed
17 fields and plugin fields, for example, ``db_obj.albumtotal``, or
18 ``db_obj.missing`` if the :doc:`/plugins/missing` plugin is enabled.
19
20 Here are a couple of examples of expressions:
21
22 ::
23
24 item_fields:
25 initial: albumartist[0].upper() + u'.'
26 disc_and_track: f"{disc:02d}.{track:02d}" if disctotal > 1 else f"{track:02d}"
27
28 Note that YAML syntax allows newlines in values if the subsequent lines are
29 indented.
30
31 These examples define ``$initial`` and ``$disc_and_track`` fields that can be
32 referenced in path templates like so:
33
34 ::
35
36 paths:
37 default: $initial/$artist/$album%aunique{}/$disc_and_track $title
38
39 Block Definitions
40 -----------------
41
42 If you need to use statements like ``import``, you can write a Python function
43 body instead of a single expression. In this case, you'll need to ``return`` a
44 result for the value of the path field, like so:
45
46 ::
47
48 item_fields:
49 filename: |
50 import os
51 from beets.util import bytestring_path
52 return bytestring_path(os.path.basename(path))
53
54 You might want to use the YAML syntax for "block literals," in which a leading
55 ``|`` character indicates a multi-line block of text.
56
57 Album Fields
58 ------------
59
60 The above examples define fields for *item* templates, but you can also define
61 fields for *album* templates. Use the ``album_fields`` configuration section. In
62 this context, all existing album fields are available as variables along with
63 ``items``, which is a list of items in the album.
64
65 This example defines a ``$bitrate`` field for albums as the average of the
66 tracks' fields:
67
68 ::
69
70 album_fields:
71 bitrate: |
72 total = 0
73 for item in items:
74 total += item.bitrate
75 return total / len(items)