flexible -f templates for album listings (and rdm)

This commit is contained in:
Adrian Sampson 2012-04-30 22:59:17 -07:00
parent ef45461cf9
commit 1ee07e116e
3 changed files with 26 additions and 10 deletions

View file

@ -1344,7 +1344,7 @@ class Album(BaseAlbum):
_, ext = os.path.splitext(image)
dest = os.path.join(item_dir, self._library.art_filename + ext)
return dest
def set_art(self, path):
"""Sets the album's cover art to the image at the given path.
The image is copied into place, replacing any existing art.
@ -1368,6 +1368,21 @@ class Album(BaseAlbum):
util.copy(path, artdest)
self.artpath = artdest
def evaluate_template(self, template):
"""Evaluates a Template object using the album's fields.
"""
# Get template field values.
mapping = {}
for key in ALBUM_KEYS:
mapping[key] = getattr(self, key)
# Get template functions.
funcs = DefaultTemplateFunctions().functions()
funcs.update(plugins.template_funcs())
# Perform substitution.
return template.substitute(mapping, funcs)
# Default path template resources.
@ -1385,9 +1400,9 @@ class DefaultTemplateFunctions(object):
"""
_prefix = 'tmpl_'
def __init__(self, item, lib=None, pathmod=None):
"""Paramaterize the functions. If `lib` is None, then some
functions (namely, ``aunique``) will always evaluate to the
def __init__(self, item=None, lib=None, pathmod=None):
"""Paramaterize the functions. If `item` or `lib` is None, then
some functions (namely, ``aunique``) will always evaluate to the
empty string.
"""
self.item = item
@ -1457,8 +1472,10 @@ class DefaultTemplateFunctions(object):
used. Both "keys" and "disam" should be given as
whitespace-separated lists of field names.
"""
# Fast paths: no album, no library, or memoized value.
if self.item.album_id is None or not self.lib:
# Fast paths: no album, no item or library, or memoized value.
if not self.item or not self.lib:
return u''
if self.item.album_id is None:
return u''
memokey = ('aunique', keys, disam, self.item.album_id)
memoval = self.lib._memotable.get(memokey)

View file

@ -817,8 +817,7 @@ def list_items(lib, query, album, path, fmt):
if path:
print_(album.item_dir())
elif fmt is not None:
#TODO: Support functions and plugin fields in album mode.
print_(template.substitute(album._record))
print_(album.evaluate_template(template))
else:
for item in lib.items(query):
if path:

View file

@ -45,13 +45,13 @@ def random_item(lib, config, opts, args):
if path:
print_(album.item_dir())
else:
print_(template.substitute(album._record))
print_(album.evaluate_template(template))
else:
for item in objs:
if path:
print_(item.path)
else:
print_(template.substitute(item.record))
print_(item.evaluate_template(template, lib))
random_cmd = Subcommand('random',
help='chose a random track or album')