Skip to content

Commit 5bc11ea

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. 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>
1 parent 1d01689 commit 5bc11ea

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

beetsplug/inline.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ def __init__(self, code, exc):
3232
)
3333

3434

35-
def _compile_func(body):
35+
def _compile_func(body, args=""):
3636
"""Given Python code for a function body, return a compiled
3737
callable that invokes that code.
3838
"""
3939
body = body.replace("\n", "\n ")
40-
body = f"def {FUNC_NAME}():\n {body}"
40+
body = f"def {FUNC_NAME}({args}):\n {body}"
4141
code = compile(body, "inline", "exec")
4242
env = {}
4343
eval(code, env)
@@ -84,7 +84,7 @@ def compile_inline(self, python_code, album, field_name):
8484
except SyntaxError:
8585
# Fall back to a function body.
8686
try:
87-
func = _compile_func(python_code)
87+
func = _compile_func(python_code, args="db_obj")
8888
except SyntaxError:
8989
self._log.error(
9090
"syntax error in inline field definition:\n{}",
@@ -111,6 +111,7 @@ def _dict_for(obj):
111111
# For expressions, just evaluate and return the result.
112112
def _expr_func(obj):
113113
values = _dict_for(obj)
114+
values["db_obj"] = obj
114115
try:
115116
return eval(code, values)
116117
except Exception as exc:
@@ -124,7 +125,7 @@ def _func_func(obj):
124125
old_globals = dict(func.__globals__)
125126
func.__globals__.update(_dict_for(obj))
126127
try:
127-
return func()
128+
return func(obj)
128129
except Exception as exc:
129130
raise InlineError(python_code, exc)
130131
finally:

docs/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ New features
2626
after upgrading to trigger the migration. Only then you can safely move
2727
the library to a new location.
2828

29+
- :doc:`plugins/inline`: Add access to the ``album`` or ``item`` object as
30+
``db_obj`` in inline fields.
31+
2932
Bug fixes
3033
~~~~~~~~~
3134

docs/plugins/inline.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ To use the ``inline`` plugin, enable it in your configuration (see
1111
Under this key, every line defines a new template field; the key is the name of
1212
the field (you'll use the name to refer to the field in your templates) and the
1313
value is a Python expression or function body. The Python code has all of a
14-
track's fields in scope, so you can refer to any normal attributes (such as
15-
``artist`` or ``title``) as Python variables.
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.
1619

1720
Here are a couple of examples of expressions:
1821

test/plugins/test_inline.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,46 @@ def test_inline_album_expression_uses_items(self):
6060

6161
album = self.add_album_fixture()
6262
assert func(album) == len(list(album.items()))
63+
64+
def test_inline_album_expression_uses_items_via_obj(self):
65+
plugin = InlinePlugin()
66+
func = plugin.compile_inline(
67+
"len(db_obj.items())", album=True, field_name="item_count"
68+
)
69+
70+
album = self.add_album_fixture()
71+
assert func(album) == len(list(album.items()))
72+
73+
def test_inline_function_body_item_field_via_obj(self):
74+
plugin = InlinePlugin()
75+
func = plugin.compile_inline(
76+
"return db_obj.track + 1", album=False, field_name="next_track"
77+
)
78+
79+
item = self.add_item_fixture(track=3)
80+
assert func(item) == 4
81+
82+
def test_inline_obj_missing(self):
83+
config["plugins"] = ["inline", "missing"]
84+
85+
config["album_fields"] = {"has_missing": ("bool(db_obj.missing)")}
86+
87+
plugins._instances.clear()
88+
plugins.load_plugins()
89+
90+
album = self.add_album_fixture(track_count=1)
91+
album.tracktotal = 3
92+
for item in album.items():
93+
item.tracktotal = 3
94+
item.store()
95+
album.store()
96+
assert album._get("has_missing")
97+
98+
def test_inline_function_body_obj(self):
99+
plugin = InlinePlugin()
100+
func = plugin.compile_inline(
101+
"return db_obj.title", album=False, field_name="title_value"
102+
)
103+
104+
item = self.add_item_fixture(track=3)
105+
assert func(item)

0 commit comments

Comments
 (0)