Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: inline: make the album/item available directly #5439

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions beetsplug/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ def __init__(self, code, exc):
)


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 = "def {}():\n {}".format(FUNC_NAME, body.replace("\n", "\n "))
body = "def {}({}):\n {}".format(
FUNC_NAME, args, body.replace("\n", "\n ")
)
code = compile(body, "inline", "exec")
env = {}
eval(code, env)
Expand Down Expand Up @@ -84,7 +86,9 @@ def compile_inline(self, python_code, album):
except SyntaxError:
# Fall back to a function body.
try:
func = _compile_func(python_code)
func = _compile_func(
python_code, args="album" if album else "item"
)
except SyntaxError:
self._log.error(
"syntax error in inline field definition:\n" "{0}",
Expand All @@ -106,6 +110,7 @@ def _dict_for(obj):
# For expressions, just evaluate and return the result.
def _expr_func(obj):
values = _dict_for(obj)
values["album" if album else "item"] = obj
try:
return eval(code, values)
except Exception as exc:
Expand All @@ -119,7 +124,7 @@ def _func_func(obj):
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:
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ New features:
* Beets now uses ``platformdirs`` to determine the default music directory.
This location varies between systems -- for example, users can configure it
on Unix systems via ``user-dirs.dirs(5)``.
* :doc:`plugins/inline`: Add access to the ``album`` or ``item`` object in
inline fields.

Bug fixes:

Expand Down
5 changes: 4 additions & 1 deletion docs/plugins/inline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ 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.
variables. The Python code also has direct access to the item object as ``item``
for item fields, and as ``album`` for album fields. This allows use of computed
fields and plugin fields, for example, ``album.albumtotal``, or ``album.missing``
if the :doc:`/plugins/missing` plugin is enabled.

Here are a couple of examples of expressions::

Expand Down
Loading