mirror of
https://github.com/beetbox/beets.git
synced 2025-12-07 09:04:33 +01:00
inline: allow album field definitions
Under the album_field heading.
This commit is contained in:
parent
a0cb31956d
commit
0176e10ccf
3 changed files with 48 additions and 9 deletions
|
|
@ -46,7 +46,7 @@ def _compile_func(body):
|
||||||
eval(code, env)
|
eval(code, env)
|
||||||
return env[FUNC_NAME]
|
return env[FUNC_NAME]
|
||||||
|
|
||||||
def compile_inline(python_code):
|
def compile_inline(python_code, album):
|
||||||
"""Given a Python expression or function body, compile it as a path
|
"""Given a Python expression or function body, compile it as a path
|
||||||
field function. The returned function takes a single argument, an
|
field function. The returned function takes a single argument, an
|
||||||
Item, and returns a Unicode string. If the expression cannot be
|
Item, and returns a Unicode string. If the expression cannot be
|
||||||
|
|
@ -68,10 +68,18 @@ def compile_inline(python_code):
|
||||||
else:
|
else:
|
||||||
is_expr = True
|
is_expr = True
|
||||||
|
|
||||||
|
def _dict_for(obj):
|
||||||
|
if album:
|
||||||
|
out = dict(obj._record)
|
||||||
|
out['items'] = list(obj.items())
|
||||||
|
return out
|
||||||
|
else:
|
||||||
|
return dict(obj.record)
|
||||||
|
|
||||||
if is_expr:
|
if is_expr:
|
||||||
# For expressions, just evaluate and return the result.
|
# For expressions, just evaluate and return the result.
|
||||||
def _expr_func(item):
|
def _expr_func(obj):
|
||||||
values = dict(item.record)
|
values = _dict_for(obj)
|
||||||
try:
|
try:
|
||||||
return eval(code, values)
|
return eval(code, values)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
|
|
@ -80,8 +88,8 @@ def compile_inline(python_code):
|
||||||
else:
|
else:
|
||||||
# For function bodies, invoke the function with values as global
|
# For function bodies, invoke the function with values as global
|
||||||
# variables.
|
# variables.
|
||||||
def _func_func(item):
|
def _func_func(obj):
|
||||||
func.__globals__.update(item.record)
|
func.__globals__.update(_dict_for(obj))
|
||||||
try:
|
try:
|
||||||
return func()
|
return func()
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
|
|
@ -94,11 +102,18 @@ class InlinePlugin(BeetsPlugin):
|
||||||
|
|
||||||
config.add({
|
config.add({
|
||||||
'pathfields': {},
|
'pathfields': {},
|
||||||
|
'album_fields': {},
|
||||||
})
|
})
|
||||||
|
|
||||||
# Add field expressions.
|
# Add field expressions.
|
||||||
for key, view in config['pathfields'].items():
|
for key, view in config['pathfields'].items():
|
||||||
log.debug(u'adding template field %s' % key)
|
log.debug(u'inline: adding item field %s' % key)
|
||||||
func = compile_inline(view.get(unicode))
|
func = compile_inline(view.get(unicode), False)
|
||||||
if func is not None:
|
if func is not None:
|
||||||
self.template_fields[key] = func
|
self.template_fields[key] = func
|
||||||
|
|
||||||
|
for key, view in config['album_fields'].items():
|
||||||
|
log.debug(u'inline: adding album field %s' % key)
|
||||||
|
func = compile_inline(view.get(unicode), True)
|
||||||
|
if func is not None:
|
||||||
|
self.album_template_fields[key] = func
|
||||||
|
|
|
||||||
|
|
@ -31,8 +31,9 @@ Changelog
|
||||||
Thanks to jayme on GitHub.
|
Thanks to jayme on GitHub.
|
||||||
* :doc:`/plugins/lyrics`: Lyrics searches should now turn up more results due
|
* :doc:`/plugins/lyrics`: Lyrics searches should now turn up more results due
|
||||||
to some fixes in dealing with special characters.
|
to some fixes in dealing with special characters.
|
||||||
* Plugins can now provide fields for both Album and Item templates. Thanks
|
* Plugins can now provide fields for both Album and Item templates, thanks
|
||||||
to Pedro Silva.
|
to Pedro Silva. Accordingly, the :doc:`/plugins/inline` can also now define
|
||||||
|
album fields.
|
||||||
* The :ref:`fields-cmd` command shows template fields provided by plugins.
|
* The :ref:`fields-cmd` command shows template fields provided by plugins.
|
||||||
Thanks again to Pedro Silva.
|
Thanks again to Pedro Silva.
|
||||||
* Album art filenames now respect the :ref:`replace` configuration.
|
* Album art filenames now respect the :ref:`replace` configuration.
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,10 @@ referenced in path templates like so::
|
||||||
paths:
|
paths:
|
||||||
default: $initial/$artist/$album%aunique{}/$disc_and_track $title
|
default: $initial/$artist/$album%aunique{}/$disc_and_track $title
|
||||||
|
|
||||||
|
|
||||||
|
Function Fields
|
||||||
|
---------------
|
||||||
|
|
||||||
If you need to use statements like ``import``, you can write a Python function
|
If you need to use statements like ``import``, you can write a Python function
|
||||||
body instead of a single expression. In this case, you'll need to ``return``
|
body instead of a single expression. In this case, you'll need to ``return``
|
||||||
a result for the value of the path field, like so::
|
a result for the value of the path field, like so::
|
||||||
|
|
@ -43,3 +47,22 @@ a result for the value of the path field, like so::
|
||||||
|
|
||||||
You might want to use the YAML syntax for "block literals," in which a leading
|
You might want to use the YAML syntax for "block literals," in which a leading
|
||||||
``|`` character indicates a multi-line block of text.
|
``|`` character indicates a multi-line block of text.
|
||||||
|
|
||||||
|
|
||||||
|
Album Fields
|
||||||
|
------------
|
||||||
|
|
||||||
|
The above examples define fields for *item* templates, but you can also define
|
||||||
|
fields for *album* templates. Use the ``album_fields`` configuration section.
|
||||||
|
In this context, all existing album fields are available as variables along
|
||||||
|
with ``items``, which is a list of items in the album.
|
||||||
|
|
||||||
|
This example defines a ``$bitrate`` field for albums as the average of the
|
||||||
|
tracks' fields::
|
||||||
|
|
||||||
|
album_fields:
|
||||||
|
bitrate: |
|
||||||
|
total = 0
|
||||||
|
for item in items:
|
||||||
|
total += item.bitrate
|
||||||
|
return total / len(items)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue