Skip to content

Commit df58c24

Browse files
committed
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. In items, the object is available as 'item', and in albums, it's available as 'album'. Examples: item_fields: test_file_size: item.filesize album_fields: test_album_path: album.path # If the missing plugin is enabled test_album_missing: album.missing Signed-off-by: Christopher Larson <[email protected]>
1 parent 1a59368 commit df58c24

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

beetsplug/inline.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ def __init__(self, code, exc):
3333
)
3434

3535

36-
def _compile_func(body):
36+
def _compile_func(body, args=""):
3737
"""Given Python code for a function body, return a compiled
3838
callable that invokes that code.
3939
"""
40-
body = "def {}():\n {}".format(FUNC_NAME, body.replace("\n", "\n "))
40+
body = "def {}({}):\n {}".format(
41+
FUNC_NAME, args, body.replace("\n", "\n ")
42+
)
4143
code = compile(body, "inline", "exec")
4244
env = {}
4345
eval(code, env)
@@ -84,7 +86,9 @@ def compile_inline(self, python_code, album):
8486
except SyntaxError:
8587
# Fall back to a function body.
8688
try:
87-
func = _compile_func(python_code)
89+
func = _compile_func(
90+
python_code, args="album" if album else "item"
91+
)
8892
except SyntaxError:
8993
self._log.error(
9094
"syntax error in inline field definition:\n" "{0}",
@@ -106,6 +110,7 @@ def _dict_for(obj):
106110
# For expressions, just evaluate and return the result.
107111
def _expr_func(obj):
108112
values = _dict_for(obj)
113+
values["album" if album else "item"] = obj
109114
try:
110115
return eval(code, values)
111116
except Exception as exc:
@@ -119,7 +124,7 @@ def _func_func(obj):
119124
old_globals = dict(func.__globals__)
120125
func.__globals__.update(_dict_for(obj))
121126
try:
122-
return func()
127+
return func(obj)
123128
except Exception as exc:
124129
raise InlineError(python_code, exc)
125130
finally:

docs/plugins/inline.rst

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ new template field; the key is the name of the field (you'll use the name to
1313
refer to the field in your templates) and the value is a Python expression or
1414
function body. The Python code has all of a track's fields in scope, so you can
1515
refer to any normal attributes (such as ``artist`` or ``title``) as Python
16-
variables.
16+
variables. The Python code also has direct access to the item object as ``item``
17+
for item fields, and as ``album`` for album fields. This allows use of computed
18+
fields and plugin fields, for example ``album.albumtotal``, or ``album.missing``
19+
if the ``missing`` plugin is enabled.
1720

1821
Here are a couple of examples of expressions::
1922

0 commit comments

Comments
 (0)