inline: make the album/item available directly

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>
This commit is contained in:
Christopher Larson
2024-09-25 19:30:11 -07:00
parent 1d01689aad
commit 5bc11ea736
4 changed files with 56 additions and 6 deletions

View File

@@ -32,12 +32,12 @@ class InlineError(Exception):
)
def _compile_func(body):
def _compile_func(body, args=""):
"""Given Python code for a function body, return a compiled
callable that invokes that code.
"""
body = body.replace("\n", "\n ")
body = f"def {FUNC_NAME}():\n {body}"
body = f"def {FUNC_NAME}({args}):\n {body}"
code = compile(body, "inline", "exec")
env = {}
eval(code, env)
@@ -84,7 +84,7 @@ class InlinePlugin(BeetsPlugin):
except SyntaxError:
# Fall back to a function body.
try:
func = _compile_func(python_code)
func = _compile_func(python_code, args="db_obj")
except SyntaxError:
self._log.error(
"syntax error in inline field definition:\n{}",
@@ -111,6 +111,7 @@ class InlinePlugin(BeetsPlugin):
# For expressions, just evaluate and return the result.
def _expr_func(obj):
values = _dict_for(obj)
values["db_obj"] = obj
try:
return eval(code, values)
except Exception as exc:
@@ -124,7 +125,7 @@ class InlinePlugin(BeetsPlugin):
old_globals = dict(func.__globals__)
func.__globals__.update(_dict_for(obj))
try:
return func()
return func(obj)
except Exception as exc:
raise InlineError(python_code, exc)
finally:

View File

@@ -26,6 +26,9 @@ New features
after upgrading to trigger the migration. Only then you can safely move
the library to a new location.
- :doc:`plugins/inline`: Add access to the ``album`` or ``item`` object as
``db_obj`` in inline fields.
Bug fixes
~~~~~~~~~

View File

@@ -11,8 +11,11 @@ To use the ``inline`` plugin, enable it in your configuration (see
Under this key, every line defines a new template field; the key is the name of
the field (you'll use the name to refer to the field in your templates) and the
value is a Python expression or function body. The Python code has all of a
track's fields in scope, so you can refer to any normal attributes (such as
``artist`` or ``title``) as Python variables.
track's normal fields in scope, so you can refer to these attributes (such as
``artist`` or ``title``) as Python variables. The Python code also has direct
access to the item or album object as ``db_obj``. This allows use of computed
fields and plugin fields, for example, ``db_obj.albumtotal``, or
``db_obj.missing`` if the :doc:`/plugins/missing` plugin is enabled.
Here are a couple of examples of expressions:

View File

@@ -60,3 +60,46 @@ class TestInlineRecursion(PluginTestCase):
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)