mirror of
https://github.com/beetbox/beets.git
synced 2026-05-16 18:20:55 -04:00
There have been multiple requests, in the past, for the ability to use
plugin fields in inline fields. This has not previously been available.
From what I can tell, it was intentionally left unavailable due to
performance concerns.
The way the item fields are made available to the inline python code
means that all fields are looked up, whether they're actually used by
the code or not. Doing that for all computed fields would be a
performance concern.
I don't believe there's a good way to postpone the field computation, as
python eval and compile requires that globals be a dictionary, not a
mapping. Instead, we can make available the album or item model object
to the code directly, and let the code access the fields it needs via
that object, resulting in postponing the computation of the fields until
they're actually accessed.
This is a simple approach that makes the computed and plugin fields
available to inline python, which allows for more code reuse, as well as
more options for shifting logic out of templates and into python code.
The object is available as `db_obj`.
Examples:
item_fields:
test_file_size: db_obj.filesize
album_fields:
test_album_path: db_obj.path
# If the missing plugin is enabled
test_album_missing: db_obj.missing
Signed-off-by: Christopher Larson <kergoth@gmail.com>
106 lines
3.3 KiB
Python
106 lines
3.3 KiB
Python
# This file is part of beets.
|
|
# Copyright 2025, Gabe Push.
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining
|
|
# a copy of this software and associated documentation files (the
|
|
# "Software"), to deal in the Software without restriction, including
|
|
# without limitation the rights to use, copy, modify, merge, publish,
|
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
# permit persons to whom the Software is furnished to do so, subject to
|
|
# the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be
|
|
# included in all copies or substantial portions of the Software.
|
|
|
|
from beets import config, plugins
|
|
from beets.test.helper import PluginTestCase
|
|
from beetsplug.inline import InlinePlugin
|
|
|
|
|
|
class TestInlineRecursion(PluginTestCase):
|
|
def test_no_recursion_when_inline_shadows_fixed_field(self):
|
|
config["plugins"] = ["inline"]
|
|
|
|
config["item_fields"] = {
|
|
"track_no": (
|
|
"f'{disc:02d}-{track:02d}' if disctotal > 1 else f'{track:02d}'"
|
|
)
|
|
}
|
|
|
|
plugins._instances.clear()
|
|
plugins.load_plugins()
|
|
|
|
item = self.add_item_fixture(
|
|
artist="Artist",
|
|
album="Album",
|
|
title="Title",
|
|
track=1,
|
|
disc=1,
|
|
disctotal=1,
|
|
)
|
|
|
|
out = item.evaluate_template("$track_no")
|
|
|
|
assert out == "01"
|
|
|
|
def test_inline_function_body_item_field(self):
|
|
plugin = InlinePlugin()
|
|
func = plugin.compile_inline(
|
|
"return track + 1", album=False, field_name="next_track"
|
|
)
|
|
|
|
item = self.add_item_fixture(track=3)
|
|
assert func(item) == 4
|
|
|
|
def test_inline_album_expression_uses_items(self):
|
|
plugin = InlinePlugin()
|
|
func = plugin.compile_inline(
|
|
"len(items)", album=True, field_name="item_count"
|
|
)
|
|
|
|
album = self.add_album_fixture()
|
|
assert func(album) == len(list(album.items()))
|
|
|
|
def test_inline_album_expression_uses_items_via_obj(self):
|
|
plugin = InlinePlugin()
|
|
func = plugin.compile_inline(
|
|
"len(db_obj.items())", album=True, field_name="item_count"
|
|
)
|
|
|
|
album = self.add_album_fixture()
|
|
assert func(album) == len(list(album.items()))
|
|
|
|
def test_inline_function_body_item_field_via_obj(self):
|
|
plugin = InlinePlugin()
|
|
func = plugin.compile_inline(
|
|
"return db_obj.track + 1", album=False, field_name="next_track"
|
|
)
|
|
|
|
item = self.add_item_fixture(track=3)
|
|
assert func(item) == 4
|
|
|
|
def test_inline_obj_missing(self):
|
|
config["plugins"] = ["inline", "missing"]
|
|
|
|
config["album_fields"] = {"has_missing": ("bool(db_obj.missing)")}
|
|
|
|
plugins._instances.clear()
|
|
plugins.load_plugins()
|
|
|
|
album = self.add_album_fixture(track_count=1)
|
|
album.tracktotal = 3
|
|
for item in album.items():
|
|
item.tracktotal = 3
|
|
item.store()
|
|
album.store()
|
|
assert album._get("has_missing")
|
|
|
|
def test_inline_function_body_obj(self):
|
|
plugin = InlinePlugin()
|
|
func = plugin.compile_inline(
|
|
"return db_obj.title", album=False, field_name="title_value"
|
|
)
|
|
|
|
item = self.add_item_fixture(track=3)
|
|
assert func(item)
|