Files
beets/beetsplug/inline.py
Christopher Larson 5bc11ea736 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>
2026-04-15 11:59:19 -07:00

136 lines
4.5 KiB
Python

# This file is part of beets.
# Copyright 2016, Adrian Sampson.
#
# 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.
"""Allows inline path template customization code in the config file."""
import itertools
import traceback
from beets import config
from beets.plugins import BeetsPlugin
FUNC_NAME = "__INLINE_FUNC__"
class InlineError(Exception):
"""Raised when a runtime error occurs in an inline expression."""
def __init__(self, code, exc):
super().__init__(
f"error in inline path field code:\n{code}\n{type(exc).__name__}: {exc}"
)
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}({args}):\n {body}"
code = compile(body, "inline", "exec")
env = {}
eval(code, env)
return env[FUNC_NAME]
class InlinePlugin(BeetsPlugin):
def __init__(self):
super().__init__()
config.add(
{
"pathfields": {}, # Legacy name.
"item_fields": {},
"album_fields": {},
}
)
# Item fields.
for key, view in itertools.chain(
config["item_fields"].items(), config["pathfields"].items()
):
self._log.debug("adding item field {}", key)
func = self.compile_inline(view.as_str(), False, key)
if func is not None:
self.template_fields[key] = func
# Album fields.
for key, view in config["album_fields"].items():
self._log.debug("adding album field {}", key)
func = self.compile_inline(view.as_str(), True, key)
if func is not None:
self.album_template_fields[key] = func
def compile_inline(self, python_code, album, field_name):
"""Given a Python expression or function body, compile it as a path
field function. The returned function takes a single argument, an
Item, and returns a Unicode string. If the expression cannot be
compiled, then an error is logged and this function returns None.
"""
# First, try compiling as a single function.
try:
code = compile(f"({python_code})", "inline", "eval")
except SyntaxError:
# Fall back to a function body.
try:
func = _compile_func(python_code, args="db_obj")
except SyntaxError:
self._log.error(
"syntax error in inline field definition:\n{}",
traceback.format_exc(),
)
return
else:
is_expr = False
else:
is_expr = True
def _dict_for(obj):
out = {}
for key in obj.keys(computed=False):
if key == field_name:
continue
out[key] = obj._get(key)
if album:
out["items"] = list(obj.items())
return out
if is_expr:
# 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:
raise InlineError(python_code, exc)
return _expr_func
else:
# For function bodies, invoke the function with values as global
# variables.
def _func_func(obj):
old_globals = dict(func.__globals__)
func.__globals__.update(_dict_for(obj))
try:
return func(obj)
except Exception as exc:
raise InlineError(python_code, exc)
finally:
func.__globals__.clear()
func.__globals__.update(old_globals)
return _func_func